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

require 'spec_helper'

RSpec.describe InvitesController do
  let(:token) { '123456' }
  let_it_be(:user) { create(:user) }
  let(:member) { create(:project_member, :invited, invite_token: token, invite_email: user.email) }
  let(:project_members) { member.source.users }

  before do
    controller.instance_variable_set(:@member, member)
    sign_in(user)
  end

  describe 'GET #show' do
    it 'accepts user if invite email matches signed in user' do
      expect do
        get :show, params: { id: token }
      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.invite_email = 'bogus@email.com'

      expect do
        get :show, params: { id: token }
      end.not_to change { project_members.include?(user) }

      expect(response).to have_gitlab_http_status(:ok)
      expect(flash[:notice]).to be_nil
    end
  end

  describe 'POST #accept' do
    it 'accepts user' do
      expect do
        post :accept, params: { id: token }
      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
  end

  describe 'GET #decline' do
    it 'declines user' do
      get :decline, params: { id: token }

      expect { member.reload }.to raise_error ActiveRecord::RecordNotFound
      expect(response).to have_gitlab_http_status(:found)
      expect(flash[:notice]).to include 'You have declined the invitation to join'
    end
  end
end