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

59 lines
1.8 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
require 'spec_helper'
2018-03-27 19:54:05 +05:30
describe Gitlab::Auth::LDAP::Authentication do
2018-03-17 18:26:18 +05:30
let(:dn) { 'uid=John Smith, ou=People, dc=example, dc=com' }
2018-03-27 19:54:05 +05:30
let(:user) { create(:omniauth_user, extern_uid: Gitlab::Auth::LDAP::Person.normalize_dn(dn)) }
2015-09-11 14:41:01 +05:30
let(:login) { 'john' }
2015-04-26 12:48:37 +05:30
let(:password) { 'password' }
2015-09-11 14:41:01 +05:30
describe 'login' do
2015-04-26 12:48:37 +05:30
before do
2018-03-27 19:54:05 +05:30
allow(Gitlab::Auth::LDAP::Config).to receive(:enabled?).and_return(true)
2015-04-26 12:48:37 +05:30
end
it "finds the user if authentication is successful" do
2015-09-11 14:41:01 +05:30
expect(user).not_to be_nil
2015-04-26 12:48:37 +05:30
# try only to fake the LDAP call
2015-09-11 14:41:01 +05:30
adapter = double('adapter', dn: dn).as_null_object
2017-09-10 17:25:29 +05:30
allow_any_instance_of(described_class)
.to receive(:adapter).and_return(adapter)
2015-09-11 14:41:01 +05:30
expect(described_class.login(login, password)).to be_truthy
2015-04-26 12:48:37 +05:30
end
it "is false if the user does not exist" do
# try only to fake the LDAP call
2015-09-11 14:41:01 +05:30
adapter = double('adapter', dn: dn).as_null_object
2017-09-10 17:25:29 +05:30
allow_any_instance_of(described_class)
.to receive(:adapter).and_return(adapter)
2015-09-11 14:41:01 +05:30
expect(described_class.login(login, password)).to be_falsey
2015-04-26 12:48:37 +05:30
end
it "is false if authentication fails" do
2015-09-11 14:41:01 +05:30
expect(user).not_to be_nil
2015-04-26 12:48:37 +05:30
# try only to fake the LDAP call
2015-09-11 14:41:01 +05:30
adapter = double('adapter', bind_as: nil).as_null_object
2017-09-10 17:25:29 +05:30
allow_any_instance_of(described_class)
.to receive(:adapter).and_return(adapter)
2015-09-11 14:41:01 +05:30
expect(described_class.login(login, password)).to be_falsey
2015-04-26 12:48:37 +05:30
end
it "fails if ldap is disabled" do
2018-03-27 19:54:05 +05:30
allow(Gitlab::Auth::LDAP::Config).to receive(:enabled?).and_return(false)
2015-09-11 14:41:01 +05:30
expect(described_class.login(login, password)).to be_falsey
2015-04-26 12:48:37 +05:30
end
it "fails if no login is supplied" do
2015-09-11 14:41:01 +05:30
expect(described_class.login('', password)).to be_falsey
2015-04-26 12:48:37 +05:30
end
it "fails if no password is supplied" do
2015-09-11 14:41:01 +05:30
expect(described_class.login(login, '')).to be_falsey
2015-04-26 12:48:37 +05:30
end
end
2015-09-11 14:41:01 +05:30
end