summaryrefslogtreecommitdiff
path: root/spec/controllers/invites_controller_spec.rb
blob: 75a972d2f95948871db173ed635db75e7b1197e5 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe InvitesController, :snowplow do
  let_it_be(:user) { create(:user) }
  let(:member) { 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(:params) { { id: raw_invite_token } }
  let(:snowplow_event) do
    {
      category: 'Growth::Acquisition::Experiment::InviteEmail',
      label: md5_member_global_id,
      property: group_type
    }
  end

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

      it 'renders the 404 page' do
        request

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  shared_examples "tracks the 'accepted' event for the invitation reminders experiment" do
    before do
      stub_experiment(invitation_reminders: true)
      allow(Gitlab::Experimentation).to receive(:enabled_for_attribute?).with(:invitation_reminders, member.invite_email).and_return(experimental_group)
    end

    context 'when in the control group' do
      let(:experimental_group) { false }

      it "tracks the 'accepted' event" do
        request

        expect_snowplow_event(
          category: 'Growth::Acquisition::Experiment::InvitationReminders',
          label: md5_member_global_id,
          property: 'control_group',
          action: 'accepted'
        )
      end
    end

    context 'when in the experimental group' do
      let(:experimental_group) { true }

      it "tracks the 'accepted' event" do
        request

        expect_snowplow_event(
          category: 'Growth::Acquisition::Experiment::InvitationReminders',
          label: md5_member_global_id,
          property: 'experimental_group',
          action: 'accepted'
        )
      end
    end
  end

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

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

      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 'forces re-confirmation if 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(:ok)
        expect(flash[:notice]).to be_nil
      end

      context 'when new_user_invite is not set' do
        it 'does not track the user as experiment group' do
          request

          expect_no_snowplow_event
        end
      end

      context 'when new_user_invite is experiment' do
        let(:params) { { id: raw_invite_token, new_user_invite: 'experiment' } }
        let(:group_type) { 'experiment_group' }

        it 'tracks the user as experiment group' do
          request

          expect_snowplow_event(**snowplow_event.merge(action: 'opened'))
          expect_snowplow_event(**snowplow_event.merge(action: 'accepted'))
        end
      end

      context 'when new_user_invite is control' do
        let(:params) { { id: raw_invite_token, new_user_invite: 'control' } }
        let(:group_type) { 'control_group' }

        it 'tracks the user as control group' do
          request

          expect_snowplow_event(**snowplow_event.merge(action: 'opened'))
          expect_snowplow_event(**snowplow_event.merge(action: 'accepted'))
        end
      end

      it_behaves_like "tracks the 'accepted' event for the invitation reminders experiment"
      it_behaves_like 'invalid token'
    end

    context 'when not logged in' do
      context 'when inviter is a member' 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 inviter is not a 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 }

    context 'when new_user_invite is not set' do
      it 'does not track an event' do
        request

        expect_no_snowplow_event
      end
    end

    context 'when new_user_invite is experiment' do
      let(:params) { { id: raw_invite_token, new_user_invite: 'experiment' } }
      let(:group_type) { 'experiment_group' }

      it 'tracks the user as experiment group' do
        request

        expect_snowplow_event(**snowplow_event.merge(action: 'accepted'))
      end
    end

    context 'when new_user_invite is control' do
      let(:params) { { id: raw_invite_token, new_user_invite: 'control' } }
      let(:group_type) { 'control_group' }

      it 'tracks the user as control group' do
        request

        expect_snowplow_event(**snowplow_event.merge(action: 'accepted'))
      end
    end

    it_behaves_like "tracks the 'accepted' event for the invitation reminders experiment"
    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