[Checkins] SVN: zope.security/trunk/ Coverage for z.s.checker.canWrite.

Tres Seaver cvs-admin at zope.org
Mon Dec 24 20:25:30 UTC 2012


Log message for revision 128900:
  Coverage for z.s.checker.canWrite.

Changed:
  _U  zope.security/trunk/
  U   zope.security/trunk/src/zope/security/checker.py
  U   zope.security/trunk/src/zope/security/tests/test_checker.py

-=-
Modified: zope.security/trunk/src/zope/security/checker.py
===================================================================
--- zope.security/trunk/src/zope/security/checker.py	2012-12-24 20:25:29 UTC (rev 128899)
+++ zope.security/trunk/src/zope/security/checker.py	2012-12-24 20:25:29 UTC (rev 128900)
@@ -52,11 +52,11 @@
 
 try:
     from zope.exceptions import DuplicationError
-except ImportError:
+except ImportError: #pragma NO COVER
     class DuplicationError(Exception):
         """A duplicate registration was attempted"""
 
-if os.environ.get('ZOPE_WATCH_CHECKERS'):
+if os.environ.get('ZOPE_WATCH_CHECKERS'): #pragma NO COVER
     try:
         WATCH_CHECKERS = int(os.environ.get('ZOPE_WATCH_CHECKERS'))
     except ValueError:

Modified: zope.security/trunk/src/zope/security/tests/test_checker.py
===================================================================
--- zope.security/trunk/src/zope/security/tests/test_checker.py	2012-12-24 20:25:29 UTC (rev 128899)
+++ zope.security/trunk/src/zope/security/tests/test_checker.py	2012-12-24 20:25:29 UTC (rev 128900)
@@ -17,7 +17,7 @@
 
 
 
-class ProxyFactoryTests(unittest.TestCase):
+class Test_ProxyFactory(unittest.TestCase):
 
     def _callFUT(self, object, checker=None):
         from zope.security.checker import ProxyFactory
@@ -103,6 +103,60 @@
             _clear()
 
 
+class Test_canWrite(unittest.TestCase):
+
+    def _callFUT(self, obj, name):
+        from zope.security.checker import canWrite
+        return canWrite(obj, name)
+
+    def _makeChecker(self, ch_get=None, ch_set=None):
+        class _Checker(object):
+            def check_getattr(self, obj, name):
+                if ch_get is not None:
+                    raise ch_get()
+            def check_setattr(self, obj, name):
+                if ch_set is not None:
+                    raise ch_set()
+        return _Checker()
+
+    def test_ok(self):
+        from zope.security._proxy import _Proxy as Proxy
+        obj = object()
+        proxy = Proxy(obj, self._makeChecker())
+        self.assertTrue(self._callFUT(proxy, 'whatever'))
+
+    def test_w_setattr_unauth(self):
+        from zope.security.interfaces import Unauthorized
+        from zope.security._proxy import _Proxy as Proxy
+        obj = object()
+        proxy = Proxy(obj, self._makeChecker(ch_set=Unauthorized))
+        self.assertFalse(self._callFUT(proxy, 'whatever'))
+
+    def test_w_setattr_forbidden_getattr_allowed(self):
+        from zope.security.interfaces import ForbiddenAttribute
+        from zope.security._proxy import _Proxy as Proxy
+        obj = object()
+        proxy = Proxy(obj, self._makeChecker(ch_set=ForbiddenAttribute))
+        self.assertFalse(self._callFUT(proxy, 'whatever'))
+
+    def test_w_setattr_forbidden_getattr_unauth(self):
+        from zope.security.interfaces import ForbiddenAttribute
+        from zope.security.interfaces import Unauthorized
+        from zope.security._proxy import _Proxy as Proxy
+        obj = object()
+        proxy = Proxy(obj, self._makeChecker(ch_get=Unauthorized,
+                                             ch_set=ForbiddenAttribute))
+        self.assertFalse(self._callFUT(proxy, 'whatever'))
+
+    def test_w_setattr_forbidden_getattr_forbidden(self):
+        from zope.security.interfaces import ForbiddenAttribute
+        from zope.security._proxy import _Proxy as Proxy
+        obj = object()
+        proxy = Proxy(obj, self._makeChecker(ch_get=ForbiddenAttribute,
+                                             ch_set=ForbiddenAttribute))
+        self.assertRaises(ForbiddenAttribute, self._callFUT, proxy, 'whatever')
+
+
 class Test(unittest.TestCase):
 
     def setUp(self):
@@ -761,7 +815,8 @@
 
 def test_suite():
     return unittest.TestSuite((
-        unittest.makeSuite(ProxyFactoryTests),
+        unittest.makeSuite(Test_ProxyFactory),
+        unittest.makeSuite(Test_canWrite),
         unittest.makeSuite(Test),
         unittest.makeSuite(TestCheckerPublic),
         unittest.makeSuite(TestCombinedChecker),



More information about the checkins mailing list