Commit 3f0a0af6 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'vs/reactivate-extend-trial-helper' into 'master'

Add helper methods for extending and reactivating trial

See merge request gitlab-org/gitlab!66040
parents a7a90663 3afd7062
......@@ -56,7 +56,7 @@ class TrialsController < ApplicationController
end
def extend_reactivate
render_404 unless Feature.enabled?(:allow_extend_reactivate_trial)
render_404 unless Feature.enabled?(:allow_extend_reactivate_trial, default_enabled: :yaml)
result = GitlabSubscriptions::ExtendReactivateTrialService.new.execute(extend_reactivate_trial_params) if valid_extension?
......
......@@ -52,6 +52,28 @@ module EE
namespace&.errors&.full_messages&.to_sentence&.presence || service_result&.dig(:errors)&.presence
end
def show_extend_reactivate_trial_button?(namespace)
return false unless ::Feature.enabled?(:allow_extend_reactivate_trial, default_enabled: :yaml)
namespace.can_extend_trial? || namespace.can_reactivate_trial?
end
def extend_reactivate_trial_button_data(namespace)
action = if namespace.can_extend_trial?
'extend'
elsif namespace.can_reactivate_trial?
'reactivate'
else
nil
end
{
namespace_id: namespace.id,
plan_name: namespace.actual_plan_name.titleize,
action: action
}
end
private
def trial_group_namespaces
......
......@@ -303,10 +303,14 @@ module EE
end
def can_extend_trial?
return false unless ::Feature.enabled?(:allow_extend_reactivate_trial, default_enabled: :yaml)
trial_active? && !trial_extended_or_reactivated?
end
def can_reactivate_trial?
return false unless ::Feature.enabled?(:allow_extend_reactivate_trial, default_enabled: :yaml)
!trial_active? && !never_had_trial? && !trial_extended_or_reactivated? && free_plan?
end
......
......@@ -180,4 +180,95 @@ RSpec.describe EE::TrialHelper do
end
end
end
describe '#show_extend_reactivate_trial_button?' do
let(:namespace) { build(:namespace) }
subject(:show_extend_reactivate_trial_button) { helper.show_extend_reactivate_trial_button?(namespace) }
context 'when feature flag is disabled' do
before do
allow(namespace).to receive(:can_extend_trial?).and_return(true)
allow(namespace).to receive(:can_reactivate_trial?).and_return(true)
stub_feature_flags(allow_extend_reactivate_trial: false)
end
it { is_expected.to be_falsey }
end
context 'when feature flag is enabled' do
where(:can_extend_trial, :can_reactivate_trial, :result) do
false | false | false
true | false | true
false | true | true
true | true | true
end
with_them do
before do
stub_feature_flags(allow_extend_reactivate_trial: true)
allow(namespace).to receive(:can_extend_trial?).and_return(can_extend_trial)
allow(namespace).to receive(:can_reactivate_trial?).and_return(can_reactivate_trial)
end
it { is_expected.to eq(result) }
end
end
end
describe '#extend_reactivate_trial_button_data' do
let(:namespace) { build(:namespace, id: 1) }
subject(:extend_reactivate_trial_button_data) { helper.extend_reactivate_trial_button_data(namespace) }
before do
allow(namespace).to receive(:actual_plan_name).and_return('ultimate')
end
context 'when feature flag is disabled' do
before do
stub_feature_flags(allow_extend_reactivate_trial: false)
end
context 'when trial can be extended' do
before do
allow(namespace).to receive(:trial_active?).and_return(true)
allow(namespace).to receive(:trial_extended_or_reactivated?).and_return(false)
end
it { is_expected.to eq({ namespace_id: 1, plan_name: 'Ultimate', action: nil })}
end
context 'when trial can be reactivated' do
before do
allow(namespace).to receive(:trial_active?).and_return(false)
allow(namespace).to receive(:never_had_trial?).and_return(false)
allow(namespace).to receive(:trial_extended_or_reactivated?).and_return(false)
allow(namespace).to receive(:free_plan?).and_return(true)
end
it { is_expected.to eq({ namespace_id: 1, plan_name: 'Ultimate', action: nil }) }
end
end
context 'when feature flag is enabled' do
context 'when trial can be extended' do
before do
allow(namespace).to receive(:can_extend_trial?).and_return(true)
end
it { is_expected.to eq({ namespace_id: 1, plan_name: 'Ultimate', action: 'extend' }) }
end
context 'when trial can be reactivated' do
before do
allow(namespace).to receive(:can_reactivate_trial?).and_return(true)
end
it { is_expected.to eq({ namespace_id: 1, plan_name: 'Ultimate', action: 'reactivate' }) }
end
end
end
end
......@@ -1232,54 +1232,82 @@ RSpec.describe Namespace do
describe '#can_extend_trial?' do
subject { namespace.can_extend_trial? }
where(:trial_active, :trial_extended_or_reactivated, :can_extend_trial) do
false | false | false
false | true | false
true | false | true
true | true | false
context 'feature flag is disabled' do
before do
allow(namespace).to receive(:trial_active?).and_return(true)
allow(namespace).to receive(:trial_extended_or_reactivated?).and_return(false)
stub_feature_flags(allow_extend_reactivate_trial: false)
end
it { is_expected.to be_falsey }
end
with_them do
before do
allow(namespace).to receive(:trial_active?).and_return(trial_active)
allow(namespace).to receive(:trial_extended_or_reactivated?).and_return(trial_extended_or_reactivated)
context 'when feature flag is enabled' do
where(:trial_active, :trial_extended_or_reactivated, :can_extend_trial) do
false | false | false
false | true | false
true | false | true
true | true | false
end
it { is_expected.to be can_extend_trial }
with_them do
before do
allow(namespace).to receive(:trial_active?).and_return(trial_active)
allow(namespace).to receive(:trial_extended_or_reactivated?).and_return(trial_extended_or_reactivated)
end
it { is_expected.to be can_extend_trial }
end
end
end
describe '#can_reactivate_trial?' do
subject { namespace.can_reactivate_trial? }
where(:trial_active, :never_had_trial, :trial_extended_or_reactivated, :free_plan, :can_reactivate_trial) do
false | false | false | false | false
false | false | false | true | true
false | false | true | false | false
false | false | true | true | false
false | true | false | false | false
false | true | false | true | false
false | true | true | false | false
false | true | true | true | false
true | false | false | false | false
true | false | false | true | false
true | false | true | false | false
true | false | true | true | false
true | true | false | false | false
true | true | false | true | false
true | true | true | false | false
true | true | true | true | false
context 'when feature flag is disabled' do
before do
allow(namespace).to receive(:trial_active?).and_return(false)
allow(namespace).to receive(:never_had_trial?).and_return(false)
allow(namespace).to receive(:trial_extended_or_reactivated?).and_return(false)
allow(namespace).to receive(:free_plan?).and_return(true)
stub_feature_flags(allow_extend_reactivate_trial: false)
end
it { is_expected.to be_falsey }
end
with_them do
before do
allow(namespace).to receive(:trial_active?).and_return(trial_active)
allow(namespace).to receive(:never_had_trial?).and_return(never_had_trial)
allow(namespace).to receive(:trial_extended_or_reactivated?).and_return(trial_extended_or_reactivated)
allow(namespace).to receive(:free_plan?).and_return(free_plan)
context 'when feature flag is enabled' do
where(:trial_active, :never_had_trial, :trial_extended_or_reactivated, :free_plan, :can_reactivate_trial) do
false | false | false | false | false
false | false | false | true | true
false | false | true | false | false
false | false | true | true | false
false | true | false | false | false
false | true | false | true | false
false | true | true | false | false
false | true | true | true | false
true | false | false | false | false
true | false | false | true | false
true | false | true | false | false
true | false | true | true | false
true | true | false | false | false
true | true | false | true | false
true | true | true | false | false
true | true | true | true | false
end
it { is_expected.to be can_reactivate_trial }
with_them do
before do
allow(namespace).to receive(:trial_active?).and_return(trial_active)
allow(namespace).to receive(:never_had_trial?).and_return(never_had_trial)
allow(namespace).to receive(:trial_extended_or_reactivated?).and_return(trial_extended_or_reactivated)
allow(namespace).to receive(:free_plan?).and_return(free_plan)
end
it { is_expected.to be can_reactivate_trial }
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