person_spec.rb 4.05 KB
Newer Older
1 2 3 4 5 6 7 8 9
require 'spec_helper'

describe Gitlab::LDAP::Person do
  include LdapHelpers

  let(:entry) { ldap_user_entry('john.doe') }

  before do
    stub_ldap_config(
10
      options: {
11
        'uid'        => 'uid',
12
        'attributes' => {
13 14 15
          'name'     => 'cn',
          'email'    => %w(mail email userPrincipalName),
          'username' => username_attribute
16
        }
17 18 19
      }
    )
  end
20
  let(:username_attribute) { %w(uid sAMAccountName userid) }
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
  describe '.normalize_dn' do
    subject { described_class.normalize_dn(given) }

    it_behaves_like 'normalizes a DN'

    context 'with an exception during normalization' do
      let(:given) { 'John "Smith,' } # just something that will cause an exception

      it 'returns the given DN unmodified' do
        expect(subject).to eq(given)
      end
    end
  end

  describe '.normalize_uid' do
    subject { described_class.normalize_uid(given) }

    it_behaves_like 'normalizes a DN attribute value'

    context 'with an exception during normalization' do
      let(:given) { 'John "Smith,' } # just something that will cause an exception

      it 'returns the given UID unmodified' do
        expect(subject).to eq(given)
      end
    end
  end

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  describe '.ldap_attributes' do
    it 'returns a compact and unique array' do
      stub_ldap_config(
        options: {
          'uid'        => nil,
          'attributes' => {
            'name'     => 'cn',
            'email'    => 'mail',
            'username' => %w(uid mail memberof)
          }
        }
      )
      config = Gitlab::LDAP::Config.new('ldapmain')
      ldap_attributes = described_class.ldap_attributes(config)

      expect(ldap_attributes).to match_array(%w(dn uid cn mail memberof))
    end
  end

  describe '.validate_entry' do
    it 'raises InvalidEntryError' do
      entry['foo'] = 'bar'

      expect { described_class.new(entry, 'ldapmain') }
        .to raise_error(Gitlab::LDAP::Person::InvalidEntryError)
    end
  end

78 79 80 81
  describe '#name' do
    it 'uses the configured name attribute and handles values as an array' do
      name = 'John Doe'
      entry['cn'] = [name]
82
      person = described_class.new(entry, 'ldapmain')
83 84 85 86 87 88 89 90 91

      expect(person.name).to eq(name)
    end
  end

  describe '#email' do
    it 'returns the value of mail, if present' do
      mail = 'john@example.com'
      entry['mail'] = mail
92
      person = described_class.new(entry, 'ldapmain')
93

94
      expect(person.email).to eq([mail])
95 96 97 98 99
    end

    it 'returns the value of userPrincipalName, if mail and email are not present' do
      user_principal_name = 'john.doe@example.com'
      entry['userPrincipalName'] = user_principal_name
100
      person = described_class.new(entry, 'ldapmain')
101

102
      expect(person.email).to eq([user_principal_name])
103 104
    end
  end
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  describe '#username' do
    context 'with default uid username attribute' do
      let(:username_attribute) { 'uid' }

      it 'returns the proper username value' do
        attr_value = 'johndoe'
        entry[username_attribute] = attr_value
        person = described_class.new(entry, 'ldapmain')

        expect(person.username).to eq(attr_value)
      end
    end

    context 'with a different username attribute' do
      let(:username_attribute) { 'sAMAccountName' }

      it 'returns the proper username value' do
        attr_value = 'johndoe'
        entry[username_attribute] = attr_value
        person = described_class.new(entry, 'ldapmain')

        expect(person.username).to eq(attr_value)
      end
    end

    context 'with a non-standard username attribute' do
      let(:username_attribute) { 'mail' }

      it 'returns the proper username value' do
        attr_value = 'john.doe@example.com'
        entry[username_attribute] = attr_value
        person = described_class.new(entry, 'ldapmain')

        expect(person.username).to eq(attr_value)
      end
    end
  end

144 145 146 147
  def assert_generic_test(test_description, got, expected)
    test_failure_message = "Failed test description: '#{test_description}'\n\n    expected: #{expected}\n         got: #{got}"
    expect(got).to eq(expected), test_failure_message
  end
148
end