[Zope-CVS] SVN: ldapadapter/trunk/tests/fakeldap.py Improved
fakeldap to return better errors.
Florent Guillaume
fg at nuxeo.com
Wed Oct 13 08:46:54 EDT 2004
Log message for revision 28070:
Improved fakeldap to return better errors.
Changed:
U ldapadapter/trunk/tests/fakeldap.py
-=-
Modified: ldapadapter/trunk/tests/fakeldap.py
===================================================================
--- ldapadapter/trunk/tests/fakeldap.py 2004-10-13 12:27:45 UTC (rev 28069)
+++ ldapadapter/trunk/tests/fakeldap.py 2004-10-13 12:46:53 UTC (rev 28070)
@@ -30,13 +30,11 @@
from ldap import MOD_DELETE
from ldap import MOD_REPLACE
-class INVALID_CREDENTIALS(Exception): pass
-class ALREADY_EXISTS(Exception): pass
-class NO_SUCH_OBJECT(Exception): pass
-#class SERVER_DOWN(Exception): pass
-#class SIZELIMIT_EXCEEDED(Exception): pass
+from ldap import SERVER_DOWN
+from ldap import INVALID_CREDENTIALS
+from ldap import ALREADY_EXISTS
+from ldap import NO_SUCH_OBJECT
-
the_data = {}
# Data is a mapping of {dnl -> entry}, dnl is a tuple of rdns
@@ -45,43 +43,30 @@
class FakeLDAPObject(object):
- def __init__(self):
- pass
+ def __init__(self, conn_str):
+ self.conn_str = conn_str
def set_option(self, option, value):
pass
def simple_bind_s(self, dn, password):
- if dn.find('Manager') >= 0:
- # Fake authentified connection.
- return 1
+ if self.conn_str.startswith('ldap://down'):
+ raise SERVER_DOWN
if dn == '' and password == '':
# Fake anonymous connection.
return 1
- results = self.search_s(dn)
- pwd = None
- for key, values in results:
- if key == 'userPassword':
- pwd = values[0]
- break
+ if dn.find('Manager') >= 0:
+ # Fake authentified connection.
+ return 1
- if pwd is None:
- raise INVALID_CREDENTIALS
+ if password == 'pwd_for_'+dn:
+ # Fake allowed connection.
+ return 1
- # Check password encoding
- if pwd.startswith('{SHA}'):
- s = sha.new(password)
- enc = '{SHA}' + base64.encodestring(s.digest())
- else:
- enc = password
+ raise INVALID_CREDENTIALS
- if pwd != enc:
- raise INVALID_CREDENTIALS
-
- return 1
-
def add_s(self, dn, attr_list):
dnl = tuple(dn.split(','))
if dnl in the_data:
@@ -118,10 +103,12 @@
# Iterate on all entries and do the search
res = []
- #import pdb; pdb.set_trace()
+ has_base = False
for dnl, entry in the_data.iteritems():
if dnl[-basellen:] != basel:
continue
+ if dnl == basel:
+ has_base = True
dnllen = len(dnl)
if not ((scope == SCOPE_SUBTREE) or
(scope == SCOPE_ONELEVEL and dnllen == basellen+1) or
@@ -168,6 +155,8 @@
else:
res_entry = deepcopy(entry)
res.append((dn, res_entry))
+ if not has_base:
+ raise NO_SUCH_OBJECT
return res
def modify_s(self, dn, mod_list):
@@ -207,5 +196,5 @@
def initialize(conn_str):
"""Initialize."""
- return FakeLDAPObject()
+ return FakeLDAPObject(conn_str)
More information about the Zope-CVS
mailing list