[hooks] Add a bunch of tests for session synchronization
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 06 Jun 2016 15:18:12 +0200
changeset 11350 de466349b742
parent 11349 865de6759a53
child 11351 97882e99138d
[hooks] Add a bunch of tests for session synchronization And change unicode into six.text_type in authobjs.py spotted by new tests.
cubicweb/entities/authobjs.py
cubicweb/hooks/test/unittest_syncsession.py
--- a/cubicweb/entities/authobjs.py	Mon Jun 06 15:17:23 2016 +0200
+++ b/cubicweb/entities/authobjs.py	Mon Jun 06 15:18:12 2016 +0200
@@ -19,7 +19,7 @@
 
 __docformat__ = "restructuredtext en"
 
-from six import string_types
+from six import string_types, text_type
 
 from logilab.common.decorators import cached
 
@@ -109,7 +109,7 @@
         return self._cw.vreg.property_value(key)
 
     def set_property(self, pkey, value):
-        value = unicode(value)
+        value = text_type(value)
         try:
             prop = self._cw.execute(
                 'CWProperty X WHERE X pkey %(k)s, X for_user U, U eid %(u)s',
--- a/cubicweb/hooks/test/unittest_syncsession.py	Mon Jun 06 15:17:23 2016 +0200
+++ b/cubicweb/hooks/test/unittest_syncsession.py	Mon Jun 06 15:18:12 2016 +0200
@@ -80,7 +80,56 @@
             cnx.commit()
         self.assertEqual(self.vreg.property_value('test.int'), 42)
 
+    def test_sync_user_props(self):
+        with self.admin_access.client_cnx() as cnx:
+            self.assertNotIn('ui.language', cnx.user.properties)
+            cnx.user.set_property(u'ui.language', u'fr')
+            self.assertNotIn('ui.language', cnx.user.properties)
+            cnx.commit()
+            self.assertEqual(cnx.user.properties['ui.language'], 'fr')
+            cnx.user.set_property(u'ui.language', u'en')
+            self.assertEqual(cnx.user.properties['ui.language'], 'fr')
+            cnx.commit()
+            self.assertEqual(cnx.user.properties['ui.language'], 'en')
+            cnx.execute('DELETE CWProperty X WHERE X for_user U, U eid %(u)s',
+                        {'u': cnx.user.eid})
+            self.assertEqual(cnx.user.properties['ui.language'], 'en')
+            cnx.commit()
+            self.assertNotIn('ui.language', cnx.user.properties)
+
+    def test_sync_sitewide_props(self):
+        with self.admin_access.client_cnx() as cnx:
+            self.assertNotIn('ui.language', cnx.vreg['propertyvalues'])
+            cwprop = cnx.create_entity('CWProperty', pkey=u'ui.language', value=u'fr')
+            self.assertNotIn('ui.language', cnx.vreg['propertyvalues'])
+            cnx.commit()
+            self.assertEqual(cnx.vreg['propertyvalues']['ui.language'], 'fr')
+            cwprop.cw_set(value=u'en')
+            self.assertEqual(cnx.vreg['propertyvalues']['ui.language'], 'fr')
+            cnx.commit()
+            self.assertEqual(cnx.vreg['propertyvalues']['ui.language'], 'en')
+            cwprop.cw_delete()
+            self.assertEqual(cnx.vreg['propertyvalues']['ui.language'], 'en')
+            cnx.commit()
+            self.assertNotIn('ui.language', cnx.vreg['propertyvalues'])
+
+
+class UserGroupsSyncTC(CubicWebTC):
+
+    def test_sync_groups(self):
+        with self.admin_access.client_cnx() as cnx:
+            cnx.execute('SET U in_group G WHERE G name "users", U eid %(u)s',
+                        {'u': cnx.user.eid})
+            self.assertEqual(cnx.user.groups, set(['managers']))
+            cnx.commit()
+            self.assertEqual(cnx.user.groups, set(['managers', 'users']))
+            cnx.execute('DELETE U in_group G WHERE G name "users", U eid %(u)s',
+                        {'u': cnx.user.eid})
+            self.assertEqual(cnx.user.groups, set(['managers', 'users']))
+            cnx.commit()
+            self.assertEqual(cnx.user.groups, set(['managers']))
+
 
 if __name__ == '__main__':
-    from logilab.common.testlib import unittest_main
-    unittest_main()
+    import unittest
+    unittest.main()