BigW Consortium Gitlab

adapter_spec.rb 3.79 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe Gitlab::LDAP::Adapter, lib: true do
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  include LdapHelpers

  let(:ldap) { double(:ldap) }
  let(:adapter) { ldap_adapter('ldapmain', ldap) }

  describe '#users' do
    before do
      stub_ldap_config(base: 'dc=example,dc=com')
    end

    it 'searches with the proper options when searching by uid' do
      # Requires this expectation style to match the filter
      expect(adapter).to receive(:ldap_search) do |arg|
        expect(arg[:filter].to_s).to eq('(uid=johndoe)')
        expect(arg[:base]).to eq('dc=example,dc=com')
        expect(arg[:attributes]).to match(%w{uid cn mail dn})
      end.and_return({})

      adapter.users('uid', 'johndoe')
    end

    it 'searches with the proper options when searching by dn' do
      expect(adapter).to receive(:ldap_search).with(
        base: 'uid=johndoe,ou=users,dc=example,dc=com',
        scope: Net::LDAP::SearchScope_BaseObject,
        attributes: %w{uid cn mail dn},
        filter: nil
      ).and_return({})

      adapter.users('dn', 'uid=johndoe,ou=users,dc=example,dc=com')
    end

    it 'searches with the proper options when searching with a limit' do
      expect(adapter)
        .to receive(:ldap_search).with(hash_including(size: 100)).and_return({})

      adapter.users('uid', 'johndoe', 100)
    end

    it 'returns an LDAP::Person if search returns a result' do
      entry = ldap_user_entry('johndoe')
      allow(adapter).to receive(:ldap_search).and_return([entry])

      results = adapter.users('uid', 'johndoe')

      expect(results.size).to eq(1)
      expect(results.first.uid).to eq('johndoe')
    end

    it 'returns empty array if search entry does not respond to uid' do
      entry = Net::LDAP::Entry.new
      entry['dn'] = user_dn('johndoe')
      allow(adapter).to receive(:ldap_search).and_return([entry])

      results = adapter.users('uid', 'johndoe')

      expect(results).to be_empty
    end

    it 'uses the right uid attribute when non-default' do
      stub_ldap_config(uid: 'sAMAccountName')
      expect(adapter).to receive(:ldap_search).with(
        hash_including(attributes: %w{sAMAccountName cn mail dn})
      ).and_return({})

      adapter.users('sAMAccountName', 'johndoe')
    end
  end
72

73
  describe '#dn_matches_filter?' do
74 75
    subject { adapter.dn_matches_filter?(:dn, :filter) }

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    context "when the search result is non-empty" do
      before { allow(adapter).to receive(:ldap_search).and_return([:foo]) }

      it { is_expected.to be_truthy }
    end

    context "when the search result is empty" do
      before { allow(adapter).to receive(:ldap_search).and_return([]) }

      it { is_expected.to be_falsey }
    end
  end

  describe '#ldap_search' do
    subject { adapter.ldap_search(base: :dn, filter: :filter) }

92 93
    context "when the search is successful" do
      context "and the result is non-empty" do
94
        before { allow(ldap).to receive(:search).and_return([:foo]) }
95

96
        it { is_expected.to eq [:foo] }
97 98 99
      end

      context "and the result is empty" do
100
        before { allow(ldap).to receive(:search).and_return([]) }
101

102
        it { is_expected.to eq [] }
103 104 105 106
      end
    end

    context "when the search encounters an error" do
107 108 109 110 111 112
      before do
        allow(ldap).to receive_messages(
          search: nil,
          get_operation_result: double(code: 1, message: 'some error')
        )
      end
113

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
      it { is_expected.to eq [] }
    end

    context "when the search raises an LDAP exception" do
      before do
        allow(ldap).to receive(:search) { raise Net::LDAP::Error, "some error" }
        allow(Rails.logger).to receive(:warn)
      end

      it { is_expected.to eq [] }

      it 'logs the error' do
        subject
        expect(Rails.logger).to have_received(:warn).with(
          "LDAP search raised exception Net::LDAP::Error: some error")
      end
130 131 132
    end
  end
end