tests.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. #
  3. # django-ldapdb
  4. # Copyright (c) 2009-2011, Bolloré telecom
  5. # All rights reserved.
  6. #
  7. # See AUTHORS file for a full list of contributors.
  8. #
  9. # Redistribution and use in source and binary forms, with or without modification,
  10. # are permitted provided that the following conditions are met:
  11. #
  12. # 1. Redistributions of source code must retain the above copyright notice,
  13. # this list of conditions and the following disclaimer.
  14. #
  15. # 2. Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in the
  17. # documentation and/or other materials provided with the distribution.
  18. #
  19. # 3. Neither the name of Bolloré telecom nor the names of its contributors
  20. # may be used to endorse or promote products derived from this software
  21. # without specific prior written permission.
  22. #
  23. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  27. # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #
  34. from django.test import TestCase
  35. from django.db.models.sql.where import Constraint, AND, OR, WhereNode
  36. from ldapdb import escape_ldap_filter
  37. from ldapdb.backends.ldap.compiler import where_as_ldap
  38. from ldapdb.models.fields import CharField, IntegerField, ListField
  39. class WhereTestCase(TestCase):
  40. def test_escape(self):
  41. self.assertEquals(escape_ldap_filter(u'fôöbàr'), u'fôöbàr')
  42. self.assertEquals(escape_ldap_filter('foo*bar'), 'foo\\2abar')
  43. self.assertEquals(escape_ldap_filter('foo(bar'), 'foo\\28bar')
  44. self.assertEquals(escape_ldap_filter('foo)bar'), 'foo\\29bar')
  45. self.assertEquals(escape_ldap_filter('foo\\bar'), 'foo\\5cbar')
  46. self.assertEquals(escape_ldap_filter('foo\\bar*wiz'), 'foo\\5cbar\\2awiz')
  47. def test_char_field_exact(self):
  48. where = WhereNode()
  49. where.add((Constraint("cn", "cn", CharField()), 'exact', "test"), AND)
  50. self.assertEquals(where_as_ldap(where), ("(cn=test)", []))
  51. where = WhereNode()
  52. where.add((Constraint("cn", "cn", CharField()), 'exact', "(test)"), AND)
  53. self.assertEquals(where_as_ldap(where), ("(cn=\\28test\\29)", []))
  54. def test_char_field_in(self):
  55. where = WhereNode()
  56. where.add((Constraint("cn", "cn", CharField()), 'in', ["foo", "bar"]), AND)
  57. self.assertEquals(where_as_ldap(where), ("(|(cn=foo)(cn=bar))", []))
  58. where = WhereNode()
  59. where.add((Constraint("cn", "cn", CharField()), 'in', ["(foo)", "(bar)"]), AND)
  60. self.assertEquals(where_as_ldap(where), ("(|(cn=\\28foo\\29)(cn=\\28bar\\29))", []))
  61. def test_char_field_startswith(self):
  62. where = WhereNode()
  63. where.add((Constraint("cn", "cn", CharField()), 'startswith', "test"), AND)
  64. self.assertEquals(where_as_ldap(where), ("(cn=test*)", []))
  65. where = WhereNode()
  66. where.add((Constraint("cn", "cn", CharField()), 'startswith', "te*st"), AND)
  67. self.assertEquals(where_as_ldap(where), ("(cn=te\\2ast*)", []))
  68. def test_char_field_endswith(self):
  69. where = WhereNode()
  70. where.add((Constraint("cn", "cn", CharField()), 'endswith', "test"), AND)
  71. self.assertEquals(where_as_ldap(where), ("(cn=*test)", []))
  72. where = WhereNode()
  73. where.add((Constraint("cn", "cn", CharField()), 'endswith', "te*st"), AND)
  74. self.assertEquals(where_as_ldap(where), ("(cn=*te\\2ast)", []))
  75. def test_char_field_contains(self):
  76. where = WhereNode()
  77. where.add((Constraint("cn", "cn", CharField()), 'contains', "test"), AND)
  78. self.assertEquals(where_as_ldap(where), ("(cn=*test*)", []))
  79. where = WhereNode()
  80. where.add((Constraint("cn", "cn", CharField()), 'contains', "te*st"), AND)
  81. self.assertEquals(where_as_ldap(where), ("(cn=*te\\2ast*)", []))
  82. def test_integer_field(self):
  83. where = WhereNode()
  84. where.add((Constraint("uid", "uid", IntegerField()), 'exact', 1), AND)
  85. self.assertEquals(where_as_ldap(where), ("(uid=1)", []))
  86. where = WhereNode()
  87. where.add((Constraint("uid", "uid", IntegerField()), 'gte', 1), AND)
  88. self.assertEquals(where_as_ldap(where), ("(uid>=1)", []))
  89. where = WhereNode()
  90. where.add((Constraint("uid", "uid", IntegerField()), 'lte', 1), AND)
  91. self.assertEquals(where_as_ldap(where), ("(uid<=1)", []))
  92. def test_list_field_contains(self):
  93. where = WhereNode()
  94. where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', 'foouser'), AND)
  95. self.assertEquals(where_as_ldap(where), ("(memberUid=foouser)", []))
  96. where = WhereNode()
  97. where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', '(foouser)'), AND)
  98. self.assertEquals(where_as_ldap(where), ("(memberUid=\\28foouser\\29)", []))
  99. def test_and(self):
  100. where = WhereNode()
  101. where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
  102. where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), AND)
  103. self.assertEquals(where_as_ldap(where), ("(&(cn=foo)(givenName=bar))", []))
  104. def test_or(self):
  105. where = WhereNode()
  106. where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
  107. where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), OR)
  108. self.assertEquals(where_as_ldap(where), ("(|(cn=foo)(givenName=bar))", []))