<!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>[12331] twext/trunk/twext/who</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/12331">12331</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2014-01-13 16:38:59 -0800 (Mon, 13 Jan 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Start work on basic queries.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunktwextwhoidirectorypy">twext/trunk/twext/who/idirectory.py</a></li>
<li><a href="#twexttrunktwextwholdap_constantspy">twext/trunk/twext/who/ldap/_constants.py</a></li>
<li><a href="#twexttrunktwextwholdap_servicepy">twext/trunk/twext/who/ldap/_service.py</a></li>
<li><a href="#twexttrunktwextwholdap_utilpy">twext/trunk/twext/who/ldap/_util.py</a></li>
<li><a href="#twexttrunktwextwholdaptesttest_servicepy">twext/trunk/twext/who/ldap/test/test_service.py</a></li>
<li><a href="#twexttrunktwextwholdaptesttest_utilpy">twext/trunk/twext/who/ldap/test/test_util.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="twexttrunktwextwhoidirectorypy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/idirectory.py (12330 => 12331)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/idirectory.py        2014-01-14 00:20:20 UTC (rev 12330)
+++ twext/trunk/twext/who/idirectory.py        2014-01-14 00:38:59 UTC (rev 12331)
</span><span class="lines">@@ -220,7 +220,7 @@
</span><span class="cx">     @staticmethod
</span><span class="cx">     def valueType(name):
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        Check for the expected type of values for a field.
</del><ins>+        Look up the expected type for values of a field.
</ins><span class="cx"> 
</span><span class="cx">         @param name: The name of the field.
</span><span class="cx">         @type name: L{NamedConstant}
</span></span></pre></div>
<a id="twexttrunktwextwholdap_constantspy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/_constants.py (12330 => 12331)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/_constants.py        2014-01-14 00:20:20 UTC (rev 12330)
+++ twext/trunk/twext/who/ldap/_constants.py        2014-01-14 00:38:59 UTC (rev 12331)
</span><span class="lines">@@ -22,6 +22,9 @@
</span><span class="cx">     Names, NamedConstant, Values, ValueConstant
</span><span class="cx"> )
</span><span class="cx"> 
</span><ins>+from ..idirectory import (
+    FieldName as BaseFieldName, RecordType as BaseRecordType
+)
</ins><span class="cx"> from ..expression import MatchType
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -153,7 +156,7 @@
</span><span class="cx">     teletexTerminalIdentifier = ValueConstant(u&quot;teletexTerminalIdentifier&quot;)
</span><span class="cx">     telexNumber = ValueConstant(u&quot;telexNumber&quot;)
</span><span class="cx">     title = ValueConstant(u&quot;title&quot;)
</span><del>-    userid = ValueConstant(u&quot;uid&quot;)
</del><ins>+    userID = ValueConstant(u&quot;uid&quot;)
</ins><span class="cx">     uniqueMember = ValueConstant(u&quot;uniqueMember&quot;)
</span><span class="cx">     userPassword = ValueConstant(u&quot;userPassword&quot;)
</span><span class="cx">     x121Address = ValueConstant(u&quot;x121Address&quot;)
</span><span class="lines">@@ -190,3 +193,23 @@
</span><span class="cx"> # http://tools.ietf.org/html/rfc2307
</span><span class="cx"> # http://tools.ietf.org/html/rfc2798
</span><span class="cx"> # http://tools.ietf.org/html/rfc2739 calendar
</span><ins>+
+
+
+# Maps field name -&gt; LDAP attribute name
+DEFAULT_FIELDNAME_MAP = {
+    BaseFieldName.uid: u&quot;uid&quot;,  # FIXME
+    BaseFieldName.guid: u&quot;entryUUID&quot;,  # FIXME
+    BaseFieldName.recordType: u&quot;objectClass&quot;,
+    BaseFieldName.shortNames: RFC4519Attribute.userID.value,
+    BaseFieldName.fullNames: RFC4519Attribute.commonName.value,
+    BaseFieldName.emailAddresses: u&quot;mail&quot;,  # FIXME
+    BaseFieldName.password: u&quot;userPassword&quot;,  # FIXME
+}
+
+
+# Maps record type -&gt; LDAP organizational unit name
+DEFAULT_RECORDTYPE_MAP = {
+    BaseRecordType.user: u&quot;People&quot;,  # FIXME
+    BaseRecordType.group: u&quot;Group&quot;,  # FIXME
+}
</ins></span></pre></div>
<a id="twexttrunktwextwholdap_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/_service.py (12330 => 12331)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/_service.py        2014-01-14 00:20:20 UTC (rev 12330)
+++ twext/trunk/twext/who/ldap/_service.py        2014-01-14 00:38:59 UTC (rev 12331)
</span><span class="lines">@@ -34,7 +34,8 @@
</span><span class="cx"> 
</span><span class="cx"> from ..idirectory import (
</span><span class="cx">     DirectoryServiceError, DirectoryAvailabilityError,
</span><del>-    # InvalidDirectoryRecordError, QueryNotSupportedError,
</del><ins>+    # InvalidDirectoryRecordError,
+    # QueryNotSupportedError,
</ins><span class="cx">     # FieldName as BaseFieldName,
</span><span class="cx">     RecordType as BaseRecordType,
</span><span class="cx">     # IPlaintextPasswordVerifier, IHTTPDigestVerifier,
</span><span class="lines">@@ -43,17 +44,21 @@
</span><span class="cx">     DirectoryService as BaseDirectoryService,
</span><span class="cx">     DirectoryRecord as BaseDirectoryRecord,
</span><span class="cx"> )
</span><del>-# from ..expression import (
-#     CompoundExpression, Operand,
-#     MatchExpression, MatchFlags,
-# )
</del><ins>+from ..expression import (
+    # CompoundExpression, Operand,
+    MatchExpression,  # MatchFlags,
+)
</ins><span class="cx"> from ..util import (
</span><span class="cx">     # iterFlags,
</span><span class="cx">     ConstantsContainer,
</span><span class="cx"> )
</span><del>-# from ._constants import LDAP_QUOTING_TABLE
</del><ins>+from ._constants import DEFAULT_FIELDNAME_MAP, DEFAULT_RECORDTYPE_MAP
+from ._util import (
+    ldapQueryStringFromMatchExpression,
+    ldapQueryStringFromCompoundExpression,
+    # ldapQueryStringFromExpression,
+)
</ins><span class="cx"> 
</span><del>-DEFAULT_URL = &quot;ldap://localhost/&quot;
</del><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> #
</span><span class="lines">@@ -121,16 +126,20 @@
</span><span class="cx"> 
</span><span class="cx">     def __init__(
</span><span class="cx">         self,
</span><del>-        url=DEFAULT_URL,
</del><ins>+        url,
+        baseDN,
</ins><span class="cx">         credentials=None,
</span><span class="cx">         timeout=None,
</span><span class="cx">         tlsCACertificateFile=None,
</span><span class="cx">         tlsCACertificateDirectory=None,
</span><span class="cx">         useTLS=False,
</span><span class="cx">         debug=False,
</span><ins>+        fieldNameMap=DEFAULT_FIELDNAME_MAP,
+        recordTypeMap=DEFAULT_RECORDTYPE_MAP,
</ins><span class="cx">     ):
</span><span class="cx">         self.url = url
</span><del>-        self.credentials = credentials
</del><ins>+        self._baseDN = baseDN
+        self._credentials = credentials
</ins><span class="cx">         self._timeout = timeout
</span><span class="cx"> 
</span><span class="cx">         if tlsCACertificateFile is None:
</span><span class="lines">@@ -150,7 +159,10 @@
</span><span class="cx">         else:
</span><span class="cx">             self._debug = None
</span><span class="cx"> 
</span><ins>+        self._fieldNameMap = fieldNameMap
+        self._recordTypeMap = recordTypeMap
</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="lines">@@ -186,33 +198,33 @@
</span><span class="cx">                 self.log.info(&quot;Starting TLS for {source.url}&quot;)
</span><span class="cx">                 yield deferToThread(connection.start_tls_s)
</span><span class="cx"> 
</span><del>-            if self.credentials is not None:
-                if IUsernamePassword.providedBy(self.credentials):
</del><ins>+            if self._credentials is not None:
+                if IUsernamePassword.providedBy(self._credentials):
</ins><span class="cx">                     try:
</span><span class="cx">                         yield deferToThread(
</span><span class="cx">                             connection.simple_bind_s,
</span><del>-                            self.credentials.username,
-                            self.credentials.password,
</del><ins>+                            self._credentials.username,
+                            self._credentials.password,
</ins><span class="cx">                         )
</span><span class="cx">                         self.log.info(
</span><span class="cx">                             &quot;Bound to LDAP as {credentials.username}&quot;,
</span><del>-                            credentials=self.credentials
</del><ins>+                            credentials=self._credentials
</ins><span class="cx">                         )
</span><span class="cx">                     except (
</span><span class="cx">                         ldap.INVALID_CREDENTIALS, ldap.INVALID_DN_SYNTAX
</span><span class="cx">                     ) as e:
</span><span class="cx">                         self.log.error(
</span><span class="cx">                             &quot;Unable to bind to LDAP as {credentials.username}&quot;,
</span><del>-                            credentials=self.credentials
</del><ins>+                            credentials=self._credentials
</ins><span class="cx">                         )
</span><span class="cx">                         raise LDAPBindAuthError(
</span><del>-                            self.credentials.username, e
</del><ins>+                            self._credentials.username, e
</ins><span class="cx">                         )
</span><span class="cx"> 
</span><span class="cx">                 else:
</span><span class="cx">                     raise LDAPConnectionError(
</span><span class="cx">                         &quot;Unknown credentials type: {0}&quot;
</span><del>-                        .format(self.credentials)
</del><ins>+                        .format(self._credentials)
</ins><span class="cx">                     )
</span><span class="cx"> 
</span><span class="cx">             self._connection = connection
</span><span class="lines">@@ -220,7 +232,51 @@
</span><span class="cx">         returnValue(self._connection)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @inlineCallbacks
+    def _recordsFromQueryString(self, queryString):
+        connection = yield self._connect()
</ins><span class="cx"> 
</span><ins>+        # print(&quot;+&quot; * 80)
+        # print(&quot;Query:&quot;, repr(queryString))
+
+        reply = connection.search_s(
+            self._baseDN, ldap.SCOPE_SUBTREE, queryString  # attrs
+        )
+
+        # print(&quot;Reply:&quot;, repr(reply))
+        # print(&quot;+&quot; * 80)
+
+        records = []
+
+        for recordData in reply:
+            raise NotImplementedError(reply)
+
+        returnValue(records)
+
+
+    def recordsFromNonCompoundExpression(self, expression, records=None):
+        if isinstance(expression, MatchExpression):
+            queryString = ldapQueryStringFromMatchExpression(
+                expression, self._fieldNameMap, self._recordTypeMap
+            )
+            return self._recordsFromQueryString(queryString)
+
+        return BaseDirectoryService.recordsFromNonCompoundExpression(
+            self, expression, records=records
+        )
+
+
+    def recordsFromCompoundExpression(self, expression, records=None):
+        if not expression.expressions:
+            return ()
+
+        queryString = ldapQueryStringFromCompoundExpression(
+            expression, self._fieldNameMap, self._recordTypeMap
+        )
+        return self._recordsFromQueryString(queryString)
+
+
+
</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="twexttrunktwextwholdap_utilpy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/_util.py (12330 => 12331)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/_util.py        2014-01-14 00:20:20 UTC (rev 12330)
+++ twext/trunk/twext/who/ldap/_util.py        2014-01-14 00:38:59 UTC (rev 12331)
</span><span class="lines">@@ -15,7 +15,7 @@
</span><span class="cx"> # limitations under the License.
</span><span class="cx"> ##
</span><span class="cx"> 
</span><del>-from ..idirectory import QueryNotSupportedError
</del><ins>+from ..idirectory import QueryNotSupportedError, FieldName
</ins><span class="cx"> from ..expression import (
</span><span class="cx">     CompoundExpression, Operand,
</span><span class="cx">     MatchExpression, MatchFlags,
</span><span class="lines">@@ -25,23 +25,29 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-def ldapQueryStringFromMatchExpression(expression, attrMap):
</del><ins>+def ldapQueryStringFromMatchExpression(
+    expression, fieldNameMap, recordTypeMap
+):
</ins><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Generates an LDAP query string from a match expression.
</span><span class="cx"> 
</span><span class="cx">     @param expression: A match expression.
</span><span class="cx">     @type expression: L{MatchExpression}
</span><span class="cx"> 
</span><del>-    @param attrMap: A mapping from L{FieldName}s to native LDAP attribute
</del><ins>+    @param fieldNameMap: A mapping from L{FieldName}s to native LDAP attribute
</ins><span class="cx">         names.
</span><del>-    @type attrMap: L{dict}
</del><ins>+    @type fieldNameMap: L{dict}
</ins><span class="cx"> 
</span><ins>+    @param recordTypeMap: A mapping from L{RecordType}s to native LDAP object
+        class names.
+    @type recordTypeMap: L{dict}
+
</ins><span class="cx">     @return: An LDAP query string.
</span><span class="cx">     @rtype: C{unicode}
</span><span class="cx"> 
</span><span class="cx">     @raises QueryNotSupportedError: If the expression's match type is unknown,
</span><span class="cx">         or if the expresion references an unknown field name (meaning a field
</span><del>-        name not in C{attrMap}).
</del><ins>+        name not in C{fieldNameMap}).
</ins><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     matchType = LDAPMatchType.fromMatchType(expression.matchType)
</span><span class="cx">     if matchType is None:
</span><span class="lines">@@ -63,14 +69,24 @@
</span><span class="cx">     # if MatchFlags.caseInsensitive not in flags:
</span><span class="cx">     #     raise NotImplementedError(&quot;Need to handle case sensitive&quot;)
</span><span class="cx"> 
</span><ins>+    fieldName = expression.fieldName
</ins><span class="cx">     try:
</span><del>-        attribute = attrMap[expression.fieldName]
</del><ins>+        attribute = fieldNameMap[fieldName]
</ins><span class="cx">     except KeyError:
</span><span class="cx">         raise QueryNotSupportedError(
</span><del>-            &quot;Unknown field name: {0}&quot;.format(expression.fieldName)
</del><ins>+            &quot;Unmapped field name: {0}&quot;.format(expression.fieldName)
</ins><span class="cx">         )
</span><span class="cx"> 
</span><del>-    value = unicode(expression.fieldValue)       # We want unicode
</del><ins>+    if fieldName is FieldName.recordType:
+        try:
+            value = recordTypeMap[expression.fieldValue]
+        except KeyError:
+            raise QueryNotSupportedError(
+                &quot;Unmapped record type: {0}&quot;.format(expression.fieldValue)
+            )
+    else:
+        value = unicode(expression.fieldValue)
+
</ins><span class="cx">     value = value.translate(LDAP_QUOTING_TABLE)  # Escape special chars
</span><span class="cx"> 
</span><span class="cx">     return matchType.queryString.format(
</span><span class="lines">@@ -78,16 +94,18 @@
</span><span class="cx">     )
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-def ldapQueryStringFromCompoundExpression(expression, attrMap):
</del><ins>+def ldapQueryStringFromCompoundExpression(
+    expression, fieldNameMap, recordTypeMap
+):
</ins><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Generates an LDAP query string from a compound expression.
</span><span class="cx"> 
</span><span class="cx">     @param expression: A compound expression.
</span><span class="cx">     @type expression: L{MatchExpression}
</span><span class="cx"> 
</span><del>-    @param attrMap: A mapping from L{FieldName}s to native LDAP attribute
</del><ins>+    @param fieldNameMap: A mapping from L{FieldName}s to native LDAP attribute
</ins><span class="cx">         names.
</span><del>-    @type attrMap: L{dict}
</del><ins>+    @type fieldNameMap: L{dict}
</ins><span class="cx"> 
</span><span class="cx">     @return: An LDAP query string.
</span><span class="cx">     @rtype: C{unicode}
</span><span class="lines">@@ -107,7 +125,9 @@
</span><span class="cx"> 
</span><span class="cx">     for subExpression in expression.expressions:
</span><span class="cx">         queryTokens.append(
</span><del>-            ldapQueryStringFromExpression(subExpression, attrMap)
</del><ins>+            ldapQueryStringFromExpression(
+                subExpression, fieldNameMap, recordTypeMap
+            )
</ins><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx">     if len(expression.expressions) &gt; 1:
</span><span class="lines">@@ -116,13 +136,15 @@
</span><span class="cx">     return u&quot;&quot;.join(queryTokens)
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-def ldapQueryStringFromExpression(expression, attrMap):
</del><ins>+def ldapQueryStringFromExpression(
+    expression, fieldNameMap, recordTypeMap
+):
</ins><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Converts an expression into an LDAP query string.
</span><span class="cx"> 
</span><del>-    @param attrMap: A mapping from L{FieldName}s to native LDAP attribute
</del><ins>+    @param fieldNameMap: A mapping from L{FieldName}s to native LDAP attribute
</ins><span class="cx">         names.
</span><del>-    @type attrMap: L{dict}
</del><ins>+    @type fieldNameMap: L{dict}
</ins><span class="cx"> 
</span><span class="cx">     @param expression: An expression.
</span><span class="cx">     @type expression: L{MatchExpression} or L{CompoundExpression}
</span><span class="lines">@@ -135,10 +157,14 @@
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><span class="cx">     if isinstance(expression, MatchExpression):
</span><del>-        return ldapQueryStringFromMatchExpression(expression, attrMap)
</del><ins>+        return ldapQueryStringFromMatchExpression(
+            expression, fieldNameMap, recordTypeMap
+        )
</ins><span class="cx"> 
</span><span class="cx">     if isinstance(expression, CompoundExpression):
</span><del>-        return ldapQueryStringFromCompoundExpression(expression, attrMap)
</del><ins>+        return ldapQueryStringFromCompoundExpression(
+            expression, fieldNameMap, recordTypeMap
+        )
</ins><span class="cx"> 
</span><span class="cx">     raise QueryNotSupportedError(
</span><span class="cx">         &quot;Unknown expression type: {0!r}&quot;.format(expression)
</span></span></pre></div>
<a id="twexttrunktwextwholdaptesttest_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/test/test_service.py (12330 => 12331)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/test/test_service.py        2014-01-14 00:20:20 UTC (rev 12330)
+++ twext/trunk/twext/who/ldap/test/test_service.py        2014-01-14 00:38:59 UTC (rev 12331)
</span><span class="lines">@@ -29,17 +29,22 @@
</span><span class="cx"> from twisted.cred.credentials import UsernamePassword
</span><span class="cx"> from twisted.trial import unittest
</span><span class="cx"> 
</span><ins>+from ...idirectory import FieldName as BaseFieldName
</ins><span class="cx"> # from ...expression import (
</span><span class="cx"> #     CompoundExpression, Operand, MatchExpression, MatchType, MatchFlags
</span><span class="cx"> # )
</span><span class="cx"> from .._service import (
</span><del>-    DEFAULT_URL,
</del><ins>+    DEFAULT_FIELDNAME_MAP, DEFAULT_RECORDTYPE_MAP,
</ins><span class="cx">     LDAPBindAuthError,
</span><span class="cx">     DirectoryService, DirectoryRecord,
</span><span class="cx"> )
</span><span class="cx"> 
</span><span class="cx"> from ...test import test_directory
</span><del>-from ...test.test_xml import xmlService
</del><ins>+from ...test.test_xml import (
+    xmlService,
+    DirectoryServiceConvenienceTestMixIn
+    as BaseDirectoryServiceConvenienceTestMixIn,
+)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> class BaseTestCase(object):
</span><span class="lines">@@ -47,12 +52,23 @@
</span><span class="cx">     Tests for L{DirectoryService}.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-    url = DEFAULT_URL
-    realmName = unicode(DEFAULT_URL)
</del><ins>+    url = &quot;ldap://localhost/&quot;
+    baseDN = u&quot;o=org&quot;
+    realmName = unicode(url)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def setUp(self):
</span><del>-        self.mockLDAP = MockLdap(mockDirectoryDataFromXML(self.mktemp()))
</del><ins>+        self.xmlSeedService = xmlService(self.mktemp())
+        self.mockData = mockDirectoryDataFromXMLService(self.xmlSeedService)
+
+        if False:
+            from pprint import pprint
+            print(&quot;&quot;)
+            print(&quot;-&quot; * 80)
+            pprint(self.mockData)
+            print(&quot;-&quot; * 80)
+
+        self.mockLDAP = MockLdap(self.mockData)
</ins><span class="cx">         self.mockLDAP.start()
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -61,25 +77,50 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def service(self, **kwargs):
</span><del>-        return DirectoryService(**kwargs)
</del><ins>+        return DirectoryService(url=self.url, baseDN=self.baseDN, **kwargs)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class DirectoryServiceConvenienceTestMixIn(object):
</del><ins>+class DirectoryServiceConvenienceTestMixIn(
+    BaseDirectoryServiceConvenienceTestMixIn
+):
</ins><span class="cx">     def _unimplemented(self):
</span><span class="cx">         raise NotImplementedError()
</span><span class="cx"> 
</span><span class="cx">     _unimplemented.todo = &quot;unimplemented&quot;
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-    test_recordWithUID = _unimplemented
-    test_recordWithGUID = _unimplemented
-    test_recordsWithRecordType = _unimplemented
-    test_recordWithShortName = _unimplemented
-    test_recordsWithEmailAddress = _unimplemented
</del><ins>+    def test_recordWithUID(self):
+        return BaseDirectoryServiceConvenienceTestMixIn.test_recordWithUID(self)
</ins><span class="cx"> 
</span><ins>+    test_recordWithUID.todo = &quot;needs a seed&quot;
</ins><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def test_recordWithGUID(self):
+        return BaseDirectoryServiceConvenienceTestMixIn.test_recordWithGUID(self)
+
+    test_recordWithGUID.todo = &quot;needs a seed&quot;
+
+
+    def test_recordsWithRecordType(self):
+        return BaseDirectoryServiceConvenienceTestMixIn.test_recordsWithRecordType(self)
+
+    test_recordsWithRecordType.todo = &quot;needs a seed&quot;
+
+
+    def test_recordWithShortName(self):
+        return BaseDirectoryServiceConvenienceTestMixIn.test_recordWithShortName(self)
+
+    test_recordWithShortName.todo = &quot;needs a seed&quot;
+
+
+    def test_recordsWithEmailAddress(self):
+        return BaseDirectoryServiceConvenienceTestMixIn.test_recordsWithEmailAddress(self)
+
+    test_recordsWithEmailAddress.todo = &quot;needs a seed&quot;
+
+
+
</ins><span class="cx"> class DirectoryServiceConnectionTestMixIn(object):
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def test_connect_defaults(self):
</span><span class="lines">@@ -195,9 +236,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-def mockDirectoryDataFromXML(tmp):
-    service = xmlService(tmp)
-
</del><ins>+def mockDirectoryDataFromXMLService(service):
</ins><span class="cx">     o = u&quot;org&quot;
</span><span class="cx">     ou = u&quot;calendarserver&quot;
</span><span class="cx"> 
</span><span class="lines">@@ -206,17 +245,6 @@
</span><span class="cx">         u&quot;ou={ou}&quot;.format(ou=ou): dict(ou=ou),
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    def toAttribute(fieldName):
-        return unicode({
-            service.fieldName.uid: u&quot;recordid&quot;,
-            service.fieldName.guid: u&quot;guid&quot;,
-            service.fieldName.recordType: u&quot;objectclass&quot;,
-            service.fieldName.shortNames: u&quot;uid&quot;,
-            service.fieldName.fullNames: u&quot;cn&quot;,
-            service.fieldName.emailAddresses: u&quot;mail&quot;,
-            service.fieldName.password: u&quot;userPassword&quot;,
-        }.get(fieldName, fieldName.name))
-
</del><span class="cx">     def toUnicode(obj):
</span><span class="cx">         if isinstance(obj, (NamedConstant, ValueConstant)):
</span><span class="cx">             return obj.name
</span><span class="lines">@@ -227,8 +255,13 @@
</span><span class="cx">         return unicode(obj)
</span><span class="cx"> 
</span><span class="cx">     def tuplify(record, fieldName):
</span><del>-        name = toAttribute(fieldName)
-        value = toUnicode(record.fields[fieldName])
</del><ins>+        name = DEFAULT_FIELDNAME_MAP.get(fieldName, fieldName.name)
+
+        if fieldName is BaseFieldName.recordType:
+            value = DEFAULT_RECORDTYPE_MAP[record.fields[fieldName]]
+        else:
+            value = toUnicode(record.fields[fieldName])
+
</ins><span class="cx">         return (name, value)
</span><span class="cx"> 
</span><span class="cx">     for records in service.index[service.fieldName.uid].itervalues():
</span><span class="lines">@@ -245,10 +278,4 @@
</span><span class="cx"> 
</span><span class="cx">             data[dn] = recordData
</span><span class="cx"> 
</span><del>-    # from pprint import pprint
-    # print(&quot;&quot;)
-    # print(&quot;-&quot; * 80)
-    # pprint(data)
-    # print(&quot;-&quot; * 80)
-
</del><span class="cx">     return data
</span></span></pre></div>
<a id="twexttrunktwextwholdaptesttest_utilpy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/test/test_util.py (12330 => 12331)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/test/test_util.py        2014-01-14 00:20:20 UTC (rev 12330)
+++ twext/trunk/twext/who/ldap/test/test_util.py        2014-01-14 00:38:59 UTC (rev 12331)
</span><span class="lines">@@ -38,7 +38,13 @@
</span><span class="cx">     Tests for LDAP query generation.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-    def attrMap(self, service):
</del><ins>+    def service(self):
+        # Use intentionally funky conenction info, since we don't expect
+        # to connect.
+        return DirectoryService(u&quot;ldap://cretin/&quot;, u&quot;o=plugh&quot;)
+
+
+    def fieldNameMap(self, service):
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Create a mapping from field names to LDAP attribute names.
</span><span class="cx">         The attribute names returned here are not real LDAP attribute names,
</span><span class="lines">@@ -48,12 +54,22 @@
</span><span class="cx">         return dict([(c, c.name) for c in service.fieldName.iterconstants()])
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def recordTypeMap(self, service):
+        &quot;&quot;&quot;
+        Create a mapping from record types to LDAP object class names.
+        The object class names returned here are not real LDAP object class
+        names, but we don't care for these tests, since we're not actually
+        connecting to LDAP.
+        &quot;&quot;&quot;
+        return dict([(c, c.name) for c in service.recordType.iterconstants()])
+
+
</ins><span class="cx">     def test_queryStringFromMatchExpression_matchTypes(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Match expressions with each match type produces the correct
</span><span class="cx">         operator=value string.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        service = DirectoryService()
</del><ins>+        service = self.service()
</ins><span class="cx"> 
</span><span class="cx">         for matchType, expected in (
</span><span class="cx">             (MatchType.equals, u&quot;=xyzzy&quot;),
</span><span class="lines">@@ -70,10 +86,11 @@
</span><span class="cx">                 matchType=matchType
</span><span class="cx">             )
</span><span class="cx">             queryString = ldapQueryStringFromMatchExpression(
</span><del>-                expression, self.attrMap(service)
</del><ins>+                expression,
+                self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">             )
</span><span class="cx">             expected = u&quot;({attribute}{expected})&quot;.format(
</span><del>-                attribute=&quot;shortNames&quot;, expected=expected
</del><ins>+                attribute=u&quot;shortNames&quot;, expected=expected
</ins><span class="cx">             )
</span><span class="cx">             self.assertEquals(queryString, expected)
</span><span class="cx"> 
</span><span class="lines">@@ -82,17 +99,18 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Match expression with the C{NOT} flag adds the C{!} operator.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        service = DirectoryService()
</del><ins>+        service = self.service()
</ins><span class="cx"> 
</span><span class="cx">         expression = MatchExpression(
</span><span class="cx">             service.fieldName.shortNames, u&quot;xyzzy&quot;,
</span><span class="cx">             flags=MatchFlags.NOT
</span><span class="cx">         )
</span><span class="cx">         queryString = ldapQueryStringFromMatchExpression(
</span><del>-            expression, self.attrMap(service)
</del><ins>+            expression,
+            self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">         )
</span><span class="cx">         expected = u&quot;(!{attribute}=xyzzy)&quot;.format(
</span><del>-            attribute=&quot;shortNames&quot;,
</del><ins>+            attribute=u&quot;shortNames&quot;,
</ins><span class="cx">         )
</span><span class="cx">         self.assertEquals(queryString, expected)
</span><span class="cx"> 
</span><span class="lines">@@ -102,17 +120,18 @@
</span><span class="cx">         Match expression with the C{caseInsensitive} flag adds the C{??????}
</span><span class="cx">         operator.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        service = DirectoryService()
</del><ins>+        service = self.service()
</ins><span class="cx"> 
</span><span class="cx">         expression = MatchExpression(
</span><span class="cx">             service.fieldName.shortNames, u&quot;xyzzy&quot;,
</span><span class="cx">             flags=MatchFlags.caseInsensitive
</span><span class="cx">         )
</span><span class="cx">         queryString = ldapQueryStringFromMatchExpression(
</span><del>-            expression, self.attrMap(service)
</del><ins>+            expression,
+            self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">         )
</span><span class="cx">         expected = u&quot;???????({attribute}=xyzzy)&quot;.format(
</span><del>-            attribute=&quot;shortNames&quot;,
</del><ins>+            attribute=u&quot;shortNames&quot;,
</ins><span class="cx">         )
</span><span class="cx">         self.assertEquals(queryString, expected)
</span><span class="cx"> 
</span><span class="lines">@@ -126,17 +145,18 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Special characters are quoted properly.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        service = DirectoryService()
</del><ins>+        service = self.service()
</ins><span class="cx"> 
</span><span class="cx">         expression = MatchExpression(
</span><span class="cx">             service.fieldName.fullNames,
</span><span class="cx">             u&quot;\\xyzzy: a/b/(c)* ~~ &gt;=&lt; ~~ &amp;| \0!!&quot;
</span><span class="cx">         )
</span><span class="cx">         queryString = ldapQueryStringFromMatchExpression(
</span><del>-            expression, self.attrMap(service)
</del><ins>+            expression,
+            self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">         )
</span><span class="cx">         expected = u&quot;({attribute}={expected})&quot;.format(
</span><del>-            attribute=&quot;fullNames&quot;,
</del><ins>+            attribute=u&quot;fullNames&quot;,
</ins><span class="cx">             expected=(
</span><span class="cx">                 u&quot;\\5Cxyzzy: a\\2Fb\\2F\\28c\\29\\2A &quot;
</span><span class="cx">                 &quot;\\7E\\7E \\3E\\3D\\3C \\7E\\7E \\26\\7C \\00!!&quot;
</span><span class="lines">@@ -149,7 +169,7 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Unknown expression.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        service = DirectoryService()
</del><ins>+        service = self.service()
</ins><span class="cx"> 
</span><span class="cx">         expression = MatchExpression(
</span><span class="cx">             object(), u&quot;xyzzy&quot;,
</span><span class="lines">@@ -158,7 +178,8 @@
</span><span class="cx">         self.assertRaises(
</span><span class="cx">             QueryNotSupportedError,
</span><span class="cx">             ldapQueryStringFromMatchExpression,
</span><del>-            expression, self.attrMap(service)
</del><ins>+            expression,
+            self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -166,7 +187,7 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Unknown expression.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        service = DirectoryService()
</del><ins>+        service = self.service()
</ins><span class="cx"> 
</span><span class="cx">         expression = MatchExpression(
</span><span class="cx">             service.fieldName.shortNames, u&quot;xyzzy&quot;,
</span><span class="lines">@@ -176,7 +197,8 @@
</span><span class="cx">         self.assertRaises(
</span><span class="cx">             QueryNotSupportedError,
</span><span class="cx">             ldapQueryStringFromMatchExpression,
</span><del>-            expression, self.attrMap(service)
</del><ins>+            expression,
+            self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -192,7 +214,7 @@
</span><span class="cx">         The Operand shouldn't make any difference here, so we test AND and OR,
</span><span class="cx">         expecting the same result.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        service = DirectoryService()
</del><ins>+        service = self.service()
</ins><span class="cx"> 
</span><span class="cx">         for operand in (Operand.AND, Operand.OR):
</span><span class="cx">             matchExpression = MatchExpression(
</span><span class="lines">@@ -203,11 +225,13 @@
</span><span class="cx">                 operand
</span><span class="cx">             )
</span><span class="cx">             queryString = queryFunction(
</span><del>-                compoundExpression, self.attrMap(service)
</del><ins>+                compoundExpression,
+                self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">             )
</span><span class="cx">             expected = u&quot;{match}&quot;.format(
</span><span class="cx">                 match=ldapQueryStringFromMatchExpression(
</span><del>-                    matchExpression, self.attrMap(service)
</del><ins>+                    matchExpression,
+                    self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">                 )
</span><span class="cx">             )
</span><span class="cx">             self.assertEquals(queryString, expected)
</span><span class="lines">@@ -221,7 +245,7 @@
</span><span class="cx"> 
</span><span class="cx">         The sub-expressions should be grouped with the given operand.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        service = DirectoryService()
</del><ins>+        service = self.service()
</ins><span class="cx"> 
</span><span class="cx">         for (operand, token) in ((Operand.AND, u&quot;&amp;&quot;), (Operand.OR, u&quot;|&quot;)):
</span><span class="cx">             matchExpression1 = MatchExpression(
</span><span class="lines">@@ -235,15 +259,18 @@
</span><span class="cx">                 operand
</span><span class="cx">             )
</span><span class="cx">             queryString = queryFunction(
</span><del>-                compoundExpression, self.attrMap(service)
</del><ins>+                compoundExpression,
+                self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">             )
</span><span class="cx">             expected = u&quot;({op}{match1}{match2})&quot;.format(
</span><span class="cx">                 op=token,
</span><span class="cx">                 match1=ldapQueryStringFromMatchExpression(
</span><del>-                    matchExpression1, self.attrMap(service)
</del><ins>+                    matchExpression1,
+                    self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">                 ),
</span><span class="cx">                 match2=ldapQueryStringFromMatchExpression(
</span><del>-                    matchExpression2, self.attrMap(service)
</del><ins>+                    matchExpression2,
+                    self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">                 ),
</span><span class="cx">             )
</span><span class="cx">             self.assertEquals(queryString, expected)
</span><span class="lines">@@ -253,16 +280,18 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Match expression.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        service = DirectoryService()
</del><ins>+        service = self.service()
</ins><span class="cx"> 
</span><span class="cx">         matchExpression = MatchExpression(
</span><span class="cx">             service.fieldName.shortNames, u&quot;xyzzy&quot;
</span><span class="cx">         )
</span><span class="cx">         queryString = ldapQueryStringFromExpression(
</span><del>-            matchExpression, self.attrMap(service)
</del><ins>+            matchExpression,
+            self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">         )
</span><span class="cx">         expected = ldapQueryStringFromMatchExpression(
</span><del>-            matchExpression, self.attrMap(service)
</del><ins>+            matchExpression,
+            self.fieldNameMap(service), self.recordTypeMap(service),
</ins><span class="cx">         )
</span><span class="cx">         self.assertEquals(queryString, expected)
</span><span class="cx"> 
</span><span class="lines">@@ -283,10 +312,10 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Unknown expression.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        service = DirectoryService()
</del><ins>+        service = self.service()
</ins><span class="cx"> 
</span><span class="cx">         self.assertRaises(
</span><span class="cx">             QueryNotSupportedError,
</span><span class="cx">             ldapQueryStringFromExpression,
</span><del>-            object(), self.attrMap(service)
</del><ins>+            object(), self.fieldNameMap(service), self.recordTypeMap(service)
</ins><span class="cx">         )
</span></span></pre></div>
<a id="twexttrunktwextwhotesttest_directorypy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/test/test_directory.py (12330 => 12331)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/test/test_directory.py        2014-01-14 00:20:20 UTC (rev 12330)
+++ twext/trunk/twext/who/test/test_directory.py        2014-01-14 00:38:59 UTC (rev 12331)
</span><span class="lines">@@ -227,8 +227,8 @@
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def test_recordsFromExpression_emptyExpression(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        L{DirectoryService.recordsFromExpression} with an unknown expression
-        type and an empty L{CompoundExpression} returns an empty result.
</del><ins>+        L{DirectoryService.recordsFromExpression} with an empty
+        L{CompoundExpression} returns an empty result.
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         service = self.service()
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>