Commit e44fbd33 authored by Dallas Reedy's avatar Dallas Reedy

Omit already trialed namespaces from trial select

- Adds a method on ee/user to get manageable groups eligible for a trial
- Adds a changelog entry
- Updates TrialHelper specs & User specs
parent 1ef0df07
......@@ -14,8 +14,13 @@ 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]]
groups = current_user.manageable_groups_eligible_for_trial.map { |g| [g.name, g.id] }
user_namespace = current_user.namespace
users = if user_namespace.gitlab_subscription&.trial?
[]
else
[[user_namespace.name, user_namespace.id]]
end
grouped_options = {
'New' => [[_('Create group'), 0]],
......
......@@ -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,149 @@ 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(:expected_prompt_option) { 'Please select' }
let(:expected_new_options) { [['Create group', 0]] }
let(:expected_group_options) { [] }
let(:expected_user_options) { [[user.namespace.name, user.namespace.id]] }
let(:generated_html) do
grouped_options_for_select({
'New' => expected_new_options,
'Groups' => expected_group_options,
'Users' => expected_user_options
}, nil, prompt: expected_prompt_option)
end
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.namespace_options_for_select }
context 'when a user owns no groups beyond their personal namespace' do
it 'shows “Create group” & user namespace options' do
expect(subject).to eq(generated_html)
end
context 'when they have already trialed their personal namespace' do
before do
create :gitlab_subscription, namespace: user.namespace, trial: true
end
let(:expected_user_options) { [] }
it 'only shows “Create group” option' do
expect(subject).to eq(generated_html)
end
end
end
context 'when a user is an owner of a group beyond their personal namespace' do
let_it_be(:group) { create :group }
before do
group.add_owner(user)
end
context 'when the group is eligible for a trial' do
let(:expected_group_options) { [[group.name, group.id]] }
it 'shows options for “Create group,” for the group namespace, & for their user namespace' do
expect(subject).to eq(generated_html)
end
end
context 'when they have already trialed the group' do
before do
create :gitlab_subscription, namespace: group, trial: true
end
it 'shows options for “Create group” & their user namespace, but not for the group' do
expect(subject).to eq(generated_html)
end
end
context 'when they have already trialed their personal namespace' do
before do
create :gitlab_subscription, namespace: user.namespace, trial: true
end
let(:expected_group_options) { [[group.name, group.id]] }
let(:expected_user_options) { [] }
it 'shows options for “Create group” & the group namespace, but not their user namespace' do
expect(subject).to eq(generated_html)
end
end
context 'when they have already trialed the group & their personal namespace' do
before do
create :gitlab_subscription, namespace: user.namespace, trial: true
create :gitlab_subscription, namespace: group, trial: true
end
let(:expected_user_options) { [] }
it 'only shows the “Create group” option' do
expect(subject).to eq(generated_html)
end
end
end
context 'when a user is an owner or maintainer of several namespaces' do
let_it_be(:group1) { create :group, name: 'Group 1' }
let_it_be(:subgroup) { 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(:subsubgroup) { create :group, parent: subgroup2, name: 'Sub-Sub-Group 1' }
before do
group1.add_owner(user)
group2.add_maintainer(user)
end
context 'when each namespace is eligible for a trial' do
let(:expected_group_options) do
[
[group1.name, group1.id],
[group2.name, group2.id],
[subgroup.name, subgroup.id],
[subgroup2.name, subgroup2.id],
[subsubgroup.name, subsubgroup.id]
]
end
it 'shows options for “Create group,” for each group namespace, & for their user namespace' do
expect(subject).to eq(generated_html)
end
end
context 'when they have already trialed for their user & some of the groups' do
before do
create :gitlab_subscription, namespace: user.namespace, trial: true
create :gitlab_subscription, namespace: group1, trial: true
create :gitlab_subscription, namespace: group2, trial: true
create :gitlab_subscription, namespace: subgroup, trial: true
end
let(:expected_group_options) do
[
[subgroup2.name, subgroup2.id],
[subsubgroup.name, subsubgroup.id]
]
end
let(:expected_user_options) { [] }
it 'only shows options for “Create group” & for the non-trialed groups' do
expect(subject).to eq(generated_html)
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