summaryrefslogtreecommitdiff
path: root/spec/controllers/invites_controller_spec.rb
blob: d4091461062c953e16d8cbacd5066d213b6d9b40 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe InvitesController do
  let_it_be(:user) { create(:user) }
  let_it_be(:member, reload: true) { create(:project_member, :invited, invite_email: user.email) }

  let(:raw_invite_token) { member.raw_invite_token }
  let(:project_members) { member.source.users }
  let(:md5_member_global_id) { Digest::MD5.hexdigest(member.to_global_id.to_s) }
  let(:extra_params) { {} }
  let(:params) { { id: raw_invite_token }.merge(extra_params) }

  shared_examples 'invalid token' do
    context 'when invite token is not valid' do
      let(:raw_invite_token) { '_bogus_token_' }

      it 'redirects to root' do
        request

        expect(response).to redirect_to(root_path)
        expect(controller).to set_flash[:alert].to('The invitation can not be found with the provided invite token.')
      end
    end
  end

  shared_examples 'invite email match enforcement' do |error_status:, flash_alert: nil|
    it 'accepts user if invite email matches signed in user' do
      expect do
        request
      end.to change { project_members.include?(user) }.from(false).to(true)

      expect(response).to have_gitlab_http_status(:found)
      expect(flash[:notice]).to include 'You have been granted'
    end

    it 'accepts invite if invite email matches confirmed secondary email' do
      secondary_email = create(:email, :confirmed, user: user)
      member.update!(invite_email: secondary_email.email)

      expect do
        request
      end.to change { project_members.include?(user) }.from(false).to(true)

      expect(response).to have_gitlab_http_status(:found)
      expect(flash[:notice]).to include 'You have been granted'
    end

    it 'does not accept if invite email matches unconfirmed secondary email' do
      secondary_email = create(:email, user: user)
      member.update!(invite_email: secondary_email.email)

      expect do
        request
      end.not_to change { project_members.include?(user) }

      expect(response).to have_gitlab_http_status(error_status)
      expect(flash[:alert]).to eq(flash_alert)
    end

    it 'does not accept if invite email does not match signed in user' do
      member.update!(invite_email: 'bogus@email.com')

      expect do
        request
      end.not_to change { project_members.include?(user) }

      expect(response).to have_gitlab_http_status(error_status)
      expect(flash[:alert]).to eq(flash_alert)
    end
  end

  describe 'GET #show', :snowplow do
    subject(:request) { get :show, params: params }

    context 'when logged in' do
      before do
        sign_in(user)
      end

      it_behaves_like 'invite email match enforcement', error_status: :ok
      it_behaves_like 'invalid token'
    end

    context 'when it is an initial invite email' do
      let(:extra_params) { { invite_type: 'initial_email' } }

      it 'tracks the initial join click from email' do
        request

        expect_snowplow_event(
          category: described_class.name,
          action: 'join_clicked',
          label: 'invite_email',
          property: member.id.to_s
        )
      end

      context 'when it is part of the invite_email_preview_text experiment' do
        let(:extra_params) { { invite_type: 'initial_email', experiment_name: 'invite_email_preview_text' } }

        it 'tracks the initial join click from email' do
          experiment = double(track: true)
          allow(controller).to receive(:experiment).with(:invite_email_preview_text, actor: member).and_return(experiment)

          request

          expect(experiment).to have_received(:track).with(:join_clicked)
        end

        context 'when member does not exist' do
          let(:raw_invite_token) { '_bogus_token_' }

          it 'does not track the experiment' do
            expect(controller).not_to receive(:experiment).with(:invite_email_preview_text, actor: member)

            request
          end
        end
      end

      context 'when it is part of the invite_email_from experiment' do
        let(:extra_params) { { invite_type: 'initial_email', experiment_name: 'invite_email_from' } }

        it 'tracks the initial join click from email' do
          experiment = double(track: true)
          allow(controller).to receive(:experiment).with(:invite_email_from, actor: member).and_return(experiment)

          request

          expect(experiment).to have_received(:track).with(:join_clicked)
        end

        context 'when member does not exist' do
          let(:raw_invite_token) { '_bogus_token_' }

          it 'does not track the experiment' do
            expect(controller).not_to receive(:experiment).with(:invite_email_from, actor: member)

            request
          end
        end
      end

      context 'when member does not exist' do
        let(:raw_invite_token) { '_bogus_token_' }

        it 'does not track join click' do
          request

          expect_no_snowplow_event(
            category: described_class.name,
            action: 'join_clicked',
            label: 'invite_email'
          )
        end
      end
    end

    context 'when it is not an initial email' do
      it 'does not track the join click' do
        request

        expect_no_snowplow_event(
          category: described_class.name,
          action: 'join_clicked',
          label: 'invite_email'
        )
      end

      context 'when it is not part of our invite email experiment' do
        it 'does not track via experiment', :aggregate_failures do
          expect(controller).not_to receive(:experiment).with(:invite_email_preview_text, actor: member)
          expect(controller).not_to receive(:experiment).with(:invite_email_from, actor: member)

          request
        end
      end
    end

    context 'when not logged in' do
      context 'when invite token belongs to a valid member' do
        context 'when instance allows sign up' do
          it 'indicates an account can be created in notice' do
            request

            expect(flash[:notice]).to include('or create an account')
          end

          context 'when user exists with the invited email' do
            it 'is redirected to a new session with invite email param' do
              request

              expect(response).to redirect_to(new_user_session_path(invite_email: member.invite_email))
            end
          end

          context 'when user exists with the invited email as secondary email' do
            before do
              secondary_email = create(:email, user: user, email: 'foo@example.com')
              member.update!(invite_email: secondary_email.email)
            end

            it 'is redirected to a new session with invite email param' do
              request

              expect(response).to redirect_to(new_user_session_path(invite_email: member.invite_email))
            end
          end

          context 'when user does not exist with the invited email' do
            before do
              member.update!(invite_email: 'bogus_email@example.com')
            end

            it 'indicates an account can be created in notice' do
              request

              expect(flash[:notice]).to include('create an account or sign in')
            end

            it 'is redirected to a new registration with invite email param and flash message', :aggregate_failures do
              request

              expect(response).to redirect_to(new_user_registration_path(invite_email: member.invite_email))
              expect(flash[:notice]).to eq 'To accept this invitation, create an account or sign in.'
            end

            it 'sets session keys for auto email confirmation on sign up' do
              request

              expect(session[:invite_email]).to eq(member.invite_email)
            end

            context 'when it is part of our invite email experiment' do
              let(:extra_params) { { invite_type: 'initial_email' } }

              it 'sets session key for invite acceptance tracking on sign-up' do
                request

                expect(session[:originating_member_id]).to eq(member.id)
              end
            end

            context 'when it is not part of our invite email experiment' do
              it 'does not set the session key for invite acceptance tracking on sign-up' do
                request

                expect(session[:originating_member_id]).to be_nil
              end
            end
          end
        end

        context 'when instance does not allow sign up' do
          before do
            stub_application_setting(allow_signup?: false)
          end

          it 'does not indicate an account can be created in notice' do
            request

            expect(flash[:notice]).not_to include('or create an account')
          end

          context 'when user exists with the invited email' do
            it 'is redirected to a new session with invite email param' do
              request

              expect(response).to redirect_to(new_user_session_path(invite_email: member.invite_email))
            end
          end

          context 'when user does not exist with the invited email' do
            before do
              member.update!(invite_email: 'bogus_email@example.com')
            end

            it 'is redirected to a new session with invite email param' do
              request

              expect(response).to redirect_to(new_user_session_path(invite_email: member.invite_email))
            end
          end
        end
      end

      context 'when invite token does not belong to a valid member' do
        let(:params) { { id: '_bogus_token_' } }

        it 'is redirected to a new session' do
          request

          expect(response).to redirect_to(new_user_session_path)
        end
      end
    end
  end

  describe 'POST #accept' do
    before do
      sign_in(user)
    end

    subject(:request) { post :accept, params: params }

    it_behaves_like 'invite email match enforcement', error_status: :redirect, flash_alert: 'The invitation could not be accepted.'
    it_behaves_like 'invalid token'
  end

  describe 'POST #decline for link in UI' do
    before do
      sign_in(user)
    end

    subject(:request) { post :decline, params: params }

    it_behaves_like 'invalid token'
  end

  describe 'GET #decline for link in email' do
    before do
      sign_in(user)
    end

    subject(:request) { get :decline, params: params }

    it_behaves_like 'invalid token'
  end
end