summaryrefslogtreecommitdiff
path: root/spec/services/namespaces/in_product_marketing_emails_service_spec.rb
blob: de84666ca1d9c1ec315fe4da88f1e9dc7b2f08f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Namespaces::InProductMarketingEmailsService, '#execute' do
  subject(:execute_service) { described_class.new(track, interval).execute }

  let(:track) { :create }
  let(:interval) { 1 }

  let(:frozen_time) { Time.zone.parse('23 Mar 2021 10:14:40 UTC') }
  let(:previous_action_completed_at) { frozen_time - 2.days }
  let(:current_action_completed_at) { nil }
  let(:user_can_perform_current_track_action) { true }
  let(:actions_completed) { { created_at: previous_action_completed_at, git_write_at: current_action_completed_at } }

  let_it_be(:group) { create(:group) }
  let_it_be(:user) { create(:user, email_opted_in: true) }

  before do
    travel_to(frozen_time)
    create(:onboarding_progress, namespace: group, **actions_completed)
    group.add_developer(user)
    allow(Ability).to receive(:allowed?).with(user, anything, anything).and_return(user_can_perform_current_track_action)
    allow(Notify).to receive(:in_product_marketing_email).and_return(double(deliver_later: nil))
  end

  RSpec::Matchers.define :send_in_product_marketing_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

  context 'for each track and series with the right conditions' do
    using RSpec::Parameterized::TableSyntax

    where(:track, :interval, :actions_completed) do
      :create       | 1  | { created_at: frozen_time - 2.days }
      :create       | 5  | { created_at: frozen_time - 6.days }
      :create       | 10 | { created_at: frozen_time - 11.days }
      :team_short   | 1  | { created_at: frozen_time - 2.days, git_write_at: frozen_time - 2.days }
      :trial_short  | 2  | { created_at: frozen_time - 3.days, git_write_at: frozen_time - 3.days }
      :admin_verify | 3  | { created_at: frozen_time - 4.days, git_write_at: frozen_time - 4.days }
      :verify       | 4  | { created_at: frozen_time - 5.days, git_write_at: frozen_time - 5.days }
      :verify       | 8  | { created_at: frozen_time - 9.days, git_write_at: frozen_time - 9.days }
      :verify       | 13 | { created_at: frozen_time - 14.days, git_write_at: frozen_time - 14.days }
      :trial        | 1  | { created_at: frozen_time - 2.days, git_write_at: frozen_time - 2.days, pipeline_created_at: frozen_time - 2.days }
      :trial        | 5  | { created_at: frozen_time - 6.days, git_write_at: frozen_time - 6.days, pipeline_created_at: frozen_time - 6.days }
      :trial        | 10 | { created_at: frozen_time - 11.days, git_write_at: frozen_time - 11.days, pipeline_created_at: frozen_time - 11.days }
      :team         | 1  | { created_at: frozen_time - 2.days, git_write_at: frozen_time - 2.days, pipeline_created_at: frozen_time - 2.days, trial_started_at: frozen_time - 2.days }
      :team         | 5  | { created_at: frozen_time - 6.days, git_write_at: frozen_time - 6.days, pipeline_created_at: frozen_time - 6.days, trial_started_at: frozen_time - 6.days }
      :team         | 10 | { created_at: frozen_time - 11.days, git_write_at: frozen_time - 11.days, pipeline_created_at: frozen_time - 11.days, trial_started_at: frozen_time - 11.days }
      :experience   | 30 | { created_at: frozen_time - 31.days, git_write_at: frozen_time - 31.days }
    end

    with_them do
      it { is_expected.to send_in_product_marketing_email(user.id, group.id, track, described_class::TRACKS[track][:interval_days].index(interval)) }
    end
  end

  context 'when initialized with a different track' do
    let(:track) { :team_short }

    it { is_expected.not_to send_in_product_marketing_email }

    context 'when the previous track actions have been completed' do
      let(:current_action_completed_at) { frozen_time - 2.days }

      it { is_expected.to send_in_product_marketing_email(user.id, group.id, track, 0) }
    end
  end

  context 'when initialized with a different interval' do
    let(:interval) { 5 }

    it { is_expected.not_to send_in_product_marketing_email }

    context 'when the previous track action was completed within the intervals range' do
      let(:previous_action_completed_at) { frozen_time - 6.days }

      it { is_expected.to send_in_product_marketing_email(user.id, group.id, :create, 1) }
    end
  end

  context 'when the previous track action is not yet completed' do
    let(:previous_action_completed_at) { nil }

    it { is_expected.not_to send_in_product_marketing_email }
  end

  context 'when the previous track action is completed outside the intervals range' do
    let(:previous_action_completed_at) { frozen_time - 3.days }

    it { is_expected.not_to send_in_product_marketing_email }
  end

  context 'when the current track action is completed' do
    let(:current_action_completed_at) { frozen_time }

    it { is_expected.not_to send_in_product_marketing_email }
  end

  context "when the user cannot perform the current track's action" do
    let(:user_can_perform_current_track_action) { false }

    it { is_expected.not_to send_in_product_marketing_email }
  end

  context 'when the user has not opted into marketing emails' do
    let(:user) { create(:user, email_opted_in: false) }

    it { is_expected.not_to send_in_product_marketing_email }
  end

  describe 'do not send emails twice' do
    subject { described_class.send_for_all_tracks_and_intervals }

    let(:user) { create(:user, email_opted_in: true) }

    context 'when user already got a specific email' do
      before do
        create(:in_product_marketing_email, user: user, track: track, series: 0)
      end

      it { is_expected.not_to send_in_product_marketing_email(user.id, anything, track, 0) }
    end

    context 'when user already got sent the whole track' do
      before do
        0.upto(2) do |series|
          create(:in_product_marketing_email, user: user, track: track, series: series)
        end
      end

      it 'does not send any of the emails anymore', :aggregate_failures do
        0.upto(2) do |series|
          expect(subject).not_to send_in_product_marketing_email(user.id, anything, track, series)
        end
      end
    end

    context 'when user is in two groups' do
      let(:other_group) { create(:group) }

      before do
        other_group.add_developer(user)
      end

      context 'when both groups would get the same email' do
        before do
          create(:onboarding_progress, namespace: other_group, **actions_completed)
        end

        it 'does not send the same email twice' do
          subject

          expect(Notify).to have_received(:in_product_marketing_email).with(user.id, anything, :create, 0).once
        end
      end

      context 'when other group gets a different email' do
        before do
          create(:onboarding_progress, namespace: other_group, created_at: previous_action_completed_at, git_write_at: frozen_time - 2.days)
        end

        it 'sends both emails' do
          subject

          expect(Notify).to have_received(:in_product_marketing_email).with(user.id, group.id, :create, 0)
          expect(Notify).to have_received(:in_product_marketing_email).with(user.id, other_group.id, :team_short, 0)
        end
      end
    end
  end

  it 'records sent emails' do
    expect { subject }.to change { Users::InProductMarketingEmail.count }.by(1)

    expect(
      Users::InProductMarketingEmail.where(
        user: user,
        track: Users::InProductMarketingEmail::ACTIVE_TRACKS[:create],
        series: 0
      )
    ).to exist
  end

  context 'when invoked with a non existing track' do
    let(:track) { :foo }

    before do
      stub_const("#{described_class}::TRACKS", { bar: {} })
    end

    it { expect { subject }.to raise_error(ArgumentError, 'Track foo not defined') }
  end

  context 'when group is a sub-group' do
    let(:root_group) { create(:group) }
    let(:group) { create(:group) }

    before do
      group.parent = root_group
      group.save!

      allow(Ability).to receive(:allowed?).and_call_original
    end

    it 'does not raise an exception' do
      expect { execute_service }.not_to raise_error
    end
  end
end