<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[12307] twext/trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/12307">12307</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2014-01-11 11:55:47 -0800 (Sat, 11 Jan 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Start LDAP service tests</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunksetuppy">twext/trunk/setup.py</a></li>
<li><a href="#twexttrunktwextwholdap_servicepy">twext/trunk/twext/who/ldap/_service.py</a></li>
<li><a href="#twexttrunktwextwholdaptesttest_servicepy">twext/trunk/twext/who/ldap/test/test_service.py</a></li>
<li><a href="#twexttrunktwextwhotesttest_directorypy">twext/trunk/twext/who/test/test_directory.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="twexttrunksetuppy"></a>
<div class="modfile"><h4>Modified: twext/trunk/setup.py (12306 => 12307)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -118,9 +118,12 @@
</span><span class="cx">     &quot;twisted&gt;=13.2.0&quot;,
</span><span class="cx"> ]
</span><span class="cx"> 
</span><del>-# FIXME: It would be better to just get `setup.py develop` to fetch the
-# extras_requirements...
</del><span class="cx"> if os.environ.get(&quot;TWEXT_DEVELOP&quot;, &quot;false&quot;) == &quot;true&quot;:
</span><ins>+    # For testing
+    install_requirements.append(&quot;mockldap&gt;=0.1.4&quot;)
+
+    # FIXME: It would be better to figure out how to get `setup.py develop` to
+    # fetch the extras_requirements...
</ins><span class="cx">     install_requirements.append(&quot;python-ldap&gt;=2.4.13&quot;)
</span><span class="cx">     install_requirements.append(&quot;sqlparse==0.1.2&quot;)
</span><span class="cx"> 
</span></span></pre></div>
<a id="twexttrunktwextwholdap_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/_service.py (12306 => 12307)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -25,7 +25,8 @@
</span><span class="cx"> 
</span><span class="cx"> # from zope.interface import implementer
</span><span class="cx"> 
</span><del>-# from twisted.internet.defer import succeed, fail
</del><ins>+from twisted.internet.defer import inlineCallbacks, returnValue
+from twisted.internet.threads import deferToThread
</ins><span class="cx"> from twisted.cred.credentials import IUsernamePassword
</span><span class="cx"> # from twisted.web.guard import DigestCredentialFactory
</span><span class="cx"> 
</span><span class="lines">@@ -114,50 +115,50 @@
</span><span class="cx">         self,
</span><span class="cx">         url=&quot;ldap://localhost/&quot;,
</span><span class="cx">         credentials=None,
</span><ins>+        timeout=None,
</ins><span class="cx">         tlsCACertificateFile=None,
</span><span class="cx">         tlsCACertificateDirectory=None,
</span><span class="cx">         useTLS=False,
</span><ins>+        debug=False,
</ins><span class="cx">     ):
</span><del>-        self._url = url
</del><ins>+        self.url = url
</ins><span class="cx">         self.credentials = credentials
</span><ins>+        self._timeout = timeout
</ins><span class="cx">         self._tlsCACertificateFile = tlsCACertificateFile
</span><span class="cx">         self._tlsCACertificateDirectory = tlsCACertificateDirectory
</span><span class="cx">         self._useTLS = useTLS,
</span><span class="cx"> 
</span><ins>+        if debug:
+            self._debug = 255
+        else:
+            self._debug = None
</ins><span class="cx"> 
</span><ins>+
</ins><span class="cx">     @property
</span><span class="cx">     def realmName(self):
</span><span class="cx">         return u&quot;{self.url}&quot;.format(self=self)
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-    @property
-    def connection(self):
-        &quot;&quot;&quot;
-        Get the underlying LDAP connection.
-        &quot;&quot;&quot;
-        self._connect()
-        return self._connection
-
-
</del><ins>+    @inlineCallbacks
</ins><span class="cx">     def _connect(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Connect to the directory server.
</span><span class="cx"> 
</span><ins>+        @returns: A deferred connection object.
+        @rtype: deferred L{ldap.ldapobject.LDAPObject}
+
</ins><span class="cx">         @raises: L{LDAPConnectionError} if unable to connect.
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         if not hasattr(self, &quot;_connection&quot;):
</span><span class="cx">             self.log.info(&quot;Connecting to LDAP at {source.url}&quot;)
</span><del>-            connection = ldap.initialize(self._url)
</del><ins>+            connection = ldap.initialize(self.url)
+
</ins><span class="cx">             # FIXME: Use trace_file option to wire up debug logging when
</span><span class="cx">             # Twisted adopts the new logging stuff.
</span><span class="cx"> 
</span><del>-            def valueFor(constant):
-                if constant is None:
-                    return None
-                else:
-                    return constant.value
-
</del><span class="cx">             for option, value in (
</span><ins>+                (ldap.OPT_DEBUG_LEVEL, self._debug),
+                (ldap.OPT_TIMEOUT, self._timeout),
</ins><span class="cx">                 (ldap.OPT_X_TLS_CACERTFILE, self._tlsCACertificateFile),
</span><span class="cx">                 (ldap.OPT_X_TLS_CACERTDIR, self._tlsCACertificateDirectory),
</span><span class="cx">             ):
</span><span class="lines">@@ -165,12 +166,13 @@
</span><span class="cx">                     connection.set_option(option, value)
</span><span class="cx"> 
</span><span class="cx">             if self._useTLS:
</span><del>-                connection.start_tls_s()
</del><ins>+                yield deferToThread(connection.start_tls_s)
</ins><span class="cx"> 
</span><span class="cx">             if self.credentials is not None:
</span><span class="cx">                 if IUsernamePassword.providedBy(self.credentials):
</span><span class="cx">                     try:
</span><del>-                        self.ldap.simple_bind_s(
</del><ins>+                        yield deferToThread(
+                            connection.simple_bind_s,
</ins><span class="cx">                             self.credentials.username,
</span><span class="cx">                             self.credentials.password,
</span><span class="cx">                         )
</span><span class="lines">@@ -193,8 +195,10 @@
</span><span class="cx"> 
</span><span class="cx">             self._connection = connection
</span><span class="cx"> 
</span><ins>+        returnValue(self._connection)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx"> class DirectoryRecord(BaseDirectoryRecord):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     LDAP directory record.
</span></span></pre></div>
<a id="twexttrunktwextwholdaptesttest_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/test/test_service.py (12306 => 12307)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -18,16 +18,66 @@
</span><span class="cx"> LDAP directory service tests.
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="cx"> 
</span><ins>+from mockldap import MockLdap
+
</ins><span class="cx"> from twisted.trial import unittest
</span><span class="cx"> 
</span><span class="cx"> # from ...expression import (
</span><span class="cx"> #     CompoundExpression, Operand, MatchExpression, MatchType, MatchFlags
</span><span class="cx"> # )
</span><del>-# from ..ldap import DirectoryService
</del><ins>+from .._service import DirectoryService, DirectoryRecord
</ins><span class="cx"> 
</span><ins>+from ...test import test_directory
</ins><span class="cx"> 
</span><span class="cx"> 
</span><del>-class LDAPServiceTestCase(unittest.TestCase):
</del><ins>+
+class BaseTestCase(object):
</ins><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Tests for L{DirectoryService}.
</span><span class="cx">     &quot;&quot;&quot;
</span><ins>+
+    realmName = url = u&quot;ldap://localhost/&quot;
+
+    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 = &quot;unimplemented&quot;
+
+
+    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(
+)
</ins></span></pre></div>
<a id="twexttrunktwextwhotesttest_directorypy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/test/test_directory.py (12306 => 12307)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -142,7 +142,9 @@
</span><span class="cx">         service = self.service()
</span><span class="cx">         self.assertEquals(
</span><span class="cx">             repr(service),
</span><del>-            &quot;&lt;{0} u'xyzzy'&gt;&quot;.format(self.serviceClass.__name__)
</del><ins>+            &quot;&lt;{0} {1!r}&gt;&quot;.format(
+                self.serviceClass.__name__, self.realmName
+            )
</ins><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>