2014-10-14 03:31:38 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 05:55:36 -05:00
|
|
|
describe Gitlab::LDAP::Config, lib: true do
|
2016-09-29 15:08:27 -04:00
|
|
|
include LdapHelpers
|
|
|
|
|
|
|
|
let(:config) { Gitlab::LDAP::Config.new('ldapmain') }
|
2014-10-14 03:31:38 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
describe '#initalize' do
|
2014-10-14 03:31:38 -04:00
|
|
|
it 'requires a provider' do
|
|
|
|
expect{ Gitlab::LDAP::Config.new }.to raise_error ArgumentError
|
|
|
|
end
|
|
|
|
|
2016-09-29 15:08:27 -04:00
|
|
|
it 'works' do
|
2014-10-14 03:31:38 -04:00
|
|
|
expect(config).to be_a described_class
|
|
|
|
end
|
|
|
|
|
2016-09-29 15:08:27 -04:00
|
|
|
it 'raises an error if a unknown provider is used' do
|
2015-06-17 21:29:18 -04:00
|
|
|
expect{ Gitlab::LDAP::Config.new 'unknown' }.to raise_error(RuntimeError)
|
2014-10-14 03:31:38 -04:00
|
|
|
end
|
|
|
|
end
|
2016-09-29 15:08:27 -04:00
|
|
|
|
2016-11-11 15:44:08 -05:00
|
|
|
describe '#adapter_options' do
|
|
|
|
it 'constructs basic options' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 386,
|
|
|
|
'method' => 'plain'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.adapter_options).to eq(
|
|
|
|
host: 'ldap.example.com',
|
|
|
|
port: 386,
|
|
|
|
encryption: nil
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes authentication options when auth is configured' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 686,
|
|
|
|
'method' => 'ssl',
|
|
|
|
'bind_dn' => 'uid=admin,dc=example,dc=com',
|
|
|
|
'password' => 'super_secret'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.adapter_options).to eq(
|
|
|
|
host: 'ldap.example.com',
|
|
|
|
port: 686,
|
|
|
|
encryption: :simple_tls,
|
|
|
|
auth: {
|
|
|
|
method: :simple,
|
|
|
|
username: 'uid=admin,dc=example,dc=com',
|
|
|
|
password: 'super_secret'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#omniauth_options' do
|
|
|
|
it 'constructs basic options' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'host' => 'ldap.example.com',
|
|
|
|
'port' => 386,
|
|
|
|
'base' => 'ou=users,dc=example,dc=com',
|
|
|
|
'method' => 'plain',
|
|
|
|
'uid' => 'uid'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.omniauth_options).to include(
|
|
|
|
host: 'ldap.example.com',
|
|
|
|
port: 386,
|
|
|
|
base: 'ou=users,dc=example,dc=com',
|
|
|
|
method: 'plain',
|
|
|
|
filter: '(uid=%{username})'
|
|
|
|
)
|
|
|
|
expect(config.omniauth_options.keys).not_to include(:bind_dn, :password)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes authentication options when auth is configured' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'uid' => 'sAMAccountName',
|
|
|
|
'user_filter' => '(memberOf=cn=group1,ou=groups,dc=example,dc=com)',
|
|
|
|
'bind_dn' => 'uid=admin,dc=example,dc=com',
|
|
|
|
'password' => 'super_secret'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.omniauth_options).to include(
|
|
|
|
filter: '(&(sAMAccountName=%{username})(memberOf=cn=group1,ou=groups,dc=example,dc=com))',
|
|
|
|
bind_dn: 'uid=admin,dc=example,dc=com',
|
|
|
|
password: 'super_secret'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-29 15:08:27 -04:00
|
|
|
describe '#has_auth?' do
|
|
|
|
it 'is true when password is set' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'bind_dn' => 'uid=admin,dc=example,dc=com',
|
|
|
|
'password' => 'super_secret'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.has_auth?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is true when bind_dn is set and password is empty' do
|
|
|
|
stub_ldap_config(
|
|
|
|
options: {
|
|
|
|
'bind_dn' => 'uid=admin,dc=example,dc=com',
|
|
|
|
'password' => ''
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(config.has_auth?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is false when password and bind_dn are not set' do
|
|
|
|
stub_ldap_config(options: { 'bind_dn' => nil, 'password' => nil })
|
|
|
|
|
|
|
|
expect(config.has_auth?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
2014-10-23 16:57:16 -04:00
|
|
|
end
|