2015-09-25 12:07:36 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe Gitlab::LDAP::AuthHash do
|
2018-03-17 18:26:18 +05:30
|
|
|
include LdapHelpers
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
let(:auth_hash) do
|
2017-09-10 17:25:29 +05:30
|
|
|
described_class.new(
|
2015-09-25 12:07:36 +05:30
|
|
|
OmniAuth::AuthHash.new(
|
2018-03-17 18:26:18 +05:30
|
|
|
uid: given_uid,
|
|
|
|
provider: 'ldapmain',
|
2015-09-25 12:07:36 +05:30
|
|
|
info: info,
|
|
|
|
extra: {
|
|
|
|
raw_info: raw_info
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:info) do
|
|
|
|
{
|
|
|
|
name: 'Smith, J.',
|
|
|
|
email: 'johnsmith@example.com',
|
|
|
|
nickname: '123456'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:raw_info) do
|
|
|
|
{
|
|
|
|
uid: ['123456'],
|
|
|
|
email: ['johnsmith@example.com'],
|
|
|
|
cn: ['Smith, J.'],
|
|
|
|
fullName: ['John Smith']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without overridden attributes" do
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:given_uid) { 'uid=John Smith,ou=People,dc=example,dc=com' }
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
it "has the correct username" do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(auth_hash.username).to eq("123456")
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "has the correct name" do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(auth_hash.name).to eq("Smith, J.")
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with overridden attributes" do
|
2018-03-17 18:26:18 +05:30
|
|
|
let(:given_uid) { 'uid=John Smith,ou=People,dc=example,dc=com' }
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
let(:attributes) do
|
|
|
|
{
|
2017-08-17 22:00:37 +05:30
|
|
|
'username' => %w(mail email),
|
2015-09-25 12:07:36 +05:30
|
|
|
'name' => 'fullName'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Gitlab::LDAP::Config).to receive(:attributes).and_return(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has the correct username" do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(auth_hash.username).to eq("johnsmith@example.com")
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "has the correct name" do
|
2018-03-17 18:26:18 +05:30
|
|
|
expect(auth_hash.name).to eq("John Smith")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#uid' do
|
|
|
|
context 'when there is extraneous (but valid) whitespace' do
|
|
|
|
let(:given_uid) { 'uid =john smith , ou = people, dc= example,dc =com' }
|
|
|
|
|
|
|
|
it 'removes the extraneous whitespace' do
|
|
|
|
expect(auth_hash.uid).to eq('uid=john smith,ou=people,dc=example,dc=com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are upper case characters' do
|
|
|
|
let(:given_uid) { 'UID=John Smith,ou=People,dc=example,dc=com' }
|
|
|
|
|
|
|
|
it 'downcases' do
|
|
|
|
expect(auth_hash.uid).to eq('uid=john smith,ou=people,dc=example,dc=com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#username' do
|
|
|
|
context 'if lowercase_usernames setting is' do
|
|
|
|
let(:given_uid) { 'uid=John Smith,ou=People,dc=example,dc=com' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
raw_info[:uid] = ['JOHN']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'enabled the username attribute is lower cased' do
|
|
|
|
stub_ldap_config(lowercase_usernames: true)
|
|
|
|
|
|
|
|
expect(auth_hash.username).to eq 'john'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'disabled the username attribute is not lower cased' do
|
|
|
|
stub_ldap_config(lowercase_usernames: false)
|
|
|
|
|
|
|
|
expect(auth_hash.username).to eq 'JOHN'
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|