diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-18 16:16:36 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-18 16:16:36 +0300 |
commit | 311b0269b4eb9839fa63f80c8d7a58f32b8138a0 (patch) | |
tree | 07e7870bca8aed6d61fdcc810731c50d2c40af47 /spec/services/namespaces | |
parent | 27909cef6c4170ed9205afa7426b8d3de47cbb0c (diff) |
Add latest changes from gitlab-org/gitlab@14-5-stable-eev14.5.0-rc42
Diffstat (limited to 'spec/services/namespaces')
-rw-r--r-- | spec/services/namespaces/in_product_marketing_email_records_spec.rb | 55 | ||||
-rw-r--r-- | spec/services/namespaces/invite_team_email_service_spec.rb | 128 |
2 files changed, 183 insertions, 0 deletions
diff --git a/spec/services/namespaces/in_product_marketing_email_records_spec.rb b/spec/services/namespaces/in_product_marketing_email_records_spec.rb new file mode 100644 index 00000000000..e5f1b275f9c --- /dev/null +++ b/spec/services/namespaces/in_product_marketing_email_records_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Namespaces::InProductMarketingEmailRecords do + let_it_be(:user) { create :user } + + subject(:records) { described_class.new } + + it 'initializes records' do + expect(subject.records).to match_array [] + end + + describe '#save!' do + before do + allow(Users::InProductMarketingEmail).to receive(:bulk_insert!) + + records.add(user, :invite_team, 0) + records.add(user, :create, 1) + end + + it 'bulk inserts added records' do + expect(Users::InProductMarketingEmail).to receive(:bulk_insert!).with(records.records) + records.save! + end + + it 'resets its records' do + records.save! + expect(records.records).to match_array [] + end + end + + describe '#add' do + it 'adds a Users::InProductMarketingEmail record to its records' do + freeze_time do + records.add(user, :invite_team, 0) + records.add(user, :create, 1) + + first, second = records.records + + expect(first).to be_a Users::InProductMarketingEmail + expect(first.track.to_sym).to eq :invite_team + expect(first.series).to eq 0 + expect(first.created_at).to eq Time.zone.now + expect(first.updated_at).to eq Time.zone.now + + expect(second).to be_a Users::InProductMarketingEmail + expect(second.track.to_sym).to eq :create + expect(second.series).to eq 1 + expect(second.created_at).to eq Time.zone.now + expect(second.updated_at).to eq Time.zone.now + end + end + end +end diff --git a/spec/services/namespaces/invite_team_email_service_spec.rb b/spec/services/namespaces/invite_team_email_service_spec.rb new file mode 100644 index 00000000000..60ba91f433d --- /dev/null +++ b/spec/services/namespaces/invite_team_email_service_spec.rb @@ -0,0 +1,128 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Namespaces::InviteTeamEmailService do + let_it_be(:user) { create(:user, email_opted_in: true) } + + let(:track) { described_class::TRACK } + let(:series) { 0 } + + let(:setup_for_company) { true } + let(:parent_group) { nil } + let(:group) { create(:group, parent: parent_group) } + + subject(:action) { described_class.send_email(user, group) } + + before do + group.add_owner(user) + allow(group).to receive(:setup_for_company).and_return(setup_for_company) + allow(Notify).to receive(:in_product_marketing_email).and_return(double(deliver_later: nil)) + end + + RSpec::Matchers.define :send_invite_team_email do |*args| + match do + expect(Notify).to have_received(:in_product_marketing_email).with(*args).once + end + + match_when_negated do + expect(Notify).not_to have_received(:in_product_marketing_email) + end + end + + shared_examples 'unexperimented' do + it { is_expected.not_to send_invite_team_email } + + it 'does not record sent email' do + expect { subject }.not_to change { Users::InProductMarketingEmail.count } + end + end + + shared_examples 'candidate' do + it { is_expected.to send_invite_team_email(user.id, group.id, track, 0) } + + it 'records sent email' do + expect { subject }.to change { Users::InProductMarketingEmail.count }.by(1) + + expect( + Users::InProductMarketingEmail.where( + user: user, + track: track, + series: 0 + ) + ).to exist + end + + it_behaves_like 'tracks assignment and records the subject', :invite_team_email, :group do + subject { group } + end + end + + context 'when group is in control path' do + before do + stub_experiments(invite_team_email: :control) + end + + it { is_expected.not_to send_invite_team_email } + + it 'does not record sent email' do + expect { subject }.not_to change { Users::InProductMarketingEmail.count } + end + + it_behaves_like 'tracks assignment and records the subject', :invite_team_email, :group do + subject { group } + end + end + + context 'when group is in candidate path' do + before do + stub_experiments(invite_team_email: :candidate) + end + + it_behaves_like 'candidate' + + context 'when the user has not opted into marketing emails' do + let(:user) { create(:user, email_opted_in: false ) } + + it_behaves_like 'unexperimented' + end + + context 'when group is not top level' do + it_behaves_like 'unexperimented' do + let(:parent_group) do + create(:group).tap { |g| g.add_owner(user) } + end + end + end + + context 'when group is not set up for a company' do + it_behaves_like 'unexperimented' do + let(:setup_for_company) { nil } + end + end + + context 'when other users have already been added to the group' do + before do + group.add_developer(create(:user)) + end + + it_behaves_like 'unexperimented' + end + + context 'when other users have already been invited to the group' do + before do + group.add_developer('not_a_user_yet@example.com') + end + + it_behaves_like 'unexperimented' + end + + context 'when the user already got sent the email' do + before do + create(:in_product_marketing_email, user: user, track: track, series: 0) + end + + it_behaves_like 'unexperimented' + end + end +end |