[CalendarServer-changes] [12307] twext/trunk

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:19:13 PDT 2014


Revision: 12307
          http://trac.calendarserver.org//changeset/12307
Author:   wsanchez at apple.com
Date:     2014-01-11 11:55:47 -0800 (Sat, 11 Jan 2014)
Log Message:
-----------
Start LDAP service tests

Modified Paths:
--------------
    twext/trunk/setup.py
    twext/trunk/twext/who/ldap/_service.py
    twext/trunk/twext/who/ldap/test/test_service.py
    twext/trunk/twext/who/test/test_directory.py

Modified: twext/trunk/setup.py
===================================================================
--- twext/trunk/setup.py	2014-01-11 19:28:24 UTC (rev 12306)
+++ twext/trunk/setup.py	2014-01-11 19:55:47 UTC (rev 12307)
@@ -118,9 +118,12 @@
     "twisted>=13.2.0",
 ]
 
-# FIXME: It would be better to just get `setup.py develop` to fetch the
-# extras_requirements...
 if os.environ.get("TWEXT_DEVELOP", "false") == "true":
+    # For testing
+    install_requirements.append("mockldap>=0.1.4")
+
+    # FIXME: It would be better to figure out how to get `setup.py develop` to
+    # fetch the extras_requirements...
     install_requirements.append("python-ldap>=2.4.13")
     install_requirements.append("sqlparse==0.1.2")
 

Modified: twext/trunk/twext/who/ldap/_service.py
===================================================================
--- twext/trunk/twext/who/ldap/_service.py	2014-01-11 19:28:24 UTC (rev 12306)
+++ twext/trunk/twext/who/ldap/_service.py	2014-01-11 19:55:47 UTC (rev 12307)
@@ -25,7 +25,8 @@
 
 # from zope.interface import implementer
 
-# from twisted.internet.defer import succeed, fail
+from twisted.internet.defer import inlineCallbacks, returnValue
+from twisted.internet.threads import deferToThread
 from twisted.cred.credentials import IUsernamePassword
 # from twisted.web.guard import DigestCredentialFactory
 
@@ -114,50 +115,50 @@
         self,
         url="ldap://localhost/",
         credentials=None,
+        timeout=None,
         tlsCACertificateFile=None,
         tlsCACertificateDirectory=None,
         useTLS=False,
+        debug=False,
     ):
-        self._url = url
+        self.url = url
         self.credentials = credentials
+        self._timeout = timeout
         self._tlsCACertificateFile = tlsCACertificateFile
         self._tlsCACertificateDirectory = tlsCACertificateDirectory
         self._useTLS = useTLS,
 
+        if debug:
+            self._debug = 255
+        else:
+            self._debug = None
 
+
     @property
     def realmName(self):
         return u"{self.url}".format(self=self)
 
 
-    @property
-    def connection(self):
-        """
-        Get the underlying LDAP connection.
-        """
-        self._connect()
-        return self._connection
-
-
+    @inlineCallbacks
     def _connect(self):
         """
         Connect to the directory server.
 
+        @returns: A deferred connection object.
+        @rtype: deferred L{ldap.ldapobject.LDAPObject}
+
         @raises: L{LDAPConnectionError} if unable to connect.
         """
         if not hasattr(self, "_connection"):
             self.log.info("Connecting to LDAP at {source.url}")
-            connection = ldap.initialize(self._url)
+            connection = ldap.initialize(self.url)
+
             # FIXME: Use trace_file option to wire up debug logging when
             # Twisted adopts the new logging stuff.
 
-            def valueFor(constant):
-                if constant is None:
-                    return None
-                else:
-                    return constant.value
-
             for option, value in (
+                (ldap.OPT_DEBUG_LEVEL, self._debug),
+                (ldap.OPT_TIMEOUT, self._timeout),
                 (ldap.OPT_X_TLS_CACERTFILE, self._tlsCACertificateFile),
                 (ldap.OPT_X_TLS_CACERTDIR, self._tlsCACertificateDirectory),
             ):
@@ -165,12 +166,13 @@
                     connection.set_option(option, value)
 
             if self._useTLS:
-                connection.start_tls_s()
+                yield deferToThread(connection.start_tls_s)
 
             if self.credentials is not None:
                 if IUsernamePassword.providedBy(self.credentials):
                     try:
-                        self.ldap.simple_bind_s(
+                        yield deferToThread(
+                            connection.simple_bind_s,
                             self.credentials.username,
                             self.credentials.password,
                         )
@@ -193,8 +195,10 @@
 
             self._connection = connection
 
+        returnValue(self._connection)
 
 
+
 class DirectoryRecord(BaseDirectoryRecord):
     """
     LDAP directory record.

Modified: twext/trunk/twext/who/ldap/test/test_service.py
===================================================================
--- twext/trunk/twext/who/ldap/test/test_service.py	2014-01-11 19:28:24 UTC (rev 12306)
+++ twext/trunk/twext/who/ldap/test/test_service.py	2014-01-11 19:55:47 UTC (rev 12307)
@@ -18,16 +18,66 @@
 LDAP directory service tests.
 """
 
+from mockldap import MockLdap
+
 from twisted.trial import unittest
 
 # from ...expression import (
 #     CompoundExpression, Operand, MatchExpression, MatchType, MatchFlags
 # )
-# from ..ldap import DirectoryService
+from .._service import DirectoryService, DirectoryRecord
 
+from ...test import test_directory
 
 
-class LDAPServiceTestCase(unittest.TestCase):
+
+class BaseTestCase(object):
     """
     Tests for L{DirectoryService}.
     """
+
+    realmName = url = u"ldap://localhost/"
+
+    def setUp(self):
+        super(BaseTestCase, self).setup()
+        self.mockLDAP = MockLdap(mockDirectoryData)
+        self.mockLDAP.start()
+
+
+    def tearDown(self):
+        self.mockLDAP.stop()
+        super(BaseTestCase, self).tearDown()
+
+
+    def service(self, subClass=None, xmlData=None):
+        return DirectoryService()
+
+
+
+class DirectoryServiceConvenienceTestMixIn(BaseTestCase):
+    def _unimplemented(self):
+        raise NotImplementedError()
+
+    _unimplemented.todo = "unimplemented"
+
+
+    test_recordWithUID = _unimplemented
+    test_recordWithGUID = _unimplemented
+    test_recordsWithRecordType = _unimplemented
+    test_recordWithShortName = _unimplemented
+    test_recordsWithEmailAddress = _unimplemented
+
+
+
+class DirectoryServiceTest(
+    unittest.TestCase,
+    DirectoryServiceConvenienceTestMixIn,
+    test_directory.BaseDirectoryServiceTest,
+):
+    serviceClass = DirectoryService
+    directoryRecordClass = DirectoryRecord
+
+
+
+mockDirectoryData = dict(
+)

Modified: twext/trunk/twext/who/test/test_directory.py
===================================================================
--- twext/trunk/twext/who/test/test_directory.py	2014-01-11 19:28:24 UTC (rev 12306)
+++ twext/trunk/twext/who/test/test_directory.py	2014-01-11 19:55:47 UTC (rev 12307)
@@ -142,7 +142,9 @@
         service = self.service()
         self.assertEquals(
             repr(service),
-            "<{0} u'xyzzy'>".format(self.serviceClass.__name__)
+            "<{0} {1!r}>".format(
+                self.serviceClass.__name__, self.realmName
+            )
         )
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/b312f7d9/attachment.html>


More information about the calendarserver-changes mailing list