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

64 lines
2 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
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
2020-01-01 13:55:28 +05:30
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:adapter).and_return(adapter)
end
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
2020-01-01 13:55:28 +05:30
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:adapter).and_return(adapter)
end
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
2020-01-01 13:55:28 +05:30
allow_next_instance_of(described_class) do |instance|
allow(instance).to receive(:adapter).and_return(adapter)
end
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