This repository was archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathldap_authenticator_spec.rb
268 lines (236 loc) · 8.08 KB
/
ldap_authenticator_spec.rb
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
require 'spec_helper'
require 'casino/ldap_authenticator'
describe CASino::LDAPAuthenticator do
let(:options) { {
:host => 'localhost',
:port => 12445,
:base => 'dc=users,dc=example.com',
:encryption => 'simple_tls',
:username_attribute => 'uid',
:extra_attributes => extra_attributes_options
} }
let(:extra_attributes_options) { Hash :email => 'mail', :fullname => :displayname, :memberof => 'memberof' }
let(:subject) { described_class.new(options) }
let(:connection) { Object.new }
before(:each) do
Net::LDAP.stub(:new).and_return(connection)
[:host=, :port=, :encryption, :bind_as, :search].each do |setting|
connection.stub(setting)
end
end
describe '#load_user_data' do
let(:username) { 'NaNiwa' }
let(:email) { 'naniwa@swarmhosts.com' }
context 'valid username' do
let(:ldap_entry) {
Net::LDAP::Entry.new.tap do |entry|
entry[:uid] = username
entry[:mail] = email
end
}
before(:each) do
connection.stub(:search) do
ldap_entry
end
end
it 'returns the username' do
subject.load_user_data(username)[:username].should eq(username)
end
it 'returns the extra attributes' do
subject.load_user_data(username)[:extra_attributes][:email].should eq(email)
end
end
context 'invalid username' do
it 'returns nil' do
subject.load_user_data(username).should eq(nil)
end
end
end
describe '#validate' do
let(:username) { 'test' }
let(:password) { 'foo' }
let(:user_filter) { Net::LDAP::Filter.eq(options[:username_attribute], username) }
let(:extra_attributes) { ['mail', :displayname, 'memberof'] }
it 'does the connection setup' do
connection.should_receive(:host=).with(options[:host])
connection.should_receive(:port=).with(options[:port])
connection.should_receive(:encryption).with(:"#{options[:encryption]}")
subject.validate(username, password)
end
it 'calls the #bind_as method on the LDAP connection' do
connection.should_receive(:bind_as).with(:base => options[:base], :size => 1, :password => password, :filter => user_filter)
subject.validate(username, password)
end
context 'when validation succeeds' do
before(:each) do
connection.stub(:bind_as).and_return(Net::LDAP::Entry.new)
end
it 'calls the #search method on the LDAP connection' do
connection.should_receive(:search).with(:base => options[:base], :filter => user_filter, :attributes => extra_attributes + [options[:username_attribute]])
subject.validate(username, password)
end
end
context 'when validation fails' do
before(:each) do
connection.stub(:bind_as).and_return(false)
end
it 'does not call the #search method on the LDAP connection' do
connection.should_not_receive(:search)
subject.validate(username, password)
end
end
context 'with an empty password' do
let(:password) { '' }
it 'does not call the #bind_as method on the LDAP connection' do
connection.should_not_receive(:bind_as)
subject.validate(username, password)
end
end
context 'when validation succeeds for user with missing data' do
let(:fullname) { 'Example User' }
let(:email) { "#{username}@example.org" }
let(:ldap_entry) {
entry = Net::LDAP::Entry.new
{:uid => username, :displayname => fullname, :mail => email}.each do |key, value|
entry[key] = [value]
end
entry
}
before(:each) do
connection.stub(:bind_as) do
ldap_entry
end
connection.stub(:search) do
ldap_entry
end
end
it 'returns the user data with blank value for missing data' do
subject.validate(username, password).should == {
username: username,
extra_attributes: {
:email => email,
:fullname => fullname,
:memberof => ''
}
}
end
end
context 'when validation succeeds for user with complete data' do
let(:fullname) { 'Example User' }
let(:email) { "#{username}@example.org" }
let(:membership) { "cn=group1" }
let(:ldap_entry) {
entry = Net::LDAP::Entry.new
{:uid => username, :displayname => fullname, :mail => email, :memberof => membership}.each do |key, value|
entry[key] = [value]
end
entry
}
before(:each) do
connection.stub(:bind_as) do
ldap_entry
end
connection.stub(:search) do
ldap_entry
end
end
it 'returns the user data' do
subject.validate(username, password).should == {
username: username,
extra_attributes: {
:email => email,
:fullname => fullname,
:memberof => membership
}
}
end
context 'when supplied the common_names option' do
let(:extra_attributes_options) { super().merge(common_names: 'common_names') }
let(:group_1) { 'CN=group1,OU=Organization,OU=Unit,DC=Domain,DC=Component' }
let(:ldap_entry) do
Net::LDAP::Entry.new.tap do |entry|
entry[:uid] = [username]
entry[:displayname] = [fullname]
entry[:mail] = [email]
end
end
context 'when the user belongs to no groups' do
it 'returns the user data with an empty list of common names' do
subject.validate(username, password).should == {
username: username,
extra_attributes: {
:email => email,
:fullname => fullname,
:memberof => '',
:common_names => []
}
}
end
end
context 'when the user belongs to many groups' do
let(:membership) do
[
group_1,
'CN=group2,OU=Organization,OU=Unit,DC=Domain,DC=Component',
'CN=group3,OU=Organization,OU=Unit,DC=Domain,DC=Component',
]
end
let(:ldap_entry) { super().tap { |entry| entry[:memberof] = membership } }
it 'returns the user data with a list of common names of the groups' do
subject.validate(username, password).should == {
username: username,
extra_attributes: {
:email => email,
:fullname => fullname,
:memberof => group_1,
:common_names => ['group1', 'group2', 'group3']
}
}
end
end
end
end
context 'when validation fails' do
before(:each) do
connection.stub(:bind_as) do
false
end
end
it 'returns false' do
subject.validate(username, password).should == false
end
end
context 'when communication error occurs' do
before(:each) do
connection.stub(:bind_as) do
raise Net::LDAP::LdapError, 'foo'
end
end
it 'raises an AuthenticatorError' do
lambda {
subject.validate(username, password)
}.should raise_error(CASino::Authenticator::AuthenticatorError)
end
end
context 'with an admin user' do
before(:each) do
options.merge! :admin_user => 'admin', :admin_password => 'password'
end
it 'authenticates the admin user' do
connection.should_receive(:auth).with(options[:admin_user], options[:admin_password])
subject.validate(username, password)
end
end
context 'with a filter' do
let(:filter_expression) { '(!(attribute=abc))' }
let(:filter) { user_filter & Net::LDAP::Filter.construct(filter_expression) }
before(:each) do
options.merge! :filter => filter_expression
end
it 'calls the #bind_as method on the LDAP connection' do
connection.should_receive(:bind_as).with(:base => options[:base], :size => 1, :password => password, :filter => filter)
subject.validate(username, password)
end
end
end
end