debian-mirror-gitlab/spec/lib/gitlab/auth/ldap/auth_hash_spec.rb

115 lines
2.7 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2015-09-25 12:07:36 +05:30
require 'spec_helper'
2020-04-08 14:13:33 +05:30
describe Gitlab::Auth::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
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(:attributes).and_return(attributes)
end
2015-09-25 12:07:36 +05:30
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
2019-12-26 22:10:19 +05:30
raw_info[:uid] = [+'JOHN']
2018-03-17 18:26:18 +05:30
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