summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Madden <jamadden@gmail.com>2017-09-12 09:56:15 -0500
committerJason Madden <jamadden@gmail.com>2017-09-12 09:56:15 -0500
commit0c6752c9148a895229cad9768f1fc2d4d6c94f1d (patch)
treecb91aeb9bc27947c970d94f1ddec59239c92e2c7
parent2aa89d3f2de219709f677b88755aaf499a390eca (diff)
downloadzope-security-0c6752c9148a895229cad9768f1fc2d4d6c94f1d.tar.gz
100% coverage for testing.py
-rw-r--r--CHANGES.rst5
-rw-r--r--src/zope/security/management.py8
-rw-r--r--src/zope/security/testing.py45
-rw-r--r--src/zope/security/tests/test_proxy.py6
-rw-r--r--src/zope/security/tests/test_testing.py48
-rw-r--r--tox.ini2
6 files changed, 66 insertions, 48 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index d098a3c..c6eacf3 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -55,7 +55,10 @@
- Fix the pure-Python proxy calling a wrapped ``__getattr__`` or
``__getattribute__`` more than once in situations where the C
- implementation only called it one time (when it raised an AttributeError).
+ implementation only called it one time (when it raised an
+ AttributeError).
+
+- Reach 100% test coverage and maintain it via automated checks.
4.1.1 (2017-05-17)
==================
diff --git a/src/zope/security/management.py b/src/zope/security/management.py
index 6539200..7a77f1a 100644
--- a/src/zope/security/management.py
+++ b/src/zope/security/management.py
@@ -76,7 +76,7 @@ def newInteraction(*participations):
"""Start a new interaction."""
if queryInteraction() is not None:
raise ExistingInteraction("newInteraction called"
- " while another interaction is active.")
+ " while another interaction is active.")
thread_local.interaction = getSecurityPolicy()(*participations)
def endInteraction():
@@ -134,13 +134,9 @@ def _clear():
global _defaultPolicy
_defaultPolicy = ParanoidSecurityPolicy
-# XXX This code is used to support automated testing. However, it shouldn't be
-# here and needs to be refactored. The empty addCleanUp-method is a temporary
-# workaround to fix packages that depend on zope.security but don't have a
-# need for zope.testing.
try:
from zope.testing.cleanup import addCleanUp
-except ImportError: #pragma NO COVER
+except ImportError: # pragma: no cover
pass
else:
addCleanUp(_clear)
diff --git a/src/zope/security/testing.py b/src/zope/security/testing.py
index ee13db8..1553060 100644
--- a/src/zope/security/testing.py
+++ b/src/zope/security/testing.py
@@ -15,33 +15,28 @@
This module provides some helper/stub objects for setting up interactions.
"""
-import sys
+import contextlib
import re
from zope import interface, component
+
from zope.security import interfaces
from zope.security.permission import Permission
-import contextlib
import zope.security.management
+from zope.security._compat import PYTHON2 as PY2
+
from zope.testing import renormalizing
-PY2 = sys.version_info[0] == 2
-
-if PY2:
- _u = unicode
- rules = [(re.compile("b('.*?')"), r"\1"),
- (re.compile('b(".*?")'), r"\1"),
- ]
- output_checker = renormalizing.RENormalizing(rules)
-else:
- _u = str
- rules = [(re.compile("u('.*?')"), r"\1"),
- (re.compile('u(".*?")'), r"\1"),
- ]
- output_checker = renormalizing.RENormalizing(rules)
+_str_prefix = 'b' if PY2 else 'u'
+
+rules = [
+ (re.compile(_str_prefix + "('.*?')"), r"\1"),
+ (re.compile(_str_prefix + '(".*?")'), r"\1"),
+]
+output_checker = renormalizing.RENormalizing(rules)
@interface.implementer(interfaces.IPrincipal)
-class Principal:
+class Principal(object):
def __init__(self, id, title=None, description='', groups=None):
self.id = id
@@ -53,7 +48,7 @@ class Principal:
@interface.implementer(interfaces.IParticipation)
-class Participation:
+class Participation(object):
def __init__(self, principal):
self.principal = principal
@@ -63,16 +58,18 @@ class Participation:
def addCheckerPublic():
"""Add the CheckerPublic permission as 'zope.Public'"""
- perm = Permission('zope.Public', 'Public',
- """Special permission used for resources that are always public
+ perm = Permission(
+ 'zope.Public', 'Public',
+ """Special permission used for resources that are always public
- The public permission is effectively an optimization, sine
- it allows security computation to be bypassed.
- """
- )
+ The public permission is effectively an optimization, sine
+ it allows security computation to be bypassed.
+ """
+ )
gsm = component.getGlobalSiteManager()
gsm.registerUtility(perm, interfaces.IPermission, perm.id)
+ return perm
def create_interaction(principal_id, **kw):
principal = Principal(principal_id, **kw)
diff --git a/src/zope/security/tests/test_proxy.py b/src/zope/security/tests/test_proxy.py
index 8f52e17..07c99cd 100644
--- a/src/zope/security/tests/test_proxy.py
+++ b/src/zope/security/tests/test_proxy.py
@@ -319,6 +319,12 @@ class AbstractProxyTestBase(object):
proxy = self._makeOne(target, checker)
self.assertTrue(proxy >= 1)
+ def test__gt__(self):
+ target = 1
+ checker = object() # checker not consulted
+ proxy = self._makeOne(target, checker)
+ self.assertTrue(proxy > 0)
+
def test___hash___w_self(self):
target = object()
checker = object() # checker not consulted
diff --git a/src/zope/security/tests/test_testing.py b/src/zope/security/tests/test_testing.py
index 06be624..50b3f0d 100644
--- a/src/zope/security/tests/test_testing.py
+++ b/src/zope/security/tests/test_testing.py
@@ -13,17 +13,17 @@
##############################################################################
import unittest
+from zope.testing.cleanup import CleanUp
-class InteractionHelperTest(unittest.TestCase):
+from zope.security import testing
- def tearDown(self):
- from zope.security.management import endInteraction
- endInteraction()
+class TestTestingFunctions(CleanUp,
+ unittest.TestCase):
def test_create_interaction_should_return_principal(self):
from zope.security.management import getInteraction
- from zope.security.testing import create_interaction
- principal = create_interaction(
+
+ principal = testing.create_interaction(
'foo', groups=['bar'], description='desc')
ix = getInteraction()
participation = ix.participations[0]
@@ -34,25 +34,41 @@ class InteractionHelperTest(unittest.TestCase):
def test_usable_as_contextmanager(self):
from zope.security.management import getInteraction
from zope.security.management import queryInteraction
- from zope.security.testing import interaction
- with interaction('foo'):
+
+ with testing.interaction('foo'):
ix = getInteraction()
participation = ix.participations[0]
self.assertEqual('foo', participation.principal.id)
+ # Nesting doesn't change anything
+ with testing.interaction('baz'):
+ ix = getInteraction()
+ participation = ix.participations[0]
+ self.assertEqual('foo', participation.principal.id)
+
self.assertFalse(queryInteraction())
def test_contextmanager_ends_interaction_on_exception(self):
from zope.security.management import queryInteraction
- from zope.security.testing import interaction
- try:
- with interaction('foo'):
- raise RuntimeError()
- except RuntimeError:
+ class MyError(Exception):
pass
+
+ with self.assertRaises(MyError):
+ with testing.interaction('foo'):
+ raise MyError()
+
self.assertFalse(queryInteraction())
+ def test_addCheckerPublic(self):
+ from zope import component
+ from zope.security.interfaces import IPermission
+
+ perm = testing.addCheckerPublic()
+ utility = component.getUtility(IPermission, name='zope.Public')
+ self.assertIs(perm, utility)
+
+
+
+
def test_suite():
- return unittest.TestSuite((
- unittest.makeSuite(InteractionHelperTest),
- ))
+ return unittest.defaultTestLoader.loadTestsFromName(__name__)
diff --git a/tox.ini b/tox.ini
index 36a065c..59a677c 100644
--- a/tox.ini
+++ b/tox.ini
@@ -34,7 +34,7 @@ basepython =
commands =
coverage run -m zope.testrunner --test-path=src []
coverage run -a -m sphinx -b doctest -d {envdir}/.cache/doctrees docs {envdir}/.cache/doctest
- coverage report --fail-under=92
+ coverage report --fail-under=100
deps =
{[testenv]deps}
coverage