From 5c838524f085fd896ddc7677e87ac13c2da69c19 Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Tue, 11 Jan 2022 14:43:12 +0100 Subject: [PATCH 01/13] Add Seats Count alert Add template and helper with basic logic --- app/helpers/seats_count_alert_helper.rb | 9 + app/views/layouts/_page.html.haml | 1 + ee/app/helpers/ee/seats_count_alert_helper.rb | 64 +++++++ .../header/_seats_count_alert.html.haml | 12 ++ .../ee/seats_count_alert_helper_spec.rb | 175 ++++++++++++++++++ locale/gitlab.pot | 9 + spec/helpers/seats_count_alert_helper_spec.rb | 11 ++ 7 files changed, 281 insertions(+) create mode 100644 app/helpers/seats_count_alert_helper.rb create mode 100644 ee/app/helpers/ee/seats_count_alert_helper.rb create mode 100644 ee/app/views/layouts/header/_seats_count_alert.html.haml create mode 100644 ee/spec/helpers/ee/seats_count_alert_helper_spec.rb create mode 100644 spec/helpers/seats_count_alert_helper_spec.rb diff --git a/app/helpers/seats_count_alert_helper.rb b/app/helpers/seats_count_alert_helper.rb new file mode 100644 index 00000000000000..0278aeb27b5b33 --- /dev/null +++ b/app/helpers/seats_count_alert_helper.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module SeatsCountAlertHelper + # Overridden in EE + def display_seats_count_alert! + end +end + +SeatsCountAlertHelper.prepend_mod_with('SeatsCountAlertHelper') diff --git a/app/views/layouts/_page.html.haml b/app/views/layouts/_page.html.haml index d655777224170a..b7299df1bc137a 100644 --- a/app/views/layouts/_page.html.haml +++ b/app/views/layouts/_page.html.haml @@ -16,6 +16,7 @@ = render "shared/service_ping_consent" = render_two_factor_auth_recovery_settings_check = render_if_exists "layouts/header/ee_subscribable_banner" + = render_if_exists "layouts/header/seats_count_alert" = render_if_exists "shared/namespace_storage_limit_alert" = render_if_exists "shared/namespace_user_cap_reached_alert" = render_if_exists "shared/new_user_signups_cap_reached_alert" diff --git a/ee/app/helpers/ee/seats_count_alert_helper.rb b/ee/app/helpers/ee/seats_count_alert_helper.rb new file mode 100644 index 00000000000000..86f44a7c37ac95 --- /dev/null +++ b/ee/app/helpers/ee/seats_count_alert_helper.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +module EE + module SeatsCountAlertHelper + extend ::Gitlab::Utils::Override + + override :display_seats_count_alert! + def display_seats_count_alert! + @display_seats_count_alert = true + end + + def learn_more_link + return '' unless show_seats_count_alert? + + link_to _('Learn more.'), help_page_path('subscriptions/quarterly_reconciliation'), target: '_blank', rel: 'noopener noreferrer' + end + + def group_name + root_namespace&.name + end + + def remaining_seats_count + return if total_seats_count.nil? || seats_in_use.nil? + + total_seats_count - seats_in_use + end + + def seats_usage_link + return '' if root_namespace.nil? + + link_to _('View seat usage'), current_usage_quotas_path, class: 'btn gl-alert-action btn-info btn-md gl-button' + end + + def show_seats_count_alert? + return false if current_subscription.nil? + + @display_seats_count_alert + end + + def total_seats_count + current_subscription&.seats + end + + private + + def root_namespace + return @project.namespace.root_ancestor unless @project&.namespace.nil? + + @group&.root_ancestor + end + + def current_subscription + root_namespace&.gitlab_subscription + end + + def seats_in_use + current_subscription&.seats_in_use + end + + def current_usage_quotas_path + usage_quotas_path(root_namespace, anchor: 'seats-quota-tab') + end + end +end diff --git a/ee/app/views/layouts/header/_seats_count_alert.html.haml b/ee/app/views/layouts/header/_seats_count_alert.html.haml new file mode 100644 index 00000000000000..3bd36b43a296d1 --- /dev/null +++ b/ee/app/views/layouts/header/_seats_count_alert.html.haml @@ -0,0 +1,12 @@ +- return unless show_seats_count_alert? +.container.container-limited.pt-3 + .gl-alert.gl-alert-info{ role: 'alert' } + = sprite_icon('information-o', size: 16, css_class: 'gl-icon gl-alert-icon') + %button.js-close.gl-alert-dismiss{ type: 'button', 'aria-label' => _('Dismiss') } + = sprite_icon('close', size: 16, css_class: 'gl-icon') + .gl-alert-body + %h4.gl-alert-title= _('%{group_name} is approaching the limit of available seats') % { group_name: group_name } + = _('Your subscription has %{remaining_seats_count} out of %{total_seats_count} seats remaining. Even if you reach the number of seats in your subscription, you can continue to add users, and GitLab will bill you for the overage.') % { remaining_seats_count: remaining_seats_count, total_seats_count: total_seats_count } + = learn_more_link + .gl-alert-actions + = seats_usage_link diff --git a/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb b/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb new file mode 100644 index 00000000000000..64c200c8299493 --- /dev/null +++ b/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb @@ -0,0 +1,175 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe EE::SeatsCountAlertHelper do + let_it_be(:user) { create(:user) } + let_it_be(:group) { nil } + let_it_be(:project) { nil } + + before do + assign(:project, project) + assign(:group, group) + end + + shared_examples 'learn more link is built' do + it 'builds the correct link' do + expect(helper.learn_more_link).to match %r{.*}m + end + end + + shared_examples 'learn more link is not built' do + it 'builds the correct link' do + expect(helper.learn_more_link).to eq('') + end + end + + shared_examples 'seats info are not populated' do + it 'sets remaining seats count to nil' do + expect(helper.remaining_seats_count).to be_nil + end + + it 'sets total seats count to nil' do + expect(helper.total_seats_count).to be_nil + end + end + + shared_examples 'seats info are populated' do + it 'sets remaining seats count to the correct number' do + expect(helper.remaining_seats_count).to eq(14) + end + + it 'sets total seats count to the correct number' do + expect(helper.total_seats_count).to eq(15) + end + end + + shared_examples 'group info are populated' do + it 'builds the correct link' do + expect(helper.seats_usage_link).to match %r{.*}m + end + + it 'has a group name' do + expect(helper.group_name).to eq(context.name) + end + end + + shared_examples 'group info are not populated' do + it 'does not build the correct link' do + expect(helper.seats_usage_link).to eq('') + end + + it 'does not have a group name' do + expect(helper.group_name).to be_nil + end + end + + shared_examples 'alert is displayed' do + it_behaves_like 'learn more link is built' + + it_behaves_like 'seats info are populated' + + it_behaves_like 'group info are populated' + + it 'does show the alert' do + expect(helper.show_seats_count_alert?).to be true + end + end + + shared_examples 'alert is not displayed and some info are populated' do + it_behaves_like 'learn more link is not built' + + it_behaves_like 'seats info are not populated' + + it_behaves_like 'group info are populated' + + it 'does show the alert' do + expect(helper.show_seats_count_alert?).to be false + end + end + + shared_examples 'user is the owner' do + before do + context.add_owner(user) + end + + describe 'with no subscription' do + it_behaves_like 'alert is not displayed and some info are populated' + end + + describe 'with a subscription' do + before do + context.create_gitlab_subscription! + context.gitlab_subscription.update!(seats: 15) + end + + it_behaves_like 'alert is displayed' + end + end + + shared_examples 'common cases for users' do + before do + helper.display_seats_count_alert! + end + + describe 'without a owner' do + before do + context.add_user(user, GroupMember::DEVELOPER) + end + + it_behaves_like 'alert is not displayed and some info are populated' + end + + describe 'with a owner' do + it_behaves_like 'user is the owner' + end + end + + it 'sets @display_seats_count_alert to true' do + expect(helper.instance_variable_get(:@display_seats_count_alert)).to be nil + + helper.display_seats_count_alert! + + expect(helper.instance_variable_get(:@display_seats_count_alert)).to be true + end + + describe 'outside a group or project context' do + before do + helper.display_seats_count_alert! + end + + it_behaves_like 'learn more link is not built' + + it_behaves_like 'seats info are not populated' + + it_behaves_like 'group info are not populated' + + it 'does show the alert' do + expect(helper.show_seats_count_alert?).to be false + end + end + + describe 'within a group context' do + let(:group) { create(:group) } + let(:context) { group } + let(:project) { nil } + + include_examples 'common cases for users' + end + + describe 'within a subgroup context' do + let(:context) { create(:group) } + let(:group) { create(:group, :private, parent: context) } + let(:project) { nil } + + include_examples 'common cases for users' + end + + describe 'within a project context' do + let(:group) { nil } + let(:context) { create(:group) } + let(:project) { create(:project, namespace: context) } + + include_examples 'common cases for users' + end +end diff --git a/locale/gitlab.pot b/locale/gitlab.pot index 08e634d28024e0..612f673d5f4632 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -632,6 +632,9 @@ msgstr "" msgid "%{group_name} group members" msgstr "" +msgid "%{group_name} is approaching the limit of available seats" +msgstr "" + msgid "%{group_name} uses group managed accounts. You need to create a new GitLab account which will be managed by %{group_name}." msgstr "" @@ -39529,6 +39532,9 @@ msgstr[1] "" msgid "View replaced file @ " msgstr "" +msgid "View seat usage" +msgstr "" + msgid "View setting" msgstr "" @@ -41588,6 +41594,9 @@ msgstr "" msgid "Your subscription expired!" msgstr "" +msgid "Your subscription has %{remaining_seats_count} out of %{total_seats_count} seats remaining. Even if you reach the number of seats in your subscription, you can continue to add users, and GitLab will bill you for the overage." +msgstr "" + msgid "Your subscription is now expired. To renew, export your license usage file and email it to %{renewal_service_email}. A new license will be emailed to the email address registered in the %{customers_dot}. You can upload this license to your instance. To use Free tier, remove your current license." msgstr "" diff --git a/spec/helpers/seats_count_alert_helper_spec.rb b/spec/helpers/seats_count_alert_helper_spec.rb new file mode 100644 index 00000000000000..cc332d5e0ff02d --- /dev/null +++ b/spec/helpers/seats_count_alert_helper_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe SeatsCountAlertHelper do + describe 'display_seats_count_alert!' do + it 'is over-written in EE' do + expect { helper.display_seats_count_alert! }.not_to raise_error + end + end +end -- GitLab From 513e049927f2a69674a67c5ccf02eb0189f52f50 Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Fri, 14 Jan 2022 16:42:14 +0100 Subject: [PATCH 02/13] Check for text inside the links --- ee/spec/helpers/ee/seats_count_alert_helper_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb b/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb index 64c200c8299493..4a5111323ee964 100644 --- a/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb +++ b/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb @@ -14,7 +14,7 @@ shared_examples 'learn more link is built' do it 'builds the correct link' do - expect(helper.learn_more_link).to match %r{.*}m + expect(helper.learn_more_link).to match %r{.+}m end end @@ -46,7 +46,7 @@ shared_examples 'group info are populated' do it 'builds the correct link' do - expect(helper.seats_usage_link).to match %r{.*}m + expect(helper.seats_usage_link).to match %r{.+}m end it 'has a group name' do -- GitLab From 1c8139060c0e502f2c193afcda184d9e931fb2b4 Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Fri, 14 Jan 2022 16:46:03 +0100 Subject: [PATCH 03/13] Raise error for overridden method --- app/helpers/seats_count_alert_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/helpers/seats_count_alert_helper.rb b/app/helpers/seats_count_alert_helper.rb index 0278aeb27b5b33..d46ead68cb4c27 100644 --- a/app/helpers/seats_count_alert_helper.rb +++ b/app/helpers/seats_count_alert_helper.rb @@ -3,6 +3,7 @@ module SeatsCountAlertHelper # Overridden in EE def display_seats_count_alert! + raise NotImplementedError end end -- GitLab From 3e5d978249f54600c1a05f1f81fca210065c1fc8 Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Fri, 14 Jan 2022 16:49:51 +0100 Subject: [PATCH 04/13] Improve tests handling and implementation --- ee/app/helpers/ee/seats_count_alert_helper.rb | 7 +- .../ee/seats_count_alert_helper_spec.rb | 88 +++++++++---------- 2 files changed, 46 insertions(+), 49 deletions(-) diff --git a/ee/app/helpers/ee/seats_count_alert_helper.rb b/ee/app/helpers/ee/seats_count_alert_helper.rb index 86f44a7c37ac95..b199ab5836721b 100644 --- a/ee/app/helpers/ee/seats_count_alert_helper.rb +++ b/ee/app/helpers/ee/seats_count_alert_helper.rb @@ -16,6 +16,8 @@ def learn_more_link end def group_name + return unless show_seats_count_alert? + root_namespace&.name end @@ -26,6 +28,7 @@ def remaining_seats_count end def seats_usage_link + return '' unless show_seats_count_alert? return '' if root_namespace.nil? link_to _('View seat usage'), current_usage_quotas_path, class: 'btn gl-alert-action btn-info btn-md gl-button' @@ -34,10 +37,12 @@ def seats_usage_link def show_seats_count_alert? return false if current_subscription.nil? - @display_seats_count_alert + !!@display_seats_count_alert end def total_seats_count + return unless show_seats_count_alert? + current_subscription&.seats end diff --git a/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb b/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb index 4a5111323ee964..7bb9de4424023c 100644 --- a/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb +++ b/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe EE::SeatsCountAlertHelper do +RSpec.describe EE::SeatsCountAlertHelper, :saas do let_it_be(:user) { create(:user) } let_it_be(:group) { nil } let_it_be(:project) { nil } @@ -65,63 +65,59 @@ end shared_examples 'alert is displayed' do - it_behaves_like 'learn more link is built' + include_examples 'learn more link is built' - it_behaves_like 'seats info are populated' + include_examples 'seats info are populated' - it_behaves_like 'group info are populated' + include_examples 'group info are populated' it 'does show the alert' do expect(helper.show_seats_count_alert?).to be true end end - shared_examples 'alert is not displayed and some info are populated' do - it_behaves_like 'learn more link is not built' + shared_examples 'no info are displayed' do + include_examples 'learn more link is not built' - it_behaves_like 'seats info are not populated' + include_examples 'seats info are not populated' - it_behaves_like 'group info are populated' + include_examples 'group info are not populated' it 'does show the alert' do expect(helper.show_seats_count_alert?).to be false end end - shared_examples 'user is the owner' do - before do - context.add_owner(user) - end - - describe 'with no subscription' do - it_behaves_like 'alert is not displayed and some info are populated' + shared_examples 'common cases for users' do + let_it_be(:gitlab_subscription) do + create(:gitlab_subscription, namespace: context, plan_code: Plan::ULTIMATE, seats: 15, seats_in_use: 1) end - describe 'with a subscription' do + describe 'without a owner' do before do - context.create_gitlab_subscription! - context.gitlab_subscription.update!(seats: 15) + context.add_user(user, GroupMember::DEVELOPER) + helper.display_seats_count_alert! end it_behaves_like 'alert is displayed' end - end - - shared_examples 'common cases for users' do - before do - helper.display_seats_count_alert! - end - describe 'without a owner' do + describe 'with a owner' do before do - context.add_user(user, GroupMember::DEVELOPER) + context.add_owner(user) end - it_behaves_like 'alert is not displayed and some info are populated' - end + context 'without display seats count' do + include_examples 'no info are displayed' + end - describe 'with a owner' do - it_behaves_like 'user is the owner' + context 'with display seats count' do + before do + helper.display_seats_count_alert! + end + + it_behaves_like 'alert is displayed' + end end end @@ -133,42 +129,38 @@ expect(helper.instance_variable_get(:@display_seats_count_alert)).to be true end + describe 'with no subscription' do + include_examples 'no info are displayed' + end + describe 'outside a group or project context' do before do helper.display_seats_count_alert! end - it_behaves_like 'learn more link is not built' - - it_behaves_like 'seats info are not populated' - - it_behaves_like 'group info are not populated' - - it 'does show the alert' do - expect(helper.show_seats_count_alert?).to be false - end + include_examples 'no info are displayed' end describe 'within a group context' do - let(:group) { create(:group) } - let(:context) { group } - let(:project) { nil } + let_it_be(:group) { create(:group) } + let_it_be(:context) { group } + let_it_be(:project) { nil } include_examples 'common cases for users' end describe 'within a subgroup context' do - let(:context) { create(:group) } - let(:group) { create(:group, :private, parent: context) } - let(:project) { nil } + let_it_be(:context) { create(:group) } + let_it_be(:group) { create(:group, parent: context) } + let_it_be(:project) { nil } include_examples 'common cases for users' end describe 'within a project context' do - let(:group) { nil } - let(:context) { create(:group) } - let(:project) { create(:project, namespace: context) } + let_it_be(:group) { nil } + let_it_be(:context) { create(:group) } + let_it_be(:project) { create(:project, namespace: context) } include_examples 'common cases for users' end -- GitLab From 2060bbdf0c76f36b9ed44d1c6177bdd2f104eca5 Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Mon, 17 Jan 2022 08:35:19 +0100 Subject: [PATCH 05/13] Simplify root namespace access --- ee/app/helpers/ee/seats_count_alert_helper.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ee/app/helpers/ee/seats_count_alert_helper.rb b/ee/app/helpers/ee/seats_count_alert_helper.rb index b199ab5836721b..736268977ab60a 100644 --- a/ee/app/helpers/ee/seats_count_alert_helper.rb +++ b/ee/app/helpers/ee/seats_count_alert_helper.rb @@ -49,9 +49,7 @@ def total_seats_count private def root_namespace - return @project.namespace.root_ancestor unless @project&.namespace.nil? - - @group&.root_ancestor + @project.&root_ancestor || @group&.root_ancestor end def current_subscription -- GitLab From 13b5738aca4598f221d42fab044a749146917237 Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Mon, 17 Jan 2022 09:40:21 +0100 Subject: [PATCH 06/13] Fixup: correct spec name --- ee/spec/helpers/ee/seats_count_alert_helper_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb b/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb index 7bb9de4424023c..460222a66682dd 100644 --- a/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb +++ b/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb @@ -83,7 +83,7 @@ include_examples 'group info are not populated' - it 'does show the alert' do + it 'does not show the alert' do expect(helper.show_seats_count_alert?).to be false end end -- GitLab From 7ef54ae0735d344ee1e453cfeec5c637bf920314 Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Mon, 17 Jan 2022 14:15:57 +0100 Subject: [PATCH 07/13] Fixup: remove some display logic from the helper --- ee/app/helpers/ee/seats_count_alert_helper.rb | 10 +---- .../ee/seats_count_alert_helper_spec.rb | 40 ++++++++++++------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/ee/app/helpers/ee/seats_count_alert_helper.rb b/ee/app/helpers/ee/seats_count_alert_helper.rb index 736268977ab60a..3c4368b1073a67 100644 --- a/ee/app/helpers/ee/seats_count_alert_helper.rb +++ b/ee/app/helpers/ee/seats_count_alert_helper.rb @@ -10,14 +10,10 @@ def display_seats_count_alert! end def learn_more_link - return '' unless show_seats_count_alert? - link_to _('Learn more.'), help_page_path('subscriptions/quarterly_reconciliation'), target: '_blank', rel: 'noopener noreferrer' end def group_name - return unless show_seats_count_alert? - root_namespace&.name end @@ -28,28 +24,26 @@ def remaining_seats_count end def seats_usage_link - return '' unless show_seats_count_alert? return '' if root_namespace.nil? link_to _('View seat usage'), current_usage_quotas_path, class: 'btn gl-alert-action btn-info btn-md gl-button' end def show_seats_count_alert? + return false unless root_namespace.has_owner?(current_user) return false if current_subscription.nil? !!@display_seats_count_alert end def total_seats_count - return unless show_seats_count_alert? - current_subscription&.seats end private def root_namespace - @project.&root_ancestor || @group&.root_ancestor + @project&.root_ancestor || @group&.root_ancestor end def current_subscription diff --git a/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb b/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb index 460222a66682dd..65b04c9ca9c009 100644 --- a/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb +++ b/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb @@ -3,9 +3,9 @@ require 'spec_helper' RSpec.describe EE::SeatsCountAlertHelper, :saas do - let_it_be(:user) { create(:user) } let_it_be(:group) { nil } let_it_be(:project) { nil } + let_it_be(:user) { create(:user) } before do assign(:project, project) @@ -18,12 +18,6 @@ end end - shared_examples 'learn more link is not built' do - it 'builds the correct link' do - expect(helper.learn_more_link).to eq('') - end - end - shared_examples 'seats info are not populated' do it 'sets remaining seats count to nil' do expect(helper.remaining_seats_count).to be_nil @@ -76,12 +70,12 @@ end end - shared_examples 'no info are displayed' do - include_examples 'learn more link is not built' + shared_examples 'alert is not displayed' do + include_examples 'learn more link is built' - include_examples 'seats info are not populated' + include_examples 'seats info are populated' - include_examples 'group info are not populated' + include_examples 'group info are populated' it 'does not show the alert' do expect(helper.show_seats_count_alert?).to be false @@ -99,7 +93,7 @@ helper.display_seats_count_alert! end - it_behaves_like 'alert is displayed' + it_behaves_like 'alert is not displayed' end describe 'with a owner' do @@ -108,7 +102,7 @@ end context 'without display seats count' do - include_examples 'no info are displayed' + include_examples 'alert is not displayed' end context 'with display seats count' do @@ -130,7 +124,15 @@ end describe 'with no subscription' do - include_examples 'no info are displayed' + include_examples 'learn more link is built' + + include_examples 'seats info are not populated' + + include_examples 'group info are populated' + + it 'does not show the alert' do + expect(helper.show_seats_count_alert?).to be false + end end describe 'outside a group or project context' do @@ -138,7 +140,15 @@ helper.display_seats_count_alert! end - include_examples 'no info are displayed' + include_examples 'learn more link is built' + + include_examples 'seats info are not populated' + + include_examples 'group info are populated' + + it 'does not show the alert' do + expect(helper.show_seats_count_alert?).to be false + end end describe 'within a group context' do -- GitLab From 5ff91b7c66c1098a0fa9524ab82659b1aa22e60d Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Mon, 17 Jan 2022 16:04:53 +0100 Subject: [PATCH 08/13] Fixup: move the helper out of FOSS context --- app/helpers/seats_count_alert_helper.rb | 10 --- ee/app/helpers/ee/seats_count_alert_helper.rb | 61 ------------------- ee/app/helpers/seats_count_alert_helper.rb | 56 +++++++++++++++++ .../{ee => }/seats_count_alert_helper_spec.rb | 41 ++++++------- spec/helpers/seats_count_alert_helper_spec.rb | 11 ---- 5 files changed, 76 insertions(+), 103 deletions(-) delete mode 100644 app/helpers/seats_count_alert_helper.rb delete mode 100644 ee/app/helpers/ee/seats_count_alert_helper.rb create mode 100644 ee/app/helpers/seats_count_alert_helper.rb rename ee/spec/helpers/{ee => }/seats_count_alert_helper_spec.rb (87%) delete mode 100644 spec/helpers/seats_count_alert_helper_spec.rb diff --git a/app/helpers/seats_count_alert_helper.rb b/app/helpers/seats_count_alert_helper.rb deleted file mode 100644 index d46ead68cb4c27..00000000000000 --- a/app/helpers/seats_count_alert_helper.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -module SeatsCountAlertHelper - # Overridden in EE - def display_seats_count_alert! - raise NotImplementedError - end -end - -SeatsCountAlertHelper.prepend_mod_with('SeatsCountAlertHelper') diff --git a/ee/app/helpers/ee/seats_count_alert_helper.rb b/ee/app/helpers/ee/seats_count_alert_helper.rb deleted file mode 100644 index 3c4368b1073a67..00000000000000 --- a/ee/app/helpers/ee/seats_count_alert_helper.rb +++ /dev/null @@ -1,61 +0,0 @@ -# frozen_string_literal: true - -module EE - module SeatsCountAlertHelper - extend ::Gitlab::Utils::Override - - override :display_seats_count_alert! - def display_seats_count_alert! - @display_seats_count_alert = true - end - - def learn_more_link - link_to _('Learn more.'), help_page_path('subscriptions/quarterly_reconciliation'), target: '_blank', rel: 'noopener noreferrer' - end - - def group_name - root_namespace&.name - end - - def remaining_seats_count - return if total_seats_count.nil? || seats_in_use.nil? - - total_seats_count - seats_in_use - end - - def seats_usage_link - return '' if root_namespace.nil? - - link_to _('View seat usage'), current_usage_quotas_path, class: 'btn gl-alert-action btn-info btn-md gl-button' - end - - def show_seats_count_alert? - return false unless root_namespace.has_owner?(current_user) - return false if current_subscription.nil? - - !!@display_seats_count_alert - end - - def total_seats_count - current_subscription&.seats - end - - private - - def root_namespace - @project&.root_ancestor || @group&.root_ancestor - end - - def current_subscription - root_namespace&.gitlab_subscription - end - - def seats_in_use - current_subscription&.seats_in_use - end - - def current_usage_quotas_path - usage_quotas_path(root_namespace, anchor: 'seats-quota-tab') - end - end -end diff --git a/ee/app/helpers/seats_count_alert_helper.rb b/ee/app/helpers/seats_count_alert_helper.rb new file mode 100644 index 00000000000000..3adb05a16ab28e --- /dev/null +++ b/ee/app/helpers/seats_count_alert_helper.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +module SeatsCountAlertHelper + def display_seats_count_alert! + @display_seats_count_alert = true + end + + def learn_more_link + link_to _('Learn more.'), help_page_path('subscriptions/quarterly_reconciliation'), target: '_blank', rel: 'noopener noreferrer' + end + + def group_name + root_namespace&.name + end + + def remaining_seats_count + return if total_seats_count.nil? || seats_in_use.nil? + + total_seats_count - seats_in_use + end + + def seats_usage_link + return '' if root_namespace.nil? + + link_to _('View seat usage'), current_usage_quotas_path, class: 'btn gl-alert-action btn-info btn-md gl-button' + end + + def show_seats_count_alert? + return false unless root_namespace&.has_owner?(current_user) + return false if current_subscription.nil? + + !!@display_seats_count_alert + end + + def total_seats_count + current_subscription&.seats + end + + private + + def root_namespace + @project&.root_ancestor || @group&.root_ancestor + end + + def current_subscription + root_namespace&.gitlab_subscription + end + + def seats_in_use + current_subscription&.seats_in_use + end + + def current_usage_quotas_path + usage_quotas_path(root_namespace, anchor: 'seats-quota-tab') + end +end diff --git a/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb b/ee/spec/helpers/seats_count_alert_helper_spec.rb similarity index 87% rename from ee/spec/helpers/ee/seats_count_alert_helper_spec.rb rename to ee/spec/helpers/seats_count_alert_helper_spec.rb index 65b04c9ca9c009..992a1b915d18bb 100644 --- a/ee/spec/helpers/ee/seats_count_alert_helper_spec.rb +++ b/ee/spec/helpers/seats_count_alert_helper_spec.rb @@ -2,7 +2,9 @@ require 'spec_helper' -RSpec.describe EE::SeatsCountAlertHelper, :saas do +RSpec.describe SeatsCountAlertHelper, :saas do + include Devise::Test::ControllerHelpers + let_it_be(:group) { nil } let_it_be(:project) { nil } let_it_be(:user) { create(:user) } @@ -10,6 +12,7 @@ before do assign(:project, project) assign(:group, group) + allow(helper).to receive(:current_user).and_return(user) end shared_examples 'learn more link is built' do @@ -58,6 +61,18 @@ end end + shared_examples 'alert is not displayed while some info are' do + it_behaves_like 'learn more link is built' + + it_behaves_like 'seats info are not populated' + + it_behaves_like 'group info are not populated' + + it 'does not show the alert' do + expect(helper.show_seats_count_alert?).to be false + end + end + shared_examples 'alert is displayed' do include_examples 'learn more link is built' @@ -93,7 +108,7 @@ helper.display_seats_count_alert! end - it_behaves_like 'alert is not displayed' + include_examples 'alert is not displayed' end describe 'with a owner' do @@ -110,7 +125,7 @@ helper.display_seats_count_alert! end - it_behaves_like 'alert is displayed' + include_examples 'alert is displayed' end end end @@ -124,15 +139,7 @@ end describe 'with no subscription' do - include_examples 'learn more link is built' - - include_examples 'seats info are not populated' - - include_examples 'group info are populated' - - it 'does not show the alert' do - expect(helper.show_seats_count_alert?).to be false - end + include_examples 'alert is not displayed while some info are' end describe 'outside a group or project context' do @@ -140,15 +147,7 @@ helper.display_seats_count_alert! end - include_examples 'learn more link is built' - - include_examples 'seats info are not populated' - - include_examples 'group info are populated' - - it 'does not show the alert' do - expect(helper.show_seats_count_alert?).to be false - end + include_examples 'alert is not displayed while some info are' end describe 'within a group context' do diff --git a/spec/helpers/seats_count_alert_helper_spec.rb b/spec/helpers/seats_count_alert_helper_spec.rb deleted file mode 100644 index cc332d5e0ff02d..00000000000000 --- a/spec/helpers/seats_count_alert_helper_spec.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe SeatsCountAlertHelper do - describe 'display_seats_count_alert!' do - it 'is over-written in EE' do - expect { helper.display_seats_count_alert! }.not_to raise_error - end - end -end -- GitLab From 90d570b245c05b32485e1636f6baed75fb50fdb0 Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Thu, 20 Jan 2022 15:23:40 +0100 Subject: [PATCH 09/13] Fixup: turn string to nil --- ee/app/helpers/seats_count_alert_helper.rb | 2 +- ee/spec/helpers/seats_count_alert_helper_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ee/app/helpers/seats_count_alert_helper.rb b/ee/app/helpers/seats_count_alert_helper.rb index 3adb05a16ab28e..1cb2a08523032d 100644 --- a/ee/app/helpers/seats_count_alert_helper.rb +++ b/ee/app/helpers/seats_count_alert_helper.rb @@ -20,7 +20,7 @@ def remaining_seats_count end def seats_usage_link - return '' if root_namespace.nil? + return if root_namespace.nil? link_to _('View seat usage'), current_usage_quotas_path, class: 'btn gl-alert-action btn-info btn-md gl-button' end diff --git a/ee/spec/helpers/seats_count_alert_helper_spec.rb b/ee/spec/helpers/seats_count_alert_helper_spec.rb index 992a1b915d18bb..f64d5eea4e9e2c 100644 --- a/ee/spec/helpers/seats_count_alert_helper_spec.rb +++ b/ee/spec/helpers/seats_count_alert_helper_spec.rb @@ -53,7 +53,7 @@ shared_examples 'group info are not populated' do it 'does not build the correct link' do - expect(helper.seats_usage_link).to eq('') + expect(helper.seats_usage_link).to be_nil end it 'does not have a group name' do -- GitLab From 1c9c6667b17c96846a0a1e5de7d981cc14ccf08a Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Thu, 20 Jan 2022 15:58:31 +0100 Subject: [PATCH 10/13] Fixup: Exclude user namespaces from check --- ee/app/helpers/seats_count_alert_helper.rb | 1 + ee/spec/helpers/seats_count_alert_helper_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/ee/app/helpers/seats_count_alert_helper.rb b/ee/app/helpers/seats_count_alert_helper.rb index 1cb2a08523032d..104ec4133fb630 100644 --- a/ee/app/helpers/seats_count_alert_helper.rb +++ b/ee/app/helpers/seats_count_alert_helper.rb @@ -26,6 +26,7 @@ def seats_usage_link end def show_seats_count_alert? + return false unless root_namespace&.group_namespace? return false unless root_namespace&.has_owner?(current_user) return false if current_subscription.nil? diff --git a/ee/spec/helpers/seats_count_alert_helper_spec.rb b/ee/spec/helpers/seats_count_alert_helper_spec.rb index f64d5eea4e9e2c..009918a254182f 100644 --- a/ee/spec/helpers/seats_count_alert_helper_spec.rb +++ b/ee/spec/helpers/seats_count_alert_helper_spec.rb @@ -173,4 +173,16 @@ include_examples 'common cases for users' end + + describe 'within a user namespace context' do + let_it_be(:project) { create(:project) } + + before do + helper.display_seats_count_alert! + end + + it 'does show the alert' do + expect(helper.show_seats_count_alert?).to be false + end + end end -- GitLab From 5d217ec1803c9e2de1338eeb9b0a6568c6a56da0 Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Tue, 25 Jan 2022 12:48:49 +0100 Subject: [PATCH 11/13] Fixup: reverse conditional --- ee/app/helpers/seats_count_alert_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ee/app/helpers/seats_count_alert_helper.rb b/ee/app/helpers/seats_count_alert_helper.rb index 104ec4133fb630..059a0339224010 100644 --- a/ee/app/helpers/seats_count_alert_helper.rb +++ b/ee/app/helpers/seats_count_alert_helper.rb @@ -28,7 +28,7 @@ def seats_usage_link def show_seats_count_alert? return false unless root_namespace&.group_namespace? return false unless root_namespace&.has_owner?(current_user) - return false if current_subscription.nil? + return false unless current_subscription !!@display_seats_count_alert end -- GitLab From 4557437ad3a0f189426de711fadf2b95e07c7eb8 Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Tue, 25 Jan 2022 12:49:38 +0100 Subject: [PATCH 12/13] Fixup: reverse conditional with logic operator --- ee/app/helpers/seats_count_alert_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ee/app/helpers/seats_count_alert_helper.rb b/ee/app/helpers/seats_count_alert_helper.rb index 059a0339224010..9afa1f0ac510eb 100644 --- a/ee/app/helpers/seats_count_alert_helper.rb +++ b/ee/app/helpers/seats_count_alert_helper.rb @@ -14,7 +14,7 @@ def group_name end def remaining_seats_count - return if total_seats_count.nil? || seats_in_use.nil? + return unless total_seats_count && seats_in_use total_seats_count - seats_in_use end -- GitLab From 46165de5255784e122286f41b294d83b7c0e3c5b Mon Sep 17 00:00:00 2001 From: Angelo Gulina Date: Tue, 25 Jan 2022 12:51:21 +0100 Subject: [PATCH 13/13] Fixup: Reverse conditional clause --- ee/app/helpers/seats_count_alert_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ee/app/helpers/seats_count_alert_helper.rb b/ee/app/helpers/seats_count_alert_helper.rb index 9afa1f0ac510eb..33b42eeb7d67d2 100644 --- a/ee/app/helpers/seats_count_alert_helper.rb +++ b/ee/app/helpers/seats_count_alert_helper.rb @@ -20,7 +20,7 @@ def remaining_seats_count end def seats_usage_link - return if root_namespace.nil? + return unless root_namespace link_to _('View seat usage'), current_usage_quotas_path, class: 'btn gl-alert-action btn-info btn-md gl-button' end -- GitLab