Commit b1a3ebfc authored by Jérome Perrin's avatar Jérome Perrin

SecurityTestCase: type annotations

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