X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=ldapdb%2Ftests.py;h=6d5c8b5c29e4833d02bfbfdeaabd9ecfd00817d2;hb=089876639ebce60a85a285d7ab076797fa96c20c;hp=396d2180690ecbae054c956517599dbbabc5ab87;hpb=7a4644723e7b31b2ef8cf499fe846740a62cdcf0;p=matthijs%2Fupstream%2Fdjango-ldapdb.git diff --git a/ldapdb/tests.py b/ldapdb/tests.py index 396d218..6d5c8b5 100644 --- a/ldapdb/tests.py +++ b/ldapdb/tests.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # django-ldapdb -# Copyright (C) 2009 Bolloré telecom +# Copyright (C) 2009-2010 Bolloré telecom # See AUTHORS file for a full list of contributors. # # This program is free software: you can redistribute it and/or modify @@ -21,23 +21,90 @@ from django.test import TestCase from django.db.models.sql.where import Constraint, AND, OR +from ldapdb import escape_ldap_filter +from ldapdb.models.fields import CharField, IntegerField, ListField from ldapdb.models.query import WhereNode class WhereTestCase(TestCase): - def test_single(self): + def test_escape(self): + self.assertEquals(escape_ldap_filter('foo*bar'), 'foo\\2abar') + self.assertEquals(escape_ldap_filter('foo(bar'), 'foo\\28bar') + self.assertEquals(escape_ldap_filter('foo)bar'), 'foo\\29bar') + self.assertEquals(escape_ldap_filter('foo\\bar'), 'foo\\5cbar') + self.assertEquals(escape_ldap_filter('foo\\bar*wiz'), 'foo\\5cbar\\2awiz') + + def test_char_field_exact(self): where = WhereNode() - where.add((Constraint("cn", "cn", None), 'exact', "test"), AND) + where.add((Constraint("cn", "cn", CharField()), 'exact', "test"), AND) self.assertEquals(where.as_sql(), "(cn=test)") + where = WhereNode() + where.add((Constraint("cn", "cn", CharField()), 'exact', "(test)"), AND) + self.assertEquals(where.as_sql(), "(cn=\\28test\\29)") + + def test_char_field_in(self): + where = WhereNode() + where.add((Constraint("cn", "cn", CharField()), 'in', ["foo", "bar"]), AND) + self.assertEquals(where.as_sql(), "(|(cn=foo)(cn=bar))") + + where = WhereNode() + where.add((Constraint("cn", "cn", CharField()), 'in', ["(foo)", "(bar)"]), AND) + self.assertEquals(where.as_sql(), "(|(cn=\\28foo\\29)(cn=\\28bar\\29))") + + def test_char_field_startswith(self): + where = WhereNode() + where.add((Constraint("cn", "cn", CharField()), 'startswith', "test"), AND) + self.assertEquals(where.as_sql(), "(cn=test*)") + + where = WhereNode() + where.add((Constraint("cn", "cn", CharField()), 'startswith', "te*st"), AND) + self.assertEquals(where.as_sql(), "(cn=te\\2ast*)") + + def test_char_field_endswith(self): + where = WhereNode() + where.add((Constraint("cn", "cn", CharField()), 'endswith', "test"), AND) + self.assertEquals(where.as_sql(), "(cn=*test)") + + where = WhereNode() + where.add((Constraint("cn", "cn", CharField()), 'endswith', "te*st"), AND) + self.assertEquals(where.as_sql(), "(cn=*te\\2ast)") + + def test_char_field_contains(self): + where = WhereNode() + where.add((Constraint("cn", "cn", CharField()), 'contains', "test"), AND) + self.assertEquals(where.as_sql(), "(cn=*test*)") + + where = WhereNode() + where.add((Constraint("cn", "cn", CharField()), 'contains', "te*st"), AND) + self.assertEquals(where.as_sql(), "(cn=*te\\2ast*)") + + def test_integer_field(self): + where = WhereNode() + where.add((Constraint("uid", "uid", IntegerField()), 'exact', 1), AND) + self.assertEquals(where.as_sql(), "(uid=1)") + + where = WhereNode() + where.add((Constraint("uid", "uid", IntegerField()), 'gte', 1), AND) + self.assertEquals(where.as_sql(), "(uid>=1)") + + where = WhereNode() + where.add((Constraint("uid", "uid", IntegerField()), 'lte', 1), AND) + self.assertEquals(where.as_sql(), "(uid<=1)") + + def test_list_field_contains(self): + where = WhereNode() + where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', 'foouser'), AND) + self.assertEquals(where.as_sql(), "(memberUid=foouser)") + def test_and(self): where = WhereNode() - where.add((Constraint("cn", "cn", None), 'exact', "foo"), AND) - where.add((Constraint("givenName", "givenName", None), 'exact', "bar"), AND) + where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND) + where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), AND) self.assertEquals(where.as_sql(), "(&(cn=foo)(givenName=bar))") def test_or(self): where = WhereNode() - where.add((Constraint("cn", "cn", None), 'exact', "foo"), AND) - where.add((Constraint("givenName", "givenName", None), 'exact', "bar"), OR) + where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND) + where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), OR) self.assertEquals(where.as_sql(), "(|(cn=foo)(givenName=bar))")