[Zope3-checkins] CVS: Zope3/src/zope/security/tests - test_checker.py:1.7.10.2

Steve Alexander steve@cat-box.net
Wed, 14 May 2003 09:54:13 -0400


Update of /cvs-repository/Zope3/src/zope/security/tests
In directory cvs.zope.org:/tmp/cvs-serv32758/src/zope/security/tests

Modified Files:
      Tag: stevea-decorators-branch
	test_checker.py 
Log Message:
Wrote tests for ProxyFactory (there were none), and made its semantics
clearer.
Implemented more decorator factory functionality.


=== Zope3/src/zope/security/tests/test_checker.py 1.7.10.1 => 1.7.10.2 ===
--- Zope3/src/zope/security/tests/test_checker.py:1.7.10.1	Wed May 14 06:39:05 2003
+++ Zope3/src/zope/security/tests/test_checker.py	Wed May 14 09:54:13 2003
@@ -26,6 +26,7 @@
 from zope.security.management import setSecurityPolicy
 from zope.security.proxy import getChecker, getObject
 from zope.security.checker import defineChecker, ProxyFactory
+from zope.security.proxy import Proxy
 import types, pickle
 
 __metaclass__ = type
@@ -319,6 +320,55 @@
     # Checker.check/check_setattr handle permission
     # values that evaluate to False
 
+    def test_ProxyFactory(self):
+        class SomeClass:
+            pass
+        import zope.security
+        checker = NamesChecker()
+        specific_checker = NamesChecker()
+        checker_as_magic_attr = NamesChecker()
+
+        obj = SomeClass()
+
+        proxy = ProxyFactory(obj)
+        self.assert_(type(proxy) is Proxy)
+        from zope.security.checker import _defaultChecker
+        self.assert_(getChecker(proxy) is _defaultChecker)
+
+        defineChecker(SomeClass, checker)
+
+        proxy = ProxyFactory(obj)
+        self.assert_(type(proxy) is Proxy)
+        self.assert_(getChecker(proxy) is checker)
+
+        obj.__Security_checker__ = checker_as_magic_attr
+
+        proxy = ProxyFactory(obj)
+        self.assert_(type(proxy) is Proxy)
+        self.assert_(getChecker(proxy) is checker_as_magic_attr)
+
+        proxy = ProxyFactory(obj, specific_checker)
+        self.assert_(type(proxy) is Proxy)
+        self.assert_(getChecker(proxy) is specific_checker)
+
+    def test_ProxyFactory_using_proxy(self):
+        class SomeClass:
+            pass
+        obj = SomeClass()
+        checker = NamesChecker()
+        proxy1 = ProxyFactory(obj)
+
+        proxy2 = ProxyFactory(proxy1)
+        self.assert_(proxy1 is proxy2)
+
+        # Trying to change the checker on a proxy.
+        self.assertRaises(TypeError, ProxyFactory, proxy1, checker)
+
+        # Setting exactly the same checker as the proxy already has.
+        proxy1 = ProxyFactory(obj, checker)
+        proxy2 = ProxyFactory(proxy1, checker)
+        self.assert_(proxy1 is proxy2)
+
 
 class TestCheckerPublic(TestCase):
 
@@ -331,38 +381,63 @@
         self.assert_(ProxyFactory(CheckerPublic) is CheckerPublic)
 
 
-class TestDecoratedChecker(TestCase):
+class TestMixinDecoratedChecker(TestCase):
 
-    def setUp(self):
-        TestCase.setUp(self)
+    def decoratedSetUp(self):
         self.policy = RecordedSecurityPolicy()
-        self.__oldpolicy = setSecurityPolicy(self.policy)
+        self._oldpolicy = setSecurityPolicy(self.policy)
 
-    def tearDown(self):
-        setSecurityPolicy(self.__oldpolicy)
-        TestCase.tearDown(self)
+    def decoratedTearDown(self):
+        setSecurityPolicy(self._oldpolicy)
 
-    def test_checking(self):
-        from zope.security.checker import DecoratedChecker
-        c = NamesChecker(['foo', 'bar', '__str__'], 'baseperm')
-        dc = DecoratedChecker(c, {'foo': 'perm1'}, {'foo': 'perm2'})
+    def checkingTestImpl(self, checker, decoratedchecker):
+        c = checker
+        dc = decoratedchecker
 
         o = object()
-        dc.check_getattr(o, 'foo')
-        self.assert_(self.policy.checkChecked(['perm1']))
-        dc.check_getattr(o, 'bar')
-        self.assert_(self.policy.checkChecked(['baseperm']))
-        self.assertRaises(ForbiddenAttribute, dc.check_getattr, o, 'baz')
+        dc.check_getattr(o, 'both_get_set')
+        self.assert_(self.policy.checkChecked(['dc_get_permission']))
+        dc.check_getattr(o, 'c_only')
+        self.assert_(self.policy.checkChecked(['get_permission']))
+        dc.check_getattr(o, 'd_only')
+        self.assert_(self.policy.checkChecked(['dc_get_permission']))
+        self.assertRaises(ForbiddenAttribute,
+                          dc.check_getattr, o, 'completely_different_attr')
         self.assert_(self.policy.checkChecked([]))
         dc.check(o, '__str__')
-        self.assert_(self.policy.checkChecked(['baseperm']))
+        self.assert_(self.policy.checkChecked(['get_permission']))
 
-        dc.check_setattr(o, 'foo')
-        self.assert_(self.policy.checkChecked(['perm2']))
-        self.assertRaises(ForbiddenAttribute, dc.check_setattr, o, 'bar')
+        dc.check_setattr(o, 'both_get_set')
+        self.assert_(self.policy.checkChecked(['dc_set_permission']))
+        self.assertRaises(ForbiddenAttribute, dc.check_setattr, o, 'c_only')
         self.assert_(self.policy.checkChecked([]))
-        self.assertRaises(ForbiddenAttribute, dc.check_setattr, o, 'baz')
+        self.assertRaises(ForbiddenAttribute, dc.check_setattr, o, 'd_only')
         self.assert_(self.policy.checkChecked([]))
+
+    originalChecker = NamesChecker(['both_get_set', 'c_only', '__str__'],
+                                   'get_permission')
+
+    decorationSetMap = {'both_get_set': 'dc_set_permission'}
+
+    decorationGetMap = {'both_get_set': 'dc_get_permission',
+                        'd_only': 'dc_get_permission'}
+
+
+class TestDecoratedChecker(TestMixinDecoratedChecker, TestCase):
+
+    def setUp(self):
+        TestCase.setUp(self)
+        self.decoratedSetUp()
+
+    def tearDown(self):
+        self.decoratedTearDown()
+        TestCase.tearDown(self)
+
+    def test_checking(self):
+        from zope.security.checker import DecoratedChecker
+        c = self.originalChecker
+        dc = DecoratedChecker(c, self.decorationGetMap, self.decorationSetMap)
+        self.checkingTestImpl(c, dc)
 
 
 def test_suite():