From 9a511e831684760bb455381eb1837d1a4c85f7ee Mon Sep 17 00:00:00 2001 From: Nicolas Dular Date: Fri, 4 Dec 2020 17:03:11 +0100 Subject: [PATCH] Record action when namespace trial starts This creates an onboarding action for a namespace when the trial starts. We track these actions to have an onboarding experience for namespaces. --- .../apply_trial_service.rb | 10 ++++ .../apply_trial_service_spec.rb | 57 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 ee/spec/services/gitlab_subscriptions/apply_trial_service_spec.rb diff --git a/ee/app/services/gitlab_subscriptions/apply_trial_service.rb b/ee/app/services/gitlab_subscriptions/apply_trial_service.rb index 343a20084ba16d..286ac93cf6ef76 100644 --- a/ee/app/services/gitlab_subscriptions/apply_trial_service.rb +++ b/ee/app/services/gitlab_subscriptions/apply_trial_service.rb @@ -6,6 +6,9 @@ def execute(apply_trial_params) response = client.generate_trial(apply_trial_params) if response[:success] + namespace_id = apply_trial_params.dig(:trial_user, :namespace_id) + record_onboarding_progress(namespace_id) if namespace_id + { success: true } else { success: false, errors: response.dig(:data, :errors) } @@ -17,5 +20,12 @@ def execute(apply_trial_params) def client Gitlab::SubscriptionPortal::Client end + + def record_onboarding_progress(namespace_id) + namespace = Namespace.find_by(id: namespace_id) # rubocop: disable CodeReuse/ActiveRecord + return unless namespace + + OnboardingProgressService.new(namespace).execute(action: :trial_started) + end end end diff --git a/ee/spec/services/gitlab_subscriptions/apply_trial_service_spec.rb b/ee/spec/services/gitlab_subscriptions/apply_trial_service_spec.rb new file mode 100644 index 00000000000000..3fe5b29a546cfb --- /dev/null +++ b/ee/spec/services/gitlab_subscriptions/apply_trial_service_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe GitlabSubscriptions::ApplyTrialService do + subject(:execute) { described_class.new.execute(apply_trial_params) } + + let_it_be(:namespace) { create(:namespace) } + let(:apply_trial_params) do + { + trial_user: { + namespace_id: namespace.id + } + } + end + + describe '#execute' do + before do + allow(Gitlab::SubscriptionPortal::Client).to receive(:generate_trial).and_return(response) + end + + context 'trial applied successfully' do + let(:response) { { success: true }} + + it 'returns success: true' do + expect(execute).to eq({ success: true }) + end + + it 'records a namespace onboarding progress action' do + expect_next_instance_of(OnboardingProgressService) do |service| + expect(service).to receive(:execute).with(action: :trial_started) + end + + execute + end + end + + context 'error while applying the trial' do + let(:response) { { success: false, data: { errors: ['some error'] } }} + + it 'returns success: false with errors' do + expected_response = { + success: false, + errors: ['some error'] + } + + expect(execute).to eq(expected_response) + end + + it 'does not record a namespace onboarding progress action' do + expect(OnboardingProgressService).not_to receive(:new) + + execute + end + end + end +end -- GitLab