Commit bc6e5559 authored by Robert Speicher's avatar Robert Speicher

Merge branch '299949-limit-trial-status-widget-experiment-participants' into 'master'

Limit trial status widget experiment participants to group owners

See merge request gitlab-org/gitlab!52537
parents b0a9871c 2959c22b
# frozen_string_literal: true # frozen_string_literal: true
module TrialStatusWidgetHelper module TrialStatusWidgetHelper
def eligible_for_trial_status_widget_experiment?(group)
group.trial_active?
end
def show_trial_status_widget?(group) def show_trial_status_widget?(group)
return false unless ::Gitlab::CurrentSettings.should_check_namespace_plan? billing_plans_and_trials_available? &&
return false unless experiment_enabled?(:show_trial_status_in_sidebar, subject: group) trial_status_widget_experiment_enabled?(group) &&
group.trial_active? &&
eligible_for_trial_status_widget_experiment?(group) user_can_administer_group?(group)
end end
def trial_days_remaining_in_words(group) def trial_days_remaining_in_words(group)
...@@ -22,4 +18,18 @@ module TrialStatusWidgetHelper ...@@ -22,4 +18,18 @@ module TrialStatusWidgetHelper
num_of_days num_of_days
) % { plan: plan_title, num: num_of_days, en_dash: '–' } ) % { plan: plan_title, num: num_of_days, en_dash: '–' }
end end
private
def billing_plans_and_trials_available?
::Gitlab::CurrentSettings.should_check_namespace_plan?
end
def trial_status_widget_experiment_enabled?(group)
experiment_enabled?(:show_trial_status_in_sidebar, subject: group)
end
def user_can_administer_group?(group)
can?(current_user, :admin_namespace, group)
end
end end
- return unless show_trial_status_widget?(group) - root_group = group.root_ancestor
- return unless show_trial_status_widget?(root_group)
= nav_link do = nav_link do
#js-trial-status-widget{ data: { href: group_billings_path(group), #js-trial-status-widget{ data: { href: group_billings_path(root_group),
nav_icon_image_path: image_path('illustrations/golden_tanuki.svg'), nav_icon_image_path: image_path('illustrations/golden_tanuki.svg'),
title: trial_days_remaining_in_words(group), title: trial_days_remaining_in_words(root_group),
percentage_complete: group.trial_percentage_complete } } percentage_complete: root_group.trial_percentage_complete } }
...@@ -3,64 +3,65 @@ ...@@ -3,64 +3,65 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe TrialStatusWidgetHelper do RSpec.describe TrialStatusWidgetHelper do
let_it_be(:group) { build(:group) } describe '#show_trial_status_widget?' do
let!(:subscription) { build(:gitlab_subscription, :active_trial, namespace: group) } let_it_be(:user) { create(:user) }
describe '#eligible_for_trial_status_widget_experiment?' do let(:trials_available) { true }
subject { helper.eligible_for_trial_status_widget_experiment?(group) } let(:experiment_enabled) { true }
let(:trial_active) { true }
let(:user_can_admin_group) { true }
let(:group) { instance_double(Group, trial_active?: trial_active) }
context 'when group has an active trial' do before do
it { is_expected.to be_truthy } # current_user
end allow(helper).to receive(:current_user).and_return(user)
context 'when group does not have an active trial' do # billing_plans_and_trials_available?
before do stub_application_setting(check_namespace_plan: trials_available)
allow(group).to receive(:trial_active?).and_return(false)
end
it { is_expected.to be_falsy } # trial_status_widget_experiment_enabled?(group)
allow(helper).to receive(:experiment_enabled?).with(:show_trial_status_in_sidebar, subject: group).and_return(experiment_enabled)
# user_can_administer_group?(group)
allow(helper).to receive(:can?).and_call_original
allow(helper).to receive(:can?).with(user, :admin_namespace, group).and_return(user_can_admin_group)
end end
end
describe '#show_trial_status_widget?' do subject { helper.show_trial_status_widget?(group) }
let(:experiment_enabled) { true }
let(:eligible_for_experiment) { true }
before do context 'when all requirements are met for the widget to be shown' do
allow(helper).to receive(:experiment_enabled?).with(:show_trial_status_in_sidebar, subject: group).and_return(experiment_enabled) it { is_expected.to be_truthy }
allow(helper).to receive(:eligible_for_trial_status_widget_experiment?).and_return(eligible_for_experiment)
end end
subject { helper.show_trial_status_widget?(group) } context 'when the app is not configured for billing plans & trials' do
let(:trials_available) { false }
context 'when the check_namespace_plan application setting is off' do it { is_expected.to be_falsey }
it { is_expected.to be_falsy }
end end
context 'when the check_namespace_plan application setting is on' do context 'when the experiment is not active or not enabled for the group' do
before do let(:experiment_enabled) { false }
stub_application_setting(check_namespace_plan: true)
end
context 'and the experiment is enabled and the user is eligible for it' do it { is_expected.to be_falsey }
it { is_expected.to be_truthy } end
end
context 'but the experiment is not enabled' do context 'when the group is not in an active trial' do
let(:experiment_enabled) { false } let(:trial_active) { false }
it { is_expected.to be_falsy } it { is_expected.to be_falsey }
end end
context 'but the user is not eligible for the experiment' do context 'when the user is not an admin/owner of the group' do
let(:eligible_for_experiment) { false } let(:user_can_admin_group) { false }
it { is_expected.to be_falsy } it { is_expected.to be_falsey }
end
end end
end end
describe '#trial_days_remaining_in_words' do describe '#trial_days_remaining_in_words' do
let_it_be(:group) { build(:group) }
let!(:subscription) { build(:gitlab_subscription, :active_trial, namespace: group) }
subject { helper.trial_days_remaining_in_words(group) } subject { helper.trial_days_remaining_in_words(group) }
context 'when there are 0 days remaining' do context 'when there are 0 days remaining' do
......
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