[devtools] Fix CubicWebTC.temporary_permissions
authorRémi Cardona <remi.cardona@logilab.fr>
Thu, 08 Oct 2015 18:24:09 +0200
changeset 10639 1ed205146ee2
parent 10638 243e96db0004
child 10640 17bdc485c5b2
[devtools] Fix CubicWebTC.temporary_permissions If the context manager is exited via an exception, the original permissions should be restored.
devtools/test/unittest_testlib.py
devtools/testlib.py
--- a/devtools/test/unittest_testlib.py	Thu Oct 08 18:09:10 2015 +0200
+++ b/devtools/test/unittest_testlib.py	Thu Oct 08 18:24:09 2015 +0200
@@ -196,6 +196,18 @@
             self.assertTrue(rdef.permissions['add'])
         self.assertTrue(rdef.permissions['read'], ())
 
+    def test_temporary_permissions_rdef_with_exception(self):
+        rdef = self.schema['CWUser'].rdef('in_group')
+        try:
+            with self.temporary_permissions((rdef, {'read': ()})):
+                self.assertEqual(rdef.permissions['read'], ())
+                self.assertTrue(rdef.permissions['add'])
+                raise ValueError('goto')
+        except ValueError:
+            self.assertTrue(rdef.permissions['read'], ())
+        else:
+            self.fail('exception was caught unexpectedly')
+
     def test_temporary_appobjects_registered(self):
 
         class AnAppobject(object):
--- a/devtools/testlib.py	Thu Oct 08 18:09:10 2015 +0200
+++ b/devtools/testlib.py	Thu Oct 08 18:24:09 2015 +0200
@@ -719,12 +719,14 @@
                 origperms = erschema.permissions[action]
                 erschema.set_action_permissions(action, actionperms)
                 torestore.append([erschema, action, origperms])
-        yield
-        for erschema, action, permissions in torestore:
-            if action is None:
-                erschema.permissions = permissions
-            else:
-                erschema.set_action_permissions(action, permissions)
+        try:
+            yield
+        finally:
+            for erschema, action, permissions in torestore:
+                if action is None:
+                    erschema.permissions = permissions
+                else:
+                    erschema.set_action_permissions(action, permissions)
 
     def assertModificationDateGreater(self, entity, olddate):
         entity.cw_attr_cache.pop('modification_date', None)