<!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>[13867] 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/13867">13867</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-08-12 12:57:18 -0700 (Tue, 12 Aug 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>Adds ExistsExpression (true if an attribute has a value) and BooleanExpression (true if the associated directory attribute has a specific value)</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunktwextwhoexpressionpy">twext/trunk/twext/who/expression.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="#twexttrunktwextwhoopendirectorytesttest_servicepy">twext/trunk/twext/who/opendirectory/test/test_service.py</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="twexttrunktwextwhoexpressionpy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/expression.py (13866 => 13867)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/expression.py        2014-08-11 21:31:01 UTC (rev 13866)
+++ twext/trunk/twext/who/expression.py        2014-08-12 19:57:18 UTC (rev 13867)
</span><span class="lines">@@ -21,8 +21,9 @@
</span><span class="cx">
</span><span class="cx"> __all__ = [
</span><span class="cx"> "Operand",
</span><ins>+ "BooleanExpression",
</ins><span class="cx"> "CompoundExpression",
</span><del>-
</del><ins>+ "ExistsExpression",
</ins><span class="cx"> "MatchType",
</span><span class="cx"> "MatchFlags",
</span><span class="cx"> "MatchExpression",
</span><span class="lines">@@ -334,3 +335,71 @@
</span><span class="cx"> return predicate(match(
</span><span class="cx"> normalize(value), normalize(self.fieldValue)
</span><span class="cx"> ))
</span><ins>+
+
+class ExistsExpression(object):
+ """
+ Query for the existence a given field.
+
+ @ivar fieldName: A L{NamedConstant} specifying the field.
+ """
+
+ def __init__(self, fieldName):
+ if not isinstance(fieldName, NamedConstant):
+ raise TypeError(
+ "Field name {name} in exists expression is not a NamedConstant."
+ .format(name=fieldName)
+ )
+
+ self.fieldName = fieldName
+
+
+ def __repr__(self):
+ return (
+ "<{self.__class__.__name__}: {fieldName!r} "
+ .format(
+ self=self,
+ fieldName=describe(self.fieldName),
+ )
+ )
+
+
+ def __eq__(self, other):
+ if isinstance(other, ExistsExpression):
+ return (self.fieldName is other.fieldName)
+ else:
+ return NotImplemented
+
+
+class BooleanExpression(object):
+ """
+ Query for the "True" value of a given field.
+
+ @ivar fieldName: A L{NamedConstant} specifying the field.
+ """
+
+ def __init__(self, fieldName):
+ if not isinstance(fieldName, NamedConstant):
+ raise TypeError(
+ "Field name {name} in boolean expression is not a NamedConstant."
+ .format(name=fieldName)
+ )
+
+ self.fieldName = fieldName
+
+
+ def __repr__(self):
+ return (
+ "<{self.__class__.__name__}: {fieldName!r} "
+ .format(
+ self=self,
+ fieldName=describe(self.fieldName),
+ )
+ )
+
+
+ def __eq__(self, other):
+ if isinstance(other, BooleanExpression):
+ return (self.fieldName is other.fieldName)
+ else:
+ return NotImplemented
</ins></span></pre></div>
<a id="twexttrunktwextwholdap_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/_service.py (13866 => 13867)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/_service.py        2014-08-11 21:31:01 UTC (rev 13866)
+++ twext/trunk/twext/who/ldap/_service.py        2014-08-12 19:57:18 UTC (rev 13867)
</span><span class="lines">@@ -47,12 +47,14 @@
</span><span class="cx"> DirectoryService as BaseDirectoryService,
</span><span class="cx"> DirectoryRecord as BaseDirectoryRecord,
</span><span class="cx"> )
</span><del>-from ..expression import MatchExpression
</del><ins>+from ..expression import MatchExpression, ExistsExpression, BooleanExpression
</ins><span class="cx"> from ..util import ConstantsContainer
</span><span class="cx"> from ._constants import LDAPAttribute, LDAPObjectClass
</span><span class="cx"> from ._util import (
</span><span class="cx"> ldapQueryStringFromMatchExpression,
</span><span class="cx"> ldapQueryStringFromCompoundExpression,
</span><ins>+ ldapQueryStringFromBooleanExpression,
+ ldapQueryStringFromExistsExpression,
</ins><span class="cx"> )
</span><span class="cx"> from zope.interface import implementer
</span><span class="cx">
</span><span class="lines">@@ -573,7 +575,12 @@
</span><span class="cx"> recordTypes = self.recordTypes()
</span><span class="cx">
</span><span class="cx"> for recordType in recordTypes:
</span><del>- rdn = self._recordTypeSchemas[recordType].relativeDN
</del><ins>+ try:
+ rdn = self._recordTypeSchemas[recordType].relativeDN
+ except KeyError:
+ # Skip this unknown record type
+ continue
+
</ins><span class="cx"> rdn = (
</span><span class="cx"> ldap.dn.str2dn(rdn.lower()) +
</span><span class="cx"> ldap.dn.str2dn(self._baseDN.lower())
</span><span class="lines">@@ -600,7 +607,7 @@
</span><span class="cx"> raise LDAPQueryError("Unable to perform query", e)
</span><span class="cx">
</span><span class="cx"> except ldap.NO_SUCH_OBJECT as e:
</span><del>- self.log.warn("RDN {rdn} does not exist, skipping", rdn=rdn)
</del><ins>+ # self.log.warn("RDN {rdn} does not exist, skipping", rdn=rdn)
</ins><span class="cx"> continue
</span><span class="cx">
</span><span class="cx"> records.extend(
</span><span class="lines">@@ -628,13 +635,19 @@
</span><span class="cx">
</span><span class="cx"> self.log.debug("Performing LDAP DN query: {dn}", dn=dn)
</span><span class="cx">
</span><del>- reply = connection.search_s(
- dn,
- ldap.SCOPE_SUBTREE,
- "(objectClass=*)",
- attrlist=self._attributesToFetch
- )
- records = self._recordsFromReply(reply)
</del><ins>+ try:
+ reply = connection.search_s(
+ dn,
+ ldap.SCOPE_SUBTREE,
+ "(objectClass=*)",
+ attrlist=self._attributesToFetch
+ )
+ records = self._recordsFromReply(reply)
+ except ldap.NO_SUCH_OBJECT:
+ records = []
+ except ldap.INVALID_DN_SYNTAX:
+ self.log.warn("Invalid LDAP DN syntax: '{dn}'", dn=dn)
+ records = []
</ins><span class="cx">
</span><span class="cx"> if len(records):
</span><span class="cx"> return records[0]
</span><span class="lines">@@ -705,6 +718,21 @@
</span><span class="cx"> else:
</span><span class="cx"> fields[fieldName] = newValues[0]
</span><span class="cx">
</span><ins>+ elif valueType is bool:
+ if not isinstance(values, list):
+ values = [values]
+ if "=" in attribute:
+ attribute, trueValue = attribute.split("=")
+ else:
+ trueValue = "true"
+
+ for value in values:
+ if value == trueValue:
+ fields[fieldName] = True
+ break
+ else:
+ fields[fieldName] = False
+
</ins><span class="cx"> else:
</span><span class="cx"> raise LDAPConfigurationError(
</span><span class="cx"> "Unknown value type {0} for field {1}".format(
</span><span class="lines">@@ -741,6 +769,24 @@
</span><span class="cx"> queryString, recordTypes=recordTypes
</span><span class="cx"> )
</span><span class="cx">
</span><ins>+ elif isinstance(expression, ExistsExpression):
+ queryString = ldapQueryStringFromExistsExpression(
+ expression,
+ self._fieldNameToAttributesMap, self._recordTypeSchemas
+ )
+ return self._recordsFromQueryString(
+ queryString, recordTypes=recordTypes
+ )
+
+ elif isinstance(expression, BooleanExpression):
+ queryString = ldapQueryStringFromBooleanExpression(
+ expression,
+ self._fieldNameToAttributesMap, self._recordTypeSchemas
+ )
+ return self._recordsFromQueryString(
+ queryString, recordTypes=recordTypes
+ )
+
</ins><span class="cx"> return BaseDirectoryService.recordsFromNonCompoundExpression(
</span><span class="cx"> self, expression, records=records
</span><span class="cx"> )
</span><span class="lines">@@ -762,9 +808,13 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx"> def recordsWithRecordType(self, recordType):
</span><del>- return self.recordsWithFieldValue(
- BaseFieldName.uid, u"*", recordTypes=[recordType]
</del><ins>+ queryString = ldapQueryStringFromExistsExpression(
+ ExistsExpression(self.fieldName.uid),
+ self._fieldNameToAttributesMap, self._recordTypeSchemas
</ins><span class="cx"> )
</span><ins>+ return self._recordsFromQueryString(
+ queryString, recordTypes=[recordType]
+ )
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx"> # def updateRecords(self, records, create=False):
</span><span class="lines">@@ -813,7 +863,16 @@
</span><span class="cx"> return self.service._authenticateUsernamePassword(self.dn, password)
</span><span class="cx">
</span><span class="cx">
</span><ins>+def normalizeDNstr(dnStr):
+ """
+ Convert to lowercase and remove extra whitespace
+ @param dnStr: dn
+ @type dnStr: C{str}
+ @return: normalized dn C{str}
+ """
+ return ' '.join(ldap.dn.dn2str(ldap.dn.str2dn(dnStr.lower())).split())
</ins><span class="cx">
</span><ins>+
</ins><span class="cx"> def reverseDict(source):
</span><span class="cx"> new = {}
</span><span class="cx">
</span></span></pre></div>
<a id="twexttrunktwextwholdap_utilpy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/_util.py (13866 => 13867)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/_util.py        2014-08-11 21:31:01 UTC (rev 13866)
+++ twext/trunk/twext/who/ldap/_util.py        2014-08-12 19:57:18 UTC (rev 13867)
</span><span class="lines">@@ -17,8 +17,8 @@
</span><span class="cx">
</span><span class="cx"> from ..idirectory import QueryNotSupportedError, FieldName
</span><span class="cx"> from ..expression import (
</span><del>- CompoundExpression, Operand,
- MatchExpression, MatchFlags,
</del><ins>+ CompoundExpression, ExistsExpression, MatchExpression,
+ MatchFlags, Operand, BooleanExpression
</ins><span class="cx"> )
</span><span class="cx"> from ._constants import LDAPOperand, LDAPMatchType, LDAPMatchFlags
</span><span class="cx">
</span><span class="lines">@@ -154,7 +154,102 @@
</span><span class="cx"> raise AssertionError("We shouldn't be here.")
</span><span class="cx">
</span><span class="cx">
</span><ins>+def ldapQueryStringFromExistsExpression(
+ expression, fieldNameToAttributesMap, recordTypeSchemas
+):
+ """
+ Generates an LDAP query string from an exists expression.
</ins><span class="cx">
</span><ins>+ @param expression: An exists expression.
+ @type expression: L{ExistsExpression}
+
+ @param fieldNameToAttributesMap: A mapping from field names to native LDAP
+ attribute names.
+ @type fieldNameToAttributesMap: L{dict} with L{FieldName} keys and sequence
+ of L{unicode} values.
+
+ @param recordTypeSchemas: Schema information for record types.
+ @type recordTypeSchemas: mapping from L{NamedConstant} to
+ L{RecordTypeSchema}
+
+ @return: An LDAP query string.
+ @rtype: L{unicode}
+
+ @raises QueryNotSupportedError: if the expresion references an unknown
+ field name (meaning a field name not in C{fieldNameToAttributeMap}).
+ """
+
+ fieldName = expression.fieldName
+
+ try:
+ attributes = fieldNameToAttributesMap[fieldName]
+ except KeyError:
+ raise QueryNotSupportedError(
+ "Unmapped field name: {0}".format(expression.fieldName)
+ )
+
+ queryStrings = [
+ u"({attribute}=*)".format(attribute=attribute)
+ for attribute in attributes
+ ]
+
+ operand = LDAPOperand.OR.value
+ return ldapQueryStringFromQueryStrings(operand, queryStrings)
+
+
+def ldapQueryStringFromBooleanExpression(
+ expression, fieldNameToAttributesMap, recordTypeSchemas
+):
+ """
+ Generates an LDAP query string from a boolean expression.
+
+ @param expression: An boolean expression.
+ @type expression: L{BooleanExpression}
+
+ @param fieldNameToAttributesMap: A mapping from field names to native LDAP
+ attribute names.
+ @type fieldNameToAttributesMap: L{dict} with L{FieldName} keys and sequence
+ of L{unicode} values.
+
+ @param recordTypeSchemas: Schema information for record types.
+ @type recordTypeSchemas: mapping from L{NamedConstant} to
+ L{RecordTypeSchema}
+
+ @return: An LDAP query string.
+ @rtype: L{unicode}
+
+ @raises QueryNotSupportedError: if the expresion references an unknown
+ field name (meaning a field name not in C{fieldNameToAttributeMap}).
+ """
+
+ fieldName = expression.fieldName
+
+ try:
+ attributes = fieldNameToAttributesMap[fieldName]
+ except KeyError:
+ raise QueryNotSupportedError(
+ "Unmapped field name: {0}".format(expression.fieldName)
+ )
+
+ queryStrings = []
+ for attribute in attributes:
+ if "=" in attribute:
+ attribute, trueValue = attribute.split("=")
+ else:
+ trueValue = "true"
+
+ queryStrings.append(
+ u"({attribute}={trueValue})".format(
+ attribute=attribute,
+ trueValue=trueValue
+ )
+ )
+
+
+ operand = LDAPOperand.OR.value
+ return ldapQueryStringFromQueryStrings(operand, queryStrings)
+
+
</ins><span class="cx"> def ldapQueryStringFromCompoundExpression(
</span><span class="cx"> expression, fieldNameToAttributesMap, recordTypeSchemas
</span><span class="cx"> ):
</span><span class="lines">@@ -227,6 +322,16 @@
</span><span class="cx"> expression, fieldNameToAttributesMap, recordTypeSchemas
</span><span class="cx"> )
</span><span class="cx">
</span><ins>+ if isinstance(expression, BooleanExpression):
+ return ldapQueryStringFromBooleanExpression(
+ expression, fieldNameToAttributesMap, recordTypeSchemas
+ )
+
+ if isinstance(expression, ExistsExpression):
+ return ldapQueryStringFromExistsExpression(
+ expression, fieldNameToAttributesMap, recordTypeSchemas
+ )
+
</ins><span class="cx"> if isinstance(expression, CompoundExpression):
</span><span class="cx"> return ldapQueryStringFromCompoundExpression(
</span><span class="cx"> expression, fieldNameToAttributesMap, recordTypeSchemas
</span><span class="lines">@@ -243,8 +348,7 @@
</span><span class="cx">
</span><span class="cx"> ord(u"("): u"\\28",
</span><span class="cx"> ord(u")"): u"\\29",
</span><del>- # Question: shouldn't we not be quoting * because that's how you specify wildcards?
- # ord(u"*"): u"\\2A",
</del><ins>+ ord(u"*"): u"\\2A",
</ins><span class="cx">
</span><span class="cx"> ord(u"<"): u"\\3C",
</span><span class="cx"> ord(u"="): u"\\3D",
</span></span></pre></div>
<a id="twexttrunktwextwholdaptesttest_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/test/test_service.py (13866 => 13867)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/test/test_service.py        2014-08-11 21:31:01 UTC (rev 13866)
+++ twext/trunk/twext/who/ldap/test/test_service.py        2014-08-12 19:57:18 UTC (rev 13867)
</span><span class="lines">@@ -57,11 +57,10 @@
</span><span class="cx"> from ...test.test_xml import (
</span><span class="cx"> xmlService,
</span><span class="cx"> BaseTest as XMLBaseTest, QueryMixIn,
</span><del>- DirectoryServiceConvenienceTestMixIn
- as BaseDirectoryServiceConvenienceTestMixIn,
</del><ins>+ DirectoryServiceConvenienceTestMixIn,
</ins><span class="cx"> DirectoryServiceRealmTestMixIn,
</span><span class="cx"> DirectoryServiceQueryTestMixIn as BaseDirectoryServiceQueryTestMixIn,
</span><del>- DirectoryServiceMutableTestMixIn as BaseDirectoryServiceMutableTestMixIn,
</del><ins>+ DirectoryServiceMutableTestMixIn as BaseDirectoryServiceMutableTestMixIn
</ins><span class="cx"> )
</span><span class="cx">
</span><span class="cx">
</span><span class="lines">@@ -153,21 +152,6 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx">
</span><del>-class DirectoryServiceConvenienceTestMixIn(
- BaseDirectoryServiceConvenienceTestMixIn
-):
- def test_recordsWithRecordType_unknown(self):
- pass
- # service = self.service()
-
- # self.assertRaises(
- # QueryNotSupportedError,
- # service.recordsWithRecordType, UnknownConstant.unknown
- # )
- test_recordsWithRecordType_unknown.todo = "After this test runs, other tests fail, need to investigate"
-
-
-
</del><span class="cx"> class DirectoryServiceQueryTestMixIn(BaseDirectoryServiceQueryTestMixIn):
</span><span class="cx"> def test_queryNot(self):
</span><span class="cx"> return BaseDirectoryServiceQueryTestMixIn.test_queryNot(self)
</span><span class="lines">@@ -268,6 +252,7 @@
</span><span class="cx"> self.assertFalse(connection.tls_enabled)
</span><span class="cx">
</span><span class="cx">
</span><ins>+ @inlineCallbacks
</ins><span class="cx"> def test_connect_withUsernamePassword_invalid(self):
</span><span class="cx"> """
</span><span class="cx"> Connect with UsernamePassword credentials.
</span><span class="lines">@@ -277,7 +262,12 @@
</span><span class="cx"> u"__password__"
</span><span class="cx"> )
</span><span class="cx"> service = self.service(credentials=credentials)
</span><del>- self.assertFailure(service._connect(), LDAPBindAuthError)
</del><ins>+ try:
+ yield service._connect()
+ except LDAPBindAuthError:
+ pass
+ else:
+ self.fail("Should have raised LDAPBindAuthError")
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx"> @inlineCallbacks
</span></span></pre></div>
<a id="twexttrunktwextwholdaptesttest_utilpy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/test/test_util.py (13866 => 13867)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/test/test_util.py        2014-08-11 21:31:01 UTC (rev 13866)
+++ twext/trunk/twext/who/ldap/test/test_util.py        2014-08-12 19:57:18 UTC (rev 13867)
</span><span class="lines">@@ -22,7 +22,8 @@
</span><span class="cx">
</span><span class="cx"> from ...idirectory import QueryNotSupportedError
</span><span class="cx"> from ...expression import (
</span><del>- CompoundExpression, Operand, MatchExpression, MatchType, MatchFlags
</del><ins>+ CompoundExpression, ExistsExpression, MatchExpression, BooleanExpression,
+ Operand, MatchType, MatchFlags
</ins><span class="cx"> )
</span><span class="cx"> from ...test.test_xml import UnknownConstant
</span><span class="cx"> from .._constants import LDAPOperand
</span><span class="lines">@@ -31,17 +32,29 @@
</span><span class="cx"> )
</span><span class="cx"> from .._util import (
</span><span class="cx"> ldapQueryStringFromQueryStrings,
</span><ins>+ ldapQueryStringFromBooleanExpression,
+ ldapQueryStringFromCompoundExpression,
+ ldapQueryStringFromExistsExpression,
</ins><span class="cx"> ldapQueryStringFromMatchExpression,
</span><del>- ldapQueryStringFromCompoundExpression,
</del><span class="cx"> ldapQueryStringFromExpression,
</span><span class="cx"> )
</span><span class="cx"> from ...idirectory import FieldName as BaseFieldName
</span><ins>+from twisted.python.constants import Names, NamedConstant
</ins><span class="cx">
</span><del>-
</del><span class="cx"> TEST_FIELDNAME_MAP = dict(DEFAULT_FIELDNAME_ATTRIBUTE_MAP)
</span><span class="cx"> TEST_FIELDNAME_MAP[BaseFieldName.uid] = (u"__who_uid__",)
</span><span class="cx">
</span><span class="cx">
</span><ins>+class TestFieldName(Names):
+ isAwesome = NamedConstant()
+ isAwesome.description = u"is awesome"
+ isAwesome.valueType = bool
+
+ isCool = NamedConstant()
+ isCool.description = u"is cool"
+ isCool.valueType = bool
+
+
</ins><span class="cx"> class LDAPQueryTestCase(unittest.TestCase):
</span><span class="cx"> """
</span><span class="cx"> Tests for LDAP query generation.
</span><span class="lines">@@ -81,9 +94,7 @@
</span><span class="cx"> c,
</span><span class="cx"> RecordTypeSchema(
</span><span class="cx"> relativeDN=NotImplemented, # Don't expect this to be used
</span><del>- attributes=(
- (u"recordTypeAttribute", c.name),
- )
</del><ins>+ attributes=((u"recordTypeAttribute", c.name),)
</ins><span class="cx"> )
</span><span class="cx"> )
</span><span class="cx"> for c in service.recordType.iterconstants()
</span><span class="lines">@@ -122,6 +133,55 @@
</span><span class="cx"> )
</span><span class="cx">
</span><span class="cx">
</span><ins>+ def test_queryStringFromExistsExpression(self):
+ """
+ Exists expressions produce the correct (attribute=*) string.
+ """
+ service = self.service()
+
+ expression = ExistsExpression(service.fieldName.shortNames)
+ queryString = ldapQueryStringFromExistsExpression(
+ expression,
+ self.fieldNameMap(service),
+ self.recordTypeSchemas(service),
+ )
+ expected = u"(shortNames=*)"
+ self.assertEquals(queryString, expected)
+
+
+ def test_queryStringFromBooleanExpression(self):
+ """
+ If a field is a boolean type and the fieldNameToAttributesMap
+ value for the field has an equals sign, the portion to the right
+ of the equals sign is the value that represents True. Make sure
+ the query string we generate includes that value.
+ """
+ service = self.service()
+
+ testFieldNameMap = {
+ TestFieldName.isAwesome: ("awesome=totally",),
+ TestFieldName.isCool: ("cool",),
+ }
+
+ expression = BooleanExpression(TestFieldName.isAwesome)
+ queryString = ldapQueryStringFromBooleanExpression(
+ expression,
+ testFieldNameMap,
+ self.recordTypeSchemas(service),
+ )
+ expected = u"(awesome=totally)"
+ self.assertEquals(queryString, expected)
+
+ expression = BooleanExpression(TestFieldName.isCool)
+ queryString = ldapQueryStringFromBooleanExpression(
+ expression,
+ testFieldNameMap,
+ self.recordTypeSchemas(service),
+ )
+ expected = u"(cool=true)"
+ self.assertEquals(queryString, expected)
+
+
</ins><span class="cx"> def test_queryStringFromMatchExpression_matchTypes(self):
</span><span class="cx"> """
</span><span class="cx"> Match expressions with each match type produces the correct
</span><span class="lines">@@ -216,7 +276,7 @@
</span><span class="cx"> expected = u"({attribute}={expected})".format(
</span><span class="cx"> attribute=u"fullNames",
</span><span class="cx"> expected=(
</span><del>- u"\\5Cxyzzy: a\\2Fb\\2F\\28c\\29* "
</del><ins>+ u"\\5Cxyzzy: a\\2Fb\\2F\\28c\\29\\2A "
</ins><span class="cx"> "\\7E\\7E \\3E\\3D\\3C \\7E\\7E \\26\\7C \\00!!"
</span><span class="cx"> )
</span><span class="cx"> )
</span></span></pre></div>
<a id="twexttrunktwextwhoopendirectorytesttest_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/opendirectory/test/test_service.py (13866 => 13867)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/opendirectory/test/test_service.py        2014-08-11 21:31:01 UTC (rev 13866)
+++ twext/trunk/twext/who/opendirectory/test/test_service.py        2014-08-12 19:57:18 UTC (rev 13867)
</span><span class="lines">@@ -185,7 +185,7 @@
</span><span class="cx"> u"({attribute}={expected})".format(
</span><span class="cx"> attribute=ODAttribute.fullName.value,
</span><span class="cx"> expected=(
</span><del>- u"\\5Cxyzzy: a\\2Fb\\2F\\28c\\29* "
</del><ins>+ u"\\5Cxyzzy: a\\2Fb\\2F\\28c\\29\\2A "
</ins><span class="cx"> "\\7E\\7E \\3E\\3D\\3C \\7E\\7E \\26\\7C \\00!!"
</span><span class="cx"> )
</span><span class="cx"> )
</span></span></pre>
</div>
</div>
</body>
</html>