Commit f672d8f1 authored by Alex Buijs's avatar Alex Buijs

Pass group data to frontend subscriptions app

Pass group data to the frontend subscriptions app
for allowing existing users to choose earlier created
groups to apply licenses to.
parent a502a900
...@@ -9,7 +9,8 @@ module SubscriptionsHelper ...@@ -9,7 +9,8 @@ module SubscriptionsHelper
full_name: current_user.name, full_name: current_user.name,
plan_data: plan_data.to_json, plan_data: plan_data.to_json,
plan_id: params[:plan_id], plan_id: params[:plan_id],
new_user: new_user?.to_s new_user: new_user?.to_s,
group_data: group_data.to_json
} }
end end
...@@ -34,4 +35,14 @@ module SubscriptionsHelper ...@@ -34,4 +35,14 @@ module SubscriptionsHelper
.reject { |plan| plan[:free] } .reject { |plan| plan[:free] }
.map { |plan| plan.slice(:id, :code, :price_per_year) } .map { |plan| plan.slice(:id, :code, :price_per_year) }
end end
def group_data
current_user.free_namespaces.map do |namespace|
{
id: namespace.id,
name: namespace.name,
users: namespace.member_count
}
end
end
end end
...@@ -240,6 +240,10 @@ module EE ...@@ -240,6 +240,10 @@ module EE
.any? .any?
end end
def free_namespaces
authorized_groups.with_counts(archived: false).where(plan: [nil, Plan.free, Plan.default]).order(:name)
end
override :has_current_license? override :has_current_license?
def has_current_license? def has_current_license?
License.current.present? License.current.present?
......
...@@ -3,12 +3,15 @@ ...@@ -3,12 +3,15 @@
# EE-only # EE-only
FactoryBot.define do FactoryBot.define do
factory :plan do factory :plan do
factory :default_plan do Plan::DEFAULT_PLANS.each do |plan|
name { Plan::DEFAULT } factory :"#{plan}_plan" do
title { name.titleize } name { plan }
title { name.titleize }
initialize_with { Plan.find_or_create_by(name: plan) }
end
end end
EE::Namespace::PLANS.each do |plan| Plan::ALL_HOSTED_PLANS.each do |plan|
factory :"#{plan}_plan" do factory :"#{plan}_plan" do
name { plan } name { plan }
title { name.titleize } title { name.titleize }
......
...@@ -28,9 +28,11 @@ describe SubscriptionsHelper do ...@@ -28,9 +28,11 @@ describe SubscriptionsHelper do
describe '#subscription_data' do describe '#subscription_data' do
let_it_be(:user) { create(:user, setup_for_company: nil, name: 'First Last') } let_it_be(:user) { create(:user, setup_for_company: nil, name: 'First Last') }
let_it_be(:group) { create(:group, name: 'My Namespace') }
before do before do
allow(helper).to receive(:current_user).and_return(user) allow(helper).to receive(:current_user).and_return(user)
group.add_owner(user)
end end
subject { helper.subscription_data } subject { helper.subscription_data }
...@@ -39,6 +41,7 @@ describe SubscriptionsHelper do ...@@ -39,6 +41,7 @@ describe SubscriptionsHelper do
it { is_expected.to include(full_name: 'First Last') } it { is_expected.to include(full_name: 'First Last') }
it { is_expected.to include(plan_data: '[{"id":"bronze_id","code":"bronze","price_per_year":48.0}]') } it { is_expected.to include(plan_data: '[{"id":"bronze_id","code":"bronze","price_per_year":48.0}]') }
it { is_expected.to include(plan_id: 'bronze_id') } it { is_expected.to include(plan_id: 'bronze_id') }
it { is_expected.to include(group_data: %Q{[{"id":#{group.id},"name":"My Namespace","users":1}]}) }
describe 'new_user' do describe 'new_user' do
where(:referer, :expected_result) do where(:referer, :expected_result) do
......
...@@ -905,4 +905,34 @@ describe User do ...@@ -905,4 +905,34 @@ describe User do
end end
end end
end end
describe '#free_namespaces' do
let_it_be(:user) { create(:user, :external) }
let_it_be(:licensed_group) { create(:group, plan: :bronze_plan) }
let_it_be(:free_group_z) { create(:group, plan: :default_plan, name: 'Z') }
let_it_be(:free_group_a) { create(:group, plan: :default_plan, name: 'A') }
subject { user.free_namespaces }
context 'user with no groups' do
it { is_expected.to eq [] }
end
context 'member of a licensed group' do
before do
licensed_group.add_guest(user)
end
it { is_expected.not_to include licensed_group }
end
context 'member of 2 free groups' do
before do
free_group_a.add_guest(user)
free_group_z.add_guest(user)
end
it { is_expected.to eq [free_group_a, free_group_z] }
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment