-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests.py
426 lines (352 loc) · 15.2 KB
/
tests.py
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# -*- coding: utf-8 -*-
#
# who_ldap, LDAP authentication for WSGI applications.
# Copyright (C) 2010-2014 by contributors <see CONTRIBUTORS file>
#
# This file is part of who_ldap
# <https://github.com/m-martinez/who_ldap.git>
#
# This software is subject to the provisions of the BSD-like license at
# http://www.repoze.org/LICENSE.txt. A copy of the license should accompany
# this distribution. THIS SOFTWARE IS PROVIDED 'AS IS' AND ANY AND ALL
# EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND
# FITNESS FOR A PARTICULAR PURPOSE.
"""
Test suite for who_ldap
Uses an actual connection, and attempts to create the testing items
"""
import unittest
BIND_HOST = 'localhost'
BIND_PORT = 389
BIND_URI = 'ldap://%s:%s' % (BIND_HOST, BIND_PORT)
BIND_DN = 'cn=root,dc=example,dc=org'
BIND_PW = 'some password'
DOMAIN_DN = 'dc=example,dc=org'
BASE_DN = 'ou=people,dc=example,dc=org'
fakeuser = {
'dn': 'uid=carla,%s' % BASE_DN,
'uid': 'carla',
'cn': 'Carla Paola',
'sn': 'Paola',
'mail': 'carla@example.org',
'telephoneNumber': '39 123 456 789',
'password': 'hello',
'hashedPassword': '{SHA}qvTGHdzF6KLavt4PO0gs2a6pQ00='
}
def setup_module():
clear()
import ldap3
# Connecting to a fake server with a fake account:
server = ldap3.Server(BIND_HOST, port=BIND_PORT)
conn = ldap3.Connection(
server, user=BIND_DN, password=BIND_PW, auto_bind=True)
# We must explicitly create the BASE_DN DIT components
# Not in ldap3?
# Adding a fake user, which is used in the tests
person_attr = {'cn': fakeuser['cn'],
'sn': fakeuser['sn'],
'uid': fakeuser['uid'],
'userPassword': fakeuser['hashedPassword'],
'telephoneNumber': fakeuser['telephoneNumber'],
'mail': fakeuser['mail']}
assert conn.add(DOMAIN_DN, 'domain'), conn.result
assert conn.add(BASE_DN, 'organizationalUnit'), conn.result
assert conn.add(fakeuser['dn'], 'inetOrgPerson', person_attr), conn.result
conn.close()
def teardown_module():
clear()
def clear():
import ldap3
# Connecting to a fake server with a fake account:
server = ldap3.Server(BIND_HOST, port=BIND_PORT)
conn = ldap3.Connection(
server, user=BIND_DN, password=BIND_PW, auto_bind=True)
conn.delete(fakeuser['dn'])
conn.delete(BASE_DN)
conn.delete(DOMAIN_DN)
conn.close()
class AssertMixin(object):
def assertCountEqual(self, *args, **kw):
if hasattr(self, 'assertItemsEqual'):
self.assertItemsEqual(*args, **kw)
else:
super(AssertMixin, self).assertCountEqual(*args, **kw)
class Base(unittest.TestCase):
"""Base test case for the plugins"""
def makeEnviron(self, kw=None):
"""Create a fake WSGI environment
This is based on the same method of the test suite of repoze.who.
"""
environ = {}
environ['wsgi.version'] = (1, 0)
if kw:
environ.update(kw)
return environ
class TestMakeLDAPAuthenticatorPlugin(unittest.TestCase):
"""Tests for the constructor of the L{LDAPAuthenticatorPlugin} plugin"""
def test_without_connection(self):
from who_ldap import LDAPAuthenticatorPlugin
self.assertRaises(ValueError, LDAPAuthenticatorPlugin, None,
'dc=example,dc=org')
def test_without_BASE_DN(self):
from who_ldap import LDAPAuthenticatorPlugin
self.assertRaises(TypeError, LDAPAuthenticatorPlugin, BIND_URI)
self.assertRaises(ValueError, LDAPAuthenticatorPlugin, BIND_URI, None)
def test_connection_is_url(self):
from who_ldap import LDAPAuthenticatorPlugin
LDAPAuthenticatorPlugin('ldap://example.org', 'dc=example,dc=org')
class TestLDAPAuthenticatorPlugin(Base):
"""Tests for the L{LDAPAuthenticatorPlugin} IAuthenticator plugin"""
def makePlugin(self):
from who_ldap import LDAPAuthenticatorPlugin
return LDAPAuthenticatorPlugin(BIND_URI, BASE_DN)
def test_implements(self):
from zope.interface.verify import verifyClass
from repoze.who.interfaces import IAuthenticator
from who_ldap import LDAPAuthenticatorPlugin
verifyClass(IAuthenticator, LDAPAuthenticatorPlugin, tentative=True)
def test_authenticate_nologin(self):
plugin = self.makePlugin()
env = self.makeEnviron()
result = plugin.authenticate(env, None)
self.assertIsNone(result)
def test_authenticate_incomplete_credentials(self):
plugin = self.makePlugin()
env = self.makeEnviron()
identity1 = {'login': fakeuser['uid']}
identity2 = {'password': fakeuser['password']}
result1 = plugin.authenticate(env, identity1)
result2 = plugin.authenticate(env, identity2)
self.assertIsNone(result1)
self.assertIsNone(result2)
def test_authenticate_noresults(self):
identity = {'login': 'i_dont_exist',
'password': 'super secure password'}
plugin = self.makePlugin()
env = self.makeEnviron()
result = plugin.authenticate(env, identity)
self.assertIsNone(result)
def test_authenticate_comparefail(self):
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': fakeuser['uid'],
'password': 'wrong password'}
result = plugin.authenticate(env, identity)
self.assertIsNone(result)
def test_authenticate_comparesuccess(self):
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': fakeuser['uid'],
'password': fakeuser['password']}
result = plugin.authenticate(env, identity)
self.assertEqual(result, fakeuser['dn'])
def test_custom_authenticator(self):
"""L{LDAPAuthenticatorPlugin._get_dn} should be overriden with no
problems"""
from who_ldap import LDAPAuthenticatorPlugin
class CustomLDAPAuthenticatorPlugin(LDAPAuthenticatorPlugin):
"""Fake class to test that L{LDAPAuthenticatorPlugin._get_dn} can
be overriden with no problems"""
def _get_dn(self, environ, identity):
self.called = True
try:
return u'uid=%s,%s' % (identity['login'], self.base_dn)
except (KeyError, TypeError):
raise ValueError(
'Could not find the DN from the identity '
'and environment')
plugin = CustomLDAPAuthenticatorPlugin(BIND_URI, BASE_DN)
env = self.makeEnviron()
identity = {'login': fakeuser['uid'],
'password': fakeuser['password']}
result = plugin.authenticate(env, identity)
expected = 'uid=%s,%s' % (fakeuser['uid'], BASE_DN)
self.assertEqual(result, expected)
self.assertTrue(plugin.called)
class TestLDAPSearchAuthenticatorPluginNaming(Base):
"""Tests for the L{LDAPSearchAuthenticatorPlugin} IAuthenticator plugin"""
def makePlugin(self):
from who_ldap import LDAPSearchAuthenticatorPlugin
return LDAPSearchAuthenticatorPlugin(
BIND_URI,
BASE_DN,
naming_attribute='telephoneNumber',
)
def test_authenticate_noresults(self):
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': 'i_dont_exist',
'password': 'super secure password'}
result = plugin.authenticate(env, identity)
self.assertIsNone(result)
def test_authenticate_comparefail(self):
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': fakeuser['telephoneNumber'],
'password': 'wrong password'}
result = plugin.authenticate(env, identity)
self.assertIsNone(result)
def test_authenticate_comparesuccess(self):
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': fakeuser['telephoneNumber'],
'password': fakeuser['password']}
result = plugin.authenticate(env, identity)
self.assertEqual(result, fakeuser['dn'])
class TestLDAPAuthenticatorReturnLogin(Base):
"""
Tests the L{LDAPAuthenticatorPlugin} IAuthenticator plugin returning
login.
"""
def makePlugin(self):
from who_ldap import LDAPAuthenticatorPlugin
return LDAPAuthenticatorPlugin(
BIND_URI,
BASE_DN,
returned_id='login',
)
def test_authenticate_noresults(self):
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': 'i_dont_exist',
'password': 'super secure password'}
result = plugin.authenticate(env, identity)
self.assertIsNone(result)
def test_authenticate_comparefail(self):
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': fakeuser['uid'],
'password': 'wrong password'}
result = plugin.authenticate(env, identity)
self.assertIsNone(result)
def test_authenticate_comparesuccess(self):
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': fakeuser['uid'],
'password': fakeuser['password']}
result = plugin.authenticate(env, identity)
self.assertEqual(result, fakeuser['uid'])
def test_authenticate_dn_in_userdata(self):
from base64 import b64encode
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': fakeuser['uid'],
'password': fakeuser['password']}
expected_dn = '<dn:%s>' % b64encode(fakeuser['dn'].encode('utf-8'))
result = plugin.authenticate(env, identity) # NOQA
self.assertEqual(identity['userdata'], expected_dn)
class TestLDAPSearchAuthenticatorReturnLogin(Base):
"""
Tests the L{LDAPSearchAuthenticatorPlugin} IAuthenticator plugin returning
login.
"""
def makePlugin(self):
from who_ldap import LDAPSearchAuthenticatorPlugin
return LDAPSearchAuthenticatorPlugin(
BIND_URI,
BASE_DN,
returned_id='login',
)
def test_authenticate_noresults(self):
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': 'i_dont_exist',
'password': 'super secure password'}
result = plugin.authenticate(env, identity)
self.assertIsNone(result)
def test_authenticate_comparefail(self):
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': fakeuser['uid'],
'password': 'wrong password'}
result = plugin.authenticate(env, identity)
self.assertIsNone(result)
def test_authenticate_comparesuccess(self):
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': fakeuser['uid'],
'password': fakeuser['password']}
result = plugin.authenticate(env, identity)
self.assertEqual(result, fakeuser['uid'])
def test_authenticate_dn_in_userdata(self):
from base64 import b64encode
plugin = self.makePlugin()
env = self.makeEnviron()
identity = {'login': fakeuser['uid'],
'password': fakeuser['password']}
expected_dn = '<dn:%s>' % b64encode(fakeuser['dn'].encode('utf-8'))
result = plugin.authenticate(env, identity) # NOQA
self.assertEqual(identity['userdata'], expected_dn)
class TestLDAPAuthenticatorPluginStartTls(Base):
"""Tests for the L{LDAPAuthenticatorPlugin} IAuthenticator plugin"""
def test_implements(self):
from zope.interface.verify import verifyClass
from repoze.who.interfaces import IAuthenticator
from who_ldap import LDAPAuthenticatorPlugin
verifyClass(IAuthenticator, LDAPAuthenticatorPlugin, tentative=True)
class TestMakeLDAPAttributesPlugin(unittest.TestCase, AssertMixin):
"""Tests for the constructor of L{LDAPAttributesPlugin}"""
def test_connection_is_invalid(self):
from who_ldap import LDAPAttributesPlugin
self.assertRaises(ValueError, LDAPAttributesPlugin, None, 'cn')
def test_attributes_is_none(self):
"""If attributes is None then fetch all the attributes"""
from who_ldap import LDAPAttributesPlugin
plugin = LDAPAttributesPlugin(BIND_URI, None)
self.assertIsNone(plugin.attributes)
def test_attributes_is_comma_separated_str(self):
from who_ldap import LDAPAttributesPlugin
attributes = "cn,uid,mail"
plugin = LDAPAttributesPlugin(BIND_URI, attributes)
self.assertCountEqual(plugin.attributes, attributes.split(','))
def test_attributes_is_only_one_as_str(self):
from who_ldap import LDAPAttributesPlugin
attributes = "mail"
plugin = LDAPAttributesPlugin(BIND_URI, attributes)
self.assertEqual(plugin.attributes, ['mail'])
def test_attributes_is_iterable(self):
from who_ldap import LDAPAttributesPlugin
# The plugin, with a tuple as attributes
attributes_t = ('cn', 'mail')
plugin_t = LDAPAttributesPlugin(BIND_URI, attributes_t)
self.assertCountEqual(plugin_t.attributes, list(attributes_t))
# The plugin, with a list as attributes
attributes_l = ['cn', 'mail']
plugin_l = LDAPAttributesPlugin(BIND_URI, attributes_l)
self.assertCountEqual(plugin_l.attributes, attributes_l)
# The plugin, with a dict as attributes
attributes_d = {'first': 'cn', 'second': 'mail'}
plugin_d = LDAPAttributesPlugin(BIND_URI, attributes_d)
self.assertCountEqual(plugin_d.attributes, list(attributes_d))
def test_attributes_is_not_iterable_nor_string(self):
from who_ldap import LDAPAttributesPlugin
self.assertRaises(ValueError, LDAPAttributesPlugin, BIND_URI,
12345)
def test_parameters_are_valid(self):
from who_ldap import LDAPAttributesPlugin
LDAPAttributesPlugin(BIND_URI, 'cn', '(objectClass=*)')
class TestLDAPAttributesPlugin(Base):
"""Tests for the L{LDAPAttributesPlugin} IMetadata plugin"""
def test_implements(self):
from who_ldap import LDAPAttributesPlugin
from zope.interface.verify import verifyClass
from repoze.who.interfaces import IMetadataProvider
verifyClass(IMetadataProvider, LDAPAttributesPlugin, tentative=True)
def test_add_metadata(self):
from who_ldap import LDAPAttributesPlugin
plugin = LDAPAttributesPlugin(BIND_URI)
environ = {}
identity = {'repoze.who.userid': fakeuser['dn']}
expected_identity = {
'repoze.who.userid': fakeuser['dn'],
'cn': [fakeuser['cn']],
'sn': [fakeuser['sn']],
'objectClass': ['inetOrgPerson'],
'userPassword': [fakeuser['hashedPassword']],
'uid': [fakeuser['uid']],
'telephoneNumber': [fakeuser['telephoneNumber']],
'mail': [fakeuser['mail']]
}
plugin.add_metadata(environ, identity)
self.assertDictEqual(identity, expected_identity)