BigW Consortium Gitlab

authentication_spec.rb 1.78 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe Gitlab::LDAP::Authentication, lib: true do
4 5 6
  let(:user)     { create(:omniauth_user, extern_uid: dn) }
  let(:dn)       { 'uid=john,ou=people,dc=example,dc=com' }
  let(:login)    { 'john' }
7 8
  let(:password) { 'password' }

9
  describe 'login' do
10
    before do
11
      allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(true)
12 13 14
    end

    it "finds the user if authentication is successful" do
15 16
      expect(user).not_to be_nil

17
      # try only to fake the LDAP call
18 19 20 21 22
      adapter = double('adapter', dn: dn).as_null_object
      allow_any_instance_of(described_class).
        to receive(:adapter).and_return(adapter)

      expect(described_class.login(login, password)).to be_truthy
23 24 25 26
    end

    it "is false if the user does not exist" do
      # try only to fake the LDAP call
27 28 29 30 31
      adapter = double('adapter', dn: dn).as_null_object
      allow_any_instance_of(described_class).
        to receive(:adapter).and_return(adapter)

      expect(described_class.login(login, password)).to be_falsey
32 33 34
    end

    it "is false if authentication fails" do
35 36
      expect(user).not_to be_nil

37
      # try only to fake the LDAP call
38 39 40 41 42
      adapter = double('adapter', bind_as: nil).as_null_object
      allow_any_instance_of(described_class).
        to receive(:adapter).and_return(adapter)

      expect(described_class.login(login, password)).to be_falsey
43 44 45
    end

    it "fails if ldap is disabled" do
46 47
      allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(false)
      expect(described_class.login(login, password)).to be_falsey
48 49 50
    end

    it "fails if no login is supplied" do
51
      expect(described_class.login('', password)).to be_falsey
52 53 54
    end

    it "fails if no password is supplied" do
55
      expect(described_class.login(login, '')).to be_falsey
56 57
    end
  end
58
end