Skip to content

Commit

Permalink
100% coverage for z.s.management.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Feb 11, 2013
1 parent d67c3c2 commit 34be81f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 37 deletions.
36 changes: 20 additions & 16 deletions src/zope/security/management.py
Expand Up @@ -14,19 +14,22 @@
"""Default 'ISecurityManagement' and 'IInteractionManagement' implementation
"""

from zope.interface import moduleProvides

import zope.interface

from zope.security import interfaces
from zope.security.checker import CheckerPublic
from zope.security._definitions import thread_local, system_user
from zope.security.interfaces import IInteractionManagement
from zope.security.interfaces import ISecurityManagement
from zope.security.interfaces import NoInteraction
from zope.security.simplepolicies import ParanoidSecurityPolicy
from zope.security._definitions import thread_local
from zope.security._definitions import system_user # API?


_defaultPolicy = ParanoidSecurityPolicy

zope.interface.moduleProvides(
interfaces.ISecurityManagement,
interfaces.IInteractionManagement)
moduleProvides(
ISecurityManagement,
IInteractionManagement)

#
# ISecurityManagement implementation
Expand Down Expand Up @@ -62,18 +65,19 @@ def getInteraction():
try:
return thread_local.interaction
except AttributeError:
raise interfaces.NoInteraction
raise NoInteraction

class ExistingInteraction(ValueError,
AssertionError, #BBB
):
pass

def newInteraction(*participations):
"""Start a new interaction."""

if queryInteraction() is not None:
raise AssertionError("newInteraction called"
raise ExistingInteraction("newInteraction called"
" while another interaction is active.")

interaction = getSecurityPolicy()(*participations)

thread_local.interaction = interaction
thread_local.interaction = getSecurityPolicy()(*participations)

def endInteraction():
"""End the current interaction."""
Expand Down Expand Up @@ -122,7 +126,7 @@ def checkPermission(permission, object, interaction=None):
try:
interaction = thread_local.interaction
except AttributeError:
raise interfaces.NoInteraction
raise NoInteraction
return interaction.checkPermission(permission, object)


Expand All @@ -136,7 +140,7 @@ def _clear():
# need for zope.testing.
try:
from zope.testing.cleanup import addCleanUp
except ImportError:
except ImportError: #pragma NO COVER
pass
else:
addCleanUp(_clear)
Expand Down
87 changes: 66 additions & 21 deletions src/zope/security/tests/test_management.py
Expand Up @@ -48,50 +48,99 @@ def test_securityPolicy(self):
setSecurityPolicy(policy)
self.assert_(getSecurityPolicy() is policy)

def test_query_new_end_restore_Interaction(self):
def test_getInteraction_none_present(self):
from zope.security.interfaces import NoInteraction
from zope.security.management import getInteraction
self.assertRaises(NoInteraction, getInteraction)

def test_queryInteraction_none_present(self):
from zope.security.management import queryInteraction
self.assertEquals(queryInteraction(), None)

def test_newInteraction(self):
from zope.security.management import newInteraction

from zope.security.management import queryInteraction
newInteraction()

interaction = queryInteraction()
self.assert_(interaction is not None)
self.assertRaises(AssertionError, newInteraction)
self.assertTrue(interaction is not None)

def test_newInteraction_repeated_without_end(self):
from zope.security.management import ExistingInteraction
from zope.security.management import newInteraction
newInteraction()
self.assertRaises(ExistingInteraction, newInteraction)

def test_endInteraction(self):
from zope.security.management import endInteraction
from zope.security.management import newInteraction
from zope.security.management import queryInteraction
newInteraction()
endInteraction()
self.assertEquals(queryInteraction(), None)

from zope.security.management import restoreInteraction
restoreInteraction()
self.assert_(interaction is queryInteraction())

def test_endInteraction_repeated(self):
from zope.security.management import endInteraction
from zope.security.management import newInteraction
from zope.security.management import queryInteraction
newInteraction()
interaction = queryInteraction()
endInteraction()
self.assertEquals(queryInteraction(), None)

endInteraction()
self.assertEquals(queryInteraction(), None)

def test_restoreInteraction_after_end(self):
from zope.security.management import endInteraction
from zope.security.management import newInteraction
from zope.security.management import queryInteraction
from zope.security.management import restoreInteraction
newInteraction()
interaction = queryInteraction()
endInteraction()
restoreInteraction()
self.assert_(interaction is queryInteraction())

def test_restoreInteraction_after_new(self):
from zope.security.management import newInteraction
from zope.security.management import queryInteraction
from zope.security.management import restoreInteraction
newInteraction()
self.assert_(queryInteraction() is not None)

restoreInteraction() # restore to no interaction
self.assert_(queryInteraction() is None)

def test_checkPermission(self):
from zope.security import checkPermission
from zope.security.management import setSecurityPolicy
def test_restoreInteraction_after_neither(self):
from zope.security.management import queryInteraction
from zope.security.management import newInteraction, endInteraction
from zope.security.management import restoreInteraction
from zope.security._definitions import thread_local
try:
del thread_local.interaction
except AttributeError:
pass
try:
del thread_local.previous_interaction
except AttributeError:
pass
restoreInteraction()
self.assert_(queryInteraction() is None)

def test_checkPermission_w_no_interaction(self):
from zope.security.management import checkPermission
from zope.security.interfaces import NoInteraction
permission = 'zope.Test'
obj = object()
self.assertRaises(NoInteraction, checkPermission, permission, obj)

def test_checkPermission_w_interaction(self):
from zope.security.management import checkPermission
from zope.security.management import setSecurityPolicy
from zope.security.management import queryInteraction
from zope.security.management import newInteraction

permission = 'zope.Test'
obj = object()

class PolicyStub(object):

def checkPermission(s, p, o,):
self.assert_(p is permission)
self.assert_(o is obj)
Expand All @@ -102,11 +151,8 @@ def checkPermission(s, p, o,):
newInteraction()
interaction = queryInteraction()
self.assertEquals(checkPermission(permission, obj), True)

endInteraction()
self.assertRaises(NoInteraction, checkPermission, permission, obj)

def test_checkPublicPermission(self):
def test_checkPermission_forbidden_policy(self):
from zope.security import checkPermission
from zope.security.checker import CheckerPublic
from zope.security.management import setSecurityPolicy
Expand All @@ -115,7 +161,6 @@ def test_checkPublicPermission(self):
obj = object()

class ForbiddenPolicyStub(object):

def checkPermission(s, p, o):
return False

Expand Down

0 comments on commit 34be81f

Please sign in to comment.