summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ldap/person_spec.rb
blob: ff29d9aa5be5872b9826dd002b337a10df7caa9e (plain)
1
2
3
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
144
145
146
147
148
require 'spec_helper'

describe Gitlab::LDAP::Person do
  include LdapHelpers

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

  before do
    stub_ldap_config(
      options: {
        'uid'        => 'uid',
        'attributes' => {
          'name'     => 'cn',
          'email'    => %w(mail email userPrincipalName),
          'username' => username_attribute
        }
      }
    )
  end
  let(:username_attribute) { %w(uid sAMAccountName userid) }

  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

  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

  describe '#name' do
    it 'uses the configured name attribute and handles values as an array' do
      name = 'John Doe'
      entry['cn'] = [name]
      person = described_class.new(entry, 'ldapmain')

      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
      person = described_class.new(entry, 'ldapmain')

      expect(person.email).to eq([mail])
    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
      person = described_class.new(entry, 'ldapmain')

      expect(person.email).to eq([user_principal_name])
    end
  end

  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

  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
end