[metadata, ldap] make backport of ldap user actually working stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 21 Jul 2011 14:21:14 +0200
branchstable
changeset 7683 a21e24831ae4
parent 7682 07b592a62be3
child 7684 9f8f8096d64b
[metadata, ldap] make backport of ldap user actually working
hooks/metadata.py
server/test/unittest_ldapuser.py
--- a/hooks/metadata.py	Thu Jul 21 14:20:45 2011 +0200
+++ b/hooks/metadata.py	Thu Jul 21 14:21:14 2011 +0200
@@ -196,6 +196,16 @@
             # copy entity if necessary
             if not oldsource.repo_source.copy_based_source:
                 entity.complete(skip_bytes=False)
+                if not entity.creation_date:
+                    entity.cw_attr_cache['creation_date'] = datetime.now()
+                if not entity.modification_date:
+                    entity.cw_attr_cache['modification_date'] = datetime.now()
+                entity.cw_attr_cache['cwuri'] = u'%s%s' % (self._cw.base_url(), entity.eid)
+                for rschema, attrschema in entity.e_schema.attribute_definitions():
+                    if attrschema == 'Password' and \
+                       rschema.rdef(entity.e_schema, attrschema).cardinality[0] == '1':
+                        from logilab.common.shellutils import generate_password
+                        entity.cw_attr_cache[rschema.type] = generate_password()
                 entity.cw_edited = EditedEntity(entity, **entity.cw_attr_cache)
                 syssource.add_entity(self._cw, entity)
             # we don't want the moved entity to be reimported later.  To
@@ -205,9 +215,8 @@
             # source='system'. External source will then have consider case
             # where `extid2eid` return a negative eid as 'this entity was known
             # but has been moved, ignore it'.
-            self._cw.system_sql('UPDATE entities SET eid=-eid,source=%(source)s '
-                                'WHERE eid=%(eid)s',
-                                {'eid': self.eidfrom, 'source': newsource.name})
+            self._cw.system_sql('UPDATE entities SET eid=-eid WHERE eid=%(eid)s',
+                                {'eid': self.eidfrom})
             attrs = {'type': entity.__regid__, 'eid': entity.eid, 'extid': None,
                      'source': 'system', 'asource': 'system',
                      'mtime': datetime.now()}
--- a/server/test/unittest_ldapuser.py	Thu Jul 21 14:20:45 2011 +0200
+++ b/server/test/unittest_ldapuser.py	Thu Jul 21 14:21:14 2011 +0200
@@ -379,9 +379,11 @@
         self.assertEqual(rset.rows, [[None]])
 
     def test_copy_to_system_source(self):
+        source = self.repo.sources_by_uri['ldapuser']
         eid = self.sexecute('CWUser X WHERE X login %(login)s', {'login': SYT})[0][0]
         self.sexecute('SET X cw_source S WHERE X eid %(x)s, S name "system"', {'x': eid})
         self.commit()
+        source.reset_caches()
         rset = self.sexecute('CWUser X WHERE X login %(login)s', {'login': SYT})
         self.assertEqual(len(rset), 1)
         e = rset.get_entity(0, 0)
@@ -390,7 +392,9 @@
                                                   'type': 'CWUser',
                                                   'extid': None})
         self.assertEqual(e.cw_source[0].name, 'system')
-        source = self.repo.sources_by_uri['ldapuser']
+        self.failUnless(e.creation_date)
+        self.failUnless(e.modification_date)
+        # XXX test some password has been set
         source.synchronize()
         rset = self.sexecute('CWUser X WHERE X login %(login)s', {'login': SYT})
         self.assertEqual(len(rset), 1)