Commit 79ac2790 authored by Jérome Perrin's avatar Jérome Perrin

SecurityTestCase: type annotations

parent 01ee4b99
...@@ -51,10 +51,15 @@ def formatNameUnion(names): ...@@ -51,10 +51,15 @@ def formatNameUnion(names):
Guard.formatNameUnion = formatNameUnion Guard.formatNameUnion = formatNameUnion
from Products.ERP5Type.Base import Base
from typing import Callable
class AssertPermissionMethod(object): class AssertPermissionMethod(object):
"""A method object to check that a user have a permission on a document. """A method object to check that a user have a permission on a document.
""" """
def __init__(self, permission_name): def __init__(self, permission_name):
# type: (str) -> None
self._permission_name = permission_name self._permission_name = permission_name
def __get__(self, instance, cls=None): def __get__(self, instance, cls=None):
...@@ -62,6 +67,7 @@ class AssertPermissionMethod(object): ...@@ -62,6 +67,7 @@ class AssertPermissionMethod(object):
return self return self
def __call__(self, username, document): def __call__(self, username, document):
# type: (str, Base) -> None
sm = getSecurityManager() sm = getSecurityManager()
try: try:
self._instance._loginAsUser(username) self._instance._loginAsUser(username)
...@@ -90,6 +96,7 @@ class AssertNoPermissionMethod(object): ...@@ -90,6 +96,7 @@ class AssertNoPermissionMethod(object):
document. document.
""" """
def __init__(self, permission_name): def __init__(self, permission_name):
# type: (str) -> None
self._permission_name = permission_name self._permission_name = permission_name
def __get__(self, instance, cls=None): def __get__(self, instance, cls=None):
...@@ -97,6 +104,7 @@ class AssertNoPermissionMethod(object): ...@@ -97,6 +104,7 @@ class AssertNoPermissionMethod(object):
return self return self
def __call__(self, username, document): def __call__(self, username, document):
# type: (str, Base) -> None
sm = getSecurityManager() sm = getSecurityManager()
try: try:
self._instance._loginAsUser(username) self._instance._loginAsUser(username)
...@@ -141,19 +149,20 @@ class SecurityTestCase(ERP5TypeTestCase): ...@@ -141,19 +149,20 @@ class SecurityTestCase(ERP5TypeTestCase):
# Permission methods # Permission methods
failIfUserCanViewDocument = assertUserCanNotViewDocument = AssertNoPermissionMethod( failIfUserCanViewDocument = assertUserCanNotViewDocument = AssertNoPermissionMethod(
Permissions.View) Permissions.View) # type: Callable[[SecurityTestCase, str, Base], None]
failIfUserCanAccessDocument = assertUserCanNotAccessDocument = AssertNoPermissionMethod( failIfUserCanAccessDocument = assertUserCanNotAccessDocument = AssertNoPermissionMethod(
Permissions.AccessContentsInformation) Permissions.AccessContentsInformation) # type: Callable[[SecurityTestCase, str, Base], None]
failIfUserCanModifyDocument = assertUserCanNotModifyDocument = AssertNoPermissionMethod( failIfUserCanModifyDocument = assertUserCanNotModifyDocument = AssertNoPermissionMethod(
Permissions.ModifyPortalContent) Permissions.ModifyPortalContent) # type: Callable[[SecurityTestCase, str, Base], None]
failIfUserCanAddDocument = assertUserCanNotAddDocument = AssertNoPermissionMethod( failIfUserCanAddDocument = assertUserCanNotAddDocument = AssertNoPermissionMethod(
Permissions.AddPortalContent) Permissions.AddPortalContent) # type: Callable[[SecurityTestCase, str, Base], None]
failIfUserCanChangeLocalRoles = assertUserCanNotChangeLocalRoles = AssertNoPermissionMethod( failIfUserCanChangeLocalRoles = assertUserCanNotChangeLocalRoles = AssertNoPermissionMethod(
Permissions.ChangeLocalRoles) Permissions.ChangeLocalRoles) # type: Callable[[SecurityTestCase, str, Base], None]
failIfUserCanDeleteDocument = assertUserCanNotDeleteDocument = AssertNoPermissionMethod( failIfUserCanDeleteDocument = assertUserCanNotDeleteDocument = AssertNoPermissionMethod(
Permissions.DeleteObjects) Permissions.DeleteObjects) # type: Callable[[SecurityTestCase, str, Base], None]
def failIfUserHavePermissionOnDocument(self, permission_name, username, document): def failIfUserHavePermissionOnDocument(self, permission_name, username, document):
# type: (str, str, Base) -> None
"""Fail If the user have a permission on document. """Fail If the user have a permission on document.
XXX why isn't it a method object ? XXX why isn't it a method object ?
""" """
...@@ -162,19 +171,20 @@ class SecurityTestCase(ERP5TypeTestCase): ...@@ -162,19 +171,20 @@ class SecurityTestCase(ERP5TypeTestCase):
return method(username, document) return method(username, document)
failUnlessUserCanViewDocument = assertUserCanViewDocument =\ failUnlessUserCanViewDocument = assertUserCanViewDocument =\
AssertPermissionMethod(Permissions.View) AssertPermissionMethod(Permissions.View) # type: Callable[[SecurityTestCase, str, Base], None]
failUnlessUserCanAccessDocument = assertUserCanAccessDocument =\ failUnlessUserCanAccessDocument = assertUserCanAccessDocument =\
AssertPermissionMethod(Permissions.AccessContentsInformation) AssertPermissionMethod(Permissions.AccessContentsInformation) # type: Callable[[SecurityTestCase, str, Base], None]
failUnlessUserCanModifyDocument = assertUserCanModifyDocument = \ failUnlessUserCanModifyDocument = assertUserCanModifyDocument = \
AssertPermissionMethod(Permissions.ModifyPortalContent) AssertPermissionMethod(Permissions.ModifyPortalContent) # type: Callable[[SecurityTestCase, str, Base], None]
failUnlessUserCanAddDocument = assertUserCanAddDocument =\ failUnlessUserCanAddDocument = assertUserCanAddDocument =\
AssertPermissionMethod(Permissions.AddPortalContent) AssertPermissionMethod(Permissions.AddPortalContent) # type: Callable[[SecurityTestCase, str, Base], None]
failUnlessUserCanChangeLocalRoles = assertUserCanChangeLocalRoles =\ failUnlessUserCanChangeLocalRoles = assertUserCanChangeLocalRoles =\
AssertPermissionMethod(Permissions.ChangeLocalRoles) AssertPermissionMethod(Permissions.ChangeLocalRoles) # type: Callable[[SecurityTestCase, str, Base], None]
failUnlessUserCanDeleteDocument = assertUserCanDeleteDocument =\ failUnlessUserCanDeleteDocument = assertUserCanDeleteDocument =\
AssertPermissionMethod(Permissions.DeleteObjects) AssertPermissionMethod(Permissions.DeleteObjects) # type: Callable[[SecurityTestCase, str, Base], None]
def failUnlessUserHavePermissionOnDocument(self, permission_name, username, document): def failUnlessUserHavePermissionOnDocument(self, permission_name, username, document):
# type: (str, str, Base) -> None
"""Fail Unless the user have a permission on document.""" """Fail Unless the user have a permission on document."""
method = AssertPermissionMethod(permission_name) method = AssertPermissionMethod(permission_name)
method._instance = self method._instance = self
...@@ -183,6 +193,7 @@ class SecurityTestCase(ERP5TypeTestCase): ...@@ -183,6 +193,7 @@ class SecurityTestCase(ERP5TypeTestCase):
# Workflow Transition Methods # Workflow Transition Methods
def failIfUserCanPassWorkflowTransition(self, username, transition, document): def failIfUserCanPassWorkflowTransition(self, username, transition, document):
# type: (str, str, Base) -> None
"""Fails if the user can pass the workflow transition on the document.""" """Fails if the user can pass the workflow transition on the document."""
sm = getSecurityManager() sm = getSecurityManager()
try: try:
...@@ -198,10 +209,11 @@ class SecurityTestCase(ERP5TypeTestCase): ...@@ -198,10 +209,11 @@ class SecurityTestCase(ERP5TypeTestCase):
finally: finally:
setSecurityManager(sm) setSecurityManager(sm)
assertUserCanNotPassWorkflowTransition = failIfUserCanPassWorkflowTransition assertUserCanNotPassWorkflowTransition = failIfUserCanPassWorkflowTransition # type: Callable[[SecurityTestCase, str, str, Base], None]
def failUnlessUserCanPassWorkflowTransition(self, username, def failUnlessUserCanPassWorkflowTransition(self, username,
transition, document): transition, document):
# type: (str, str, Base) -> None
"""Fails unless the user can pass the workflow transition on the document.""" """Fails unless the user can pass the workflow transition on the document."""
sm = getSecurityManager() sm = getSecurityManager()
try: try:
...@@ -244,9 +256,10 @@ class SecurityTestCase(ERP5TypeTestCase): ...@@ -244,9 +256,10 @@ class SecurityTestCase(ERP5TypeTestCase):
finally: finally:
setSecurityManager(sm) setSecurityManager(sm)
assertUserCanPassWorkflowTransition = failUnlessUserCanPassWorkflowTransition assertUserCanPassWorkflowTransition = failUnlessUserCanPassWorkflowTransition # type: Callable[[SecurityTestCase, str, str, Base], None]
def assertUserHasWorklist(self, username, worklist_id, document_count): def assertUserHasWorklist(self, username, worklist_id, document_count):
# type: (str, str, int) -> None
self.portal.portal_workflow.refreshWorklistCache() self.portal.portal_workflow.refreshWorklistCache()
self.portal.portal_caches.clearAllCache() self.portal.portal_caches.clearAllCache()
sm = getSecurityManager() sm = getSecurityManager()
...@@ -268,6 +281,7 @@ class SecurityTestCase(ERP5TypeTestCase): ...@@ -268,6 +281,7 @@ class SecurityTestCase(ERP5TypeTestCase):
setSecurityManager(sm) setSecurityManager(sm)
def assertUserHasNoWorklist(self, username, worklist_id): def assertUserHasNoWorklist(self, username, worklist_id):
# type: (str, str) -> None
self.portal.portal_workflow.refreshWorklistCache() self.portal.portal_workflow.refreshWorklistCache()
self.portal.portal_caches.clearAllCache() self.portal.portal_caches.clearAllCache()
sm = getSecurityManager() sm = getSecurityManager()
...@@ -283,6 +297,7 @@ class SecurityTestCase(ERP5TypeTestCase): ...@@ -283,6 +297,7 @@ class SecurityTestCase(ERP5TypeTestCase):
# Simple check for an user Role # Simple check for an user Role
def failIfUserHaveRoleOnDocument(self, username, role, document): def failIfUserHaveRoleOnDocument(self, username, role, document):
# type: (str, str, Base) -> None
"""Fails if the user have the role on the document.""" """Fails if the user have the role on the document."""
sm = getSecurityManager() sm = getSecurityManager()
try: try:
...@@ -294,9 +309,10 @@ class SecurityTestCase(ERP5TypeTestCase): ...@@ -294,9 +309,10 @@ class SecurityTestCase(ERP5TypeTestCase):
finally: finally:
setSecurityManager(sm) setSecurityManager(sm)
assertUserDoesNotHaveRoleOnDocument = failIfUserHaveRoleOnDocument assertUserDoesNotHaveRoleOnDocument = failIfUserHaveRoleOnDocument # type: Callable[[SecurityTestCase, str, str, Base], None]
def failUnlessUserHaveRoleOnDocument(self, username, role, document): def failUnlessUserHaveRoleOnDocument(self, username, role, document):
# type: (str, str, Base) -> None
"""Fails if the user does not have the role on the document.""" """Fails if the user does not have the role on the document."""
sm = getSecurityManager() sm = getSecurityManager()
try: try:
...@@ -310,5 +326,4 @@ class SecurityTestCase(ERP5TypeTestCase): ...@@ -310,5 +326,4 @@ class SecurityTestCase(ERP5TypeTestCase):
finally: finally:
setSecurityManager(sm) setSecurityManager(sm)
assertUserHaveRoleOnDocument = failUnlessUserHaveRoleOnDocument assertUserHaveRoleOnDocument = failUnlessUserHaveRoleOnDocument # type: Callable[[SecurityTestCase, str, str, Base], None]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment