77#
88
99import logging
10- from unittest import mock
1110
1211from django .apps import apps
1312from django .conf import settings
2322
2423import ldap
2524import slapdtest
26- from django_auth_ldap .backend import _LDAPUserGroups
2725from django_auth_ldap .config import GroupOfNamesType
2826from django_auth_ldap .config import LDAPSearch
2927from guardian .shortcuts import assign_perm
3937Component = apps .get_model ("component_catalog" , "Component" )
4038Product = apps .get_model ("product_portfolio" , "Product" )
4139
42-
4340LDIF = """
4441dn: o=test
4542objectClass: organization
7269cn: active
7370objectClass: groupOfNames
7471member: uid=bob,ou=people,o=test
72+
73+ dn: cn=not_in_database,ou=groups,o=test
74+ cn: not_in_database
75+ objectClass: groupOfNames
76+ member: uid=bob,ou=people,o=test
77+
78+ dn: cn=superuser,ou=groups,o=test
79+ cn: superuser
80+ objectClass: groupOfNames
81+ member: uid=bob,ou=people,o=test
7582"""
7683
7784
93100 AUTH_LDAP_GROUP_TYPE = GroupOfNamesType (),
94101)
95102class DejaCodeLDAPBackendTestCase (TestCase ):
103+ # https://www.python-ldap.org/en/latest/reference/slapdtest.html
96104 server_class = slapdtest .SlapdObject
97105 ldap_object_class = SimpleLDAPObject
98106
@@ -147,17 +155,6 @@ def test_ldap_authentication_populate_user(self):
147155 self .assertEqual (user .last_name , "Smith" )
148156 self .
assertEqual (
user .
email ,
"[email protected] " )
149157
150- def test_bind_and_search (self ):
151- # Connect to the temporary slapd server
152- conn = self .ldap_object_class (self .server .ldap_uri )
153- conn .simple_bind_s (self .server .root_dn , self .server .root_pw )
154-
155- # Search for the top entry
156- result = conn .search_s (self .server .suffix , ldap .SCOPE_BASE )
157- self .assertEqual (len (result ), 1 )
158- dn , entry = result [0 ]
159- self .assertEqual (dn , self .server .suffix )
160-
161158 def test_ldap_group_active_properly_setup_and_searchable (self ):
162159 conn = self .ldap_object_class (self .server .ldap_uri )
163160 results = conn .search_s ("ou=groups,o=test" , ldap .SCOPE_ONELEVEL , "(cn=active)" )
@@ -201,9 +198,9 @@ def test_ldap_authentication_autocreate_user_proper_dataspace(self):
201198
202199 # User was created on first login
203200 created_user = DejacodeUser .objects .get (username = "bob" )
204- self .assertEqual ("" , created_user .first_name )
205- self .assertEqual ("" , created_user .last_name )
206- self .assertEqual ("" , created_user .email )
201+ self .assertEqual ("Robert " , created_user .first_name )
202+ self .assertEqual ("Smith " , created_user .last_name )
203+ self .
assertEqual (
"[email protected] " ,
created_user .
email )
207204 self .assertEqual (self .nexb_dataspace , created_user .dataspace )
208205
209206 self .assertTrue (created_user .is_active )
@@ -217,7 +214,6 @@ def test_ldap_authentication_autocreate_user_proper_dataspace(self):
217214 # Next login, the DB user is re-used
218215 self .assertTrue (self .client .login (username = "bob" , password = "secret" ))
219216
220- # @override_settings(AUTH_LDAP_USER_ATTR_MAP=AUTH_LDAP_USER_ATTR_MAP)
221217 def test_ldap_authentication_autocreate_user_with_attr_map (self ):
222218 self .assertFalse (DejacodeUser .objects .filter (username = "bob" ).exists ())
223219
@@ -229,10 +225,7 @@ def test_ldap_authentication_autocreate_user_with_attr_map(self):
229225 self .
assertEqual (
"[email protected] " ,
created_user .
email )
230226 self .assertEqual (self .nexb_dataspace , created_user .dataspace )
231227
232- @override_settings (
233- # AUTH_LDAP_USER_ATTR_MAP=AUTH_LDAP_USER_ATTR_MAP,
234- AUTH_LDAP_ALWAYS_UPDATE_USER = True ,
235- )
228+ @override_settings (AUTH_LDAP_ALWAYS_UPDATE_USER = True )
236229 def test_ldap_authentication_update_user_with_attr_map (self ):
237230 # Manually create the user first, then see if the values are updated
238231 create_user (
"bob" ,
self .
nexb_dataspace ,
email = "[email protected] " )
@@ -268,9 +261,9 @@ def test_ldap_authentication_group_permissions(self):
268261 self .assertFalse (Group .objects .filter (name = "not_in_database" ).exists ())
269262 self .assertEqual ({"active" , "not_in_database" , "superuser" }, bob .ldap_user .group_names )
270263 expected_group_dns = {
271- "cn=active,ou=groups,dc=nexb,dc=com " ,
272- "cn=not_in_database,ou=groups,dc=nexb,dc=com " ,
273- "cn=superuser,ou=groups,dc=nexb,dc=com " ,
264+ "cn=active,ou=groups,o=test " ,
265+ "cn=not_in_database,ou=groups,o=test " ,
266+ "cn=superuser,ou=groups,o=test " ,
274267 }
275268 self .assertEqual (expected_group_dns , bob .ldap_user .group_dns )
276269
@@ -307,25 +300,14 @@ def test_ldap_user_flags_assigned_through_groups(self):
307300 self .assertEqual ({"active" , "not_in_database" , "superuser" }, bob .ldap_user .group_names )
308301
309302 user_flags_by_group = {
310- "is_superuser" : "cn=superuser,ou=groups,dc=nexb,dc=com " ,
303+ "is_superuser" : "cn=superuser,ou=groups,o=test " ,
311304 }
312305
313- # WARNING: This is a workaround for a bug in mockldap.
314- # There's a comparison issue in `mockldap.ldapobject.LDAPObject._compare_s`
315- # where the `value` is bytes b'' and `values` is a list of strings.
316- # For example:
317- # value = b'cn=bob,ou=people,dc=nexb,dc=com'
318- # values = ['cn=bob,ou=people,dc=nexb,dc=com']
319- # Note that mockldap has been replaced by `slapdtest` in recent `python-ldap` versions.
320- # The migration to `slapdtest` requires a slaptd daemon runnning plus the rewrite of this
321- # whole TestCase.
322- # https://www.python-ldap.org/en/latest/reference/slapdtest.html
323- with mock .patch .object (_LDAPUserGroups , "is_member_of" , return_value = True ):
324- with override_settings (AUTH_LDAP_USER_FLAGS_BY_GROUP = user_flags_by_group ):
325- bob = DejaCodeLDAPBackend ().authenticate (
326- request = None , username = "bob" , password = "secret"
327- )
328- self .assertTrue (bob .is_superuser )
306+ with override_settings (AUTH_LDAP_USER_FLAGS_BY_GROUP = user_flags_by_group ):
307+ bob = DejaCodeLDAPBackend ().authenticate (
308+ request = None , username = "bob" , password = "secret"
309+ )
310+ self .assertTrue (bob .is_superuser )
329311
330312 def test_ldap_tab_set_mixin_get_tabsets (self ):
331313 from component_catalog .views import ComponentDetailsView
@@ -383,65 +365,3 @@ def test_ldap_object_secured_access(self):
383365 # The `ObjectPermissionBackend` is not needed since `ProductSecuredManager.get_queryset()`
384366 # calls directly `guardian.shortcuts.get_objects_for_user`
385367 self .assertEqual (200 , self .client .get (url ).status_code )
386-
387-
388- # class DejaCodeLDAPBackendTestCase(TestCase):
389- # top = ("dc=com", {"dc": "com"})
390- # nexb = ("dc=nexb,dc=com", {"dc": "nexb"})
391- # people = ("ou=people,dc=nexb,dc=com", {"ou": "people"})
392- # groups = ("ou=groups,dc=nexb,dc=com", {"ou": "groups"})
393- #
394- # bob = (
395- # "cn=bob,ou=people,dc=nexb,dc=com",
396- # {
397- # "cn": "bob",
398- # "samaccountname": "bob",
399- # "uid": ["bob"],
400- # "userPassword": ["secret"],
401- 402- # "givenName": ["Robert"],
403- # "sn": ["Smith"],
404- # },
405- # )
406- #
407- # group_active = (
408- # "cn=active,ou=groups,dc=nexb,dc=com",
409- # {
410- # "cn": ["active"],
411- # "objectClass": ["groupOfNames"],
412- # "member": ["cn=bob,ou=people,dc=nexb,dc=com"],
413- # },
414- # )
415- #
416- # group_not_in_database = (
417- # "cn=not_in_database,ou=groups,dc=nexb,dc=com",
418- # {
419- # "cn": ["not_in_database"],
420- # "objectClass": ["groupOfNames"],
421- # "member": ["cn=bob,ou=people,dc=nexb,dc=com"],
422- # },
423- # )
424- #
425- # group_superuser = (
426- # "cn=superuser,ou=groups,dc=nexb,dc=com",
427- # {
428- # "cn": ["superuser"],
429- # "objectClass": ["groupOfNames"],
430- # "member": ["cn=bob,ou=people,dc=nexb,dc=com"],
431- # },
432- # )
433- #
434- # # This is the content of our mock LDAP directory. It takes the form
435- # # {dn: {attr: [value, ...], ...}, ...}.
436- # directory = dict(
437- # [
438- # top,
439- # nexb,
440- # people,
441- # groups,
442- # bob,
443- # group_active,
444- # group_not_in_database,
445- # group_superuser,
446- # ]
447- # )
0 commit comments