create / drop LDAP in tests
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 31 May 2010 11:52:10 +0000 (11:52 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 31 May 2010 11:52:10 +0000 (11:52 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@880 e071eeec-0327-468d-9b6a-08194a12b294

examples/tests.py

index a8e3a3e178fce413c719ddce9320bfa9b9e0da60..38a122ab05d2c95a0b3996efb957cf76673d179d 100644 (file)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+import ldap
+
 from django.test import TestCase
 
+from ldapdb import connection
 from examples.models import LdapUser, LdapGroup
 
-class GroupTestCase(TestCase):
+class BaseTestCase(TestCase):
+    def setUp(self):
+        cursor = connection._cursor()
+        for base in [LdapGroup.base_dn, LdapUser.base_dn]:
+            ou = base.split(',')[0].split('=')[1]
+            attrs = [('objectClass', ['top', 'organizationalUnit']), ('ou', [ou])]
+            try:
+                cursor.connection.add_s(base, attrs)
+            except ldap.ALREADY_EXISTS:
+                pass
+
+    def tearDown(self):
+        cursor = connection._cursor()
+        for base in [LdapGroup.base_dn, LdapUser.base_dn]:
+            results = cursor.connection.search_s(base, ldap.SCOPE_SUBTREE)
+            for dn, attrs in reversed(results):
+                cursor.connection.delete_s(dn)
+
+class GroupTestCase(BaseTestCase):
     def test_create(self):
         g = LdapGroup()
         g.name = "foogroup"
@@ -30,7 +51,7 @@ class GroupTestCase(TestCase):
         g.usernames = ['foouser']
         g.save()
  
-class UserTestCase(TestCase):
+class UserTestCase(BaseTestCase):
     def test_create(self):
         u = LdapUser()
         u.first_name = "Foo"