debian-mirror-gitlab/spec/lib/gitlab/auth/o_auth/user_spec.rb

839 lines
27 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
require 'spec_helper'
2018-03-27 19:54:05 +05:30
describe Gitlab::Auth::OAuth::User do
2018-05-09 12:01:36 +05:30
include LdapHelpers
2017-09-10 17:25:29 +05:30
let(:oauth_user) { described_class.new(auth_hash) }
2015-04-26 12:48:37 +05:30
let(:gl_user) { oauth_user.gl_user }
let(:uid) { 'my-uid' }
2018-03-17 18:26:18 +05:30
let(:dn) { 'uid=user1,ou=people,dc=example' }
2015-04-26 12:48:37 +05:30
let(:provider) { 'my-provider' }
2015-09-25 12:07:36 +05:30
let(:auth_hash) { OmniAuth::AuthHash.new(uid: uid, provider: provider, info: info_hash) }
2015-04-26 12:48:37 +05:30
let(:info_hash) do
{
nickname: '-john+gitlab-ETC%.git@gmail.com',
name: 'John',
2018-03-17 18:26:18 +05:30
email: 'john@mail.com',
address: {
locality: 'locality',
country: 'country'
}
2015-04-26 12:48:37 +05:30
}
end
2020-04-08 14:13:33 +05:30
let(:ldap_user) { Gitlab::Auth::Ldap::Person.new(Net::LDAP::Entry.new, 'ldapmain') }
2015-04-26 12:48:37 +05:30
2016-06-02 11:05:42 +05:30
describe '#persisted?' do
2015-04-26 12:48:37 +05:30
let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }
it "finds an existing user based on uid and provider (facebook)" do
expect( oauth_user.persisted? ).to be_truthy
end
2016-06-02 11:05:42 +05:30
it 'returns false if user is not found in database' do
2015-09-11 14:41:01 +05:30
allow(auth_hash).to receive(:uid).and_return('non-existing')
2015-04-26 12:48:37 +05:30
expect( oauth_user.persisted? ).to be_falsey
end
end
2017-09-10 17:25:29 +05:30
def stub_omniauth_config(messages)
allow(Gitlab.config.omniauth).to receive_messages(messages)
end
2015-09-11 14:41:01 +05:30
2017-09-10 17:25:29 +05:30
describe '#save' do
2015-04-26 12:48:37 +05:30
let(:provider) { 'twitter' }
2018-03-17 18:26:18 +05:30
describe 'when account exists on server' do
it 'does not mark the user as external' do
create(:omniauth_user, extern_uid: 'my-uid', provider: provider)
stub_omniauth_config(allow_single_sign_on: [provider], external_providers: [provider])
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user.external).to be_falsey
end
end
2015-04-26 12:48:37 +05:30
describe 'signup' do
2017-08-17 22:00:37 +05:30
context 'when signup is disabled' do
before do
stub_application_setting signup_enabled: false
end
it 'creates the user' do
2018-03-17 18:26:18 +05:30
stub_omniauth_config(allow_single_sign_on: [provider])
2017-08-17 22:00:37 +05:30
oauth_user.save
expect(gl_user).to be_persisted
end
end
context 'when user confirmation email is enabled' do
before do
stub_application_setting send_user_confirmation_email: true
end
it 'creates and confirms the user anyway' do
2018-03-17 18:26:18 +05:30
stub_omniauth_config(allow_single_sign_on: [provider])
2017-08-17 22:00:37 +05:30
oauth_user.save
expect(gl_user).to be_persisted
expect(gl_user).to be_confirmed
end
end
2020-03-13 15:44:24 +05:30
context 'when the current minimum password length is different from the default minimum password length' do
before do
stub_application_setting minimum_password_length: 21
end
it 'creates the user' do
stub_omniauth_config(allow_single_sign_on: [provider])
oauth_user.save
expect(gl_user).to be_persisted
end
end
2017-08-17 22:00:37 +05:30
it 'marks user as having password_automatically_set' do
2018-03-17 18:26:18 +05:30
stub_omniauth_config(allow_single_sign_on: [provider], external_providers: [provider])
2017-08-17 22:00:37 +05:30
oauth_user.save
expect(gl_user).to be_persisted
expect(gl_user).to be_password_automatically_set
end
2016-06-02 11:05:42 +05:30
shared_examples 'to verify compliance with allow_single_sign_on' do
context 'provider is marked as external' do
2016-09-13 17:45:13 +05:30
it 'marks user as external' do
2018-03-17 18:26:18 +05:30
stub_omniauth_config(allow_single_sign_on: [provider], external_providers: [provider])
2016-06-02 11:05:42 +05:30
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user.external).to be_truthy
end
end
context 'provider was external, now has been removed' do
2016-09-13 17:45:13 +05:30
it 'does not mark external user as internal' do
2018-03-17 18:26:18 +05:30
create(:omniauth_user, extern_uid: 'my-uid', provider: provider, external: true)
stub_omniauth_config(allow_single_sign_on: [provider], external_providers: ['facebook'])
2016-06-02 11:05:42 +05:30
oauth_user.save
expect(gl_user).to be_valid
2016-08-24 12:49:21 +05:30
expect(gl_user.external).to be_truthy
end
end
context 'provider is not external' do
context 'when adding a new OAuth identity' do
2016-09-13 17:45:13 +05:30
it 'does not promote an external user to internal' do
2016-08-24 12:49:21 +05:30
user = create(:user, email: 'john@mail.com', external: true)
user.identities.create(provider: provider, extern_uid: uid)
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user.external).to be_truthy
end
2016-06-02 11:05:42 +05:30
end
end
context 'with new allow_single_sign_on enabled syntax' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
stub_omniauth_config(allow_single_sign_on: [provider])
2017-09-10 17:25:29 +05:30
end
2016-04-02 18:10:28 +05:30
it "creates a user from Omniauth" do
oauth_user.save
expect(gl_user).to be_valid
identity = gl_user.identities.first
expect(identity.extern_uid).to eql uid
2018-03-17 18:26:18 +05:30
expect(identity.provider).to eql provider
2016-04-02 18:10:28 +05:30
end
end
context "with old allow_single_sign_on enabled syntax" do
2017-09-10 17:25:29 +05:30
before do
stub_omniauth_config(allow_single_sign_on: true)
end
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
it "creates a user from Omniauth" do
oauth_user.save
expect(gl_user).to be_valid
identity = gl_user.identities.first
expect(identity.extern_uid).to eql uid
2018-03-17 18:26:18 +05:30
expect(identity.provider).to eql provider
2015-09-11 14:41:01 +05:30
end
end
2015-04-26 12:48:37 +05:30
2016-06-02 11:05:42 +05:30
context 'with new allow_single_sign_on disabled syntax' do
2017-09-10 17:25:29 +05:30
before do
stub_omniauth_config(allow_single_sign_on: [])
end
2016-06-02 11:05:42 +05:30
it 'throws an error' do
2018-03-17 18:26:18 +05:30
expect { oauth_user.save }.to raise_error StandardError
2016-04-02 18:10:28 +05:30
end
end
2016-06-02 11:05:42 +05:30
context 'with old allow_single_sign_on disabled (Default)' do
2017-09-10 17:25:29 +05:30
before do
stub_omniauth_config(allow_single_sign_on: false)
end
2016-06-02 11:05:42 +05:30
it 'throws an error' do
2018-03-17 18:26:18 +05:30
expect { oauth_user.save }.to raise_error StandardError
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
end
end
2015-09-11 14:41:01 +05:30
context "with auto_link_ldap_user disabled (default)" do
2017-09-10 17:25:29 +05:30
before do
stub_omniauth_config(auto_link_ldap_user: false)
end
2015-09-11 14:41:01 +05:30
include_examples "to verify compliance with allow_single_sign_on"
end
context "with auto_link_ldap_user enabled" do
2017-09-10 17:25:29 +05:30
before do
stub_omniauth_config(auto_link_ldap_user: true)
end
2015-09-11 14:41:01 +05:30
context "and no LDAP provider defined" do
2017-09-10 17:25:29 +05:30
before do
stub_ldap_config(providers: [])
end
2015-09-11 14:41:01 +05:30
include_examples "to verify compliance with allow_single_sign_on"
end
context "and at least one LDAP provider is defined" do
2017-09-10 17:25:29 +05:30
before do
stub_ldap_config(providers: %w(ldapmain))
end
2015-09-11 14:41:01 +05:30
context "and a corresponding LDAP person" do
before do
allow(ldap_user).to receive(:uid) { uid }
allow(ldap_user).to receive(:username) { uid }
2019-07-07 11:18:12 +05:30
allow(ldap_user).to receive(:name) { 'John Doe' }
2016-08-24 12:49:21 +05:30
allow(ldap_user).to receive(:email) { ['johndoe@example.com', 'john2@example.com'] }
2018-03-17 18:26:18 +05:30
allow(ldap_user).to receive(:dn) { dn }
2015-09-11 14:41:01 +05:30
end
context "and no account for the LDAP user" do
2018-03-17 18:26:18 +05:30
before do
2020-04-08 14:13:33 +05:30
allow(Gitlab::Auth::Ldap::Person).to receive(:find_by_uid).and_return(ldap_user)
2016-11-24 13:41:30 +05:30
2015-09-11 14:41:01 +05:30
oauth_user.save
2018-03-17 18:26:18 +05:30
end
2015-09-11 14:41:01 +05:30
2018-03-17 18:26:18 +05:30
it "creates a user with dual LDAP and omniauth identities" do
2015-09-11 14:41:01 +05:30
expect(gl_user).to be_valid
expect(gl_user.username).to eql uid
2019-07-07 11:18:12 +05:30
expect(gl_user.name).to eql 'John Doe'
2015-09-11 14:41:01 +05:30
expect(gl_user.email).to eql 'johndoe@example.com'
2017-08-17 22:00:37 +05:30
expect(gl_user.identities.length).to be 2
2015-09-11 14:41:01 +05:30
identities_as_hash = gl_user.identities.map { |id| { provider: id.provider, extern_uid: id.extern_uid } }
expect(identities_as_hash).to match_array(
2017-08-17 22:00:37 +05:30
[
2018-03-17 18:26:18 +05:30
{ provider: 'ldapmain', extern_uid: dn },
2015-09-11 14:41:01 +05:30
{ provider: 'twitter', extern_uid: uid }
2017-08-17 22:00:37 +05:30
]
)
2015-09-11 14:41:01 +05:30
end
2018-03-17 18:26:18 +05:30
2019-07-07 11:18:12 +05:30
it "has name and email set as synced" do
expect(gl_user.user_synced_attributes_metadata.name_synced).to be_truthy
2018-03-17 18:26:18 +05:30
expect(gl_user.user_synced_attributes_metadata.email_synced).to be_truthy
end
2019-07-07 11:18:12 +05:30
it "has name and email set as read-only" do
expect(gl_user.read_only_attribute?(:name)).to be_truthy
2018-03-17 18:26:18 +05:30
expect(gl_user.read_only_attribute?(:email)).to be_truthy
end
it "has synced attributes provider set to ldapmain" do
expect(gl_user.user_synced_attributes_metadata.provider).to eql 'ldapmain'
end
2015-09-11 14:41:01 +05:30
end
context "and LDAP user has an account already" do
2019-07-07 11:18:12 +05:30
let!(:existing_user) { create(:omniauth_user, name: 'John Doe', email: 'john@example.com', extern_uid: dn, provider: 'ldapmain', username: 'john') }
2020-01-01 13:55:28 +05:30
2015-09-11 14:41:01 +05:30
it "adds the omniauth identity to the LDAP account" do
2020-04-08 14:13:33 +05:30
allow(Gitlab::Auth::Ldap::Person).to receive(:find_by_uid).and_return(ldap_user)
2016-11-24 13:41:30 +05:30
2015-09-11 14:41:01 +05:30
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user.username).to eql 'john'
2019-07-07 11:18:12 +05:30
expect(gl_user.name).to eql 'John Doe'
2015-09-11 14:41:01 +05:30
expect(gl_user.email).to eql 'john@example.com'
2017-08-17 22:00:37 +05:30
expect(gl_user.identities.length).to be 2
2015-09-11 14:41:01 +05:30
identities_as_hash = gl_user.identities.map { |id| { provider: id.provider, extern_uid: id.extern_uid } }
expect(identities_as_hash).to match_array(
2017-08-17 22:00:37 +05:30
[
2018-03-17 18:26:18 +05:30
{ provider: 'ldapmain', extern_uid: dn },
2015-09-11 14:41:01 +05:30
{ provider: 'twitter', extern_uid: uid }
2017-08-17 22:00:37 +05:30
]
)
2015-09-11 14:41:01 +05:30
end
end
2016-11-24 13:41:30 +05:30
context 'when an LDAP person is not found by uid' do
2018-05-09 12:01:36 +05:30
it 'tries to find an LDAP person by email and adds the omniauth identity to the user' do
2020-04-08 14:13:33 +05:30
allow(Gitlab::Auth::Ldap::Person).to receive(:find_by_uid).and_return(nil)
allow(Gitlab::Auth::Ldap::Person).to receive(:find_by_email).and_return(ldap_user)
2018-05-09 12:01:36 +05:30
oauth_user.save
identities_as_hash = gl_user.identities.map { |id| { provider: id.provider, extern_uid: id.extern_uid } }
expect(identities_as_hash).to match_array(result_identities(dn, uid))
end
context 'when also not found by email' do
it 'tries to find an LDAP person by DN and adds the omniauth identity to the user' do
2020-04-08 14:13:33 +05:30
allow(Gitlab::Auth::Ldap::Person).to receive(:find_by_uid).and_return(nil)
allow(Gitlab::Auth::Ldap::Person).to receive(:find_by_email).and_return(nil)
allow(Gitlab::Auth::Ldap::Person).to receive(:find_by_dn).and_return(ldap_user)
2018-05-09 12:01:36 +05:30
oauth_user.save
identities_as_hash = gl_user.identities.map { |id| { provider: id.provider, extern_uid: id.extern_uid } }
expect(identities_as_hash).to match_array(result_identities(dn, uid))
end
end
end
2016-11-24 13:41:30 +05:30
2018-05-09 12:01:36 +05:30
def result_identities(dn, uid)
[
{ provider: 'ldapmain', extern_uid: dn },
{ provider: 'twitter', extern_uid: uid }
]
end
context 'when there is an LDAP connection error' do
before do
raise_ldap_connection_error
end
it 'does not save the identity' do
2016-11-24 13:41:30 +05:30
oauth_user.save
identities_as_hash = gl_user.identities.map { |id| { provider: id.provider, extern_uid: id.extern_uid } }
2018-05-09 12:01:36 +05:30
expect(identities_as_hash).to match_array([{ provider: 'twitter', extern_uid: uid }])
2016-11-24 13:41:30 +05:30
end
end
2015-09-11 14:41:01 +05:30
end
2018-03-17 18:26:18 +05:30
context 'and a corresponding LDAP person with a non-default username' do
before do
allow(ldap_user).to receive(:uid) { uid }
allow(ldap_user).to receive(:username) { 'johndoe@example.com' }
allow(ldap_user).to receive(:email) { %w(johndoe@example.com john2@example.com) }
allow(ldap_user).to receive(:dn) { dn }
end
context 'and no account for the LDAP user' do
it 'creates a user favoring the LDAP username and strips email domain' do
2020-04-08 14:13:33 +05:30
allow(Gitlab::Auth::Ldap::Person).to receive(:find_by_uid).and_return(ldap_user)
2018-03-17 18:26:18 +05:30
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user.username).to eql 'johndoe'
end
end
end
2015-09-11 14:41:01 +05:30
context "and no corresponding LDAP person" do
2017-09-10 17:25:29 +05:30
before do
2020-04-08 14:13:33 +05:30
allow(Gitlab::Auth::Ldap::Person).to receive(:find_by_uid).and_return(nil)
2017-09-10 17:25:29 +05:30
end
2015-09-11 14:41:01 +05:30
include_examples "to verify compliance with allow_single_sign_on"
end
2015-04-26 12:48:37 +05:30
end
end
end
describe 'blocking' do
let(:provider) { 'twitter' }
2017-09-10 17:25:29 +05:30
before do
stub_omniauth_config(allow_single_sign_on: ['twitter'])
end
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
context 'signup with omniauth only' do
2015-04-26 12:48:37 +05:30
context 'dont block on create' do
2017-09-10 17:25:29 +05:30
before do
stub_omniauth_config(block_auto_created_users: false)
end
2015-04-26 12:48:37 +05:30
it do
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user).not_to be_blocked
end
end
context 'block on create' do
2017-09-10 17:25:29 +05:30
before do
stub_omniauth_config(block_auto_created_users: true)
end
2015-04-26 12:48:37 +05:30
it do
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user).to be_blocked
end
end
end
2015-09-11 14:41:01 +05:30
context 'signup with linked omniauth and LDAP account' do
before do
stub_omniauth_config(auto_link_ldap_user: true)
allow(ldap_user).to receive(:uid) { uid }
allow(ldap_user).to receive(:username) { uid }
2016-08-24 12:49:21 +05:30
allow(ldap_user).to receive(:email) { ['johndoe@example.com', 'john2@example.com'] }
2018-03-17 18:26:18 +05:30
allow(ldap_user).to receive(:dn) { dn }
2020-04-08 14:13:33 +05:30
allow(Gitlab::Auth::Ldap::Person).to receive(:find_by_uid).and_return(ldap_user)
2015-09-11 14:41:01 +05:30
end
context "and no account for the LDAP user" do
context 'dont block on create (LDAP)' do
2017-09-10 17:25:29 +05:30
before do
2020-04-08 14:13:33 +05:30
allow_next_instance_of(Gitlab::Auth::Ldap::Config) do |instance|
2020-01-01 13:55:28 +05:30
allow(instance).to receive_messages(block_auto_created_users: false)
end
2017-09-10 17:25:29 +05:30
end
2015-09-11 14:41:01 +05:30
it do
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user).not_to be_blocked
end
end
context 'block on create (LDAP)' do
2017-09-10 17:25:29 +05:30
before do
2020-04-08 14:13:33 +05:30
allow_next_instance_of(Gitlab::Auth::Ldap::Config) do |instance|
2020-01-01 13:55:28 +05:30
allow(instance).to receive_messages(block_auto_created_users: true)
end
2017-09-10 17:25:29 +05:30
end
2015-09-11 14:41:01 +05:30
it do
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user).to be_blocked
end
end
end
context 'and LDAP user has an account already' do
2018-03-17 18:26:18 +05:30
let!(:existing_user) { create(:omniauth_user, email: 'john@example.com', extern_uid: dn, provider: 'ldapmain', username: 'john') }
2015-09-11 14:41:01 +05:30
context 'dont block on create (LDAP)' do
2017-09-10 17:25:29 +05:30
before do
2020-04-08 14:13:33 +05:30
allow_next_instance_of(Gitlab::Auth::Ldap::Config) do |instance|
2020-01-01 13:55:28 +05:30
allow(instance).to receive_messages(block_auto_created_users: false)
end
2017-09-10 17:25:29 +05:30
end
2015-09-11 14:41:01 +05:30
it do
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user).not_to be_blocked
end
end
context 'block on create (LDAP)' do
2017-09-10 17:25:29 +05:30
before do
2020-04-08 14:13:33 +05:30
allow_next_instance_of(Gitlab::Auth::Ldap::Config) do |instance|
2020-01-01 13:55:28 +05:30
allow(instance).to receive_messages(block_auto_created_users: true)
end
2017-09-10 17:25:29 +05:30
end
2015-09-11 14:41:01 +05:30
it do
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user).not_to be_blocked
end
end
end
end
2015-04-26 12:48:37 +05:30
context 'sign-in' do
before do
oauth_user.save
oauth_user.gl_user.activate
end
context 'dont block on create' do
2017-09-10 17:25:29 +05:30
before do
stub_omniauth_config(block_auto_created_users: false)
end
2015-04-26 12:48:37 +05:30
it do
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user).not_to be_blocked
end
end
context 'block on create' do
2017-09-10 17:25:29 +05:30
before do
stub_omniauth_config(block_auto_created_users: true)
end
2015-09-11 14:41:01 +05:30
it do
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user).not_to be_blocked
end
end
context 'dont block on create (LDAP)' do
2017-09-10 17:25:29 +05:30
before do
2020-04-08 14:13:33 +05:30
allow_next_instance_of(Gitlab::Auth::Ldap::Config) do |instance|
2020-01-01 13:55:28 +05:30
allow(instance).to receive_messages(block_auto_created_users: false)
end
2017-09-10 17:25:29 +05:30
end
2015-09-11 14:41:01 +05:30
it do
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user).not_to be_blocked
end
end
context 'block on create (LDAP)' do
2017-09-10 17:25:29 +05:30
before do
2020-04-08 14:13:33 +05:30
allow_next_instance_of(Gitlab::Auth::Ldap::Config) do |instance|
2020-01-01 13:55:28 +05:30
allow(instance).to receive_messages(block_auto_created_users: true)
end
2017-09-10 17:25:29 +05:30
end
2015-04-26 12:48:37 +05:30
it do
oauth_user.save
expect(gl_user).to be_valid
expect(gl_user).not_to be_blocked
end
end
end
end
end
2017-09-10 17:25:29 +05:30
2019-02-15 15:39:39 +05:30
describe 'ensure backwards compatibility with sync email from provider option' do
2017-09-10 17:25:29 +05:30
let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }
before do
stub_omniauth_config(sync_email_from_provider: 'my-provider')
2018-03-17 18:26:18 +05:30
stub_omniauth_config(sync_profile_from_provider: ['my-provider'])
2017-09-10 17:25:29 +05:30
end
context "when provider sets an email" do
it "updates the user email" do
expect(gl_user.email).to eq(info_hash[:email])
end
2018-03-17 18:26:18 +05:30
it "has email set as synced" do
expect(gl_user.user_synced_attributes_metadata.email_synced).to be_truthy
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
it "has email set as read-only" do
expect(gl_user.read_only_attribute?(:email)).to be_truthy
end
it "has synced attributes provider set to my-provider" do
expect(gl_user.user_synced_attributes_metadata.provider).to eql 'my-provider'
2017-09-10 17:25:29 +05:30
end
end
context "when provider doesn't set an email" do
before do
info_hash.delete(:email)
end
it "does not update the user email" do
expect(gl_user.email).not_to eq(info_hash[:email])
end
2018-03-17 18:26:18 +05:30
it "has email set as not synced" do
expect(gl_user.user_synced_attributes_metadata.email_synced).to be_falsey
end
it "does not have email set as read-only" do
expect(gl_user.read_only_attribute?(:email)).to be_falsey
2017-09-10 17:25:29 +05:30
end
end
end
describe 'generating username' do
context 'when no collision with existing user' do
it 'generates the username with no counter' do
expect(gl_user.username).to eq('johngitlab-ETC')
end
end
context 'when collision with existing user' do
it 'generates the username with a counter' do
oauth_user.save
oauth_user2 = described_class.new(OmniAuth::AuthHash.new(uid: 'my-uid2', provider: provider, info: { nickname: 'johngitlab-ETC@othermail.com', email: 'john@othermail.com' }))
expect(oauth_user2.gl_user.username).to eq('johngitlab-ETC1')
end
end
context 'when username is a reserved word' do
let(:info_hash) do
{
nickname: 'admin@othermail.com',
email: 'admin@othermail.com'
}
end
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
it 'generates the username with a counter' do
expect(gl_user.username).to eq('admin1')
end
end
end
2018-03-17 18:26:18 +05:30
describe 'updating email with sync profile' do
let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }
before do
stub_omniauth_config(sync_profile_from_provider: ['my-provider'])
stub_omniauth_config(sync_profile_attributes: true)
end
context "when provider sets an email" do
it "updates the user email" do
expect(gl_user.email).to eq(info_hash[:email])
end
it "has email set as synced" do
expect(gl_user.user_synced_attributes_metadata.email_synced).to be(true)
end
it "has email set as read-only" do
expect(gl_user.read_only_attribute?(:email)).to be_truthy
end
it "has synced attributes provider set to my-provider" do
expect(gl_user.user_synced_attributes_metadata.provider).to eql 'my-provider'
end
end
context "when provider doesn't set an email" do
before do
info_hash.delete(:email)
end
it "does not update the user email" do
expect(gl_user.email).not_to eq(info_hash[:email])
end
it "has email set as not synced" do
expect(gl_user.user_synced_attributes_metadata.email_synced).to be_falsey
end
it "does not have email set as read-only" do
expect(gl_user.read_only_attribute?(:email)).to be_falsey
end
end
end
describe 'updating name' do
let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }
before do
stub_omniauth_setting(sync_profile_from_provider: ['my-provider'])
stub_omniauth_setting(sync_profile_attributes: true)
end
context "when provider sets a name" do
it "updates the user name" do
expect(gl_user.name).to eq(info_hash[:name])
end
end
context "when provider doesn't set a name" do
before do
info_hash.delete(:name)
end
it "does not update the user name" do
expect(gl_user.name).not_to eq(info_hash[:name])
expect(gl_user.user_synced_attributes_metadata.name_synced).to be(false)
end
end
end
describe 'updating location' do
let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }
before do
stub_omniauth_setting(sync_profile_from_provider: ['my-provider'])
stub_omniauth_setting(sync_profile_attributes: true)
end
context "when provider sets a location" do
it "updates the user location" do
expect(gl_user.location).to eq(info_hash[:address][:locality] + ', ' + info_hash[:address][:country])
expect(gl_user.user_synced_attributes_metadata.location_synced).to be(true)
end
end
context "when provider doesn't set a location" do
before do
info_hash[:address].delete(:country)
info_hash[:address].delete(:locality)
end
it "does not update the user location" do
expect(gl_user.location).to be_nil
expect(gl_user.user_synced_attributes_metadata.location_synced).to be(false)
end
end
end
describe 'updating user info' do
let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }
context "update all info" do
before do
stub_omniauth_setting(sync_profile_from_provider: ['my-provider'])
stub_omniauth_setting(sync_profile_attributes: true)
end
it "updates the user email" do
expect(gl_user.email).to eq(info_hash[:email])
expect(gl_user.user_synced_attributes_metadata.email_synced).to be(true)
end
it "updates the user name" do
expect(gl_user.name).to eq(info_hash[:name])
expect(gl_user.user_synced_attributes_metadata.name_synced).to be(true)
end
it "updates the user location" do
expect(gl_user.location).to eq(info_hash[:address][:locality] + ', ' + info_hash[:address][:country])
expect(gl_user.user_synced_attributes_metadata.location_synced).to be(true)
end
it "sets my-provider as the attributes provider" do
expect(gl_user.user_synced_attributes_metadata.provider).to eql('my-provider')
end
end
context "update only requested info" do
before do
stub_omniauth_setting(sync_profile_from_provider: ['my-provider'])
stub_omniauth_setting(sync_profile_attributes: %w(name location))
end
it "updates the user name" do
expect(gl_user.name).to eq(info_hash[:name])
expect(gl_user.user_synced_attributes_metadata.name_synced).to be(true)
end
it "updates the user location" do
expect(gl_user.location).to eq(info_hash[:address][:locality] + ', ' + info_hash[:address][:country])
expect(gl_user.user_synced_attributes_metadata.location_synced).to be(true)
end
it "does not update the user email" do
expect(gl_user.user_synced_attributes_metadata.email_synced).to be(false)
end
end
context "update default_scope" do
before do
stub_omniauth_setting(sync_profile_from_provider: ['my-provider'])
end
it "updates the user email" do
expect(gl_user.email).to eq(info_hash[:email])
expect(gl_user.user_synced_attributes_metadata.email_synced).to be(true)
end
end
context "update no info when profile sync is nil" do
it "does not have sync_attribute" do
expect(gl_user.user_synced_attributes_metadata).to be(nil)
end
it "does not update the user email" do
expect(gl_user.email).not_to eq(info_hash[:email])
end
it "does not update the user name" do
expect(gl_user.name).not_to eq(info_hash[:name])
end
it "does not update the user location" do
expect(gl_user.location).not_to eq(info_hash[:address][:country])
end
it 'does not create associated user synced attributes metadata' do
expect(gl_user.user_synced_attributes_metadata).to be_nil
end
end
end
describe '.find_by_uid_and_provider' do
let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }
it 'normalizes extern_uid' do
allow(oauth_user.auth_hash).to receive(:uid).and_return('MY-UID')
expect(oauth_user.find_user).to eql gl_user
end
end
2018-05-09 12:01:36 +05:30
describe '#find_ldap_person' do
context 'when LDAP connection fails' do
before do
raise_ldap_connection_error
end
it 'returns nil' do
2020-04-08 14:13:33 +05:30
adapter = Gitlab::Auth::Ldap::Adapter.new('ldapmain')
2018-05-09 12:01:36 +05:30
hash = OmniAuth::AuthHash.new(uid: 'whatever', provider: 'ldapmain')
expect(oauth_user.send(:find_ldap_person, hash, adapter)).to be_nil
end
end
end
2018-11-08 19:23:39 +05:30
2019-12-04 20:38:33 +05:30
describe "#bypass_two_factor?" do
it "when with allow_bypass_two_factor disabled (Default)" do
stub_omniauth_config(allow_bypass_two_factor: false)
expect(oauth_user.bypass_two_factor?).to be_falsey
end
it "when with allow_bypass_two_factor enabled" do
stub_omniauth_config(allow_bypass_two_factor: true)
expect(oauth_user.bypass_two_factor?).to be_truthy
end
it "when provider in allow_bypass_two_factor array" do
stub_omniauth_config(allow_bypass_two_factor: [provider])
expect(oauth_user.bypass_two_factor?).to be_truthy
end
2018-11-08 19:23:39 +05:30
2019-12-04 20:38:33 +05:30
it "when provider not in allow_bypass_two_factor array" do
stub_omniauth_config(allow_bypass_two_factor: ["foo"])
expect(oauth_user.bypass_two_factor?).to be_falsey
2018-11-08 19:23:39 +05:30
end
end
2015-04-26 12:48:37 +05:30
end