summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/auth/saml/identity_linker_spec.rb
blob: 6c4db25a02f12b9eecf9770adbd83a0cf5a151de (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
require 'spec_helper'

describe Gitlab::Auth::Saml::IdentityLinker do
  let(:user) { create(:user) }
  let(:provider) { 'saml' }
  let(:uid) { user.email }
  let(:in_response_to) { '12345' }
  let(:saml_response) { instance_double(OneLogin::RubySaml::Response, in_response_to: in_response_to) }
  let(:session) { { 'last_authn_request_id' => in_response_to } }

  let(:oauth) do
    OmniAuth::AuthHash.new(provider: provider, uid: uid, extra: { response_object: saml_response })
  end

  subject { described_class.new(user, oauth, session) }

  context 'with valid GitLab initiated request' do
    context 'linked identity exists' do
      let!(:identity) { user.identities.create!(provider: provider, extern_uid: uid) }

      it "doesn't create new identity" do
        expect { subject.link }.not_to change { Identity.count }
      end

      it "sets #changed? to false" do
        subject.link

        expect(subject).not_to be_changed
      end
    end

    context 'identity needs to be created' do
      it 'creates linked identity' do
        expect { subject.link }.to change { user.identities.count }
      end

      it 'sets identity provider' do
        subject.link

        expect(user.identities.last.provider).to eq provider
      end

      it 'sets identity extern_uid' do
        subject.link

        expect(user.identities.last.extern_uid).to eq uid
      end

      it 'sets #changed? to true' do
        subject.link

        expect(subject).to be_changed
      end
    end
  end

  context 'with identity provider initiated request' do
    let(:session) { { 'last_authn_request_id' => nil } }

    it 'attempting to link accounts raises an exception' do
      expect { subject.link }.to raise_error(Gitlab::Auth::Saml::IdentityLinker::UnverifiedRequest)
    end
  end
end