2014-10-14 03:31:38 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Gitlab::LDAP::Authentication do
|
2015-05-21 17:49:06 -04:00
|
|
|
let(:user) { create(:omniauth_user, extern_uid: dn) }
|
|
|
|
let(:dn) { 'uid=john,ou=people,dc=example,dc=com' }
|
|
|
|
let(:login) { 'john' }
|
2014-10-14 03:31:38 -04:00
|
|
|
let(:password) { 'password' }
|
|
|
|
|
2015-05-21 17:49:06 -04:00
|
|
|
describe 'login' do
|
2014-10-14 03:31:38 -04:00
|
|
|
before do
|
2015-05-21 17:49:06 -04:00
|
|
|
allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(true)
|
2014-10-14 03:31:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "finds the user if authentication is successful" do
|
2015-05-21 17:49:06 -04:00
|
|
|
expect(user).not_to be_nil
|
|
|
|
|
2014-10-14 03:31:38 -04:00
|
|
|
# try only to fake the LDAP call
|
2015-05-21 17:49:06 -04:00
|
|
|
adapter = double('adapter', dn: dn).as_null_object
|
2017-06-21 09:48:12 -04:00
|
|
|
allow_any_instance_of(described_class)
|
|
|
|
.to receive(:adapter).and_return(adapter)
|
2015-05-21 17:49:06 -04:00
|
|
|
|
|
|
|
expect(described_class.login(login, password)).to be_truthy
|
2014-10-14 03:31:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is false if the user does not exist" do
|
|
|
|
# try only to fake the LDAP call
|
2015-05-21 17:49:06 -04:00
|
|
|
adapter = double('adapter', dn: dn).as_null_object
|
2017-06-21 09:48:12 -04:00
|
|
|
allow_any_instance_of(described_class)
|
|
|
|
.to receive(:adapter).and_return(adapter)
|
2015-05-21 17:49:06 -04:00
|
|
|
|
|
|
|
expect(described_class.login(login, password)).to be_falsey
|
2014-10-14 03:31:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is false if authentication fails" do
|
2015-05-21 17:49:06 -04:00
|
|
|
expect(user).not_to be_nil
|
|
|
|
|
2014-10-14 03:31:38 -04:00
|
|
|
# try only to fake the LDAP call
|
2015-05-21 17:49:06 -04:00
|
|
|
adapter = double('adapter', bind_as: nil).as_null_object
|
2017-06-21 09:48:12 -04:00
|
|
|
allow_any_instance_of(described_class)
|
|
|
|
.to receive(:adapter).and_return(adapter)
|
2015-05-21 17:49:06 -04:00
|
|
|
|
|
|
|
expect(described_class.login(login, password)).to be_falsey
|
2014-10-14 03:31:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if ldap is disabled" do
|
2015-05-21 17:49:06 -04:00
|
|
|
allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(false)
|
|
|
|
expect(described_class.login(login, password)).to be_falsey
|
2014-10-14 03:31:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if no login is supplied" do
|
2015-05-21 17:49:06 -04:00
|
|
|
expect(described_class.login('', password)).to be_falsey
|
2014-10-14 03:31:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if no password is supplied" do
|
2015-05-21 17:49:06 -04:00
|
|
|
expect(described_class.login(login, '')).to be_falsey
|
2014-10-14 03:31:38 -04:00
|
|
|
end
|
|
|
|
end
|
2015-05-21 17:49:06 -04:00
|
|
|
end
|