Commit e185099a authored by Ash McKenzie's avatar Ash McKenzie

Merge branch '222455-remove-active-trials-from-subscription-list-in-the-trial-flow' into 'master'

Omit already-trialed namespaces from trial select

See merge request gitlab-org/gitlab!34770
parents 4fec016f 721e1709
......@@ -14,18 +14,26 @@ module EE
end
def namespace_options_for_select(selected = nil)
groups = current_user.manageable_groups.map { |g| [g.name, g.id] }
users = [[current_user.namespace.name, current_user.namespace_id]]
grouped_options = {
'New' => [[_('Create group'), 0]],
'Groups' => groups,
'Users' => users
'Groups' => trial_groups,
'Users' => trial_users
}
grouped_options_for_select(grouped_options, selected, prompt: _('Please select'))
end
def trial_users
user_namespace = current_user.namespace
return [] if user_namespace.gitlab_subscription&.trial?
[[user_namespace.name, user_namespace.id]]
end
def trial_groups
current_user.manageable_groups_eligible_for_trial.map { |g| [g.name, g.id] }
end
def show_trial_errors?(namespace, service_result)
namespace&.invalid? || (service_result && !service_result[:success])
end
......
......@@ -257,6 +257,13 @@ module EE
.order(:name)
end
def manageable_groups_eligible_for_trial
manageable_groups
.left_joins(:gitlab_subscription)
.where(gitlab_subscriptions: { trial: [nil, false] })
.order(:name)
end
override :has_current_license?
def has_current_license?
License.current.present?
......
---
title: Omit already-trialed namespaces from trial select options
merge_request: 34770
author:
type: changed
......@@ -5,6 +5,159 @@ require 'spec_helper'
RSpec.describe EE::TrialHelper do
using RSpec::Parameterized::TableSyntax
describe '#namespace_options_for_select' do
let_it_be(:user) { create :user }
let_it_be(:group1) { create :group }
let_it_be(:group2) { create :group }
let(:expected_group_options) { [] }
let(:expected_user_options) { [[user.namespace.name, user.namespace.id]] }
let(:generated_html) do
grouped_options_for_select({
'New' => [['Create group', 0]],
'Groups' => expected_group_options,
'Users' => expected_user_options
}, nil, prompt: 'Please select')
end
before do
allow(helper).to receive(:trial_groups).and_return(expected_group_options)
allow(helper).to receive(:trial_users).and_return(expected_user_options)
end
subject { helper.namespace_options_for_select }
context 'when the user’s namespace can be trialed' do
context 'and the user has no groups or none of their groups can be trialed' do
it { is_expected.to eq(generated_html) }
end
context 'and the user has some groups which can be trialed' do
let(:expected_group_options) { [group1, group2].map {|g| [g.name, g.id]} }
it { is_expected.to eq(generated_html) }
end
end
context 'when the user’s namespace has already been trialed' do
let(:expected_user_options) { [] }
context 'and the user has no groups or none of their groups can be trialed' do
it { is_expected.to eq(generated_html) }
end
context 'and the user has some groups which can be trialed' do
let(:expected_group_options) { [group1, group2].map {|g| [g.name, g.id]} }
it { is_expected.to eq(generated_html) }
end
end
end
describe '#trial_users' do
let_it_be(:user) { create :user }
let(:user_eligible_for_trial_result) { [[user.namespace.name, user.namespace.id]] }
let(:user_ineligible_for_trial_result) { [] }
before do
user.reload # necessary to cache-bust the user.namespace.gitlab_subscription object
allow(helper).to receive(:current_user).and_return(user)
end
subject { helper.trial_users }
context 'when the user has no subscription on their namespace' do
it { is_expected.to eq(user_eligible_for_trial_result) }
end
context 'when the user has a subscription on their namespace' do
let(:trialed) { false }
let!(:subscription) { create :gitlab_subscription, namespace: user.namespace, trial: trialed }
context 'and the user has not yet trialed their namespace' do
it { is_expected.to eq(user_eligible_for_trial_result) }
end
context 'and the user has already trialed their namespace' do
let(:trialed) { true }
it { is_expected.to eq(user_ineligible_for_trial_result) }
end
end
end
describe '#trial_groups' do
let_it_be(:user) { create :user }
let(:no_groups) { [] }
before do
allow(helper).to receive(:current_user).and_return(user)
end
subject { helper.trial_groups }
context 'when the user is not an owner/maintainer of any groups' do
it { is_expected.to eq(no_groups) }
end
context 'when the user is an owner/maintainer of some groups' do
let_it_be(:group1) { create :group, name: 'Group 1' }
let_it_be(:subgroup1) { create :group, parent: group1, name: 'Sub-Group 1' }
let_it_be(:group2) { create :group, name: 'Group 2' }
let_it_be(:subgroup2) { create :group, parent: group2, name: 'Sub-Group 2' }
let_it_be(:subsubgroup1) { create :group, parent: subgroup2, name: 'Sub-Sub-Group 1' }
let(:all_groups) { [group1, group2, subgroup1, subgroup2, subsubgroup1].map {|g| [g.name, g.id] } }
before do
group1.add_owner(user)
group2.add_maintainer(user)
end
context 'and none of the groups have subscriptions' do
it { is_expected.to eq(all_groups) }
end
context 'and the groups have subscriptions' do
let(:trialed_group1) { false }
let(:trialed_subgroup1) { false }
let(:trialed_group2) { false }
let(:trialed_subgroup2) { false }
let(:trialed_subsubgroup1) { false }
let!(:subscription_group1) { create :gitlab_subscription, namespace: group1, trial: trialed_group1 }
let!(:subscription_subgroup1) { create :gitlab_subscription, namespace: subgroup1, trial: trialed_subgroup1 }
let!(:subscription_group2) { create :gitlab_subscription, namespace: group2, trial: trialed_group2 }
let!(:subscription_subgroup2) { create :gitlab_subscription, namespace: subgroup2, trial: trialed_subgroup2 }
let!(:subscription_subsubgroup1) { create :gitlab_subscription, namespace: subsubgroup1, trial: trialed_subsubgroup1 }
context 'and none of the groups have been trialed yet' do
it { is_expected.to eq(all_groups) }
end
context 'and some of the groups have been trialed' do
let(:trialed_group1) { true }
let(:trialed_subgroup1) { true }
let(:trialed_subgroup2) { true }
let(:some_groups) { [group2, subsubgroup1].map {|g| [g.name, g.id]} }
it { is_expected.to eq(some_groups) }
end
context 'and all of the groups have already been trialed' do
let(:trialed_group1) { true }
let(:trialed_subgroup1) { true }
let(:trialed_group2) { true }
let(:trialed_subgroup2) { true }
let(:trialed_subsubgroup1) { true }
it { is_expected.to eq(no_groups) }
end
end
end
end
describe '#show_trial_errors?' do
shared_examples 'shows errors based on trial generation result' do
where(:trial_result, :expected_result) do
......
......@@ -1050,6 +1050,60 @@ RSpec.describe User do
end
end
describe '#manageable_groups_eligible_for_trial' do
let_it_be(:user) { create :user }
let_it_be(:non_trialed_group_z) { create :group, name: 'Zeta', gitlab_subscription: create(:gitlab_subscription) }
let_it_be(:non_trialed_group_a) { create :group, name: 'Alpha', gitlab_subscription: create(:gitlab_subscription) }
let_it_be(:trialed_group) { create :group, name: 'Omitted', gitlab_subscription: create(:gitlab_subscription, trial: true) }
subject { user.manageable_groups_eligible_for_trial }
context 'user with no groups' do
it { is_expected.to eq [] }
end
context 'owner of an already-trialed group' do
before do
trialed_group.add_owner(user)
end
it { is_expected.not_to include trialed_group }
end
context 'guest of a non-trialed group' do
before do
non_trialed_group_a.add_guest(user)
end
it { is_expected.not_to include non_trialed_group_a }
end
context 'developer of a non-trialed group' do
before do
non_trialed_group_a.add_developer(user)
end
it { is_expected.not_to include non_trialed_group_a }
end
context 'maintainer of a non-trialed group' do
before do
non_trialed_group_a.add_maintainer(user)
end
it { is_expected.to include non_trialed_group_a }
end
context 'owner of 2 non-trialed groups' do
before do
non_trialed_group_z.add_owner(user)
non_trialed_group_a.add_owner(user)
end
it { is_expected.to eq [non_trialed_group_a, non_trialed_group_z] }
end
end
describe '#active_for_authentication?' do
subject { user.active_for_authentication? }
......
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