refactor LDAP access in tests
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 11 Apr 2011 14:04:02 +0000 (14:04 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 11 Apr 2011 14:04:02 +0000 (14:04 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@1040 e071eeec-0327-468d-9b6a-08194a12b294

examples/tests.py

index 65f051b80b20cbc2d99e4c8fd50d31605212b470..e8f386e037fa6ec674f6004da9230965869245c6 100644 (file)
@@ -37,35 +37,41 @@ from django.db.models import Q
 from django.test import TestCase
 
 import ldap
-import ldapdb
+
 from ldapdb.backends.ldap.compiler import query_as_ldap
 from examples.models import LdapUser, LdapGroup
-      
+
 class BaseTestCase(TestCase):
+    def _add_base_dn(self, model):
+        using = router.db_for_write(model)
+        connection = connections[using]
+
+        rdn = model.base_dn.split(',')[0]
+        key, val = rdn.split('=')
+        attrs = [('objectClass', ['top', 'organizationalUnit']), (key, [val])]
+        try:
+            connection.add_s(model.base_dn, attrs)
+        except ldap.ALREADY_EXISTS:
+            pass
+
+    def _remove_base_dn(self, model):
+        using = router.db_for_write(model)
+        connection = connections[using]
+
+        try:
+            results = connection.search_s(model.base_dn, ldap.SCOPE_SUBTREE)
+            for dn, attrs in reversed(results):
+                connection.delete_s(dn)
+        except ldap.NO_SUCH_OBJECT:
+            pass
+
     def setUp(self):
         for model in [LdapGroup, LdapUser]:
-            using = router.db_for_write(model)
-            connection = connections[using]
-
-            rdn = model.base_dn.split(',')[0]
-            key, val = rdn.split('=')
-            attrs = [('objectClass', ['top', 'organizationalUnit']), (key, [val])]
-            try:
-                connection.add_s(model.base_dn, attrs)
-            except ldap.ALREADY_EXISTS:
-                pass
+            self._add_base_dn(model)
 
     def tearDown(self):
         for model in [LdapGroup, LdapUser]:
-            using = router.db_for_write(model)
-            connection = connections[using]
-
-            try:
-                results = connection.search_s(model.base_dn, ldap.SCOPE_SUBTREE)
-                for dn, attrs in reversed(results):
-                    connection.delete_s(dn)
-            except ldap.NO_SUCH_OBJECT:
-                pass
+            self._remove_base_dn(model)
 
 class GroupTestCase(BaseTestCase):
     def setUp(self):
@@ -316,12 +322,15 @@ class ScopedTestCase(BaseTestCase):
     def setUp(self):
         super(ScopedTestCase, self).setUp()
 
-        self.scoped_dn = "ou=contacts,%s" % LdapGroup.base_dn
-        attrs = [('objectClass', ['top', 'organizationalUnit']), ("ou", ["contacts"])]
-        ldapdb.connection.add_s(self.scoped_dn, attrs)
+        self.scoped_model = LdapGroup.scoped("ou=contacts,%s" % LdapGroup.base_dn)
+        self._add_base_dn(self.scoped_model)
+
+    def tearDown(self):
+        self._remove_base_dn(self.scoped_model)
+        super(ScopedTestCase, self).tearDown()
 
     def test_scope(self):
-        ScopedGroup = LdapGroup.scoped(self.scoped_dn)
+        ScopedGroup = self.scoped_model
 
         # create group
         g = LdapGroup()