Skip to content

Commit

Permalink
Merge branch 'moretests'
Browse files Browse the repository at this point in the history
  • Loading branch information
kedder committed Feb 19, 2013
2 parents e1d5f7c + aad8574 commit 67e4c91
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 110 deletions.
113 changes: 27 additions & 86 deletions docs/api/checker.rst
Expand Up @@ -82,8 +82,7 @@ directly, or via interface:

>>> context = AContext()
>>> allow(context, attributes=['foo', 'bar'], interface=[I1, I2])
>>> context.actions.sort(
... lambda a, b: cmp(a['discriminator'], b['discriminator']))
>>> context.actions.sort(key=lambda a: a['discriminator'])
>>> pprint(context.actions)
[{'args': ('testmodule', 'a', 'zope.Public'),
'callable': 1,
Expand Down Expand Up @@ -136,8 +135,7 @@ directly, or via interface:
>>> require(context, attributes=['foo', 'bar'],
... interface=[I1, I2], permission='p')

>>> context.actions.sort(
... lambda a, b: cmp(a['discriminator'], b['discriminator']))
>>> context.actions.sort(key=lambda a: a['discriminator'])
>>> pprint(context.actions)
[{'args': ('testmodule', 'a', 'p'),
'callable': 1,
Expand Down Expand Up @@ -182,26 +180,26 @@ Protections for standard objects
... from zope.security.interfaces import ForbiddenAttribute
... try:
... return getattr(object, attr)
... except ForbiddenAttribute, e:
... return 'ForbiddenAttribute: %s' % e[0]
... except ForbiddenAttribute as e:
... return 'ForbiddenAttribute: %s' % e.args[0]
>>> def check_forbidden_setitem(object, item, value):
... from zope.security.interfaces import ForbiddenAttribute
... try:
... object[item] = value
... except ForbiddenAttribute, e:
... return 'ForbiddenAttribute: %s' % e[0]
... except ForbiddenAttribute as e:
... return 'ForbiddenAttribute: %s' % e.args[0]
>>> def check_forbidden_delitem(object, item):
... from zope.security.interfaces import ForbiddenAttribute
... try:
... del object[item]
... except ForbiddenAttribute, e:
... return 'ForbiddenAttribute: %s' % e[0]
... except ForbiddenAttribute as e:
... return 'ForbiddenAttribute: %s' % e.args[0]
>>> def check_forbidden_call(callable, *args): # **
... from zope.security.interfaces import ForbiddenAttribute
... try:
... return callable(*args) # **
... except ForbiddenAttribute, e:
... return 'ForbiddenAttribute: %s' % e[0]
... except ForbiddenAttribute as e:
... return 'ForbiddenAttribute: %s' % e.args[0]

Rocks
#####
Expand All @@ -217,16 +215,12 @@ Rocks are immuatle, non-callable objects without interesting methods. They
1
>>> int(type(ProxyFactory( 1.0 )) is float)
1
>>> int(type(ProxyFactory( 1l )) is long)
1
>>> int(type(ProxyFactory( 1j )) is complex)
1
>>> int(type(ProxyFactory( None )) is type(None))
1
>>> int(type(ProxyFactory( 'xxx' )) is str)
1
>>> int(type(ProxyFactory( u'xxx' )) is unicode)
1
>>> int(type(ProxyFactory( True )) is type(True))
1

Expand Down Expand Up @@ -270,18 +264,18 @@ We can do everything we expect to be able to do with proxied dicts.
1
>>> len(d)
2
>>> list(d)
>>> sorted(list(d))
['a', 'b']
>>> d.get('a')
1
>>> int(d.has_key('a'))
>>> int('a' in d)
1
>>> c = d.copy()
>>> check_forbidden_get(c, 'clear')
'ForbiddenAttribute: clear'
>>> int(str(c) in ("{'a': 1, 'b': 2}", "{'b': 2, 'a': 1}"))
1
>>> int(`c` in ("{'a': 1, 'b': 2}", "{'b': 2, 'a': 1}"))
>>> int(repr(c) in ("{'a': 1, 'b': 2}", "{'b': 2, 'a': 1}"))
1
>>> def sorted(x):
... x = list(x)
Expand All @@ -293,27 +287,12 @@ We can do everything we expect to be able to do with proxied dicts.
[1, 2]
>>> sorted(d.items())
[('a', 1), ('b', 2)]
>>> sorted(d.iterkeys())
['a', 'b']
>>> sorted(d.itervalues())
[1, 2]
>>> sorted(d.iteritems())
[('a', 1), ('b', 2)]

Always available:
Always available (note, that dicts in python-3.x are not orderable, so we are
not checking that under python > 2):

.. doctest::

>>> int(d < d)
0
>>> int(d > d)
0
>>> int(d <= d)
1
>>> int(d >= d)
1
>>> int(d == d)
1
>>> int(d != d)
0
>>> int(bool(d))
Expand Down Expand Up @@ -351,7 +330,7 @@ We can do everything we expect to be able to do with proxied lists.
1
>>> str(l)
'[1, 2]'
>>> `l`
>>> repr(l)
'[1, 2]'
>>> l + l
[1, 2, 1, 2]
Expand Down Expand Up @@ -398,7 +377,7 @@ We can do everything we expect to be able to do with proxied tuples.
1
>>> str(l)
'(1, 2)'
>>> `l`
>>> repr(l)
'(1, 2)'
>>> l + l
(1, 2, 1, 2)
Expand Down Expand Up @@ -607,8 +586,8 @@ we can do everything we expect to be able to do with proxied frozensets.
... from zope.security.interfaces import ForbiddenAttribute
... try:
... return getattr(object, attr)
... except ForbiddenAttribute, e:
... return 'ForbiddenAttribute: %s' % e[0]
... except ForbiddenAttribute as e:
... return 'ForbiddenAttribute: %s' % e.args[0]
>>> from zope.security.checker import ProxyFactory
>>> from zope.security.interfaces import ForbiddenAttribute
>>> us = frozenset((1, 2))
Expand Down Expand Up @@ -779,6 +758,8 @@ iterators

.. doctest::

>>> [a for a in ProxyFactory(iter([1, 2]))]
[1, 2]
>>> list(ProxyFactory(iter([1, 2])))
[1, 2]
>>> list(ProxyFactory(iter((1, 2))))
Expand Down Expand Up @@ -815,7 +796,7 @@ We can iterate over sequences

>>> from zope.security.checker import NamesChecker
>>> from zope.security.checker import ProxyFactory
>>> c = NamesChecker(['__getitem__'])
>>> c = NamesChecker(['__getitem__', '__len__'])
>>> p = ProxyFactory(x, c)

Even if they are proxied
Expand Down Expand Up @@ -856,7 +837,7 @@ New-style classes
>>> check_forbidden_get(C, '__dict__')
'ForbiddenAttribute: __dict__'
>>> s = str(C)
>>> s = `C`
>>> s = repr(C)
>>> int(C.__module__ == __name__)
1
>>> len(C.__bases__)
Expand All @@ -868,14 +849,6 @@ Always available:

.. doctest::

>>> int(C < C)
0
>>> int(C > C)
0
>>> int(C <= C)
1
>>> int(C >= C)
1
>>> int(C == C)
1
>>> int(C != C)
Expand Down Expand Up @@ -907,14 +880,6 @@ Always available:

.. doctest::

>>> int(c < c)
0
>>> int(c > c)
0
>>> int(c <= c)
1
>>> int(c >= c)
1
>>> int(c == c)
1
>>> int(c != c)
Expand All @@ -938,24 +903,16 @@ Classic Classes
>>> check_forbidden_get(C, '__dict__')
'ForbiddenAttribute: __dict__'
>>> s = str(C)
>>> s = `C`
>>> s = repr(C)
>>> int(C.__module__ == __name__)
1
>>> len(C.__bases__)
0
1

Always available:

.. doctest::

>>> int(C < C)
0
>>> int(C > C)
0
>>> int(C <= C)
1
>>> int(C >= C)
1
>>> int(C == C)
1
>>> int(C != C)
Expand Down Expand Up @@ -984,14 +941,6 @@ Always available:

.. doctest::

>>> int(c < c)
0
>>> int(c > c)
0
>>> int(c <= c)
1
>>> int(c >= c)
1
>>> int(c == c)
1
>>> int(c != c)
Expand Down Expand Up @@ -1055,7 +1004,7 @@ We work with the ABCMeta meta class:
>>> check_forbidden_get(PBar, '__dict__')
'ForbiddenAttribute: __dict__'
>>> s = str(PBar)
>>> s = `PBar`
>>> s = repr(PBar)
>>> int(PBar.__module__ == __name__)
1
>>> len(PBar.__bases__)
Expand All @@ -1065,19 +1014,11 @@ Always available:

.. doctest::

>>> int(PBar < PBar)
0
>>> int(PBar > PBar)
0
>>> int(PBar <= PBar)
1
>>> int(PBar >= PBar)
1
>>> int(PBar == PBar)
1
>>> int(PBar != PBar)
0
>>> int(bool(PBar))
1
>>> int(PBar.__class__ == abc.ABCMeta)
>>> int(PBar.__class__ == type)
1
25 changes: 16 additions & 9 deletions docs/api/decorator.rst
Expand Up @@ -48,13 +48,16 @@ Using `selectChecker()`, we can confirm that a `Foo` object uses
.. doctest::

>>> from zope.security.checker import selectChecker
>>> from zope.security.interfaces import ForbiddenAttribute
>>> foo = Foo()
>>> selectChecker(foo) is fooChecker
True
>>> fooChecker.check(foo, 'a')
>>> fooChecker.check(foo, 'b') # doctest: +ELLIPSIS
Traceback (most recent call last):
ForbiddenAttribute: ('b', <zope.security.decorator.Foo object ...>)
>>> try:
... fooChecker.check(foo, 'b') # doctest: +ELLIPSIS
... except ForbiddenAttribute as e:
... e
ForbiddenAttribute('b', <Foo object ...>)

and that a `Wrapper` object uses `wrappeChecker`:

Expand All @@ -64,9 +67,11 @@ and that a `Wrapper` object uses `wrappeChecker`:
>>> selectChecker(wrapper) is wrapperChecker
True
>>> wrapperChecker.check(wrapper, 'b')
>>> wrapperChecker.check(wrapper, 'a') # doctest: +ELLIPSIS
Traceback (most recent call last):
ForbiddenAttribute: ('a', <zope.security.decorator.Foo object ...>)
>>> try:
... wrapperChecker.check(wrapper, 'a') # doctest: +ELLIPSIS
... except ForbiddenAttribute as e:
... e
ForbiddenAttribute('a', <Foo object ...>)

(Note that the object description says `Foo` because the object is a
proxy and generally looks and acts like the object it's proxying.)
Expand Down Expand Up @@ -94,9 +99,11 @@ illustrate, we'll proxify `foo`:
>>> secure_foo = ProxyFactory(foo)
>>> secure_foo.a
'a'
>>> secure_foo.b # doctest: +ELLIPSIS
Traceback (most recent call last):
ForbiddenAttribute: ('b', <zope.security.decorator.Foo object ...>)
>>> try:
... secure_foo.b # doctest: +ELLIPSIS
... except ForbiddenAttribute as e:
... e
ForbiddenAttribute('b', <Foo object ...>)

when we wrap the secured `foo`:

Expand Down
10 changes: 5 additions & 5 deletions docs/api/permission.rst
Expand Up @@ -48,7 +48,7 @@ The :data:`zope.security.checker.CheckerPublic` permission always exists:
>>> ids = list(allPermissions(None))
>>> ids.sort()
>>> ids
[u'x', u'y']
['x', 'y']


.. autofunction:: zope.security.permission.PermissionsVocabulary
Expand Down Expand Up @@ -100,9 +100,9 @@ The non-public permissions 'x' and 'y' are string values:
.. doctest::

>>> vocab.getTermByToken('x').value
u'x'
'x'
>>> vocab.getTermByToken('y').value
u'y'
'y'

However, the public permission value is CheckerPublic:

Expand All @@ -116,15 +116,15 @@ and its title is shortened:
.. doctest::

>>> vocab.getTermByToken('zope.Public').title
u'Public'
'Public'

The terms are sorted by title except for the public permission, which is
listed first:

.. doctest::

>>> [term.title for term in vocab]
[u'Public', u'x', u'y']
['Public', 'x', 'y']


.. testcleanup::
Expand Down
14 changes: 9 additions & 5 deletions docs/api/zcml.rst
Expand Up @@ -11,8 +11,9 @@ a couple of permissions:

>>> from zope.component import getGlobalSiteManager
>>> from zope.configuration.xmlconfig import XMLConfig
>>> from zope.component.testing import setUp
>>> import zope.security

>>> setUp() # clear global component registry
>>> XMLConfig('permissions.zcml', zope.security)()

>>> len(list(getGlobalSiteManager().registeredUtilities()))
Expand Down Expand Up @@ -64,10 +65,13 @@ Now let's see whether validation works alright
>>> field._validate('zope.ManageCode')
>>> context._actions[0]['args']
(None, 'zope.foo')
>>> field._validate('3 foo')
Traceback (most recent call last):
...
InvalidId: 3 foo

>>> from zope.schema.interfaces import InvalidId
>>> try:
... field._validate('3 foo')
... except InvalidId as e:
... e
InvalidId('3 foo')

zope.Public is always valid
>>> field._validate('zope.Public')
Expand Down

0 comments on commit 67e4c91

Please sign in to comment.