/usr/lib/python2.7/dist-packages/twext/who/ldap/test/test_util.py is in python-twext 0.1.b2.dev15059-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | ##
# Copyright (c) 2010-2015 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
"""
OpenDirectory service tests.
"""
from twisted.trial import unittest
from ...idirectory import QueryNotSupportedError
from ...expression import (
CompoundExpression, ExistsExpression, MatchExpression, BooleanExpression,
Operand, MatchType, MatchFlags
)
from ...test.test_xml import UnknownConstant
from .._constants import LDAPOperand
from .._service import (
DirectoryService, RecordTypeSchema, DEFAULT_FIELDNAME_ATTRIBUTE_MAP
)
from .._util import (
ldapQueryStringFromQueryStrings,
ldapQueryStringFromBooleanExpression,
ldapQueryStringFromCompoundExpression,
ldapQueryStringFromExistsExpression,
ldapQueryStringFromMatchExpression,
ldapQueryStringFromExpression,
)
from ...idirectory import FieldName as BaseFieldName
from twisted.python.constants import Names, NamedConstant
TEST_FIELDNAME_MAP = dict(DEFAULT_FIELDNAME_ATTRIBUTE_MAP)
TEST_FIELDNAME_MAP[BaseFieldName.uid] = (u"__who_uid__",)
class TestFieldName(Names):
isAwesome = NamedConstant()
isAwesome.description = u"is awesome"
isAwesome.valueType = bool
isCool = NamedConstant()
isCool.description = u"is cool"
isCool.valueType = bool
class LDAPQueryTestCase(unittest.TestCase):
"""
Tests for LDAP query generation.
"""
def service(self):
# Use intentionally funky connection info, since we don't expect
# to connect.
return DirectoryService(
u"ldap://cretin/", u"o=plugh",
fieldNameToAttributesMap=TEST_FIELDNAME_MAP
)
def fieldNameMap(self, service):
"""
Create a mapping from field names to LDAP attribute names.
The attribute names returned here are not real LDAP attribute names,
but we don't care for these tests, since we're not actually connecting
to LDAP.
"""
return dict([
(c, (c.name,))
for c in service.fieldName.iterconstants()
])
def recordTypeSchemas(self, service):
"""
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.
"""
return dict([
(
c,
RecordTypeSchema(
relativeDN=NotImplemented, # Don't expect this to be used
attributes=((u"recordTypeAttribute", c.name),)
)
)
for c in service.recordType.iterconstants()
])
def _test_ldapQueryStringFromQueryStrings(self, queryStrings, expected):
for operand in (LDAPOperand.AND.value, LDAPOperand.OR.value):
compound = ldapQueryStringFromQueryStrings(operand, queryStrings)
self.assertEquals(compound, expected.format(operand=operand))
def test_ldapQueryStringFromQueryStrings_empty(self):
"""
A single expression should just be returned as-is.
"""
return self._test_ldapQueryStringFromQueryStrings((), u"")
def test_ldapQueryStringFromQueryStrings_single(self):
"""
A single expression should just be returned as-is.
"""
queryStrings = (u"(x=yzzy)",)
return self._test_ldapQueryStringFromQueryStrings(
queryStrings, queryStrings[0]
)
def test_ldapQueryStringFromQueryStrings_multiple(self):
"""
Multiple expressions should just be combined with an operator.
"""
return self._test_ldapQueryStringFromQueryStrings(
(u"(x=yzzy)", u"(xy=zzy)"), u"({operand}(x=yzzy)(xy=zzy))"
)
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)
def test_queryStringFromMatchExpression_matchTypes(self):
"""
Match expressions with each match type produces the correct
operator=value string.
"""
service = self.service()
for matchType, expected in (
(MatchType.equals, u"=xyzzy"),
(MatchType.startsWith, u"=xyzzy*"),
(MatchType.endsWith, u"=*xyzzy"),
(MatchType.contains, u"=*xyzzy*"),
(MatchType.lessThan, u"<xyzzy"),
(MatchType.greaterThan, u">xyzzy"),
(MatchType.lessThanOrEqualTo, u"<=xyzzy"),
(MatchType.greaterThanOrEqualTo, u">=xyzzy"),
):
expression = MatchExpression(
service.fieldName.shortNames, u"xyzzy",
matchType=matchType
)
queryString = ldapQueryStringFromMatchExpression(
expression,
self.fieldNameMap(service), self.recordTypeSchemas(service),
)
expected = u"({attribute}{expected})".format(
attribute=u"shortNames", expected=expected
)
self.assertEquals(queryString, expected)
def test_queryStringFromMatchExpression_match_not(self):
"""
Match expression with the C{NOT} flag adds the C{!} operator.
"""
service = self.service()
expression = MatchExpression(
service.fieldName.shortNames, u"xyzzy",
flags=MatchFlags.NOT
)
queryString = ldapQueryStringFromMatchExpression(
expression,
self.fieldNameMap(service), self.recordTypeSchemas(service),
)
expected = u"(!{attribute}=xyzzy)".format(
attribute=u"shortNames",
)
self.assertEquals(queryString, expected)
def test_queryStringFromMatchExpression_match_caseInsensitive(self):
"""
Match expression with the C{caseInsensitive} flag adds the C{??????}
operator.
"""
service = self.service()
expression = MatchExpression(
service.fieldName.shortNames, u"xyzzy",
flags=MatchFlags.caseInsensitive
)
queryString = ldapQueryStringFromMatchExpression(
expression,
self.fieldNameMap(service), self.recordTypeSchemas(service),
)
expected = u"???????({attribute}=xyzzy)".format(
attribute=u"shortNames",
)
self.assertEquals(queryString, expected)
test_queryStringFromMatchExpression_match_caseInsensitive.todo = (
"unimplemented"
)
def test_queryStringFromMatchExpression_match_quoting(self):
"""
Special characters are quoted properly.
"""
service = self.service()
expression = MatchExpression(
service.fieldName.fullNames,
u"\\xyzzy: a/b/(c)* ~~ >=< ~~ &| \0!!"
)
queryString = ldapQueryStringFromMatchExpression(
expression,
self.fieldNameMap(service), self.recordTypeSchemas(service),
)
expected = u"({attribute}={expected})".format(
attribute=u"fullNames",
expected=(
u"\\5Cxyzzy: a\\2Fb\\2F\\28c\\29\\2A "
"\\7E\\7E \\3E\\3D\\3C \\7E\\7E \\26\\7C \\00!!"
)
)
self.assertEquals(queryString, expected)
def test_queryStringFromMatchExpression_unknownFieldName(self):
"""
Unknown expression.
"""
service = self.service()
expression = MatchExpression(
UnknownConstant.unknown, u"xyzzy",
)
self.assertRaises(
QueryNotSupportedError,
ldapQueryStringFromMatchExpression,
expression,
self.fieldNameMap(service), self.recordTypeSchemas(service),
)
def test_queryStringFromMatchExpression_unknownMatchType(self):
"""
Unknown expression.
"""
service = self.service()
expression = MatchExpression(
service.fieldName.shortNames, u"xyzzy",
matchType=object()
)
self.assertRaises(
QueryNotSupportedError,
ldapQueryStringFromMatchExpression,
expression,
self.fieldNameMap(service), self.recordTypeSchemas(service),
)
def _test_queryStringFromMatchExpression_multiAttribute(
self, flags, expected
):
service = self.service()
expression = MatchExpression(
service.fieldName.emailAddresses, u"xyzzy",
flags=flags,
)
fieldNameToAttributeMap = {
service.fieldName.emailAddresses: (u"mail", u"alternateMail"),
}
queryString = ldapQueryStringFromMatchExpression(
expression,
fieldNameToAttributeMap, self.recordTypeSchemas(service)
)
self.assertEquals(queryString, expected)
def test_queryStringFromMatchExpression_multipleAttribute(self):
"""
Match expression when the queried field name maps to multiple
attributes.
"""
# We want a match for either attribute.
expected = u"(|(mail=xyzzy)(alternateMail=xyzzy))"
return self._test_queryStringFromMatchExpression_multiAttribute(
MatchFlags.none, expected
)
def test_queryStringFromMatchExpression_multipleAttribute_not(self):
"""
Match expression when the queried field name maps to multiple
attributes and the NOT flag is set.
"""
# We want a NOT match for both attributes.
expected = u"(&(!mail=xyzzy)(!alternateMail=xyzzy))"
return self._test_queryStringFromMatchExpression_multiAttribute(
MatchFlags.NOT, expected
)
def _test_queryStringFromMatchExpression_multiRecordType(
self, flags, expected
):
service = self.service()
recordTypeField = service.fieldName.recordType
expression = MatchExpression(
recordTypeField, service.recordType.user,
flags=flags,
)
fieldNameToAttributeMap = self.fieldNameMap(service)
recordTypeAttr = fieldNameToAttributeMap[recordTypeField][0]
type1 = u"person"
type2 = u"coolPerson"
statusAttr = u"accountStatus"
status1 = u"active"
recordTypeSchemas = {
service.recordType.user: RecordTypeSchema(
relativeDN=NotImplemented, # Don't expect this to be used.
attributes=(
(recordTypeAttr, type1),
(recordTypeAttr, type2),
(statusAttr, status1),
)
)
}
queryString = ldapQueryStringFromMatchExpression(
expression, fieldNameToAttributeMap, recordTypeSchemas
)
self.assertEquals(
queryString,
expected.format(
recordType=recordTypeAttr,
type1=type1,
type2=type2,
accountStatus=statusAttr,
status1=status1,
)
)
def test_queryStringFromMatchExpression_multipleRecordType(self):
"""
Match expression when the queried field name is the record type field,
which maps to multiple attributes.
"""
# We want a match for both values.
expected = (
u"(&({recordType}={type1})"
u"({recordType}={type2})"
u"({accountStatus}={status1}))"
)
return self._test_queryStringFromMatchExpression_multiRecordType(
MatchFlags.none, expected
)
def test_queryStringFromMatchExpression_multipleRecordType_not(self):
"""
Match expression when the queried field name is the record type field,
which maps to multiple attributes and the NOT flag is set.
"""
# We want a NOT match for either value.
expected = (
u"(|(!{recordType}={type1})"
u"(!{recordType}={type2})"
u"(!{accountStatus}={status1}))"
)
return self._test_queryStringFromMatchExpression_multiRecordType(
MatchFlags.NOT, expected
)
def test_queryStringFromCompoundExpression_single(
self, queryFunction=ldapQueryStringFromCompoundExpression
):
"""
Compound expression with a single sub-expression.
Should result in the same query string as just the sub-expression
would.
The Operand shouldn't make any difference here, so we test AND and OR,
expecting the same result.
"""
service = self.service()
for operand in (Operand.AND, Operand.OR):
matchExpression = MatchExpression(
service.fieldName.shortNames, u"xyzzy"
)
compoundExpression = CompoundExpression(
[matchExpression],
operand
)
queryString = queryFunction(
compoundExpression,
self.fieldNameMap(service), self.recordTypeSchemas(service),
)
expected = u"{match}".format(
match=ldapQueryStringFromMatchExpression(
matchExpression,
self.fieldNameMap(service),
self.recordTypeSchemas(service),
)
)
self.assertEquals(queryString, expected)
def test_queryStringFromCompoundExpression_multiple(
self, queryFunction=ldapQueryStringFromCompoundExpression
):
"""
Compound expression with multiple sub-expressions.
The sub-expressions should be grouped with the given operand.
"""
service = self.service()
for (operand, token) in ((Operand.AND, u"&"), (Operand.OR, u"|")):
matchExpression1 = MatchExpression(
service.fieldName.shortNames, u"xyzzy"
)
matchExpression2 = MatchExpression(
service.fieldName.shortNames, u"plugh"
)
compoundExpression = CompoundExpression(
[matchExpression1, matchExpression2],
operand
)
queryString = queryFunction(
compoundExpression,
self.fieldNameMap(service), self.recordTypeSchemas(service),
)
expected = u"({op}{match1}{match2})".format(
op=token,
match1=ldapQueryStringFromMatchExpression(
matchExpression1,
self.fieldNameMap(service),
self.recordTypeSchemas(service),
),
match2=ldapQueryStringFromMatchExpression(
matchExpression2,
self.fieldNameMap(service),
self.recordTypeSchemas(service),
),
)
self.assertEquals(queryString, expected)
def test_queryStringFromExpression_match(self):
"""
Match expression.
"""
service = self.service()
matchExpression = MatchExpression(
service.fieldName.shortNames, u"xyzzy"
)
queryString = ldapQueryStringFromExpression(
matchExpression,
self.fieldNameMap(service), self.recordTypeSchemas(service),
)
expected = ldapQueryStringFromMatchExpression(
matchExpression,
self.fieldNameMap(service), self.recordTypeSchemas(service),
)
self.assertEquals(queryString, expected)
def test_queryStringFromExpression_compound(self):
"""
Compound expression.
"""
self.test_queryStringFromCompoundExpression_single(
queryFunction=ldapQueryStringFromExpression
)
self.test_queryStringFromCompoundExpression_multiple(
queryFunction=ldapQueryStringFromExpression
)
def test_queryStringFromExpression_unknown(self):
"""
Unknown expression.
"""
service = self.service()
self.assertRaises(
QueryNotSupportedError,
ldapQueryStringFromExpression,
object(),
self.fieldNameMap(service), self.recordTypeSchemas(service)
)
|