1 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009-2011, Bolloré telecom
7 # See AUTHORS file for a full list of contributors.
9 # Redistribution and use in source and binary forms, with or without modification,
10 # are permitted provided that the following conditions are met:
12 # 1. Redistributions of source code must retain the above copyright notice,
13 # this list of conditions and the following disclaimer.
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.
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.
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.
35 from django.test import TestCase
36 from django.db.models.sql.where import Constraint, AND, OR
38 from ldapdb import escape_ldap_filter
39 from ldapdb.backends.ldap.compiler import where_as_ldap
40 from ldapdb.models.fields import CharField, IntegerField, ListField
41 from ldapdb.models.query import WhereNode
43 class WhereTestCase(TestCase):
44 def test_escape(self):
45 self.assertEquals(escape_ldap_filter(u'fôöbàr'), u'fôöbàr')
46 self.assertEquals(escape_ldap_filter('foo*bar'), 'foo\\2abar')
47 self.assertEquals(escape_ldap_filter('foo(bar'), 'foo\\28bar')
48 self.assertEquals(escape_ldap_filter('foo)bar'), 'foo\\29bar')
49 self.assertEquals(escape_ldap_filter('foo\\bar'), 'foo\\5cbar')
50 self.assertEquals(escape_ldap_filter('foo\\bar*wiz'), 'foo\\5cbar\\2awiz')
52 def test_char_field_exact(self):
54 where.add((Constraint("cn", "cn", CharField()), 'exact', "test"), AND)
55 self.assertEquals(where_as_ldap(where), ("(cn=test)", []))
58 where.add((Constraint("cn", "cn", CharField()), 'exact', "(test)"), AND)
59 self.assertEquals(where_as_ldap(where), ("(cn=\\28test\\29)", []))
61 def test_char_field_in(self):
63 where.add((Constraint("cn", "cn", CharField()), 'in', ["foo", "bar"]), AND)
64 self.assertEquals(where_as_ldap(where), ("(|(cn=foo)(cn=bar))", []))
67 where.add((Constraint("cn", "cn", CharField()), 'in', ["(foo)", "(bar)"]), AND)
68 self.assertEquals(where_as_ldap(where), ("(|(cn=\\28foo\\29)(cn=\\28bar\\29))", []))
70 def test_char_field_startswith(self):
72 where.add((Constraint("cn", "cn", CharField()), 'startswith', "test"), AND)
73 self.assertEquals(where_as_ldap(where), ("(cn=test*)", []))
76 where.add((Constraint("cn", "cn", CharField()), 'startswith', "te*st"), AND)
77 self.assertEquals(where_as_ldap(where), ("(cn=te\\2ast*)", []))
79 def test_char_field_endswith(self):
81 where.add((Constraint("cn", "cn", CharField()), 'endswith', "test"), AND)
82 self.assertEquals(where_as_ldap(where), ("(cn=*test)", []))
85 where.add((Constraint("cn", "cn", CharField()), 'endswith', "te*st"), AND)
86 self.assertEquals(where_as_ldap(where), ("(cn=*te\\2ast)", []))
88 def test_char_field_contains(self):
90 where.add((Constraint("cn", "cn", CharField()), 'contains', "test"), AND)
91 self.assertEquals(where_as_ldap(where), ("(cn=*test*)", []))
94 where.add((Constraint("cn", "cn", CharField()), 'contains', "te*st"), AND)
95 self.assertEquals(where_as_ldap(where), ("(cn=*te\\2ast*)", []))
97 def test_integer_field(self):
99 where.add((Constraint("uid", "uid", IntegerField()), 'exact', 1), AND)
100 self.assertEquals(where_as_ldap(where), ("(uid=1)", []))
103 where.add((Constraint("uid", "uid", IntegerField()), 'gte', 1), AND)
104 self.assertEquals(where_as_ldap(where), ("(uid>=1)", []))
107 where.add((Constraint("uid", "uid", IntegerField()), 'lte', 1), AND)
108 self.assertEquals(where_as_ldap(where), ("(uid<=1)", []))
110 def test_list_field_contains(self):
112 where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', 'foouser'), AND)
113 self.assertEquals(where_as_ldap(where), ("(memberUid=foouser)", []))
116 where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', '(foouser)'), AND)
117 self.assertEquals(where_as_ldap(where), ("(memberUid=\\28foouser\\29)", []))
121 where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
122 where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), AND)
123 self.assertEquals(where_as_ldap(where), ("(&(cn=foo)(givenName=bar))", []))
127 where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
128 where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), OR)
129 self.assertEquals(where_as_ldap(where), ("(|(cn=foo)(givenName=bar))", []))