[test/ldap] fix ldap tests stable
authorDavid Douard <david.douard@logilab.fr>
Mon, 08 Jul 2013 12:41:08 +0200
branchstable
changeset 9142 41fb930dc751
parent 9141 492216093bc3
child 9143 1890e0df68fa
[test/ldap] fix ldap tests - make sure the url is properly updated on database setup (when the test database already exists and the ldap URI has changed) - fix the ldapuser test setup process.
server/test/unittest_ldapsource.py
--- a/server/test/unittest_ldapsource.py	Fri Jul 05 10:40:57 2013 +0200
+++ b/server/test/unittest_ldapsource.py	Mon Jul 08 12:41:08 2013 +0200
@@ -143,16 +143,26 @@
         return self._pull(self.session)
 
     def setup_database(self):
-        if self.test_db_id == 'ldap-feed':
-            with self.session.repo.internal_session(safe=True) as session:
-                session.execute('DELETE Any E WHERE E cw_source S, S name "ldap"')
-                session.commit()
-        if self.test_db_id == 'ldap-feed':
-            src = self.sexecute('CWSource S WHERE S name "ldap"').get_entity(0,0)
-            src.cw_set(config=CONFIG_LDAPFEED)
-        self.session.commit()
+        with self.session.repo.internal_session(safe=True) as session:
+            session.execute('DELETE Any E WHERE E cw_source S, S name "ldap"')
+            session.execute('SET S config %(conf)s, S url %(url)s '
+                            'WHERE S is CWSource, S name "ldap"',
+                            {"conf": CONFIG_LDAPFEED, 'url': URL} )
+            session.commit()
         self.pull()
 
+    def add_ldap_entry(self, dn, mods):
+        """
+        add an LDAP entity
+        """
+        modcmd = ['dn: %s'%dn, 'changetype: add']
+        for key, values in mods.iteritems():
+            if isinstance(values, basestring):
+                values = [values]
+            for value in values:
+                modcmd.append('%s: %s'%(key, value))
+        self._ldapmodify(modcmd)
+
     def delete_ldap_entry(self, dn):
         """
         delete an LDAP entity
@@ -329,9 +339,23 @@
                          'deactivated')
         # check that it doesn't choke
         self.pull()
-        # reset the ldap database
-        self.tearDownClass()
-        self.setUpClass()
+        # reinsert syt
+        self.add_ldap_entry('uid=syt,ou=People,dc=cubicweb,dc=test',
+                            { 'objectClass': ['OpenLDAPperson','posixAccount','top','shadowAccount'],
+                              'cn': 'Sylvain Thenault',
+                              'sn': 'Thenault',
+                              'gidNumber': '1004',
+                              'uid': 'syt',
+                              'homeDirectory': '/home/syt',
+                              'shadowFlag': '134538764',
+                              'uidNumber': '1004',
+                              'givenName': 'Sylvain',
+                              'telephoneNumber': '106',
+                              'displayName': 'sthenault',
+                              'gecos': 'Sylvain Thenault',
+                              'mail': ['sylvain.thenault@logilab.fr','syt@logilab.fr'],
+                              'userPassword': 'syt',
+                             })
         self.pull()
         self.assertEqual(self.execute('Any N WHERE U login "syt", '
                                       'U in_state S, S name N').rows[0][0],
@@ -441,6 +465,18 @@
         # XXX keep it there
         session.execute('CWUser U')
 
+    def setup_database(self):
+        # XXX a traceback may appear in the logs of the test due to
+        # the _init_repo method that may fail to connect to the ldap
+        # source if its URI has changed (from what is stored in the
+        # database). This TB is NOT a failure or so.
+        with self.session.repo.internal_session(safe=True) as session:
+            session.execute('SET S url %(url)s, S config %(conf)s '
+                            'WHERE S is CWSource, S name "ldap"',
+                            {"conf": CONFIG_LDAPUSER, 'url': URL} )
+            session.commit()
+        self.pull()
+
     def assertMetadata(self, entity):
         self.assertEqual(entity.creation_date, None)
         self.assertEqual(entity.modification_date, None)