2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-18 10:03:27 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe Gitlab::Auth::Saml::IdentityLinker do
|
2018-04-18 10:03:27 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:provider) { 'saml' }
|
|
|
|
let(:uid) { user.email }
|
2019-08-19 09:19:19 -04:00
|
|
|
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 } }
|
2018-04-18 10:03:27 -04:00
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
let(:oauth) do
|
|
|
|
OmniAuth::AuthHash.new(provider: provider, uid: uid, extra: { response_object: saml_response })
|
|
|
|
end
|
2018-04-18 10:03:27 -04:00
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
subject { described_class.new(user, oauth, session) }
|
2018-04-18 10:03:27 -04:00
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
context 'with valid GitLab initiated request' do
|
|
|
|
context 'linked identity exists' do
|
|
|
|
let!(:identity) { user.identities.create!(provider: provider, extern_uid: uid) }
|
2018-04-18 10:03:27 -04:00
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
it "doesn't create new identity" do
|
|
|
|
expect { subject.link }.not_to change { Identity.count }
|
|
|
|
end
|
2018-04-18 10:03:27 -04:00
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
it "sets #changed? to false" do
|
|
|
|
subject.link
|
2018-04-18 10:03:27 -04:00
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
expect(subject).not_to be_changed
|
|
|
|
end
|
2018-04-18 10:03:27 -04:00
|
|
|
end
|
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
context 'identity needs to be created' do
|
|
|
|
it 'creates linked identity' do
|
|
|
|
expect { subject.link }.to change { user.identities.count }
|
|
|
|
end
|
2018-04-18 10:03:27 -04:00
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
it 'sets identity provider' do
|
|
|
|
subject.link
|
2018-04-18 10:03:27 -04:00
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
expect(user.identities.last.provider).to eq provider
|
|
|
|
end
|
2018-04-18 10:03:27 -04:00
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
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
|
2018-04-18 10:03:27 -04:00
|
|
|
end
|
2019-08-19 09:19:19 -04:00
|
|
|
end
|
2018-04-18 10:03:27 -04:00
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
context 'with identity provider initiated request' do
|
|
|
|
let(:session) { { 'last_authn_request_id' => nil } }
|
2018-04-18 10:03:27 -04:00
|
|
|
|
2019-08-19 09:19:19 -04:00
|
|
|
it 'attempting to link accounts raises an exception' do
|
|
|
|
expect { subject.link }.to raise_error(Gitlab::Auth::Saml::IdentityLinker::UnverifiedRequest)
|
2018-04-18 10:03:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|