[Zope-Checkins] CVS: Zope3/lib/python/Zope/Security/tests - test_Proxy.py:1.1.2.11
Guido van Rossum
guido@python.org
Thu, 18 Apr 2002 16:38:23 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/Security/tests
In directory cvs.zope.org:/tmp/cvs-serv9239/tests
Modified Files:
Tag: SecurityProxy-branch
test_Proxy.py
Log Message:
Add the remaining operations, except for numeric ops,
and whatever was missing from Fred's original wrapper module.
=== Zope3/lib/python/Zope/Security/tests/test_Proxy.py 1.1.2.10 => 1.1.2.11 ===
-from Zope.Security._Proxy import _Proxy, getObject, getChecker
+from Zope.Security._Proxy import getObject, getChecker
+from Zope.Security.Proxy import Proxy
class Checker:
ok = 1
def check_getattr(self, object, name):
- if name != "foo":
+ if name not in ("foo", "next"):
raise RuntimeError
def check_setattr(self, object, name):
@@ -19,9 +20,7 @@
raise RuntimeError
def proxy(self, value):
- if isinstance(value, str):
- return value
- return [value, "hello"]
+ return Proxy(value, self)
class Something:
@@ -35,13 +34,32 @@
return 42
def __eq__(self, other):
return self is other
+ def __hash__(self):
+ return 42
+ def __iter__(self):
+ return self
+ def next(self):
+ return 42 # Infinite sequence
+ def __len__(self):
+ return 42
+ def __getslice__(self, i, j):
+ return [42]
+ def __setslice__(self, i, j, value):
+ if value != [42]:
+ raise ValueError
+ def __contains__(self, x):
+ return x == 42
class ProxyTests(unittest.TestCase):
def setUp(self):
self.x = Something()
self.c = Checker()
- self.p = _Proxy(self.x, self.c)
+ self.p = Proxy(self.x, self.c)
+
+ def shouldFail(self, *args):
+ self.c.ok = 0
+ self.assertRaises(RuntimeError, *args)
def testStr(self):
self.assertEqual(str(self.p), str(self.x))
@@ -50,49 +68,99 @@
self.assertEqual(repr(self.p), repr(self.x))
def testGetAttrOK(self):
- self.assertEqual(self.p.foo, [[1,2,3], "hello"])
+ self.assertEqual(getObject(self.p.foo), [1,2,3])
def testGetAttrFail(self):
self.assertRaises(RuntimeError, lambda: self.p.bar)
def testSetAttrOK(self):
self.p.foo = 42
- self.assertEqual(self.p.foo, [42, "hello"])
+ self.assertEqual(self.p.foo, 42)
def testSetAttrFail(self):
def doit(): self.p.bar = 42
self.assertRaises(RuntimeError, doit)
def testGetItemOK(self):
- self.assertEqual(self.p[0], [1, "hello"])
+ self.assertEqual(self.p[0], 1)
def testGetItemFail(self):
- self.c.ok = 0
- def doit(): return self.p[10]
- self.assertRaises(RuntimeError, doit)
+ self.shouldFail(lambda: self.p[10])
def testSetItemOK(self):
self.p[0] = 42
- self.assertEqual(self.p[0], [42, "hello"])
+ self.assertEqual(self.p[0], 42)
def testSetItemFail(self):
- self.c.ok = 0
def doit(): self.p[10] = 42
- self.assertRaises(RuntimeError, doit)
+ self.shouldFail(doit)
def testCallOK(self):
- self.assertEqual(self.p(None), [42, "hello"])
+ self.assertEqual(self.p(None), 42)
def testCallFail(self):
- self.c.ok = 0
- self.assertRaises(RuntimeError, self.p, None)
+ self.shouldFail(self.p, None)
def testRichCompareOK(self):
- self.assertEqual(self.p == self.x, [1, "hello"])
+ self.assertEqual(self.p == self.x, 1)
def testRichCompareFail(self):
- self.c.ok = 0
- self.assertRaises(RuntimeError, lambda: self.p == self.x)
+ self.shouldFail(lambda: self.p == self.x)
+
+ def testIterOK(self):
+ self.assertEqual(getObject(iter(self.p)), self.x)
+
+ def testIterFail(self):
+ self.shouldFail(iter, self.p)
+
+ def testNextOK(self):
+ self.assertEqual(self.p.next(), 42)
+
+ def testNextFail(self):
+ self.shouldFail(self.p.next)
+
+ def testCompareOK(self):
+ self.assertEqual(cmp(self.p, self.x), 0)
+
+ def testCompareFail(self):
+ self.shouldFail(cmp, self.p, self.x)
+
+ def testHashOK(self):
+ self.assertEqual(hash(self.p), hash(self.x))
+
+ def testHashFail(self):
+ self.shouldFail(hash, self.p)
+
+ def testNonzeroOK(self):
+ self.assertEqual(not self.p, 0)
+
+ def testNonzeroFail(self):
+ self.shouldFail(lambda: not self.p)
+
+ def testLenOK(self):
+ self.assertEqual(len(self.p), 42)
+
+ def testLenFail(self):
+ self.shouldFail(len, self.p)
+
+ def testSliceOK(self):
+ self.assertEqual(getObject(self.p[:]), [42])
+
+ def testSliceFail(self):
+ self.shouldFail(lambda: self.p[:])
+
+ def testSetSliceOK(self):
+ self.p[:] = [42]
+
+ def testSetSliceFail(self):
+ def doit(): self.p[:] = [42]
+ self.shouldFail(doit)
+
+ def testContainsOK(self):
+ self.assertEqual(42 in self.p, 1)
+
+ def testContainsFail(self):
+ self.shouldFail(lambda: 42 in self.p)
def testGetObject(self):
self.assertEqual(self.x, getObject(self.p))