diff --git a/ee/app/controllers/gitlab_subscriptions/trials/duo_enterprise_controller.rb b/ee/app/controllers/gitlab_subscriptions/trials/duo_enterprise_controller.rb index cb972b68a85faea6267c5dbf30b8704e2ee0e7d3..47d07895c9a33b0858502c5f2b7d64590d538c46 100644 --- a/ee/app/controllers/gitlab_subscriptions/trials/duo_enterprise_controller.rb +++ b/ee/app/controllers/gitlab_subscriptions/trials/duo_enterprise_controller.rb @@ -82,6 +82,11 @@ def trial_params end def success_flash_message(add_on_purchase) + # Added due to this issue we do not know why it happens for premium subscription cases + # https://gitlab.com/gitlab-org/gitlab/-/issues/492646#note_2163935257 + # So for now we'll default back to 60 days in case it isn't synchronized. + # https://gitlab.com/gitlab-org/gitlab/-/issues/499720 + expires_date = add_on_purchase&.expires_on.presence || 60.days.from_now safe_format( s_( 'DuoEnterpriseTrial|You have successfully started a Duo Enterprise trial that will ' \ @@ -89,7 +94,7 @@ def success_flash_message(add_on_purchase) '%{assign_link_start}assign them%{assign_link_end} to GitLab Duo Enterprise seats.' ), success_doc_link, - exp_date: l(add_on_purchase.expires_on.to_date, format: :long) + exp_date: l(expires_date.to_date, format: :long) ) end end diff --git a/ee/app/controllers/gitlab_subscriptions/trials/duo_pro_controller.rb b/ee/app/controllers/gitlab_subscriptions/trials/duo_pro_controller.rb index 92042ea59da7cfe777258a97fc479f2050536882..68aa42e958e861d084995d4ed9eaada4fa95aedf 100644 --- a/ee/app/controllers/gitlab_subscriptions/trials/duo_pro_controller.rb +++ b/ee/app/controllers/gitlab_subscriptions/trials/duo_pro_controller.rb @@ -71,6 +71,11 @@ def trial_params end def success_flash_message(add_on_purchase) + # Added due to this issue we do not know why it happens for premium subscription cases + # https://gitlab.com/gitlab-org/gitlab/-/issues/492646#note_2163935257 + # So for now we'll default back to 60 days in case it isn't synchronized. + # https://gitlab.com/gitlab-org/gitlab/-/issues/499720 + expires_date = add_on_purchase&.expires_on.presence || 60.days.from_now safe_format( s_( 'DuoProTrial|You have successfully started a Duo Pro trial that will ' \ @@ -78,7 +83,7 @@ def success_flash_message(add_on_purchase) '%{assign_link_start}assign them%{assign_link_end} to GitLab Duo Pro seats.' ), success_doc_link, - exp_date: l(add_on_purchase.expires_on.to_date, format: :long) + exp_date: l(expires_date.to_date, format: :long) ) end end diff --git a/ee/app/controllers/gitlab_subscriptions/trials_controller.rb b/ee/app/controllers/gitlab_subscriptions/trials_controller.rb index bf9536289faa8200b98a9884a3f226381a2d1ad1..39288ce3fd732cf5c3f665d6f8b6b857ee0c9cfc 100644 --- a/ee/app/controllers/gitlab_subscriptions/trials_controller.rb +++ b/ee/app/controllers/gitlab_subscriptions/trials_controller.rb @@ -106,6 +106,11 @@ def success_flash_message(add_on_purchase) if discover_group_security_flow? || Feature.disabled?(:duo_enterprise_trials, current_user) s_("BillingPlans|Congratulations, your free trial is activated.") else + # Added due to this issue we do not know why it happens for premium subscription cases + # https://gitlab.com/gitlab-org/gitlab/-/issues/492646#note_2163935257 + # So for now we'll default back to 60 days in case it isn't synchronized. + # https://gitlab.com/gitlab-org/gitlab/-/issues/499720 + expires_date = add_on_purchase&.expires_on.presence || 60.days.from_now safe_format( s_( "BillingPlans|You have successfully started an Ultimate and GitLab Duo Enterprise trial that will " \ @@ -120,7 +125,7 @@ def success_flash_message(add_on_purchase) ), :assign_link_start, :assign_link_end ), - exp_date: l(add_on_purchase.expires_on.to_date, format: :long) + exp_date: l(expires_date.to_date, format: :long) ) end end diff --git a/ee/spec/requests/gitlab_subscriptions/trials/duo_enterprise_controller_spec.rb b/ee/spec/requests/gitlab_subscriptions/trials/duo_enterprise_controller_spec.rb index eb25a762f680888383538383c00e357c9e3a61d3..e87b8fc53ad043250f3b0aaa7eedd6dba1fed681 100644 --- a/ee/spec/requests/gitlab_subscriptions/trials/duo_enterprise_controller_spec.rb +++ b/ee/spec/requests/gitlab_subscriptions/trials/duo_enterprise_controller_spec.rb @@ -178,22 +178,52 @@ end context 'when successful' do - before do - expect_create_success(group_for_trial) + context 'when add_on_purchase exists' do + before do + expect_create_success(group_for_trial) + end + + it { is_expected.to redirect_to(group_settings_gitlab_duo_seat_utilization_index_path(group_for_trial)) } + + it 'shows valid flash message', :freeze_time do + post_create + + message = s_( + 'DuoEnterpriseTrial|You have successfully started a Duo Enterprise trial that will expire on %{exp_date}.' + ) + formatted_message = format( + message, + exp_date: I18n.l(61.days.from_now.to_date, format: :long) + ) + expect(flash[:success]).to have_content(formatted_message) + end end - it { is_expected.to redirect_to(group_settings_gitlab_duo_seat_utilization_index_path(group_for_trial)) } + context 'when add_on_purchase is not found upon success for expiration date' do + it 'shows valid flash message', :freeze_time do + service_params = { + step: step, + lead_params: lead_params, + trial_params: trial_params, + user: user + } - it 'shows valid flash message', :freeze_time do - post_create + expect_next_instance_of(GitlabSubscriptions::Trials::CreateDuoEnterpriseService, service_params) do |inst| + expect(inst) + .to receive(:execute).and_return(ServiceResponse.success(payload: { namespace: group_for_trial })) + end - message = format( - s_( + post_create + + message = s_( 'DuoEnterpriseTrial|You have successfully started a Duo Enterprise trial that will expire on %{exp_date}.' - ), - exp_date: I18n.l(60.days.from_now.to_date, format: :long) - ) - expect(flash[:success]).to have_content(message) + ) + formatted_message = format( + message, + exp_date: I18n.l(60.days.from_now.to_date, format: :long) + ) + expect(flash[:success]).to have_content(formatted_message) + end end def expect_create_success(namespace) @@ -206,7 +236,10 @@ def expect_create_success(namespace) expect_next_instance_of(GitlabSubscriptions::Trials::CreateDuoEnterpriseService, service_params) do |instance| expect(instance).to receive(:execute) do - create(:gitlab_subscription_add_on_purchase, :trial, add_on: add_on, namespace: namespace) + create( + :gitlab_subscription_add_on_purchase, + :trial, add_on: add_on, expires_on: 61.days.from_now, namespace: namespace + ) end.and_return(ServiceResponse.success(payload: { namespace: namespace })) end end diff --git a/ee/spec/requests/gitlab_subscriptions/trials/duo_pro_controller_spec.rb b/ee/spec/requests/gitlab_subscriptions/trials/duo_pro_controller_spec.rb index 19307e8199a97d53fd373fd6c4a916a331d137b0..71f35717e19eff838275a439650b794134cfd278 100644 --- a/ee/spec/requests/gitlab_subscriptions/trials/duo_pro_controller_spec.rb +++ b/ee/spec/requests/gitlab_subscriptions/trials/duo_pro_controller_spec.rb @@ -159,28 +159,57 @@ end context 'when successful' do - before do - expect_create_success(group_for_trial) + context 'when add_on_purchase exists' do + before do + expect_create_success(group_for_trial) + end + + it { is_expected.to redirect_to(group_settings_gitlab_duo_seat_utilization_index_path(group_for_trial)) } + + it 'shows valid flash message', :freeze_time do + post_create + + message = format( + s_( + 'DuoProTrial|You have successfully started a Duo Pro trial that will expire on %{exp_date}.' + ), + exp_date: I18n.l(61.days.from_now.to_date, format: :long) + ) + expect(flash[:success]).to have_content(message) + end end - it { is_expected.to redirect_to(group_settings_gitlab_duo_seat_utilization_index_path(group_for_trial)) } + context 'when add_on_purchase is not found upon success for expiration date' do + it 'shows valid flash message', :freeze_time do + service_params = { + step: step, + lead_params: lead_params, + trial_params: trial_params, + user: user + } + + expect_next_instance_of(GitlabSubscriptions::Trials::CreateDuoProService, service_params) do |instance| + expect(instance) + .to receive(:execute).and_return(ServiceResponse.success(payload: { namespace: group_for_trial })) + end - it 'shows valid flash message', :freeze_time do - post_create + post_create - message = format( - s_( - 'DuoProTrial|You have successfully started a Duo Pro trial that will expire on %{exp_date}.' - ), - exp_date: I18n.l(60.days.from_now.to_date, format: :long) - ) - expect(flash[:success]).to have_content(message) + message = format( + s_( + 'DuoProTrial|You have successfully started a Duo Pro trial that will expire on %{exp_date}.' + ), + exp_date: I18n.l(60.days.from_now.to_date, format: :long) + ) + expect(flash[:success]).to have_content(message) + end end context 'when feature flag duo_enterprise_trials is disabled' do let(:group_for_trial) { ineligible_paid_group } before do + expect_create_success(group_for_trial) stub_feature_flags(duo_enterprise_trials: false) end @@ -197,7 +226,10 @@ def expect_create_success(namespace) expect_next_instance_of(GitlabSubscriptions::Trials::CreateDuoProService, service_params) do |instance| expect(instance).to receive(:execute) do - create(:gitlab_subscription_add_on_purchase, :trial, add_on: add_on, namespace: namespace) + create( + :gitlab_subscription_add_on_purchase, + :trial, add_on: add_on, expires_on: 61.days.from_now, namespace: namespace + ) end.and_return(ServiceResponse.success(payload: { namespace: namespace })) end end diff --git a/ee/spec/requests/gitlab_subscriptions/trials_controller_spec.rb b/ee/spec/requests/gitlab_subscriptions/trials_controller_spec.rb index 7c52b644e96296bade50b96835c92e0dd626ef0b..e3800e053acab411daf3190a11cd852525308eea 100644 --- a/ee/spec/requests/gitlab_subscriptions/trials_controller_spec.rb +++ b/ee/spec/requests/gitlab_subscriptions/trials_controller_spec.rb @@ -166,21 +166,50 @@ let_it_be(:namespace) { create(:group_with_plan, plan: :premium_plan) } let_it_be(:ultimate_trial_paid_customer_plan) { create(:ultimate_trial_paid_customer_plan) } - before do - expect_create_with_premium_success(namespace) + context 'when add_on_purchase exists' do + before do + expect_create_with_premium_success(namespace) + end + + it 'shows valid flash message', :freeze_time do + post_create + + message = format( + s_( + 'BillingPlans|You have successfully started an Ultimate and GitLab Duo Enterprise trial that will ' \ + 'expire on %{exp_date}.' + ), + exp_date: I18n.l(61.days.from_now.to_date, format: :long) + ) + expect(flash[:success]).to have_content(message) + end end - it 'shows valid flash message', :freeze_time do - post_create + context 'when add_on_purchase is not found upon success for expiration date' do + it 'shows valid flash message', :freeze_time do + service_params = { + step: step, + lead_params: lead_params.merge(glm_params), + trial_params: trial_params.merge(glm_params), + user: user + } - message = format( - s_( - 'BillingPlans|You have successfully started an Ultimate and GitLab Duo Enterprise trial that will ' \ - 'expire on %{exp_date}.' - ), - exp_date: I18n.l(60.days.from_now.to_date, format: :long) - ) - expect(flash[:success]).to have_content(message) + expect_next_instance_of(GitlabSubscriptions::Trials::CreateService, service_params) do |instance| + expect(instance) + .to receive(:execute).and_return(ServiceResponse.success(payload: { namespace: namespace })) + end + + post_create + + message = format( + s_( + 'BillingPlans|You have successfully started an Ultimate and GitLab Duo Enterprise trial that will ' \ + 'expire on %{exp_date}.' + ), + exp_date: I18n.l(60.days.from_now.to_date, format: :long) + ) + expect(flash[:success]).to have_content(message) + end end end @@ -217,7 +246,10 @@ def expect_create_with_premium_success(namespace) expect_next_instance_of(GitlabSubscriptions::Trials::CreateService, service_params) do |instance| expect(instance).to receive(:execute) do - create(:gitlab_subscription_add_on_purchase, :trial, add_on: add_on, namespace: namespace) + create( + :gitlab_subscription_add_on_purchase, + :trial, add_on: add_on, expires_on: 61.days.from_now, namespace: namespace + ) namespace.gitlab_subscription.update!(hosted_plan: ultimate_trial_paid_customer_plan) end.and_return(ServiceResponse.success(payload: { namespace: namespace })) end