summaryrefslogtreecommitdiff
path: root/src/zope/security/tests
diff options
context:
space:
mode:
authorJason Madden <jamadden@gmail.com>2017-09-12 09:29:19 -0500
committerJason Madden <jamadden@gmail.com>2017-09-12 09:29:19 -0500
commitb5f2d262639d8d1d24e09e4c275c7f84435f000d (patch)
tree4fe437417e87a658adabef5bcefd7be26d964964 /src/zope/security/tests
parentb710673d9b05c9aab3100c2a2d8dc97c083c87f6 (diff)
downloadzope-security-b5f2d262639d8d1d24e09e4c275c7f84435f000d.tar.gz
100% coverage for proxy.py
- The implementation of __getattribute__/__getattr__ now behaves like C and will not call a target's version of those functions more than once if they raise an AttributeError.
Diffstat (limited to 'src/zope/security/tests')
-rw-r--r--src/zope/security/tests/test_proxy.py63
1 files changed, 60 insertions, 3 deletions
diff --git a/src/zope/security/tests/test_proxy.py b/src/zope/security/tests/test_proxy.py
index 077023f..8f52e17 100644
--- a/src/zope/security/tests/test_proxy.py
+++ b/src/zope/security/tests/test_proxy.py
@@ -57,6 +57,7 @@ class AbstractProxyTestBase(object):
checker = DummyChecker()
proxy = self._makeOne(target, checker)
self.assertEqual(proxy.bar, 'Bar')
+ self.assertEqual(getattr(proxy, 'bar'), 'Bar')
self.assertEqual(checker._checked, 'bar')
self.assertEqual(checker._proxied, 'Bar')
@@ -77,9 +78,29 @@ class AbstractProxyTestBase(object):
target = Foo()
checker = DummyChecker(ForbiddenAttribute)
proxy = self._makeOne(target, checker)
- self.assertRaises(ForbiddenAttribute, getattr, proxy, 'bar')
+
+ with self.assertRaises(ForbiddenAttribute):
+ getattr(proxy, 'bar')
self.assertEqual(checker._checked, 'bar')
+ def test__getattr__w_checker_ok_dynamic_attribute_called_once(self):
+ class Dynamic(object):
+ count = 0
+ def __getattr__(self, name):
+ self.count += 1
+ if self.count == 1:
+ # Called from __getattribute__
+ raise AttributeError(name)
+ raise AssertionError("We should not be called more than once")
+
+ target = Dynamic()
+ checker = DummyChecker()
+ proxy = self._makeOne(target, checker)
+
+ with self.assertRaisesRegexp(AttributeError, "name"):
+ getattr(proxy, 'name')
+ self.assertEqual(1, target.count)
+
def test___setattr___w_checker_ok(self):
class Foo(object):
bar = 'Bar'
@@ -1290,7 +1311,7 @@ class AbstractProxyTestBase(object):
pass
class Get(object):
def __getitem__(self, x):
- raise Missing('__getitem__')
+ raise Missing('__getitem__') # pragma: no cover (only py3)
def __getslice__(self, start, stop):
raise Missing("__getslice__")
target = Get()
@@ -1355,7 +1376,7 @@ class AbstractProxyTestBase(object):
pass
class Set(object):
def __setitem__(self, k, v):
- raise Missing('__setitem__')
+ raise Missing('__setitem__') # pragma: no cover (only py3)
def __setslice__(self, start, stop, value):
raise Missing("__setslice__")
target = Set()
@@ -1470,6 +1491,16 @@ class ProxyPyTests(AbstractProxyTestBase,
self.assertRaises(AttributeError, getattr, proxy, '_wrapped')
self.assertRaises(AttributeError, getattr, proxy, '_checker')
+ def test_access_checker_from_subclass(self):
+ target = object()
+ checker = DummyChecker()
+ class Sub(self._getTargetClass()):
+ def get_checker(self):
+ return self._checker
+
+ sub = Sub(target, checker)
+ self.assertIs(checker, sub.get_checker())
+
def test_ctor_w_checker(self):
from zope.security.proxy import getObjectPy, getCheckerPy
# Can't access '_wrapped' / '_checker' in C version
@@ -1535,6 +1566,32 @@ class ProxyPyTests(AbstractProxyTestBase,
finally:
zope.security.proxy._builtin_isinstance = orig_builtin_isinstance
+ def test_getObjectPy_other_object(self):
+ # If it's not a proxy, return it
+ from zope.security.proxy import getObjectPy
+ self.assertIs(self, getObjectPy(self))
+
+ def test_get_reduce(self):
+ class Reduce(object):
+ def __reduce__(self):
+ return 1
+
+ def __reduce_ex__(self, prot):
+ return prot
+
+ reduce_ = Reduce()
+ proxy = self._makeOne(reduce_, DummyChecker())
+ self.assertEqual(1, proxy.__reduce__())
+ self.assertEqual(2, proxy.__reduce_ex__(2))
+
+ def test__module__(self):
+ class WithModule(object):
+ __module__ = 'foo'
+
+ module = WithModule()
+ proxy = self._makeOne(module, DummyChecker())
+ self.assertEqual(WithModule.__module__, proxy.__module__)
+
class DummyChecker(object):
_proxied = _checked = None
def __init__(self, raising=None, allowed=()):