diff --git a/mayan/apps/common/tests/__init__.py b/mayan/apps/common/tests/__init__.py index 2da5088d15..94b5479a25 100644 --- a/mayan/apps/common/tests/__init__.py +++ b/mayan/apps/common/tests/__init__.py @@ -1,2 +1,2 @@ -from .base import BaseTestCase # NOQA +from .base import BaseTestCase, GenericViewTestCase # NOQA from .decorators import skip_file_descriptor_check # NOQA diff --git a/mayan/apps/common/tests/base.py b/mayan/apps/common/tests/base.py index b76417d14f..2638cd5452 100644 --- a/mayan/apps/common/tests/base.py +++ b/mayan/apps/common/tests/base.py @@ -1,12 +1,23 @@ from __future__ import absolute_import, unicode_literals +from django.conf.urls import url +from django.contrib.auth import get_user_model +from django.http import HttpResponse +from django.template import Context, Template from django.test import TestCase +from django.test.utils import ContextList +from django.urls import clear_url_caches, reverse from django_downloadview import assert_download_response from permissions.classes import Permission from smart_settings.classes import Namespace +from user_management.tests import ( + TEST_ADMIN_PASSWORD, TEST_ADMIN_USERNAME, TEST_USER_USERNAME, + TEST_USER_PASSWORD +) +from .literals import TEST_VIEW_NAME, TEST_VIEW_URL from .mixins import ( ContentTypeCheckMixin, OpenFileCheckMixin, TempfileCheckMixin, UserMixin ) @@ -22,3 +33,83 @@ class BaseTestCase(UserMixin, ContentTypeCheckMixin, OpenFileCheckMixin, Tempfil super(BaseTestCase, self).setUp() Namespace.invalidate_cache_all() Permission.invalidate_cache() + + +class GenericViewTestCase(BaseTestCase): + def setUp(self): + super(GenericViewTestCase, self).setUp() + self.has_test_view = False + + def tearDown(self): + from mayan.urls import urlpatterns + + self.client.logout() + if self.has_test_view: + urlpatterns.pop(0) + super(GenericViewTestCase, self).tearDown() + + def add_test_view(self, test_object): + from mayan.urls import urlpatterns + + def test_view(request): + template = Template('{{ object }}') + context = Context( + {'object': test_object, 'resolved_object': test_object} + ) + return HttpResponse(template.render(context=context)) + + urlpatterns.insert(0, url(TEST_VIEW_URL, test_view, name=TEST_VIEW_NAME)) + clear_url_caches() + self.has_test_view = True + + def get_test_view(self): + response = self.get(TEST_VIEW_NAME) + if isinstance(response.context, ContextList): + # template widget rendering causes test client response to be + # ContextList rather than RequestContext. Typecast to dictionary + # before updating. + result = dict(response.context).copy() + result.update({'request': response.wsgi_request}) + return Context(result) + else: + response.context.update({'request': response.wsgi_request}) + return Context(response.context) + + 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) + + 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 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 + ) diff --git a/mayan/apps/common/tests/literals.py b/mayan/apps/common/tests/literals.py index 028cd26cda..b3e3bb2ef5 100644 --- a/mayan/apps/common/tests/literals.py +++ b/mayan/apps/common/tests/literals.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals +TEST_ERROR_LOG_ENTRY_RESULT = 'test_error_log_entry_result_text' TEST_VIEW_NAME = 'test view name' TEST_VIEW_URL = 'test-view-url' diff --git a/mayan/apps/common/tests/test_views.py b/mayan/apps/common/tests/test_views.py index 4ae12c4dea..407a1c2495 100644 --- a/mayan/apps/common/tests/test_views.py +++ b/mayan/apps/common/tests/test_views.py @@ -1,106 +1,15 @@ from __future__ import absolute_import, unicode_literals -from django.conf.urls import url from django.contrib.auth import get_user_model from django.contrib.contenttypes.models import ContentType -from django.http import HttpResponse -from django.template import Context, Template -from django.test.utils import ContextList -from django.urls import clear_url_caches, reverse from acls import ModelPermission -from user_management.tests import ( - TEST_ADMIN_PASSWORD, TEST_ADMIN_USERNAME, TEST_USER_USERNAME, - TEST_USER_PASSWORD -) from ..models import ErrorLogEntry from ..permissions_runtime import permission_error_log_view -from .base import BaseTestCase -from .literals import TEST_VIEW_NAME, TEST_VIEW_URL - -TEST_ERROR_LOG_ENTRY_RESULT = 'test_error_log_entry_result_text' - - -class GenericViewTestCase(BaseTestCase): - def setUp(self): - super(GenericViewTestCase, self).setUp() - self.has_test_view = False - - def tearDown(self): - from mayan.urls import urlpatterns - - self.client.logout() - if self.has_test_view: - urlpatterns.pop(0) - super(GenericViewTestCase, self).tearDown() - - def add_test_view(self, test_object): - from mayan.urls import urlpatterns - - def test_view(request): - template = Template('{{ object }}') - context = Context( - {'object': test_object, 'resolved_object': test_object} - ) - return HttpResponse(template.render(context=context)) - - urlpatterns.insert(0, url(TEST_VIEW_URL, test_view, name=TEST_VIEW_NAME)) - clear_url_caches() - self.has_test_view = True - - def get_test_view(self): - response = self.get(TEST_VIEW_NAME) - if isinstance(response.context, ContextList): - # template widget rendering causes test client response to be - # ContextList rather than RequestContext. Typecast to dictionary - # before updating. - result = dict(response.context).copy() - result.update({'request': response.wsgi_request}) - return Context(result) - else: - response.context.update({'request': response.wsgi_request}) - return Context(response.context) - - 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) - - 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 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 - ) +from .base import GenericViewTestCase +from .literals import TEST_ERROR_LOG_ENTRY_RESULT class CommonViewTestCase(GenericViewTestCase): diff --git a/mayan/apps/converter/tests/test_classes.py b/mayan/apps/converter/tests/test_transformations.py similarity index 99% rename from mayan/apps/converter/tests/test_classes.py rename to mayan/apps/converter/tests/test_transformations.py index f58adb5ccd..5c6f5c85b6 100644 --- a/mayan/apps/converter/tests/test_classes.py +++ b/mayan/apps/converter/tests/test_transformations.py @@ -2,7 +2,7 @@ from __future__ import unicode_literals from django.test import TestCase -from ..classes import ( +from ..transformations import ( BaseTransformation, TransformationResize, TransformationRotate, TransformationZoom ) diff --git a/mayan/apps/django_gpg/tests/test_views.py b/mayan/apps/django_gpg/tests/test_views.py index f4925d30ba..51488cb5da 100644 --- a/mayan/apps/django_gpg/tests/test_views.py +++ b/mayan/apps/django_gpg/tests/test_views.py @@ -2,7 +2,7 @@ from __future__ import absolute_import, unicode_literals from django_downloadview.test import assert_download_response -from common.tests.test_views import GenericViewTestCase +from common.tests import GenericViewTestCase from ..models import Key from ..permissions import permission_key_download, permission_key_upload diff --git a/mayan/apps/document_parsing/tests/test_views.py b/mayan/apps/document_parsing/tests/test_views.py index a26ccd715c..c1c0e468ff 100644 --- a/mayan/apps/document_parsing/tests/test_views.py +++ b/mayan/apps/document_parsing/tests/test_views.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +from django.test import override_settings + from documents.tests import ( GenericDocumentViewTestCase, TEST_DOCUMENT_FILENAME ) @@ -8,6 +10,7 @@ from ..permissions import permission_content_view from ..utils import get_document_content +@override_settings(OCR_AUTO_OCR=True) class DocumentContentViewsTestCase(GenericDocumentViewTestCase): _skip_file_descriptor_test = True diff --git a/mayan/apps/document_states/tests/test_views.py b/mayan/apps/document_states/tests/test_views.py index 273ccb1cc4..211561b9a6 100644 --- a/mayan/apps/document_states/tests/test_views.py +++ b/mayan/apps/document_states/tests/test_views.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals -from common.tests.test_views import GenericViewTestCase +from common.tests import GenericViewTestCase from documents.tests import ( GenericDocumentViewTestCase, TEST_SMALL_DOCUMENT_PATH ) diff --git a/mayan/apps/documents/tests/base.py b/mayan/apps/documents/tests/base.py index 43f7fb6c5d..767998d43b 100644 --- a/mayan/apps/documents/tests/base.py +++ b/mayan/apps/documents/tests/base.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals from django.test import override_settings -from common.tests.test_views import GenericViewTestCase +from common.tests import GenericViewTestCase from ..models import DocumentType diff --git a/mayan/apps/dynamic_search/tests/test_views.py b/mayan/apps/dynamic_search/tests/test_views.py index 8b99cb757c..d63c0816af 100644 --- a/mayan/apps/dynamic_search/tests/test_views.py +++ b/mayan/apps/dynamic_search/tests/test_views.py @@ -2,7 +2,7 @@ from __future__ import unicode_literals from django.test import override_settings -from common.tests.test_views import GenericViewTestCase +from common.tests import GenericViewTestCase from documents.models import DocumentType from documents.search import document_search from documents.tests import TEST_DOCUMENT_TYPE_LABEL, TEST_SMALL_DOCUMENT_PATH diff --git a/mayan/apps/mayan_statistics/tests/test_views.py b/mayan/apps/mayan_statistics/tests/test_views.py index 93cf06d632..6bff2405b3 100644 --- a/mayan/apps/mayan_statistics/tests/test_views.py +++ b/mayan/apps/mayan_statistics/tests/test_views.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals -from common.tests.test_views import GenericViewTestCase +from common.tests import GenericViewTestCase from ..classes import Statistic from ..permissions import permission_statistics_view diff --git a/mayan/apps/navigation/tests/test_classes.py b/mayan/apps/navigation/tests/test_classes.py index 6ef8dd128d..96661b8807 100644 --- a/mayan/apps/navigation/tests/test_classes.py +++ b/mayan/apps/navigation/tests/test_classes.py @@ -6,8 +6,8 @@ from django.urls import reverse from furl import furl from acls.models import AccessControlList +from common.tests import GenericViewTestCase from common.tests.literals import TEST_VIEW_NAME -from common.tests.test_views import GenericViewTestCase from permissions import Permission, PermissionNamespace from ..classes import Link, Menu diff --git a/mayan/apps/smart_settings/tests/test_view_permissions.py b/mayan/apps/smart_settings/tests/test_view_permissions.py index 7afca43d83..8aad9ace77 100644 --- a/mayan/apps/smart_settings/tests/test_view_permissions.py +++ b/mayan/apps/smart_settings/tests/test_view_permissions.py @@ -1,6 +1,6 @@ from __future__ import absolute_import, unicode_literals -from common.tests.test_views import GenericViewTestCase +from common.tests import GenericViewTestCase from ..permissions import permission_settings_view diff --git a/mayan/apps/sources/tests/test_views.py b/mayan/apps/sources/tests/test_views.py index e61c43ef93..1ed3953bea 100644 --- a/mayan/apps/sources/tests/test_views.py +++ b/mayan/apps/sources/tests/test_views.py @@ -9,7 +9,7 @@ from django.test import override_settings from django.urls import reverse from checkouts.models import NewVersionBlock -from common.tests.test_views import GenericViewTestCase +from common.tests import GenericViewTestCase from common.utils import fs_cleanup, mkdtemp from documents.models import Document, DocumentType from documents.permissions import permission_document_create diff --git a/mayan/apps/task_manager/tests/test_views.py b/mayan/apps/task_manager/tests/test_views.py index 4c1a796bdf..3f09ba40c3 100644 --- a/mayan/apps/task_manager/tests/test_views.py +++ b/mayan/apps/task_manager/tests/test_views.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals -from common.tests.test_views import GenericViewTestCase +from common.tests import GenericViewTestCase from ..classes import CeleryQueue from ..permissions import permission_task_view diff --git a/mayan/apps/user_management/tests/test_views.py b/mayan/apps/user_management/tests/test_views.py index 9e45dd38d0..15b13be5a9 100644 --- a/mayan/apps/user_management/tests/test_views.py +++ b/mayan/apps/user_management/tests/test_views.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals from django.contrib.auth import get_user_model from django.contrib.auth.models import Group -from common.tests.test_views import GenericViewTestCase +from common.tests import GenericViewTestCase from documents.tests import GenericDocumentViewTestCase from metadata.models import MetadataType