Skip to content

Commit 2e2913b

Browse files
spaceonedroideck
authored andcommitted
feat(ldap.dn): add ldap.dn.normalize()
1 parent ef837a2 commit 2e2913b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Lib/ldap/dn.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,8 @@ def is_dn(s,flags=0):
122122
return False
123123
else:
124124
return True
125+
126+
127+
def normalize(s, flags=0):
128+
"""Returns a normalized distinguished name (DN)"""
129+
return dn2str(str2dn(s, flags), flags)

Tests/t_ldap_dn.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ def test_dn2str(self):
222222
], ldap.DN_FORMAT_DCE),
223223
'/dc=com/dc=example/ou=Testing/uid=test42,cn=test42'
224224
)
225-
226225
self.assertEqual(
227226
ldap.dn.dn2str([
228227
[('cn', 'äöüÄÖÜß', ldap.AVA_BINARY)],
@@ -446,6 +445,35 @@ def test_explode_rdn(self):
446445
['cn=äöüÄÖÜß']
447446
)
448447

448+
def test_normalize(self):
449+
"""
450+
test function normalize()
451+
"""
452+
self.assertEqual(
453+
ldap.dn.normalize('uid = test42 , ou = Testing , dc = example , dc = com', flags=ldap.DN_FORMAT_LDAPV3),
454+
'uid=test42,ou=Testing,dc=example,dc=com'
455+
)
456+
self.assertEqual(
457+
ldap.dn.normalize('cn=äöüÄÖÜß,dc=example,dc=com', flags=0),
458+
'cn=äöüÄÖÜß,dc=example,dc=com'
459+
)
460+
self.assertEqual(
461+
ldap.dn.normalize('cn=#C3A4C3B6C3BCC384C396C39CC39F,dc=example,dc=com', flags=0),
462+
'cn=äöüÄÖÜß,dc=example,dc=com'
463+
)
464+
self.assertEqual(
465+
ldap.dn.normalize('cn=#C3A4C3B6C3BCC384C396C39CC39F,dc=example,dc=com', flags=ldap.DN_FORMAT_LDAPV3),
466+
'cn=#C3A4C3B6C3BCC384C396C39CC39F,dc=example,dc=com'
467+
)
468+
self.assertEqual(
469+
ldap.dn.normalize('cn=äöüÄÖÜß,dc=example,dc=com', flags=ldap.DN_FORMAT_LDAPV3),
470+
r'cn=\C3\A4\C3\B6\C3\BC\C3\84\C3\96\C3\9C\C3\9F,dc=example,dc=com'
471+
)
472+
self.assertEqual(
473+
ldap.dn.normalize('/ dc = com / dc = example / ou = Testing / uid = test42 , cn = test42', flags=ldap.DN_FORMAT_DCE),
474+
'/dc=com/dc=example/ou=Testing/uid=test42,cn=test42'
475+
)
476+
449477

450478
if __name__ == '__main__':
451479
unittest.main()

0 commit comments

Comments
 (0)