From 2a4815e1420c0a33a3230d2b1230572b424e07a2 Mon Sep 17 00:00:00 2001 From: kakwa Date: Sun, 26 Jul 2015 09:26:04 +0200 Subject: [PATCH] fix unit tests --- ldapcherry/backend/backendAD.py | 14 +++++------ tests/cfg/ldapcherry.ini | 8 ++++--- tests/test_LdapCherry.py | 41 +++++++++++++++++++++++++++++---- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/ldapcherry/backend/backendAD.py b/ldapcherry/backend/backendAD.py index d459d7f..4490113 100644 --- a/ldapcherry/backend/backendAD.py +++ b/ldapcherry/backend/backendAD.py @@ -29,6 +29,7 @@ class CaFileDontExist(Exception): self.cafile = cafile self.log = "CA file %(cafile)s don't exist" % {'cafile': cafile} + NO_ATTR = 0 DISPLAYED_ATTRS = 1 LISTED_ATTRS = 2 @@ -69,6 +70,7 @@ AD_BUILTIN_GROUPS = [ 'Users', ] + class Backend(ldapcherry.backend.backendLdap.Backend): def __init__(self, config, logger, name, attrslist, key): @@ -77,8 +79,8 @@ class Backend(ldapcherry.backend.backendLdap.Backend): self.backend_name = name self.domain = self.get_param('domain') self.login = self.get_param('login') - basedn = 'dc=' + re.sub(r'\.',',DC=', self.domain) - self.binddn = self.get_param('login') + '@' + self.domain + basedn = 'dc=' + re.sub(r'\.', ',DC=', self.domain) + self.binddn = self.get_param('login') + '@' + self.domain self.bindpassword = self.get_param('password') self.ca = self.get_param('ca', False) self.checkcert = self.get_param('checkcert', 'on') @@ -86,11 +88,12 @@ class Backend(ldapcherry.backend.backendLdap.Backend): self.uri = self.get_param('uri') self.timeout = self.get_param('timeout', 1) self.userdn = 'CN=Users,' + basedn - self.groupdn = self.userdn + self.groupdn = self.userdn self.builtin = 'CN=Builtin,' + basedn self.user_filter_tmpl = '(sAMAccountName=%(username)s)' self.group_filter_tmpl = '(uid=%(userdn)s)' - self.search_filter_tmpl = '(|(sAMAccountName=%(searchstring)s*)(sn=%(searchstring)s*)(cn=%(searchstring)s*))' + self.search_filter_tmpl = '(|(sAMAccountName=%(searchstring)s*)' \ + '(sn=%(searchstring)s*)(cn=%(searchstring)s*))' self.dn_user_attr = 'cn' self.key = 'sAMAccountName' self.objectlasses = ['top', 'person', 'organizationalPerson', 'user'] @@ -118,9 +121,7 @@ class Backend(ldapcherry.backend.backendLdap.Backend): ldap_client.unbind_s() return r - def get_groups(self, username): - username = ldap.filter.escape_filter_chars(username) userdn = self._get_user(username, NO_ATTR) @@ -140,4 +141,3 @@ class Backend(ldapcherry.backend.backendLdap.Backend): ret.append(self._uni(entry[0]['CN'])) return ret - diff --git a/tests/cfg/ldapcherry.ini b/tests/cfg/ldapcherry.ini index 0be7a75..3551119 100644 --- a/tests/cfg/ldapcherry.ini +++ b/tests/cfg/ldapcherry.ini @@ -82,9 +82,11 @@ ldap.objectclasses = 'top, person, organizationalPerson, user' ldap.dn_user_attr = 'uid' ldap.timeout = 1 -ad.module = 'ldapcherry.backend.backendSamba4' -ad.auth = 'Administrator' -ad.password = 'password' +ad.module = 'ldapcherry.backend.backendAD' +ad.domain = 'dc.ldapcherry.org' +ad.login = 'administrator' +ad.password = 'qwertyP455' +ad.uri = 'ldap://ldap.ldapcherry.org' # authentification parameters [auth] diff --git a/tests/test_LdapCherry.py b/tests/test_LdapCherry.py index 2ffe506..ff9fc36 100644 --- a/tests/test_LdapCherry.py +++ b/tests/test_LdapCherry.py @@ -144,7 +144,7 @@ class TestError(object): def testLogin(self): app = LdapCherry() - loadconf('./tests/cfg/ldapcherry.ini', app) + loadconf('./tests/cfg/ldapcherry_test.ini', app) try: app.login('jwatson', 'passwordwatson') except cherrypy.HTTPRedirect as e: @@ -155,15 +155,46 @@ class TestError(object): def testSearch(self): app = LdapCherry() - loadconf('./tests/cfg/ldapcherry.ini', app) - expected = {u'ssmith': {'password': u'passwordsmith', 'cn': u'Sheri Smith', 'name': u'smith', 'uid': u'ssmith'}, u'jsmith': {'password': u'passwordsmith', 'cn': u'John Smith', 'name': u'Smith', 'uid': u'jsmith'}} + loadconf('./tests/cfg/ldapcherry_test.ini', app) + expected = { + u'ssmith': { + 'password': u'passwordsmith', + 'cn': u'Sheri Smith', + 'name': u'smith', + 'uid': u'ssmith', + 'email': [u's.smith@example.com', + u'ssmith@example.com', + u'sheri.smith@example.com' + ], + }, + u'jsmith': { + 'password': u'passwordsmith', + 'cn': u'John Smith', + 'name': u'Smith', + 'uid': u'jsmith', + 'email': [ + 'j.smith@example.com', + 'jsmith@example.com', + 'jsmith.smith@example.com' + ], + } + } ret = app._search('smith') assert expected == ret def testGetUser(self): app = LdapCherry() - loadconf('./tests/cfg/ldapcherry.ini', app) - expected = {'password': u'passwordsmith', 'cn': u'Sheri Smith', 'uid': u'ssmith', 'name': u'smith'} + loadconf('./tests/cfg/ldapcherry_test.ini', app) + expected = { + 'password': u'passwordsmith', + 'cn': u'Sheri Smith', + 'uid': u'ssmith', + 'name': u'smith', + 'email': [u's.smith@example.com', + u'ssmith@example.com', + u'sheri.smith@example.com' + ], + } ret = app._get_user('ssmith') assert expected == ret