From: jlaine Date: Mon, 11 Apr 2011 14:04:02 +0000 (+0000) Subject: refactor LDAP access in tests X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=10c1a45bf11b3718847f6796f7ec0eb771fe667f;p=matthijs%2Fupstream%2Fdjango-ldapdb.git refactor LDAP access in tests git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@1040 e071eeec-0327-468d-9b6a-08194a12b294 --- diff --git a/examples/tests.py b/examples/tests.py index 65f051b..e8f386e 100644 --- a/examples/tests.py +++ b/examples/tests.py @@ -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()