Add base test for actions. Add tests for actions of the tag

and acls apps.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2017-08-27 01:39:02 -04:00
parent c9bd9fb474
commit c27d922b3a
6 changed files with 131 additions and 26 deletions

View File

@@ -0,0 +1,49 @@
from __future__ import unicode_literals
from django.contrib.contenttypes.models import ContentType
from document_states.tests.test_actions import ActionTestCase
from documents.permissions import permission_document_view
from ..models import AccessControlList
from ..workflow_actions import GrantAccessAction, RevokeAccessAction
class ACLActionTestCase(ActionTestCase):
def setUp(self):
super(ACLActionTestCase, self).setUp()
def test_grant_access_action(self):
action = GrantAccessAction(
form_data={
'content_type': ContentType.objects.get_for_model(model=self.document).pk,
'object_id': self.document.pk,
'roles': [self.role.pk],
'permissions': [permission_document_view.uuid],
}
)
action.execute(context={'entry_log': self.entry_log})
self.assertEqual(self.document.acls.count(), 1)
self.assertEqual(
list(self.document.acls.first().permissions.all()),
[permission_document_view.stored_permission]
)
self.assertEqual(self.document.acls.first().role, self.role)
def test_revoke_access_action(self):
self.grant_access(
obj=self.document, permission=permission_document_view
)
action = RevokeAccessAction(
form_data={
'content_type': ContentType.objects.get_for_model(model=self.document).pk,
'object_id': self.document.pk,
'roles': [self.role.pk],
'permissions': [permission_document_view.uuid],
}
)
action.execute(context={'entry_log': self.entry_log})
self.assertEqual(self.document.acls.count(), 0)

View File

@@ -16,7 +16,7 @@ from permissions.models import Role
from .classes import ModelPermission
from .permissions import permission_acl_edit
__all__ = ('GrantAccessAction', 'RemoveAccessAction')
__all__ = ('GrantAccessAction', 'RevokeAccessAction')
logger = logging.getLogger(__name__)
@@ -126,7 +126,7 @@ class GrantAccessAction(WorkflowAction):
)
class RemoveAccessAction(GrantAccessAction):
class RevokeAccessAction(GrantAccessAction):
label = _('Revoke access')
def execute(self, context):

View File

@@ -1,11 +1,20 @@
from __future__ import absolute_import, unicode_literals
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.test import TestCase
from django_downloadview import assert_download_response
from acls.models import AccessControlList
from permissions.classes import Permission
from permissions.models import Role
from permissions.tests.literals import TEST_ROLE_LABEL
from smart_settings.classes import Namespace
from user_management.tests import (
TEST_ADMIN_PASSWORD, TEST_ADMIN_USERNAME, TEST_ADMIN_EMAIL,
TEST_GROUP_NAME, TEST_USER_EMAIL, TEST_USER_USERNAME, TEST_USER_PASSWORD
)
from .mixins import (
ContentTypeCheckMixin, OpenFileCheckMixin, TempfileCheckMixin
@@ -22,3 +31,28 @@ class BaseTestCase(ContentTypeCheckMixin, OpenFileCheckMixin, TempfileCheckMixin
super(BaseTestCase, self).setUp()
Namespace.invalidate_cache_all()
Permission.invalidate_cache()
self.admin_user = get_user_model().objects.create_superuser(
username=TEST_ADMIN_USERNAME, email=TEST_ADMIN_EMAIL,
password=TEST_ADMIN_PASSWORD
)
self.user = get_user_model().objects.create_user(
username=TEST_USER_USERNAME, email=TEST_USER_EMAIL,
password=TEST_USER_PASSWORD
)
self.group = Group.objects.create(name=TEST_GROUP_NAME)
self.role = Role.objects.create(label=TEST_ROLE_LABEL)
self.group.user_set.add(self.user)
self.role.groups.add(self.group)
def grant_access(self, permission, obj):
AccessControlList.objects.grant(
permission=permission, role=self.role, obj=obj
)
def grant_permission(self, permission):
self.role.permissions.add(
permission.stored_permission
)

View File

@@ -24,20 +24,6 @@ class GenericViewTestCase(BaseTestCase):
def setUp(self):
super(GenericViewTestCase, self).setUp()
self.has_test_view = False
self.admin_user = get_user_model().objects.create_superuser(
username=TEST_ADMIN_USERNAME, email=TEST_ADMIN_EMAIL,
password=TEST_ADMIN_PASSWORD
)
self.user = get_user_model().objects.create_user(
username=TEST_USER_USERNAME, email=TEST_USER_EMAIL,
password=TEST_USER_PASSWORD
)
self.group = Group.objects.create(name=TEST_GROUP_NAME)
self.role = Role.objects.create(label=TEST_ROLE_LABEL)
self.group.user_set.add(self.user)
self.role.groups.add(self.group)
def tearDown(self):
from mayan.urls import urlpatterns
@@ -83,16 +69,6 @@ class GenericViewTestCase(BaseTestCase):
data=data, follow=follow
)
def grant_access(self, permission, obj):
AccessControlList.objects.grant(
permission=permission, role=self.role, obj=obj
)
def grant_permission(self, permission):
self.role.permissions.add(
permission.stored_permission
)
def login(self, username, password):
logged_in = self.client.login(username=username, password=password)

View File

@@ -0,0 +1,15 @@
from __future__ import unicode_literals
from documents.tests.test_models import GenericDocumentTestCase
class ActionTestCase(GenericDocumentTestCase):
def setUp(self):
super(ActionTestCase, self).setUp()
class MockWorkflowInstance(object):
document = self.document
class MockEntryLog(object):
workflow_instance = MockWorkflowInstance()
self.entry_log = MockEntryLog()

View File

@@ -0,0 +1,31 @@
from __future__ import unicode_literals
from document_states.tests.test_actions import ActionTestCase
from ..models import Tag
from ..workflow_actions import AttachTagAction, RemoveTagAction
from .literals import TEST_TAG_COLOR, TEST_TAG_LABEL
class TagActionTestCase(ActionTestCase):
def setUp(self):
super(TagActionTestCase, self).setUp()
self.tag = Tag.objects.create(
color=TEST_TAG_COLOR, label=TEST_TAG_LABEL
)
def test_tag_attach_action(self):
action = AttachTagAction(form_data={'tags': Tag.objects.all()})
action.execute(context={'entry_log': self.entry_log})
self.assertEqual(self.tag.documents.count(), 1)
self.assertEqual(list(self.tag.documents.all()), [self.document])
def test_tag_remove_action(self):
self.tag.attach_to(document=self.document)
action = RemoveTagAction(form_data={'tags': Tag.objects.all()})
action.execute(context={'entry_log': self.entry_log})
self.assertEqual(self.tag.documents.count(), 0)