Reorganize reusable test code

Extract test views and user code into their own separate test case
mixins. Append TestCase to test case mixins with base test code
to differentiate them from test mixins with reusable view calls.

Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2019-01-03 14:04:16 -04:00
parent c6aab93f98
commit 65ccbd3b7b
9 changed files with 268 additions and 232 deletions

View File

@@ -3,6 +3,7 @@ from __future__ import absolute_import
from __future__ import unicode_literals
from django.core.exceptions import PermissionDenied
from django.http import Http404
from rest_framework.permissions import BasePermission
@@ -33,6 +34,10 @@ class MayanPermission(BasePermission):
view, 'mayan_object_permissions', {}
).get(request.method, None)
object_permissions_raise_404 = getattr(
view, 'mayan_object_permissions_raise_404', ()
)
if required_permission:
try:
if hasattr(view, 'mayan_permission_attribute_check'):
@@ -47,7 +52,10 @@ class MayanPermission(BasePermission):
obj=obj
)
except PermissionDenied:
return False
if request.method in object_permissions_raise_404:
raise Http404
else:
return False
else:
return True
else:

View File

@@ -1,20 +1,15 @@
from __future__ import absolute_import, unicode_literals
from django.contrib.auth import get_user_model
from django.urls import reverse
from rest_framework.test import APITestCase
from mayan.apps.acls.tests.mixins import ACLBaseTestMixin
from mayan.apps.acls.tests.mixins import ACLTestCaseMixin
from mayan.apps.common.tests.mixins import ClientMethodsTestCaseMixin
from mayan.apps.permissions.classes import Permission
from mayan.apps.smart_settings.classes import Namespace
from mayan.apps.user_management.tests import (
TEST_ADMIN_PASSWORD, TEST_ADMIN_USERNAME, TEST_USER_USERNAME,
TEST_USER_PASSWORD
)
from mayan.apps.user_management.tests.mixins import UserTestCaseMixin
class BaseAPITestCase(ACLBaseTestMixin, APITestCase):
class BaseAPITestCase(ClientMethodsTestCaseMixin, ACLTestCaseMixin, UserTestCaseMixin, APITestCase):
"""
API test case class that invalidates permissions and smart settings
"""
@@ -24,78 +19,4 @@ class BaseAPITestCase(ACLBaseTestMixin, APITestCase):
Permission.invalidate_cache()
def tearDown(self):
self.client.logout()
super(BaseAPITestCase, self).tearDown()
def delete(self, viewname=None, path=None, *args, **kwargs):
data = kwargs.pop('data', {})
follow = kwargs.pop('follow', False)
if viewname:
path = reverse(viewname=viewname, *args, **kwargs)
return self.client.delete(
path=path, data=data, follow=follow
)
def get(self, viewname=None, path=None, *args, **kwargs):
data = kwargs.pop('data', {})
follow = kwargs.pop('follow', False)
if viewname:
path = reverse(viewname=viewname, *args, **kwargs)
return self.client.get(
path=path, data=data, follow=follow
)
def login(self, username, password):
logged_in = self.client.login(username=username, password=password)
user = get_user_model().objects.get(username=username)
self.assertTrue(logged_in)
self.assertTrue(user.is_authenticated)
return user.is_authenticated
def login_user(self):
self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD)
def login_admin_user(self):
self.login(username=TEST_ADMIN_USERNAME, password=TEST_ADMIN_PASSWORD)
def logout(self):
self.client.logout()
def patch(self, viewname=None, path=None, *args, **kwargs):
data = kwargs.pop('data', {})
follow = kwargs.pop('follow', False)
if viewname:
path = reverse(viewname=viewname, *args, **kwargs)
return self.client.patch(
path=path, data=data, follow=follow
)
def post(self, viewname=None, path=None, *args, **kwargs):
data = kwargs.pop('data', {})
follow = kwargs.pop('follow', False)
if viewname:
path = reverse(viewname=viewname, *args, **kwargs)
return self.client.post(
path=path, data=data, follow=follow
)
def put(self, viewname=None, path=None, *args, **kwargs):
data = kwargs.pop('data', {})
follow = kwargs.pop('follow', False)
if viewname:
path = reverse(viewname=viewname, *args, **kwargs)
return self.client.put(
path=path, data=data, follow=follow
)