From a99b04455518f3a59b2f7768be8d6fa66463cb78 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sat, 6 Oct 2018 02:13:36 -0400 Subject: [PATCH] Code style improvement. Test code consolidation. PEP8 cleanups. Signed-off-by: Roberto Rosario --- mayan/apps/acls/tests/test_api.py | 31 +-- mayan/apps/acls/tests/test_models.py | 6 +- mayan/apps/cabinets/tests/test_api.py | 61 +++--- mayan/apps/cabinets/tests/test_models.py | 20 +- .../apps/cabinets/tests/test_wizard_steps.py | 2 +- mayan/apps/checkouts/tests/test_api.py | 8 +- mayan/apps/checkouts/tests/test_models.py | 4 +- mayan/apps/checkouts/tests/test_views.py | 48 ++--- mayan/apps/common/tests/mixins.py | 6 +- .../document_signatures/tests/test_links.py | 12 +- .../document_signatures/tests/test_models.py | 52 ++--- .../document_signatures/tests/test_views.py | 176 +++++++-------- .../apps/document_states/tests/test_views.py | 2 +- mayan/apps/documents/tests/test_api.py | 6 +- .../tests/test_deleted_document_views.py | 55 ++--- .../tests/test_document_page_views.py | 4 +- .../tests/test_document_version_views.py | 53 +++-- .../documents/tests/test_document_views.py | 201 +++++++++--------- .../tests/test_duplicated_document_views.py | 25 +-- mayan/apps/documents/tests/test_events.py | 6 +- mayan/apps/documents/tests/test_links.py | 6 +- mayan/apps/documents/tests/test_models.py | 7 +- mayan/apps/documents/tests/test_search.py | 12 +- mayan/apps/dynamic_search/tests/test_api.py | 23 +- mayan/apps/dynamic_search/tests/test_views.py | 2 +- .../apps/lock_manager/tests/test_backends.py | 2 +- mayan/apps/ocr/forms.py | 32 +-- mayan/apps/ocr/tests/literals.py | 1 + mayan/apps/ocr/tests/test_api.py | 4 +- mayan/apps/ocr/tests/test_indexing.py | 6 +- mayan/apps/ocr/tests/test_models.py | 12 +- mayan/apps/ocr/tests/test_views.py | 43 +++- mayan/apps/ocr/urls.py | 20 +- mayan/apps/ocr/views.py | 8 +- mayan/apps/permissions/tests/test_views.py | 32 ++- mayan/apps/smart_settings/classes.py | 2 +- mayan/apps/smart_settings/forms.py | 2 +- mayan/apps/sources/tests/test_links.py | 4 +- mayan/apps/sources/tests/test_models.py | 2 +- mayan/apps/sources/tests/test_views.py | 136 ++++++------ mayan/apps/tags/tests/test_api.py | 53 +++-- mayan/apps/tags/tests/test_models.py | 13 -- mayan/apps/tags/tests/test_views.py | 121 +++++------ mayan/apps/tags/tests/test_wizard_steps.py | 3 +- mayan/settings/base.py | 7 +- mayan/settings/literals.py | 2 - mayan/settings/staging/docker.py | 2 +- mayan/settings/testing/development.py | 1 - mayan/settings/testing/docker/base.py | 1 - mayan/settings/utils.py | 1 - 50 files changed, 656 insertions(+), 682 deletions(-) diff --git a/mayan/apps/acls/tests/test_api.py b/mayan/apps/acls/tests/test_api.py index 66c6c94878..359530fcf0 100644 --- a/mayan/apps/acls/tests/test_api.py +++ b/mayan/apps/acls/tests/test_api.py @@ -3,11 +3,10 @@ from __future__ import absolute_import, unicode_literals from django.contrib.contenttypes.models import ContentType from django.test import override_settings -from documents.models import DocumentType +from rest_framework import status + from documents.permissions import permission_document_view -from documents.tests.literals import ( - TEST_DOCUMENT_TYPE_LABEL, TEST_SMALL_DOCUMENT_PATH -) +from documents.tests import DocumentTestMixin from permissions.tests.literals import TEST_ROLE_LABEL from rest_api.tests import BaseAPITestCase @@ -16,27 +15,15 @@ from ..permissions import permission_acl_view @override_settings(OCR_AUTO_OCR=False) -class ACLAPITestCase(BaseAPITestCase): +class ACLAPITestCase(DocumentTestMixin, BaseAPITestCase): def setUp(self): super(ACLAPITestCase, self).setUp() self.login_admin_user() - self.document_type = DocumentType.objects.create( - label=TEST_DOCUMENT_TYPE_LABEL - ) - - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: - self.document = self.document_type.new_document( - file_object=file_object - ) self.document_content_type = ContentType.objects.get_for_model( self.document ) - def tearDown(self): - if hasattr(self, 'document_type'): - self.document_type.delete() - def _create_acl(self): self.acl = AccessControlList.objects.create( content_object=self.document, @@ -77,7 +64,7 @@ class ACLAPITestCase(BaseAPITestCase): ) ) - self.assertEqual(response.status_code, 204) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(AccessControlList.objects.count(), 0) def test_object_acl_detail_view(self): @@ -112,7 +99,7 @@ class ACLAPITestCase(BaseAPITestCase): permission.pk ) ) - self.assertEqual(response.status_code, 204) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(self.acl.permissions.count(), 0) def test_object_acl_permission_detail_view(self): @@ -162,7 +149,7 @@ class ACLAPITestCase(BaseAPITestCase): ), data={'permission_pk': permission_acl_view.pk} ) - self.assertEqual(response.status_code, 201) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertQuerysetEqual( ordered=False, qs=self.acl.permissions.all(), values=( repr(permission_document_view.stored_permission), @@ -180,7 +167,7 @@ class ACLAPITestCase(BaseAPITestCase): ), data={'role_pk': self.role.pk} ) - self.assertEqual(response.status_code, 201) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual( self.document.acls.first().role, self.role ) @@ -205,7 +192,7 @@ class ACLAPITestCase(BaseAPITestCase): } ) - self.assertEqual(response.status_code, 201) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual( self.document.acls.first().content_object, self.document ) diff --git a/mayan/apps/acls/tests/test_models.py b/mayan/apps/acls/tests/test_models.py index eed37b700d..691d35553b 100644 --- a/mayan/apps/acls/tests/test_models.py +++ b/mayan/apps/acls/tests/test_models.py @@ -26,17 +26,17 @@ class PermissionTestCase(BaseTestCase): label=TEST_DOCUMENT_TYPE_2_LABEL ) - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document_1 = self.document_type_1.new_document( file_object=file_object ) - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document_2 = self.document_type_1.new_document( file_object=file_object ) - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document_3 = self.document_type_2.new_document( file_object=file_object ) diff --git a/mayan/apps/cabinets/tests/test_api.py b/mayan/apps/cabinets/tests/test_api.py index 98cf912019..145194dbe9 100644 --- a/mayan/apps/cabinets/tests/test_api.py +++ b/mayan/apps/cabinets/tests/test_api.py @@ -5,10 +5,10 @@ from django.test import override_settings from django.urls import reverse from django.utils.encoding import force_text +from rest_framework import status from rest_framework.test import APITestCase -from documents.models import DocumentType -from documents.tests import TEST_DOCUMENT_TYPE_LABEL, TEST_SMALL_DOCUMENT_PATH +from documents.tests import DocumentTestMixin from user_management.tests.literals import ( TEST_ADMIN_EMAIL, TEST_ADMIN_PASSWORD, TEST_ADMIN_USERNAME ) @@ -19,10 +19,12 @@ from .literals import TEST_CABINET_EDITED_LABEL, TEST_CABINET_LABEL @override_settings(OCR_AUTO_OCR=False) -class CabinetAPITestCase(APITestCase): +class CabinetAPITestCase(DocumentTestMixin, APITestCase): """ Test the cabinet API endpoints """ + auto_upload_document = False + def setUp(self): super(CabinetAPITestCase, self).setUp() @@ -35,28 +37,14 @@ class CabinetAPITestCase(APITestCase): username=TEST_ADMIN_USERNAME, password=TEST_ADMIN_PASSWORD ) - self.document_type = DocumentType.objects.create( - label=TEST_DOCUMENT_TYPE_LABEL - ) - - with open(TEST_SMALL_DOCUMENT_PATH) as file_object: - self.document = self.document_type.new_document( - file_object=file_object, - ) - - with open(TEST_SMALL_DOCUMENT_PATH) as file_object: - self.document_2 = self.document_type.new_document( - file_object=file_object, - ) - - def tearDown(self): - self.document_type.delete() - super(CabinetAPITestCase, self).tearDown() + self.document = self.upload_document() + self.document_2 = self.upload_document() def test_cabinet_create(self): response = self.client.post( reverse('rest_api:cabinet-list'), {'label': TEST_CABINET_LABEL} ) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) cabinet = Cabinet.objects.first() @@ -74,6 +62,7 @@ class CabinetAPITestCase(APITestCase): ) } ) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) cabinet = Cabinet.objects.first() @@ -94,6 +83,7 @@ class CabinetAPITestCase(APITestCase): ) } ) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) cabinet = Cabinet.objects.first() @@ -114,12 +104,13 @@ class CabinetAPITestCase(APITestCase): cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) cabinet.documents.add(self.document) - self.client.delete( + response = self.client.delete( reverse( 'rest_api:cabinet-document', args=(cabinet.pk, self.document.pk) ) ) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(cabinet.documents.count(), 0) @@ -133,8 +124,11 @@ class CabinetAPITestCase(APITestCase): args=(cabinet.pk, self.document.pk) ) ) + self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data['uuid'], force_text(self.document.uuid)) + self.assertEqual( + response.data['uuid'], force_text(self.document.uuid) + ) def test_cabinet_document_list(self): cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) @@ -143,6 +137,7 @@ class CabinetAPITestCase(APITestCase): response = self.client.get( reverse('rest_api:cabinet-document-list', args=(cabinet.pk,)) ) + self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response.data['results'][0]['uuid'], force_text(self.document.uuid) @@ -151,19 +146,21 @@ class CabinetAPITestCase(APITestCase): def test_cabinet_delete(self): cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) - self.client.delete( + response = self.client.delete( reverse('rest_api:cabinet-detail', args=(cabinet.pk,)) ) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(Cabinet.objects.count(), 0) def test_cabinet_edit_via_patch(self): cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) - self.client.patch( + response = self.client.patch( reverse('rest_api:cabinet-detail', args=(cabinet.pk,)), {'label': TEST_CABINET_EDITED_LABEL} ) + self.assertEqual(response.status_code, status.HTTP_200_OK) cabinet.refresh_from_db() @@ -172,10 +169,11 @@ class CabinetAPITestCase(APITestCase): def test_cabinet_edit_via_put(self): cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) - self.client.put( + response = self.client.put( reverse('rest_api:cabinet-detail', args=(cabinet.pk,)), {'label': TEST_CABINET_EDITED_LABEL} ) + self.assertEqual(response.status_code, status.HTTP_200_OK) cabinet.refresh_from_db() @@ -184,11 +182,12 @@ class CabinetAPITestCase(APITestCase): def test_cabinet_add_document(self): cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) - self.client.post( + response = self.client.post( reverse('rest_api:cabinet-document-list', args=(cabinet.pk,)), { 'documents_pk_list': '{}'.format(self.document.pk) } ) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertQuerysetEqual( cabinet.documents.all(), (repr(self.document),) @@ -197,13 +196,14 @@ class CabinetAPITestCase(APITestCase): def test_cabinet_add_multiple_documents(self): cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) - self.client.post( + response = self.client.post( reverse('rest_api:cabinet-document-list', args=(cabinet.pk,)), { 'documents_pk_list': '{},{}'.format( self.document.pk, self.document_2.pk ) } ) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertQuerysetEqual( cabinet.documents.all(), map( @@ -220,8 +220,7 @@ class CabinetAPITestCase(APITestCase): response = self.client.get( reverse('rest_api:cabinet-list') ) - - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['results'][0]['label'], cabinet.label) def test_cabinet_remove_document(self): @@ -229,12 +228,12 @@ class CabinetAPITestCase(APITestCase): cabinet.documents.add(self.document) - self.client.delete( + response = self.client.delete( reverse( 'rest_api:cabinet-document', args=( cabinet.pk, self.document.pk ) ), ) - + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(cabinet.documents.count(), 0) diff --git a/mayan/apps/cabinets/tests/test_models.py b/mayan/apps/cabinets/tests/test_models.py index 9d3f65e14b..6f40297b88 100644 --- a/mayan/apps/cabinets/tests/test_models.py +++ b/mayan/apps/cabinets/tests/test_models.py @@ -4,8 +4,7 @@ from django.core.exceptions import ValidationError from django.test import override_settings from common.tests import BaseTestCase -from documents.models import DocumentType -from documents.tests import TEST_DOCUMENT_TYPE_LABEL, TEST_SMALL_DOCUMENT_PATH +from documents.tests import DocumentTestMixin from ..models import Cabinet @@ -13,22 +12,7 @@ from .literals import TEST_CABINET_LABEL @override_settings(OCR_AUTO_OCR=False) -class CabinetTestCase(BaseTestCase): - def setUp(self): - super(CabinetTestCase, self).setUp() - self.document_type = DocumentType.objects.create( - label=TEST_DOCUMENT_TYPE_LABEL - ) - - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: - self.document = self.document_type.new_document( - file_object=file_object - ) - - def tearDown(self): - self.document_type.delete() - super(CabinetTestCase, self).tearDown() - +class CabinetTestCase(DocumentTestMixin, BaseTestCase): def test_cabinet_creation(self): cabinet = Cabinet.objects.create(label=TEST_CABINET_LABEL) diff --git a/mayan/apps/cabinets/tests/test_wizard_steps.py b/mayan/apps/cabinets/tests/test_wizard_steps.py index f0566bfb06..09e444d1b7 100644 --- a/mayan/apps/cabinets/tests/test_wizard_steps.py +++ b/mayan/apps/cabinets/tests/test_wizard_steps.py @@ -33,7 +33,7 @@ class CabinetDocumentUploadTestCase(GenericDocumentViewTestCase): WizardStep.reregister_all() def _request_upload_interactive_document_create_view(self): - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: return self.post( viewname='sources:upload_interactive', args=(self.source.pk,), data={ diff --git a/mayan/apps/checkouts/tests/test_api.py b/mayan/apps/checkouts/tests/test_api.py index 2adac6e3f6..5fb2122658 100644 --- a/mayan/apps/checkouts/tests/test_api.py +++ b/mayan/apps/checkouts/tests/test_api.py @@ -99,7 +99,7 @@ class CheckoutsAPITestCase(DocumentTestMixin, BaseAPITestCase): self._checkout_document() response = self._request_checkout_list_view() self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertNotContains(response, text=self.document.uuid) + self.assertNotContains(response=response, text=self.document.uuid) def test_checkout_list_view_with_document_access(self): self._checkout_document() @@ -108,7 +108,7 @@ class CheckoutsAPITestCase(DocumentTestMixin, BaseAPITestCase): ) response = self._request_checkout_list_view() self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertNotContains(response, text=self.document.uuid) + self.assertNotContains(response=response, text=self.document.uuid) def test_checkout_list_view_with_checkout_access(self): self._checkout_document() @@ -117,7 +117,7 @@ class CheckoutsAPITestCase(DocumentTestMixin, BaseAPITestCase): ) response = self._request_checkout_list_view() self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertNotContains(response, text=self.document.uuid) + self.assertNotContains(response=response, text=self.document.uuid) def test_checkout_list_view_with_access(self): self._checkout_document() @@ -129,4 +129,4 @@ class CheckoutsAPITestCase(DocumentTestMixin, BaseAPITestCase): ) response = self._request_checkout_list_view() self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertContains(response, text=self.document.uuid) + self.assertContains(response=response, text=self.document.uuid) diff --git a/mayan/apps/checkouts/tests/test_models.py b/mayan/apps/checkouts/tests/test_models.py index 9d5ce8efc6..071709d167 100644 --- a/mayan/apps/checkouts/tests/test_models.py +++ b/mayan/apps/checkouts/tests/test_models.py @@ -47,7 +47,7 @@ class DocumentCheckoutTestCase(DocumentTestMixin, BaseTestCase): ) with self.assertRaises(NewDocumentVersionNotAllowed): - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document.new_version(file_object=file_object) def test_checkin_in(self): @@ -107,7 +107,7 @@ class DocumentCheckoutTestCase(DocumentTestMixin, BaseTestCase): NewVersionBlock.objects.block(document=self.document) with self.assertRaises(NewDocumentVersionNotAllowed): - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document.new_version(file_object=file_object) diff --git a/mayan/apps/checkouts/tests/test_views.py b/mayan/apps/checkouts/tests/test_views.py index 95c96750b6..b1eb783da3 100644 --- a/mayan/apps/checkouts/tests/test_views.py +++ b/mayan/apps/checkouts/tests/test_views.py @@ -21,6 +21,11 @@ from ..permissions import ( class DocumentCheckoutViewTestCase(GenericDocumentViewTestCase): + def _request_document_check_in_view(self): + return self.post( + viewname='checkouts:checkin_document', args=(self.document.pk,), + ) + def test_checkin_document_view_no_permission(self): self.login_user() @@ -33,12 +38,8 @@ class DocumentCheckoutViewTestCase(GenericDocumentViewTestCase): self.assertTrue(self.document.is_checked_out()) - response = self.post( - 'checkouts:checkin_document', args=(self.document.pk,), follow=True - ) - + response = self._request_document_check_in_view() self.assertEquals(response.status_code, 403) - self.assertTrue(self.document.is_checked_out()) def test_checkin_document_view_with_access(self): @@ -61,33 +62,30 @@ class DocumentCheckoutViewTestCase(GenericDocumentViewTestCase): permission=permission_document_checkout_detail_view ) - response = self.post( - 'checkouts:checkin_document', args=(self.document.pk,), follow=True - ) - - self.assertEquals(response.status_code, 200) - + response = self._request_document_check_in_view() + self.assertEquals(response.status_code, 302) self.assertFalse(self.document.is_checked_out()) - self.assertFalse( DocumentCheckout.objects.is_document_checked_out( document=self.document ) ) - def test_checkout_document_view_no_permission(self): - self.login_user() - - response = self.post( - 'checkouts:checkout_document', args=(self.document.pk,), data={ + def _request_document_checkout_view(self): + return self.post( + viewname='checkouts:checkout_document', args=(self.document.pk,), + data={ 'expiration_datetime_0': 2, 'expiration_datetime_1': TIME_DELTA_UNIT_DAYS, 'block_new_version': True - }, follow=True + } ) - self.assertEquals(response.status_code, 403) + def test_checkout_document_view_no_permission(self): + self.login_user() + response = self._request_document_checkout_view() + self.assertEquals(response.status_code, 403) self.assertFalse(self.document.is_checked_out()) def test_checkout_document_view_with_access(self): @@ -100,16 +98,8 @@ class DocumentCheckoutViewTestCase(GenericDocumentViewTestCase): permission=permission_document_checkout_detail_view ) - response = self.post( - 'checkouts:checkout_document', args=(self.document.pk,), data={ - 'expiration_datetime_0': 2, - 'expiration_datetime_1': TIME_DELTA_UNIT_DAYS, - 'block_new_version': True - }, follow=True - ) - - self.assertEquals(response.status_code, 200) - + response = self._request_document_checkout_view() + self.assertEquals(response.status_code, 302) self.assertTrue(self.document.is_checked_out()) def test_document_new_version_after_checkout(self): diff --git a/mayan/apps/common/tests/mixins.py b/mayan/apps/common/tests/mixins.py index 4470495788..85006a73bc 100644 --- a/mayan/apps/common/tests/mixins.py +++ b/mayan/apps/common/tests/mixins.py @@ -8,9 +8,6 @@ from django.contrib.auth import get_user_model from django.contrib.auth.models import Group from django.core import management -if getattr(settings, 'COMMON_TEST_FILE_HANDLES', False): - import psutil - from acls.models import AccessControlList from permissions.models import Role from permissions.tests.literals import TEST_ROLE_LABEL @@ -21,6 +18,9 @@ from user_management.tests import ( from ..settings import setting_temporary_directory +if getattr(settings, 'COMMON_TEST_FILE_HANDLES', False): + import psutil + class ContentTypeCheckMixin(object): expected_content_type = 'text/html; charset=utf-8' diff --git a/mayan/apps/document_signatures/tests/test_links.py b/mayan/apps/document_signatures/tests/test_links.py index e7b4802dd8..e6c9c5ce94 100644 --- a/mayan/apps/document_signatures/tests/test_links.py +++ b/mayan/apps/document_signatures/tests/test_links.py @@ -25,7 +25,7 @@ class DocumentSignatureLinksTestCase(GenericDocumentViewTestCase): self.login_user() def test_document_version_signature_detail_link_no_permission(self): - with open(TEST_SIGNED_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SIGNED_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) @@ -41,7 +41,7 @@ class DocumentSignatureLinksTestCase(GenericDocumentViewTestCase): self.assertEqual(resolved_link, None) def test_document_version_signature_detail_link_with_permission(self): - with open(TEST_SIGNED_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SIGNED_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) @@ -68,12 +68,12 @@ class DocumentSignatureLinksTestCase(GenericDocumentViewTestCase): ) def test_document_version_signature_delete_link_no_permission(self): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -90,12 +90,12 @@ class DocumentSignatureLinksTestCase(GenericDocumentViewTestCase): self.assertEqual(resolved_link, None) def test_document_version_signature_delete_link_with_permission(self): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) diff --git a/mayan/apps/document_signatures/tests/test_models.py b/mayan/apps/document_signatures/tests/test_models.py index 5f22cb486c..42aa9d7586 100644 --- a/mayan/apps/document_signatures/tests/test_models.py +++ b/mayan/apps/document_signatures/tests/test_models.py @@ -35,7 +35,7 @@ class DocumentSignaturesTestCase(BaseTestCase): super(DocumentSignaturesTestCase, self).tearDown() def test_embedded_signature_no_key(self): - with open(TEST_SIGNED_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SIGNED_DOCUMENT_PATH, mode='rb') as file_object: signed_document = self.document_type.new_document( file_object=file_object ) @@ -51,7 +51,7 @@ class DocumentSignaturesTestCase(BaseTestCase): self.assertEqual(signature.signature_id, None) def test_embedded_signature_post_key_verify(self): - with open(TEST_SIGNED_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SIGNED_DOCUMENT_PATH, mode='rb') as file_object: signed_document = self.document_type.new_document( file_object=file_object ) @@ -66,7 +66,7 @@ class DocumentSignaturesTestCase(BaseTestCase): self.assertEqual(signature.key_id, TEST_KEY_ID) self.assertEqual(signature.signature_id, None) - with open(TEST_KEY_FILE, 'rb') as file_object: + with open(TEST_KEY_FILE, mode='rb') as file_object: Key.objects.create(key_data=file_object.read()) signature = EmbeddedSignature.objects.first() @@ -74,10 +74,10 @@ class DocumentSignaturesTestCase(BaseTestCase): self.assertEqual(signature.signature_id, TEST_SIGNATURE_ID) def test_embedded_signature_post_no_key_verify(self): - with open(TEST_KEY_FILE, 'rb') as file_object: + with open(TEST_KEY_FILE, mode='rb') as file_object: key = Key.objects.create(key_data=file_object.read()) - with open(TEST_SIGNED_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SIGNED_DOCUMENT_PATH, mode='rb') as file_object: signed_document = self.document_type.new_document( file_object=file_object ) @@ -99,10 +99,10 @@ class DocumentSignaturesTestCase(BaseTestCase): self.assertEqual(signature.signature_id, None) def test_embedded_signature_with_key(self): - with open(TEST_KEY_FILE, 'rb') as file_object: + with open(TEST_KEY_FILE, mode='rb') as file_object: key = Key.objects.create(key_data=file_object.read()) - with open(TEST_SIGNED_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SIGNED_DOCUMENT_PATH, mode='rb') as file_object: self.signed_document = self.document_type.new_document( file_object=file_object ) @@ -120,12 +120,12 @@ class DocumentSignaturesTestCase(BaseTestCase): self.assertEqual(signature.signature_id, TEST_SIGNATURE_ID) def test_detached_signature_no_key(self): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -140,15 +140,15 @@ class DocumentSignaturesTestCase(BaseTestCase): self.assertEqual(signature.public_key_fingerprint, None) def test_detached_signature_with_key(self): - with open(TEST_KEY_FILE, 'rb') as file_object: + with open(TEST_KEY_FILE, mode='rb') as file_object: key = Key.objects.create(key_data=file_object.read()) - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -163,12 +163,12 @@ class DocumentSignaturesTestCase(BaseTestCase): self.assertEqual(signature.public_key_fingerprint, key.fingerprint) def test_detached_signature_post_key_verify(self): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -182,7 +182,7 @@ class DocumentSignaturesTestCase(BaseTestCase): self.assertEqual(signature.key_id, TEST_KEY_ID) self.assertEqual(signature.public_key_fingerprint, None) - with open(TEST_KEY_FILE, 'rb') as file_object: + with open(TEST_KEY_FILE, mode='rb') as file_object: key = Key.objects.create(key_data=file_object.read()) signature = DetachedSignature.objects.first() @@ -190,15 +190,15 @@ class DocumentSignaturesTestCase(BaseTestCase): self.assertEqual(signature.public_key_fingerprint, key.fingerprint) def test_detached_signature_post_no_key_verify(self): - with open(TEST_KEY_FILE, 'rb') as file_object: + with open(TEST_KEY_FILE, mode='rb') as file_object: key = Key.objects.create(key_data=file_object.read()) - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -219,7 +219,7 @@ class DocumentSignaturesTestCase(BaseTestCase): self.assertEqual(signature.public_key_fingerprint, None) def test_document_no_signature(self): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: self.document_type.new_document( file_object=file_object ) @@ -227,12 +227,12 @@ class DocumentSignaturesTestCase(BaseTestCase): self.assertEqual(EmbeddedSignature.objects.count(), 0) def test_new_signed_version(self): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNED_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SIGNED_DOCUMENT_PATH, mode='rb') as file_object: signed_version = document.new_version( file_object=file_object, comment='test comment 1' ) @@ -268,13 +268,13 @@ class EmbeddedSignaturesTestCase(BaseTestCase): TEST_SIGNED_DOCUMENT_COUNT = 2 for count in range(TEST_UNSIGNED_DOCUMENT_COUNT): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: self.document_type.new_document( file_object=file_object ) for count in range(TEST_SIGNED_DOCUMENT_COUNT): - with open(TEST_SIGNED_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SIGNED_DOCUMENT_PATH, mode='rb') as file_object: self.document_type.new_document( file_object=file_object ) @@ -296,13 +296,13 @@ class EmbeddedSignaturesTestCase(BaseTestCase): TEST_SIGNED_DOCUMENT_COUNT = 2 for count in range(TEST_UNSIGNED_DOCUMENT_COUNT): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: self.document_type.new_document( file_object=file_object ) for count in range(TEST_SIGNED_DOCUMENT_COUNT): - with open(TEST_SIGNED_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SIGNED_DOCUMENT_PATH, mode='rb') as file_object: self.document_type.new_document( file_object=file_object ) @@ -324,7 +324,7 @@ class EmbeddedSignaturesTestCase(BaseTestCase): def test_signing(self): key = Key.objects.create(key_data=TEST_KEY_DATA) - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) diff --git a/mayan/apps/document_signatures/tests/test_views.py b/mayan/apps/document_signatures/tests/test_views.py index 1d31d31e2a..c09a28ab1b 100644 --- a/mayan/apps/document_signatures/tests/test_views.py +++ b/mayan/apps/document_signatures/tests/test_views.py @@ -30,16 +30,22 @@ TEST_SIGNED_DOCUMENT_COUNT = 2 class SignaturesViewTestCase(GenericDocumentViewTestCase): + def _request_document_version_signature_list_view(self, document): + return self.get( + viewname='signatures:document_version_signature_list', + args=(document.latest_version.pk,) + ) + def test_signature_list_view_no_permission(self): - with open(TEST_KEY_FILE, 'rb') as file_object: + with open(TEST_KEY_FILE, mode='rb') as file_object: Key.objects.create(key_data=file_object.read()) - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -47,23 +53,21 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): self.login_user() - response = self.get( - 'signatures:document_version_signature_list', - args=(document.latest_version.pk,) + response = self._request_document_version_signature_list_view( + document=document ) - self.assertEqual(response.status_code, 403) def test_signature_list_view_with_access(self): - with open(TEST_KEY_FILE, 'rb') as file_object: + with open(TEST_KEY_FILE, mode='rb') as file_object: Key.objects.create(key_data=file_object.read()) - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -76,24 +80,28 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): permission=permission_document_version_signature_view ) - response = self.get( - 'signatures:document_version_signature_list', - args=(document.latest_version.pk,) + response = self._request_document_version_signature_list_view( + document=document ) - self.assertEqual(response.status_code, 200) self.assertEqual(response.context['object_list'].count(), 1) + def _request_document_version_signature_details_view(self, signature): + return self.get( + viewname='signatures:document_version_signature_details', + args=(signature.pk,) + ) + def test_signature_detail_view_no_permission(self): - with open(TEST_KEY_FILE, 'rb') as file_object: + with open(TEST_KEY_FILE, mode='rb') as file_object: Key.objects.create(key_data=file_object.read()) - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: signature = DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -101,23 +109,21 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): self.login_user() - response = self.get( - 'signatures:document_version_signature_details', - args=(signature.pk,) + response = self._request_document_version_signature_details_view( + signature=signature ) - self.assertEqual(response.status_code, 403) def test_signature_detail_view_with_access(self): - with open(TEST_KEY_FILE, 'rb') as file_object: + with open(TEST_KEY_FILE, mode='rb') as file_object: Key.objects.create(key_data=file_object.read()) - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: signature = DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -130,33 +136,37 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): permission=permission_document_version_signature_view ) - response = self.get( - 'signatures:document_version_signature_details', - args=(signature.pk,) + response = self._request_document_version_signature_details_view( + signature=signature + ) + self.assertContains( + response=response, text=signature.signature_id, status_code=200 ) - self.assertContains(response, signature.signature_id, status_code=200) + def _request_document_version_signature_upload_view(self, document_version): + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: + return self.post( + viewname='signatures:document_version_signature_upload', + args=(document_version.pk,), + data={'signature_file': file_object} + ) def test_signature_upload_view_no_permission(self): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) self.login_user() - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: - response = self.post( - 'signatures:document_version_signature_upload', - args=(document.latest_version.pk,), - data={'signature_file': file_object} - ) - + response = self._request_document_version_signature_upload_view( + document_version=document.latest_version + ) self.assertEqual(response.status_code, 403) self.assertEqual(DetachedSignature.objects.count(), 0) def test_signature_upload_view_with_access(self): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) @@ -168,23 +178,25 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): permission=permission_document_version_signature_upload ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: - response = self.post( - 'signatures:document_version_signature_upload', - args=(document.latest_version.pk,), - data={'signature_file': file_object} - ) - + response = self._request_document_version_signature_upload_view( + document_version=document.latest_version + ) self.assertEqual(response.status_code, 302) self.assertEqual(DetachedSignature.objects.count(), 1) + def _request_document_version_signature_download_view(self, signature): + return self.get( + viewname='signatures:document_version_signature_download', + args=(signature.pk,), + ) + def test_signature_download_view_no_permission(self): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: signature = DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -192,20 +204,18 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): self.login_user() - response = self.get( - 'signatures:document_version_signature_download', - args=(signature.pk,), + response = self._request_document_version_signature_download_view( + signature=signature ) - self.assertEqual(response.status_code, 403) def test_signature_download_view_with_access(self): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: signature = DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -220,9 +230,8 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): self.expected_content_type = 'application/octet-stream; charset=utf-8' - response = self.get( - 'signatures:document_version_signature_download', - args=(signature.pk,), + response = self._request_document_version_signature_download_view( + signature=signature ) with signature.signature_file as file_object: @@ -230,16 +239,22 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): self, response=response, content=file_object.read(), ) + def _request_document_version_signature_delete_view(self, signature): + return self.post( + viewname='signatures:document_version_signature_delete', + args=(signature.pk,) + ) + def test_signature_delete_view_no_permission(self): - with open(TEST_KEY_FILE, 'rb') as file_object: + with open(TEST_KEY_FILE, mode='rb') as file_object: Key.objects.create(key_data=file_object.read()) - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: signature = DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -252,24 +267,22 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): permission=permission_document_version_signature_view ) - response = self.post( - 'signatures:document_version_signature_delete', - args=(signature.pk,) + response = self._request_document_version_signature_delete_view( + signature=signature ) - self.assertEqual(response.status_code, 403) self.assertEqual(DetachedSignature.objects.count(), 1) def test_signature_delete_view_with_access(self): - with open(TEST_KEY_FILE, 'rb') as file_object: + with open(TEST_KEY_FILE, mode='rb') as file_object: Key.objects.create(key_data=file_object.read()) - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: document = self.document_type.new_document( file_object=file_object ) - with open(TEST_SIGNATURE_FILE_PATH, 'rb') as file_object: + with open(TEST_SIGNATURE_FILE_PATH, mode='rb') as file_object: signature = DetachedSignature.objects.create( document_version=document.latest_version, signature_file=File(file_object) @@ -286,14 +299,17 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): permission=permission_document_version_signature_view ) - response = self.post( - 'signatures:document_version_signature_delete', - args=(signature.pk,), follow=True + response = self._request_document_version_signature_delete_view( + signature=signature ) - - self.assertContains(response, 'deleted', status_code=200) + self.assertEqual(response.status_code, 302) self.assertEqual(DetachedSignature.objects.count(), 0) + def _request_all_document_version_signature_verify_view(self): + return self.post( + viewname='signatures:all_document_version_signature_verify' + ) + def test_missing_signature_verify_view_no_permission(self): # Silence converter logging logging.getLogger('converter.backends').setLevel(logging.CRITICAL) @@ -304,13 +320,13 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): old_hooks = DocumentVersion._post_save_hooks DocumentVersion._post_save_hooks = {} for count in range(TEST_UNSIGNED_DOCUMENT_COUNT): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: self.document_type.new_document( file_object=file_object ) for count in range(TEST_SIGNED_DOCUMENT_COUNT): - with open(TEST_SIGNED_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SIGNED_DOCUMENT_PATH, mode='rb') as file_object: self.document_type.new_document( file_object=file_object ) @@ -324,10 +340,7 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): self.login_user() - response = self.post( - 'signatures:all_document_version_signature_verify', follow=True - ) - + response = self._request_all_document_version_signature_verify_view() self.assertEqual(response.status_code, 403) self.assertEqual( @@ -345,13 +358,13 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): old_hooks = DocumentVersion._post_save_hooks DocumentVersion._post_save_hooks = {} for count in range(TEST_UNSIGNED_DOCUMENT_COUNT): - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: self.document_type.new_document( file_object=file_object ) for count in range(TEST_SIGNED_DOCUMENT_COUNT): - with open(TEST_SIGNED_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SIGNED_DOCUMENT_PATH, mode='rb') as file_object: self.document_type.new_document( file_object=file_object ) @@ -369,11 +382,8 @@ class SignaturesViewTestCase(GenericDocumentViewTestCase): permission=permission_document_version_signature_verify ) - response = self.post( - 'signatures:all_document_version_signature_verify', follow=True - ) - - self.assertContains(response, 'queued', status_code=200) + response = self._request_all_document_version_signature_verify_view() + self.assertEqual(response.status_code, 302) self.assertEqual( EmbeddedSignature.objects.unsigned_document_versions().count(), diff --git a/mayan/apps/document_states/tests/test_views.py b/mayan/apps/document_states/tests/test_views.py index 4bd3fee317..45f31aaadf 100644 --- a/mayan/apps/document_states/tests/test_views.py +++ b/mayan/apps/document_states/tests/test_views.py @@ -287,7 +287,7 @@ class DocumentStateTransitionViewTestCase(WorkflowTestMixin, GenericDocumentView self.login_user() def _create_document(self): - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document_2 = self.document_type.new_document( file_object=file_object ) diff --git a/mayan/apps/documents/tests/test_api.py b/mayan/apps/documents/tests/test_api.py index 43a2181ef9..f80e74dac0 100644 --- a/mayan/apps/documents/tests/test_api.py +++ b/mayan/apps/documents/tests/test_api.py @@ -155,7 +155,7 @@ class DocumentAPITestCase(DocumentTestMixin, BaseAPITestCase): self.login_user() def _request_document_upload(self): - with open(TEST_DOCUMENT_PATH, 'rb') as file_descriptor: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_descriptor: return self.post( viewname='rest_api:document-list', data={ 'document_type': self.document_type.pk, @@ -208,7 +208,7 @@ class DocumentAPITestCase(DocumentTestMixin, BaseAPITestCase): # is the latest. time.sleep(1) - with open(TEST_DOCUMENT_PATH, 'rb') as file_descriptor: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_descriptor: return self.post( viewname='rest_api:document-version-list', args=( self.document.pk, @@ -245,7 +245,7 @@ class DocumentAPITestCase(DocumentTestMixin, BaseAPITestCase): # Needed by MySQL as milliseconds value is not store in timestamp field time.sleep(1) - with open(TEST_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DOCUMENT_PATH, mode='rb') as file_object: self.document.new_version(file_object=file_object) def _request_document_version_revert(self): diff --git a/mayan/apps/documents/tests/test_deleted_document_views.py b/mayan/apps/documents/tests/test_deleted_document_views.py index de9f1f4e6a..eb48482fa0 100644 --- a/mayan/apps/documents/tests/test_deleted_document_views.py +++ b/mayan/apps/documents/tests/test_deleted_document_views.py @@ -16,13 +16,16 @@ class DeletedDocumentTestCase(GenericDocumentViewTestCase): super(DeletedDocumentTestCase, self).setUp() self.login_user() + def _request_document_restore_view(self): + return self.post( + viewname='documents:document_restore', args=(self.document.pk,) + ) + def test_document_restore_view_no_permission(self): self.document.delete() self.assertEqual(Document.objects.count(), 0) - response = self.post( - 'documents:document_restore', args=(self.document.pk,) - ) + response = self._request_document_restore_view() self.assertEqual(response.status_code, 403) self.assertEqual(DeletedDocument.objects.count(), 1) self.assertEqual(Document.objects.count(), 0) @@ -34,18 +37,18 @@ class DeletedDocumentTestCase(GenericDocumentViewTestCase): self.grant_access( obj=self.document, permission=permission_document_restore ) - response = self.post( - 'documents:document_restore', args=(self.document.pk,), - ) + response = self._request_document_restore_view() self.assertEqual(response.status_code, 302) self.assertEqual(DeletedDocument.objects.count(), 0) self.assertEqual(Document.objects.count(), 1) - def test_document_trash_no_permissions(self): - response = self.post( - 'documents:document_trash', args=(self.document.pk,) + def _request_document_trash_view(self): + return self.post( + viewname='documents:document_trash', args=(self.document.pk,) ) + def test_document_trash_no_permissions(self): + response = self._request_document_trash_view() self.assertEqual(response.status_code, 403) self.assertEqual(DeletedDocument.objects.count(), 0) self.assertEqual(Document.objects.count(), 1) @@ -55,22 +58,22 @@ class DeletedDocumentTestCase(GenericDocumentViewTestCase): obj=self.document, permission=permission_document_trash ) - response = self.post( - 'documents:document_trash', args=(self.document.pk,), - ) - + response = self._request_document_trash_view() self.assertEqual(response.status_code, 302) self.assertEqual(DeletedDocument.objects.count(), 1) self.assertEqual(Document.objects.count(), 0) + def _request_document_delete_view(self): + return self.post( + viewname='documents:document_delete', args=(self.document.pk,), + ) + def test_document_delete_no_permissions(self): self.document.delete() self.assertEqual(Document.objects.count(), 0) self.assertEqual(DeletedDocument.objects.count(), 1) - response = self.post( - 'documents:document_delete', args=(self.document.pk,), - ) + response = self._request_document_delete_view() self.assertEqual(response.status_code, 403) self.assertEqual(Document.objects.count(), 0) self.assertEqual(DeletedDocument.objects.count(), 1) @@ -84,19 +87,21 @@ class DeletedDocumentTestCase(GenericDocumentViewTestCase): obj=self.document, permission=permission_document_delete ) - response = self.post( - 'documents:document_delete', args=(self.document.pk,), - ) + response = self._request_document_delete_view() self.assertEqual(response.status_code, 302) self.assertEqual(DeletedDocument.objects.count(), 0) self.assertEqual(Document.objects.count(), 0) + def _request_document_list_deleted_view(self): + return self.get(viewname='documents:document_list_deleted') + def test_deleted_document_list_view_no_permissions(self): self.document.delete() - response = self.get('documents:document_list_deleted') - - self.assertNotContains(response, self.document.label, status_code=200) + response = self._request_document_list_deleted_view() + self.assertNotContains( + response=response, text=self.document.label, status_code=200 + ) def test_deleted_document_list_view_with_access(self): self.document.delete() @@ -104,6 +109,8 @@ class DeletedDocumentTestCase(GenericDocumentViewTestCase): self.grant_access( obj=self.document, permission=permission_document_view ) - response = self.get('documents:document_list_deleted') + response = self._request_document_list_deleted_view() - self.assertContains(response, self.document.label, status_code=200) + self.assertContains( + response=response, text=self.document.label, status_code=200 + ) diff --git a/mayan/apps/documents/tests/test_document_page_views.py b/mayan/apps/documents/tests/test_document_page_views.py index 12c21e9948..68300c8e25 100644 --- a/mayan/apps/documents/tests/test_document_page_views.py +++ b/mayan/apps/documents/tests/test_document_page_views.py @@ -14,7 +14,7 @@ class DocumentPageViewTestCase(GenericDocumentViewTestCase): def _document_page_list_view(self): return self.get( - 'documents:document_pages', args=(self.document.pk,) + viewname='documents:document_pages', args=(self.document.pk,) ) def test_document_page_list_view_no_permission(self): @@ -27,5 +27,5 @@ class DocumentPageViewTestCase(GenericDocumentViewTestCase): ) response = self._document_page_list_view() self.assertContains( - response, text=self.document.label, status_code=200 + response=response, text=self.document.label, status_code=200 ) diff --git a/mayan/apps/documents/tests/test_document_version_views.py b/mayan/apps/documents/tests/test_document_version_views.py index 30eda8de5b..e4e8766713 100644 --- a/mayan/apps/documents/tests/test_document_version_views.py +++ b/mayan/apps/documents/tests/test_document_version_views.py @@ -17,64 +17,59 @@ class DocumentVersionTestCase(GenericDocumentViewTestCase): super(DocumentVersionTestCase, self).setUp() self.login_user() - def test_document_version_list_no_permission(self): - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + def _upload_new_version(self): + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document.new_version( comment=TEST_VERSION_COMMENT, file_object=file_object ) - response = self.get( - 'documents:document_version_list', args=(self.document.pk,) + def _request_document_version_list_view(self): + return self.get( + viewname='documents:document_version_list', + args=(self.document.pk,) ) + def test_document_version_list_no_permission(self): + self._upload_new_version() + response = self._request_document_version_list_view() self.assertEqual(response.status_code, 403) def test_document_version_list_with_access(self): + self._upload_new_version() self.grant_access( obj=self.document, permission=permission_document_version_view ) - - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: - self.document.new_version( - comment=TEST_VERSION_COMMENT, file_object=file_object - ) - - response = self.get( - 'documents:document_version_list', args=(self.document.pk,) + response = self._request_document_version_list_view() + self.assertContains( + response=response, text=TEST_VERSION_COMMENT, status_code=200 ) - self.assertContains(response, TEST_VERSION_COMMENT, status_code=200) + def _request_document_version_revert_view(self, document_version): + return self.post( + viewname='documents:document_version_revert', + args=(document_version.pk,) + ) def test_document_version_revert_no_permission(self): first_version = self.document.latest_version + self._upload_new_version() - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: - self.document.new_version( - file_object=file_object - ) - - response = self.post( - 'documents:document_version_revert', args=(first_version.pk,) + response = self._request_document_version_revert_view( + document_version=first_version ) - self.assertEqual(response.status_code, 403) self.assertEqual(self.document.versions.count(), 2) def test_document_version_revert_with_access(self): first_version = self.document.latest_version - - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: - self.document.new_version( - file_object=file_object - ) + self._upload_new_version() self.grant_access( obj=self.document, permission=permission_document_version_revert ) - response = self.post( - 'documents:document_version_revert', args=(first_version.pk,), + response = self._request_document_version_revert_view( + document_version=first_version ) - self.assertEqual(response.status_code, 302) self.assertEqual(self.document.versions.count(), 1) diff --git a/mayan/apps/documents/tests/test_document_views.py b/mayan/apps/documents/tests/test_document_views.py index 8f6fd12ed8..a5bb421d81 100644 --- a/mayan/apps/documents/tests/test_document_views.py +++ b/mayan/apps/documents/tests/test_document_views.py @@ -32,28 +32,30 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): super(DocumentsViewsTestCase, self).setUp() self.login_user() - def test_document_view_no_permissions(self): - response = self.get( - 'documents:document_properties', args=(self.document.pk,) + def _request_document_properties_view(self): + return self.get( + viewname='documents:document_properties', + args=(self.document.pk,) ) + def test_document_view_no_permissions(self): + response = self._request_document_properties_view() self.assertEqual(response.status_code, 403) def test_document_view_with_permissions(self): self.grant_access( obj=self.document, permission=permission_document_view ) - response = self.get( - 'documents:document_properties', args=(self.document.pk,), - follow=True + response = self._request_document_properties_view() + self.assertContains( + response=response, text=self.document.label, status_code=200 ) - self.assertContains( - response, 'roperties for document', status_code=200 - ) + def _request_document_list_view(self): + return self.get(viewname='documents:document_list') def test_document_list_view_no_permissions(self): - response = self.get('documents:document_list') + response = self._request_document_list_view() self.assertEqual(response.status_code, 200) self.assertEqual(response.context['object_list'].count(), 0) @@ -61,12 +63,14 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): self.grant_access( obj=self.document, permission=permission_document_view ) - response = self.get('documents:document_list') - self.assertContains(response, self.document.label, status_code=200) + response = self._request_document_list_view() + self.assertContains( + response=response, text=self.document.label, status_code=200 + ) def _request_document_type_edit(self, document_type): return self.post( - 'documents:document_document_type_edit', + viewname='documents:document_document_type_edit', args=(self.document.pk,), data={'document_type': document_type.pk} ) @@ -85,7 +89,7 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): ) self.assertContains( - response, text='Select a valid choice', status_code=200 + response=response, text='Select a valid choice', status_code=200 ) self.assertEqual( @@ -122,7 +126,7 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): def _request_multiple_document_type_edit(self, document_type): return self.post( - 'documents:document_multiple_document_type_edit', + viewname='documents:document_multiple_document_type_edit', data={ 'id_list': self.document.pk, 'document_type': document_type.pk @@ -143,7 +147,7 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): ) self.assertContains( - response, text='Select a valid choice.', status_code=200 + response=response, text='Select a valid choice.', status_code=200 ) self.assertEqual( @@ -178,15 +182,15 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): def _request_document_download_form_view(self): return self.get( - 'documents:document_download_form', args=(self.document.pk,), - follow=True, + viewname='documents:document_download_form', + args=(self.document.pk,), ) def test_document_download_form_view_no_permission(self): response = self._request_document_download_form_view() self.assertNotContains( - response, text=self.document.label, status_code=200 + response=response, text=self.document.label, status_code=200 ) def test_document_download_form_view_with_access(self): @@ -196,14 +200,16 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): response = self._request_document_download_form_view() self.assertContains( - response, text=self.document.label, status_code=200 + response=response, text=self.document.label, status_code=200 + ) + + def _request_document_download_view(self): + return self.get( + viewname='documents:document_download', args=(self.document.pk,) ) def test_document_download_view_no_permission(self): - response = self.get( - 'documents:document_download', args=(self.document.pk,) - ) - + response = self._request_document_download_view() self.assertEqual(response.status_code, 403) def test_document_download_view_with_permission(self): @@ -216,25 +222,24 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): self.grant_access( obj=self.document, permission=permission_document_download ) - response = self.get( - 'documents:document_download', args=(self.document.pk,) - ) - + response = self._request_document_download_view() self.assertEqual(response.status_code, 200) with self.document.open() as file_object: self.assert_download_response( - response, content=file_object.read(), + response=response, content=file_object.read(), basename=TEST_SMALL_DOCUMENT_FILENAME, mime_type=self.document.file_mimetype ) - def test_document_multiple_download_view_no_permission(self): - response = self.get( - 'documents:document_multiple_download', + def _request_document_multiple_download_view(self): + return self.get( + viewname='documents:document_multiple_download', data={'id_list': self.document.pk} ) + def test_document_multiple_download_view_no_permission(self): + response = self._request_document_multiple_download_view() self.assertEqual(response.status_code, 403) def test_document_multiple_download_view_with_permission(self): @@ -247,16 +252,12 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): obj=self.document, permission=permission_document_download ) - response = self.get( - 'documents:document_multiple_download', - data={'id_list': self.document.pk} - ) - + response = self._request_document_multiple_download_view() self.assertEqual(response.status_code, 200) with self.document.open() as file_object: self.assert_download_response( - response, content=file_object.read(), + response=response, content=file_object.read(), basename=TEST_SMALL_DOCUMENT_FILENAME, mime_type=self.document.file_mimetype ) @@ -264,14 +265,13 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): def _request_document_version_download(self, data=None): data = data or {} return self.get( - 'documents:document_version_download', args=( + viewname='documents:document_version_download', args=( self.document.latest_version.pk, ), data=data ) def test_document_version_download_view_no_permission(self): response = self._request_document_version_download() - self.assertEqual(response.status_code, 403) def test_document_version_download_view_with_permission(self): @@ -285,12 +285,11 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): obj=self.document, permission=permission_document_download ) response = self._request_document_version_download() - self.assertEqual(response.status_code, 200) with self.document.open() as file_object: self.assert_download_response( - response, content=file_object.read(), + response=response, content=file_object.read(), basename=force_text(self.document.latest_version), mime_type='{}; charset=utf-8'.format( self.document.latest_version.mimetype @@ -315,7 +314,7 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): with self.document.open() as file_object: self.assert_download_response( - response, content=file_object.read(), + response=response, content=file_object.read(), basename=self.document.latest_version.get_rendered_string( preserve_extension=True ), mime_type='{}; charset=utf-8'.format( @@ -323,14 +322,17 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): ) ) + def _request_document_update_page_count_view(self): + return self.post( + viewname='documents:document_update_page_count', + args=(self.document.pk,) + ) + def test_document_update_page_count_view_no_permission(self): self.document.pages.all().delete() self.assertEqual(self.document.pages.count(), 0) - response = self.post( - 'documents:document_update_page_count', args=(self.document.pk,) - ) - + response = self._request_document_update_page_count_view() self.assertEqual(response.status_code, 302) self.assertEqual(self.document.pages.count(), 0) @@ -343,22 +345,21 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): self.grant_permission(permission=permission_document_tools) - response = self.post( - 'documents:document_update_page_count', - args=(self.document.pk,) - ) + response = self._request_document_update_page_count_view() self.assertEqual(response.status_code, 302) self.assertEqual(self.document.pages.count(), page_count) + def _request_document_multiple_update_page_count_view(self): + return self.post( + viewname='documents:document_multiple_update_page_count', + data={'id_list': self.document.pk} + ) + def test_document_multiple_update_page_count_view_no_permission(self): self.document.pages.all().delete() self.assertEqual(self.document.pages.count(), 0) - response = self.post( - 'documents:document_multiple_update_page_count', - data={'id_list': self.document.pk} - ) - + response = self._request_document_multiple_update_page_count_view() self.assertEqual(response.status_code, 302) self.assertEqual(self.document.pages.count(), 0) @@ -369,13 +370,16 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): self.grant_permission(permission=permission_document_tools) - response = self.post( - 'documents:document_multiple_update_page_count', - data={'id_list': self.document.pk} - ) + response = self._request_document_multiple_update_page_count_view() self.assertEqual(response.status_code, 302) self.assertEqual(self.document.pages.count(), page_count) + def _request_document_clear_transformations_view(self): + return self.post( + viewname='documents:document_clear_transformations', + args=(self.document.pk,) + ) + def test_document_clear_transformations_view_no_permission(self): document_page = self.document.pages.first() content_type = ContentType.objects.get_for_model(document_page) @@ -394,11 +398,7 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): obj=self.document, permission=permission_document_view ) - response = self.post( - 'documents:document_clear_transformations', - args=(self.document.pk,) - ) - + response = self._request_document_clear_transformations_view() self.assertEqual(response.status_code, 302) self.assertQuerysetEqual( Transformation.objects.get_for_model(document_page), @@ -425,16 +425,18 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): obj=self.document, permission=permission_document_view ) - response = self.post( - 'documents:document_clear_transformations', - args=(self.document.pk,) - ) - + response = self._request_document_clear_transformations_view() self.assertEqual(response.status_code, 302) self.assertEqual( Transformation.objects.get_for_model(document_page).count(), 0 ) + def _request_document_multiple_clear_transformations(self): + return self.post( + viewname='documents:document_multiple_clear_transformations', + data={'id_list': self.document.pk} + ) + def test_document_multiple_clear_transformations_view_no_permission(self): document_page = self.document.pages.first() content_type = ContentType.objects.get_for_model(document_page) @@ -451,11 +453,7 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): self.grant_permission(permission=permission_document_view) - response = self.post( - 'documents:document_multiple_clear_transformations', - data={'id_list': self.document.pk} - ) - + response = self._request_document_multiple_clear_transformations() self.assertEqual(response.status_code, 302) self.assertQuerysetEqual( Transformation.objects.get_for_model(document_page), @@ -483,27 +481,21 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): obj=self.document, permission=permission_transformation_delete ) - response = self.post( - 'documents:document_multiple_clear_transformations', - data={'id_list': self.document.pk}, follow=True - ) - - self.assertEqual(response.status_code, 200) + response = self._request_document_multiple_clear_transformations() + self.assertEqual(response.status_code, 302) self.assertEqual( Transformation.objects.get_for_model(document_page).count(), 0 ) - def _empty_trash(self): - return self.post('documents:trash_can_empty') + def _request_empty_trash_view(self): + return self.post(viewname='documents:trash_can_empty') def test_trash_can_empty_view_no_permission(self): self.document.delete() self.assertEqual(DeletedDocument.objects.count(), 1) - response = self._empty_trash() - + response = self._request_empty_trash_view() self.assertEqual(response.status_code, 403) - self.assertEqual(DeletedDocument.objects.count(), 1) def test_trash_can_empty_view_with_permission(self): @@ -512,39 +504,39 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): self.grant_permission(permission=permission_empty_trash) - response = self._empty_trash() - + response = self._request_empty_trash_view() self.assertEqual(response.status_code, 302) self.assertEqual(DeletedDocument.objects.count(), 0) self.assertEqual(Document.objects.count(), 0) - def test_document_page_view_no_permissions(self): - response = self.get( - 'documents:document_page_view', args=( - self.document.pages.first().pk, + def _request_document_page_view(self, document_page): + return self.get( + viewname='documents:document_page_view', args=( + document_page.pk, ) ) + def test_document_page_view_no_permissions(self): + response = self._request_document_page_view( + document_page=self.document.pages.first() + ) self.assertEqual(response.status_code, 403) def test_document_page_view_with_access(self): self.grant_access( obj=self.document, permission=permission_document_view ) - response = self.get( - 'documents:document_page_view', args=( - self.document.pages.first().pk, - ), - follow=True + response = self._request_document_page_view( + document_page=self.document.pages.first() ) - self.assertContains( - response, force_text(self.document.pages.first()), status_code=200 + response=response, text=force_text(self.document.pages.first()), + status_code=200 ) - def _request_print_view(self): + def _request_document_print_view(self): return self.get( - 'documents:document_print', args=( + viewname='documents:document_print', args=( self.document.pk, ), data={ 'page_group': PAGE_RANGE_ALL @@ -552,14 +544,14 @@ class DocumentsViewsTestCase(GenericDocumentViewTestCase): ) def test_document_print_view_no_access(self): - response = self ._request_print_view() + response = self._request_document_print_view() self.assertEqual(response.status_code, 403) def test_document_print_view_with_access(self): self.grant_access( obj=self.document, permission=permission_document_print ) - response = self._request_print_view() + response = self._request_document_print_view() self.assertEqual(response.status_code, 200) @@ -571,7 +563,8 @@ class DocumentsQuickLabelViewsTestCase(DocumentTypeQuickLabelTestMixin, GenericD def _request_document_quick_label_edit_view(self, extra_data=None): data = { 'document_type_available_filenames': self.document_type_filename.pk, - 'label': '' # View needs at least an empty label for quick + 'label': '' + # View needs at least an empty label for quick # label to work. Cause is unknown. } data.update(extra_data or {}) diff --git a/mayan/apps/documents/tests/test_duplicated_document_views.py b/mayan/apps/documents/tests/test_duplicated_document_views.py index c71c44517b..29be7c96d7 100644 --- a/mayan/apps/documents/tests/test_duplicated_document_views.py +++ b/mayan/apps/documents/tests/test_duplicated_document_views.py @@ -16,25 +16,26 @@ class DuplicatedDocumentsViewsTestCase(GenericDocumentViewTestCase): self.login_user() def _upload_duplicate_document(self): - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document_duplicate = self.document_type.new_document( file_object=file_object, label=TEST_SMALL_DOCUMENT_FILENAME ) - def _request_duplicated_document_list(self): - return self.get('documents:duplicated_document_list') + def _request_duplicated_document_list_view(self): + return self.get(viewname='documents:duplicated_document_list') - def _request_document_duplicates_list(self): + def _request_document_duplicates_list_view(self): return self.get( - 'documents:document_duplicates_list', args=(self.document.pk,) + viewname='documents:document_duplicates_list', + args=(self.document.pk,) ) def test_duplicated_document_list_no_permissions(self): self._upload_duplicate_document() - response = self._request_duplicated_document_list() + response = self._request_duplicated_document_list_view() self.assertNotContains( - response, text=self.document.label, status_code=200 + response=response, text=self.document.label, status_code=200 ) def test_duplicated_document_list_with_access(self): @@ -46,15 +47,15 @@ class DuplicatedDocumentsViewsTestCase(GenericDocumentViewTestCase): obj=self.document_duplicate, permission=permission_document_view ) - response = self._request_duplicated_document_list() + response = self._request_duplicated_document_list_view() self.assertContains( - response, text=self.document.label, status_code=200 + response=response, text=self.document.label, status_code=200 ) def test_document_duplicates_list_no_permissions(self): self._upload_duplicate_document() - response = self._request_document_duplicates_list() + response = self._request_document_duplicates_list_view() self.assertEqual(response.status_code, 403) @@ -67,8 +68,8 @@ class DuplicatedDocumentsViewsTestCase(GenericDocumentViewTestCase): obj=self.document_duplicate, permission=permission_document_view ) - response = self._request_document_duplicates_list() + response = self._request_document_duplicates_list_view() self.assertContains( - response, text=self.document.label, status_code=200 + response=response, text=self.document.label, status_code=200 ) diff --git a/mayan/apps/documents/tests/test_events.py b/mayan/apps/documents/tests/test_events.py index ea11f31e93..540fca55b4 100644 --- a/mayan/apps/documents/tests/test_events.py +++ b/mayan/apps/documents/tests/test_events.py @@ -52,7 +52,7 @@ class DocumentEventsTestCase(GenericDocumentViewTestCase): self.expected_content_type = 'image/png; charset=utf-8' response = self.get( - 'documents:document_download', args=(self.document.pk,), + viewname='documents:document_download', args=(self.document.pk,), ) # Download the file to close the file descriptor @@ -76,7 +76,7 @@ class DocumentEventsTestCase(GenericDocumentViewTestCase): Action.objects.all().delete() response = self.get( - 'documents:document_preview', args=(self.document.pk,) + viewname='documents:document_preview', args=(self.document.pk,) ) self.assertEqual(response.status_code, 403) @@ -93,7 +93,7 @@ class DocumentEventsTestCase(GenericDocumentViewTestCase): permission_document_view.stored_permission ) self.get( - 'documents:document_preview', args=(self.document.pk,), + viewname='documents:document_preview', args=(self.document.pk,), ) event = Action.objects.any(obj=self.document).first() diff --git a/mayan/apps/documents/tests/test_links.py b/mayan/apps/documents/tests/test_links.py index 1e67404d62..4ee96b545c 100644 --- a/mayan/apps/documents/tests/test_links.py +++ b/mayan/apps/documents/tests/test_links.py @@ -23,7 +23,7 @@ from .literals import TEST_SMALL_DOCUMENT_PATH class DocumentsLinksTestCase(GenericDocumentViewTestCase): def test_document_version_revert_link_no_permission(self): - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document.new_version(file_object=file_object) self.assertTrue(self.document.versions.count(), 2) @@ -39,9 +39,9 @@ class DocumentsLinksTestCase(GenericDocumentViewTestCase): def test_document_version_revert_link_with_permission(self): # Needed by MySQL as milliseconds value is not store in timestamp # field - time.sleep(2) + time.sleep(1.01) - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document.new_version(file_object=file_object) self.assertTrue(self.document.versions.count(), 2) diff --git a/mayan/apps/documents/tests/test_models.py b/mayan/apps/documents/tests/test_models.py index 1bb99983ae..251b04f03e 100644 --- a/mayan/apps/documents/tests/test_models.py +++ b/mayan/apps/documents/tests/test_models.py @@ -18,7 +18,7 @@ from .literals import ( TEST_PDF_INDIRECT_ROTATE_PATH, TEST_OFFICE_DOCUMENT_PATH, TEST_SMALL_DOCUMENT_CHECKSUM, TEST_SMALL_DOCUMENT_FILENAME, TEST_SMALL_DOCUMENT_MIMETYPE, TEST_SMALL_DOCUMENT_PATH, - TEST_SMALL_DOCUMENT_SIZE, TEST_DOCUMENT_FILENAME + TEST_SMALL_DOCUMENT_SIZE ) from .mixins import DocumentTestMixin @@ -27,7 +27,6 @@ from .mixins import DocumentTestMixin class DocumentTestCase(DocumentTestMixin, BaseTestCase): def test_natural_keys(self): self.document.pages.first().generate_image() - self._test_database_conversion('documents') def test_document_creation(self): @@ -47,10 +46,10 @@ class DocumentTestCase(DocumentTestMixin, BaseTestCase): self.assertEqual(self.document.page_count, 1) def test_version_creation(self): - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document.new_version(file_object=file_object) - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document.new_version( file_object=file_object, comment='test comment 1' ) diff --git a/mayan/apps/documents/tests/test_search.py b/mayan/apps/documents/tests/test_search.py index c7cd45ed3d..660fcecdaf 100644 --- a/mayan/apps/documents/tests/test_search.py +++ b/mayan/apps/documents/tests/test_search.py @@ -12,12 +12,12 @@ from documents.tests import DocumentTestMixin class DocumentSearchTestCase(DocumentTestMixin, BaseTestCase): def _perform_document_page_search(self): return document_page_search.search( - {'q': self.document.label}, user=self.user + query_string={'q': self.document.label}, user=self.user ) def _perform_document_search(self): return document_search.search( - {'q': self.document.label}, user=self.user + query_string={'q': self.document.label}, user=self.user ) def test_document_page_search_no_access(self): @@ -25,7 +25,9 @@ class DocumentSearchTestCase(DocumentTestMixin, BaseTestCase): self.assertFalse(self.document.pages.first() in queryset) def test_document_page_search_with_access(self): - self.grant_access(permission=permission_document_view, obj=self.document) + self.grant_access( + permission=permission_document_view, obj=self.document + ) queryset, elapsed_time = self._perform_document_page_search() self.assertTrue(self.document.pages.first() in queryset) @@ -34,6 +36,8 @@ class DocumentSearchTestCase(DocumentTestMixin, BaseTestCase): self.assertFalse(self.document in queryset) def test_document_search_with_access(self): - self.grant_access(permission=permission_document_view, obj=self.document) + self.grant_access( + permission=permission_document_view, obj=self.document + ) queryset, elapsed_time = self._perform_document_search() self.assertTrue(self.document in queryset) diff --git a/mayan/apps/dynamic_search/tests/test_api.py b/mayan/apps/dynamic_search/tests/test_api.py index 6aba9462e3..7628795063 100644 --- a/mayan/apps/dynamic_search/tests/test_api.py +++ b/mayan/apps/dynamic_search/tests/test_api.py @@ -5,19 +5,18 @@ from django.urls import reverse from rest_framework import status -from documents.models import DocumentType from documents.search import document_search from documents.permissions import permission_document_view -from documents.tests import ( - TEST_DOCUMENT_TYPE_LABEL, TEST_SMALL_DOCUMENT_PATH -) +from documents.tests import DocumentTestMixin from rest_api.tests import BaseAPITestCase from ..classes import SearchModel @override_settings(OCR_AUTO_OCR=False) -class SearchAPITestCase(BaseAPITestCase): +class SearchAPITestCase(DocumentTestMixin, BaseAPITestCase): + auto_upload_document = False + def setUp(self): super(SearchAPITestCase, self).setUp() self.login_user() @@ -33,24 +32,14 @@ class SearchAPITestCase(BaseAPITestCase): ) ) - def _create_document(self): - self.document_type = DocumentType.objects.create( - label=TEST_DOCUMENT_TYPE_LABEL - ) - - with open(TEST_SMALL_DOCUMENT_PATH) as file_object: - self.document = self.document_type.new_document( - file_object=file_object, - ) - def test_search_no_access(self): - self._create_document() + self.document = self.upload_document() response = self._request_search_view() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 0) def test_search_with_access(self): - self._create_document() + self.document = self.upload_document() self.grant_access( permission=permission_document_view, obj=self.document ) diff --git a/mayan/apps/dynamic_search/tests/test_views.py b/mayan/apps/dynamic_search/tests/test_views.py index 1fc6cde37d..7ab2ffb5b7 100644 --- a/mayan/apps/dynamic_search/tests/test_views.py +++ b/mayan/apps/dynamic_search/tests/test_views.py @@ -25,7 +25,7 @@ class Issue46TestCase(GenericViewTestCase): # Upload many instances of the same test document for i in range(self.document_count): - with open(TEST_SMALL_DOCUMENT_PATH) as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: self.document_type.new_document( file_object=file_object, label='test document', diff --git a/mayan/apps/lock_manager/tests/test_backends.py b/mayan/apps/lock_manager/tests/test_backends.py index 56efaec516..e302c309dd 100644 --- a/mayan/apps/lock_manager/tests/test_backends.py +++ b/mayan/apps/lock_manager/tests/test_backends.py @@ -60,7 +60,7 @@ class FileLockTestCase(TestCase): # would be successfull, even after an extended lapse of time def test_release_expired_reaquired(self): - lock_1 = self.locking_backend.acquire_lock(name=TEST_LOCK_1, timeout=1) + self.locking_backend.acquire_lock(name=TEST_LOCK_1, timeout=1) time.sleep(1.01) # TEST_LOCK_1 is expired so trying to acquire it should not return an # error. diff --git a/mayan/apps/ocr/forms.py b/mayan/apps/ocr/forms.py index 76be2c3078..9aef5501bf 100644 --- a/mayan/apps/ocr/forms.py +++ b/mayan/apps/ocr/forms.py @@ -13,6 +13,16 @@ from .models import DocumentPageOCRContent class DocumentPageOCRContentForm(forms.Form): + contents = forms.CharField( + label=_('Contents'), + widget=TextAreaDiv( + attrs={ + 'class': 'text_area_div full-height', + 'data-height-difference': 360 + } + ) + ) + def __init__(self, *args, **kwargs): page = kwargs.pop('instance', None) super(DocumentPageOCRContentForm, self).__init__(*args, **kwargs) @@ -28,6 +38,12 @@ class DocumentPageOCRContentForm(forms.Form): self.fields['contents'].initial = mark_safe(content) + +class DocumentOCRContentForm(forms.Form): + """ + Form that concatenates all of a document pages' text content into a + single textarea widget + """ contents = forms.CharField( label=_('Contents'), widget=TextAreaDiv( @@ -38,12 +54,6 @@ class DocumentPageOCRContentForm(forms.Form): ) ) - -class DocumentOCRContentForm(forms.Form): - """ - Form that concatenates all of a document pages' text content into a - single textarea widget - """ def __init__(self, *args, **kwargs): self.document = kwargs.pop('instance', None) super(DocumentOCRContentForm, self).__init__(*args, **kwargs) @@ -71,16 +81,6 @@ class DocumentOCRContentForm(forms.Form): self.fields['contents'].initial = mark_safe(''.join(content)) - contents = forms.CharField( - label=_('Contents'), - widget=TextAreaDiv( - attrs={ - 'class': 'text_area_div full-height', - 'data-height-difference': 360 - } - ) - ) - class DocumentTypeSelectForm(forms.Form): document_type = forms.ModelChoiceField( diff --git a/mayan/apps/ocr/tests/literals.py b/mayan/apps/ocr/tests/literals.py index 49de0a9aae..6837767bd3 100644 --- a/mayan/apps/ocr/tests/literals.py +++ b/mayan/apps/ocr/tests/literals.py @@ -1,3 +1,4 @@ from __future__ import unicode_literals TEST_OCR_INDEX_NODE_TEMPLATE = '{% if "mayan" in document.latest_version.ocr_content|join:" "|lower %}mayan{% endif %}' +TEST_OCR_INDEX_NODE_TEMPLATE_LEVEL = 'mayan' diff --git a/mayan/apps/ocr/tests/test_api.py b/mayan/apps/ocr/tests/test_api.py index ab14eb91bb..c3aadceac3 100644 --- a/mayan/apps/ocr/tests/test_api.py +++ b/mayan/apps/ocr/tests/test_api.py @@ -11,6 +11,8 @@ from ..permissions import ( permission_ocr_document, permission_ocr_content_view, ) +TEST_DOCUMENT_CONTENT = 'Mayan EDMS Documentation' + @override_settings(OCR_AUTO_OCR=False) @override_settings(DOCUMENT_PARSING_PDFTOTEXT_PATH='') @@ -85,5 +87,5 @@ class OCRAPITestCase(DocumentTestMixin, BaseAPITestCase): response = self._request_document_page_content_view() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue( - 'Mayan EDMS Documentation' in response.data['content'] + TEST_DOCUMENT_CONTENT in response.data['content'] ) diff --git a/mayan/apps/ocr/tests/test_indexing.py b/mayan/apps/ocr/tests/test_indexing.py index 56c330c1f1..79a9ad3bfb 100644 --- a/mayan/apps/ocr/tests/test_indexing.py +++ b/mayan/apps/ocr/tests/test_indexing.py @@ -7,7 +7,9 @@ from documents.tests import DocumentTestMixin from document_indexing.models import Index, IndexInstanceNode from document_indexing.tests.literals import TEST_INDEX_LABEL -from .literals import TEST_OCR_INDEX_NODE_TEMPLATE +from .literals import ( + TEST_OCR_INDEX_NODE_TEMPLATE, TEST_OCR_INDEX_NODE_TEMPLATE_LEVEL +) @override_settings(OCR_AUTO_OCR=False) @@ -30,6 +32,6 @@ class OCRIndexingTestCase(DocumentTestMixin, BaseTestCase): self.assertTrue( self.document in IndexInstanceNode.objects.get( - value='mayan' + value=TEST_OCR_INDEX_NODE_TEMPLATE_LEVEL ).documents.all() ) diff --git a/mayan/apps/ocr/tests/test_models.py b/mayan/apps/ocr/tests/test_models.py index 4e44c7e50a..ae805a2427 100644 --- a/mayan/apps/ocr/tests/test_models.py +++ b/mayan/apps/ocr/tests/test_models.py @@ -9,6 +9,10 @@ from documents.tests import ( DocumentTestMixin, TEST_DEU_DOCUMENT_PATH, TEST_DOCUMENT_TYPE_LABEL ) +TEST_DOCUMENT_CONTENT = 'Mayan EDMS Documentation' +TEST_DOCUMENT_CONTENT_DEU_1 = 'Repository für elektronische Dokumente.' +TEST_DOCUMENT_CONTENT_DEU_2 = 'Es bietet einen' + class DocumentOCRTestCase(DocumentTestMixin, BaseTestCase): # PyOCR's leak descriptor in get_available_languages and image_to_string @@ -17,7 +21,7 @@ class DocumentOCRTestCase(DocumentTestMixin, BaseTestCase): def test_ocr_language_backends_end(self): content = self.document.pages.first().ocr_content.content - self.assertTrue('Mayan EDMS Documentation' in content) + self.assertTrue(TEST_DOCUMENT_CONTENT in content) class GermanOCRSupportTestCase(BaseTestCase): @@ -40,7 +44,7 @@ class GermanOCRSupportTestCase(BaseTestCase): self.assertEqual('deu', language_code) - with open(TEST_DEU_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_DEU_DOCUMENT_PATH, mode='rb') as file_object: self.document = self.document_type.new_document( file_object=file_object, language=language_code ) @@ -53,8 +57,8 @@ class GermanOCRSupportTestCase(BaseTestCase): content = self.document.pages.first().ocr_content.content self.assertTrue( - 'Repository für elektronische Dokumente.' in content + TEST_DOCUMENT_CONTENT_DEU_1 in content ) self.assertTrue( - 'Es bietet einen' in content + TEST_DOCUMENT_CONTENT_DEU_2 in content ) diff --git a/mayan/apps/ocr/tests/test_views.py b/mayan/apps/ocr/tests/test_views.py index 61cec6d49f..7777f1e245 100644 --- a/mayan/apps/ocr/tests/test_views.py +++ b/mayan/apps/ocr/tests/test_views.py @@ -8,6 +8,8 @@ from ..permissions import ( ) from ..utils import get_document_ocr_content +TEST_DOCUMENT_CONTENT = 'Mayan EDMS Documentation' + class OCRViewsTestCase(GenericDocumentViewTestCase): # PyOCR's leak descriptor in get_available_languages and image_to_string @@ -20,7 +22,7 @@ class OCRViewsTestCase(GenericDocumentViewTestCase): def _request_document_content_view(self): return self.get( - 'ocr:document_ocr_content', args=(self.document.pk,) + viewname='ocr:document_ocr_content', args=(self.document.pk,) ) def test_document_content_view_no_permissions(self): @@ -38,12 +40,37 @@ class OCRViewsTestCase(GenericDocumentViewTestCase): response = self._request_document_content_view() self.assertContains( - response, 'Mayan EDMS Documentation', status_code=200 + response=response, text=TEST_DOCUMENT_CONTENT, status_code=200 + ) + + def _request_document_page_content_view(self): + return self.get( + viewname='ocr:document_page_ocr_content', args=( + self.document.pages.first().pk, + ) + ) + + def test_document_page_content_view_no_permissions(self): + self.document.submit_for_ocr() + response = self._request_document_page_content_view() + + self.assertEqual(response.status_code, 403) + + def test_document_page_content_view_with_access(self): + self.document.submit_for_ocr() + self.grant_access( + permission=permission_ocr_content_view, obj=self.document + ) + + response = self._request_document_page_content_view() + + self.assertContains( + response=response, text=TEST_DOCUMENT_CONTENT, status_code=200 ) def _request_document_submit_view(self): return self.post( - 'ocr:document_submit', args=(self.document.pk,) + viewname='ocr:document_submit', args=(self.document.pk,) ) def test_document_submit_view_no_permission(self): @@ -58,14 +85,14 @@ class OCRViewsTestCase(GenericDocumentViewTestCase): ) self._request_document_submit_view() self.assertTrue( - 'Mayan EDMS Documentation' in ''.join( + TEST_DOCUMENT_CONTENT in ''.join( self.document.latest_version.ocr_content() ) ) def _request_multiple_document_submit_view(self): return self.post( - 'ocr:document_submit_multiple', + viewname='ocr:document_submit_multiple', data={ 'id_list': self.document.pk, } @@ -83,14 +110,14 @@ class OCRViewsTestCase(GenericDocumentViewTestCase): ) self._request_multiple_document_submit_view() self.assertTrue( - 'Mayan EDMS Documentation' in ''.join( + TEST_DOCUMENT_CONTENT in ''.join( self.document.latest_version.ocr_content() ) ) def _request_document_ocr_download_view(self): return self.get( - 'ocr:document_ocr_download', args=(self.document.pk,) + viewname='ocr:document_ocr_download', args=(self.document.pk,) ) def test_document_ocr_download_view_no_permission(self): @@ -108,7 +135,7 @@ class OCRViewsTestCase(GenericDocumentViewTestCase): self.assertEqual(response.status_code, 200) self.assert_download_response( - response, content=( + response=response, content=( ''.join(get_document_ocr_content(document=self.document)) ), ) diff --git a/mayan/apps/ocr/urls.py b/mayan/apps/ocr/urls.py index d23837718d..6ae236f16e 100644 --- a/mayan/apps/ocr/urls.py +++ b/mayan/apps/ocr/urls.py @@ -7,34 +7,34 @@ from .api_views import ( APIDocumentVersionOCRView ) from .views import ( - DocumentOCRContent, DocumentOCRDownloadView, DocumentOCRErrorsListView, - DocumentPageOCRContent, DocumentSubmitView, DocumentTypeSettingsEditView, - DocumentTypeSubmitView, EntryListView + DocumentOCRContentView, DocumentOCRDownloadView, + DocumentOCRErrorsListView, DocumentPageOCRContentView, DocumentSubmitView, + DocumentTypeSettingsEditView, DocumentTypeSubmitView, EntryListView ) urlpatterns = [ url( - r'^document/page/(?P\d+)/content/$', - DocumentPageOCRContent.as_view(), name='document_page_ocr_content' + r'^documents/pages/(?P\d+)/content/$', + DocumentPageOCRContentView.as_view(), name='document_page_ocr_content' ), url( - r'^document/(?P\d+)/content/$', DocumentOCRContent.as_view(), + r'^documents/(?P\d+)/content/$', DocumentOCRContentView.as_view(), name='document_ocr_content' ), url( - r'^document/(?P\d+)/submit/$', DocumentSubmitView.as_view(), + r'^documents/(?P\d+)/submit/$', DocumentSubmitView.as_view(), name='document_submit' ), url( - r'^document/type/submit/$', DocumentTypeSubmitView.as_view(), + r'^document_types/submit/$', DocumentTypeSubmitView.as_view(), name='document_type_submit' ), url( - r'^document/multiple/submit/$', DocumentSubmitView.as_view(), + r'^documents/multiple/submit/$', DocumentSubmitView.as_view(), name='document_submit_multiple' ), url( - r'^document_type/(?P\d+)/ocr/settings/$', + r'^document_types/(?P\d+)/ocr/settings/$', DocumentTypeSettingsEditView.as_view(), name='document_type_ocr_settings' ), diff --git a/mayan/apps/ocr/views.py b/mayan/apps/ocr/views.py index 10ad7cc5f4..a4de687a90 100644 --- a/mayan/apps/ocr/views.py +++ b/mayan/apps/ocr/views.py @@ -24,13 +24,13 @@ from .permissions import ( from .utils import get_document_ocr_content -class DocumentOCRContent(SingleObjectDetailView): +class DocumentOCRContentView(SingleObjectDetailView): form_class = DocumentOCRContentForm model = Document object_permission = permission_ocr_content_view def dispatch(self, request, *args, **kwargs): - result = super(DocumentOCRContent, self).dispatch( + result = super(DocumentOCRContentView, self).dispatch( request, *args, **kwargs ) self.get_object().add_as_recent_document_for_user(user=request.user) @@ -45,13 +45,13 @@ class DocumentOCRContent(SingleObjectDetailView): } -class DocumentPageOCRContent(SingleObjectDetailView): +class DocumentPageOCRContentView(SingleObjectDetailView): form_class = DocumentPageOCRContentForm model = DocumentPage object_permission = permission_ocr_content_view def dispatch(self, request, *args, **kwargs): - result = super(DocumentPageOCRContent, self).dispatch( + result = super(DocumentPageOCRContentView, self).dispatch( request, *args, **kwargs ) self.get_object().document.add_as_recent_document_for_user( diff --git a/mayan/apps/permissions/tests/test_views.py b/mayan/apps/permissions/tests/test_views.py index ddfad6236a..3ed47361ee 100644 --- a/mayan/apps/permissions/tests/test_views.py +++ b/mayan/apps/permissions/tests/test_views.py @@ -32,14 +32,18 @@ class PermissionsViewsTestCase(GenericViewTestCase): response = self._request_create_role_view() self.assertEqual(response.status_code, 403) self.assertEqual(Role.objects.count(), 1) - self.assertFalse(TEST_ROLE_2_LABEL in Role.objects.values_list('label', flat=True)) + self.assertFalse( + TEST_ROLE_2_LABEL in Role.objects.values_list('label', flat=True) + ) def test_role_creation_view_with_permission(self): self.grant_permission(permission=permission_role_create) response = self._request_create_role_view() self.assertEqual(response.status_code, 302) self.assertEqual(Role.objects.count(), 2) - self.assertTrue(TEST_ROLE_2_LABEL in Role.objects.values_list('label', flat=True)) + self.assertTrue( + TEST_ROLE_2_LABEL in Role.objects.values_list('label', flat=True) + ) def _request_role_delete_view(self): return self.post( @@ -54,7 +58,9 @@ class PermissionsViewsTestCase(GenericViewTestCase): response = self._request_role_delete_view() self.assertEqual(response.status_code, 403) self.assertEqual(Role.objects.count(), 2) - self.assertTrue(TEST_ROLE_2_LABEL in Role.objects.values_list('label', flat=True)) + self.assertTrue( + TEST_ROLE_2_LABEL in Role.objects.values_list('label', flat=True) + ) def test_role_delete_view_with_access(self): self._create_role() @@ -62,7 +68,9 @@ class PermissionsViewsTestCase(GenericViewTestCase): response = self._request_role_delete_view() self.assertEqual(response.status_code, 302) self.assertEqual(Role.objects.count(), 1) - self.assertFalse(TEST_ROLE_2_LABEL in Role.objects.values_list('label', flat=True)) + self.assertFalse( + TEST_ROLE_2_LABEL in Role.objects.values_list('label', flat=True) + ) def _request_role_edit_view(self): return self.post( @@ -99,13 +107,17 @@ class PermissionsViewsTestCase(GenericViewTestCase): self._create_role() response = self._request_role_list_view() self.assertEqual(response.status_code, 200) - self.assertNotContains(response, text=TEST_ROLE_2_LABEL, status_code=200) + self.assertNotContains( + response=response, text=TEST_ROLE_2_LABEL, status_code=200 + ) def test_role_list_view_with_access(self): self._create_role() self.grant_access(permission=permission_role_view, obj=self.role_2) response = self._request_role_list_view() - self.assertContains(response, text=TEST_ROLE_2_LABEL, status_code=200) + self.assertContains( + response=response, text=TEST_ROLE_2_LABEL, status_code=200 + ) def _request_role_permissions_view(self): return self.get( @@ -119,13 +131,17 @@ class PermissionsViewsTestCase(GenericViewTestCase): def test_role_permissions_view_with_permission_grant(self): self._create_role() - self.grant_access(permission=permission_permission_grant, obj=self.role_2) + self.grant_access( + permission=permission_permission_grant, obj=self.role_2 + ) response = self._request_role_permissions_view() self.assertEqual(response.status_code, 200) def test_role_permissions_view_with_permission_revoke(self): self._create_role() - self.grant_access(permission=permission_permission_revoke, obj=self.role_2) + self.grant_access( + permission=permission_permission_revoke, obj=self.role_2 + ) response = self._request_role_permissions_view() self.assertEqual(response.status_code, 200) diff --git a/mayan/apps/smart_settings/classes.py b/mayan/apps/smart_settings/classes.py index 0436200106..8448a3f611 100644 --- a/mayan/apps/smart_settings/classes.py +++ b/mayan/apps/smart_settings/classes.py @@ -127,7 +127,7 @@ class Setting(object): def save_last_known_good(cls): # Don't write over the last good configuration if we are trying # to restore the last good configuration - if not 'revertsettings' in sys.argv: + if 'revertsettings' not in sys.argv: cls.save_configuration( path=settings.CONFIGURATION_LAST_GOOD_FILEPATH ) diff --git a/mayan/apps/smart_settings/forms.py b/mayan/apps/smart_settings/forms.py index 4de944332e..3f008ff1ca 100644 --- a/mayan/apps/smart_settings/forms.py +++ b/mayan/apps/smart_settings/forms.py @@ -34,7 +34,7 @@ class SettingForm(forms.Form): try: yaml.safe_load(self.cleaned_data['value']) - except yaml.YAMLError as exception: + except yaml.YAMLError: raise ValidationError( _( '"%s" not a valid entry.' diff --git a/mayan/apps/sources/tests/test_links.py b/mayan/apps/sources/tests/test_links.py index 2b86b1c20a..500e563332 100644 --- a/mayan/apps/sources/tests/test_links.py +++ b/mayan/apps/sources/tests/test_links.py @@ -22,6 +22,8 @@ class SourcesLinksTestCase(GenericDocumentViewTestCase): self.assertEqual(resolved_link, None) def test_document_create_link_with_access(self): - self.grant_access(permission=permission_document_create, obj=self.document_type) + self.grant_access( + permission=permission_document_create, obj=self.document_type + ) resolved_link = self._get_document_create_link() self.assertNotEqual(resolved_link, None) diff --git a/mayan/apps/sources/tests/test_models.py b/mayan/apps/sources/tests/test_models.py index ca050d6546..1460afeb80 100644 --- a/mayan/apps/sources/tests/test_models.py +++ b/mayan/apps/sources/tests/test_models.py @@ -46,7 +46,7 @@ class CompressedUploadsTestCase(BaseTestCase): label='test source', uncompress=SOURCE_UNCOMPRESS_CHOICE_Y ) - with open(TEST_COMPRESSED_DOCUMENT_PATH) as file_object: + with open(TEST_COMPRESSED_DOCUMENT_PATH, mode='rb') as file_object: source.handle_upload( document_type=self.document_type, file_object=file_object, diff --git a/mayan/apps/sources/tests/test_views.py b/mayan/apps/sources/tests/test_views.py index 2994fc24dc..abe2490e8d 100644 --- a/mayan/apps/sources/tests/test_views.py +++ b/mayan/apps/sources/tests/test_views.py @@ -39,10 +39,11 @@ class DocumentUploadTestCase(GenericDocumentViewTestCase): self.document.delete() - def _request_upload_wizard(self): - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + def _request_upload_wizard_view(self): + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: return self.post( - 'sources:upload_interactive', args=(self.source.pk,), data={ + viewname='sources:upload_interactive', args=(self.source.pk,), + data={ 'source-file': file_object, 'document_type_id': self.document_type.pk, }, follow=True @@ -51,8 +52,7 @@ class DocumentUploadTestCase(GenericDocumentViewTestCase): def test_upload_wizard_without_permission(self): self.login_user() - response = self._request_upload_wizard() - + response = self._request_upload_wizard_view() self.assertEqual(response.status_code, 403) self.assertEqual(Document.objects.count(), 0) @@ -61,7 +61,7 @@ class DocumentUploadTestCase(GenericDocumentViewTestCase): self.grant_permission(permission=permission_document_create) - response = self._request_upload_wizard() + response = self._request_upload_wizard_view() self.assertTrue(b'queued' in response.content) self.assertEqual(Document.objects.count(), 1) @@ -82,9 +82,10 @@ class DocumentUploadTestCase(GenericDocumentViewTestCase): obj=self.document_type, permission=permission_document_create ) - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: response = self.post( - 'sources:upload_interactive', args=(self.source.pk,), data={ + viewname='sources:upload_interactive', args=(self.source.pk,), + data={ 'source-file': file_object, 'document_type_id': self.document_type.pk, }, follow=True @@ -95,7 +96,7 @@ class DocumentUploadTestCase(GenericDocumentViewTestCase): def _request_upload_interactive_view(self): return self.get( - 'sources:upload_interactive', data={ + viewname='sources:upload_interactive', data={ 'document_type_id': self.document_type.pk, } ) @@ -104,7 +105,6 @@ class DocumentUploadTestCase(GenericDocumentViewTestCase): self.login_user() response = self._request_upload_interactive_view() - self.assertEqual(response.status_code, 403) def test_upload_interactive_view_with_access(self): @@ -113,9 +113,8 @@ class DocumentUploadTestCase(GenericDocumentViewTestCase): permission=permission_document_create, obj=self.document_type ) response = self._request_upload_interactive_view() - self.assertContains( - response, text=self.source.label, status_code=200 + response=response, text=self.source.label, status_code=200 ) @@ -136,7 +135,8 @@ class DocumentUploadIssueTestCase(GenericViewTestCase): # Create new webform source self.post( - 'sources:setup_source_create', args=(SOURCE_CHOICE_WEB_FORM,), + viewname='sources:setup_source_create', + args=(SOURCE_CHOICE_WEB_FORM,), data={'label': 'test', 'uncompress': 'n', 'enabled': True} ) self.assertEqual(WebFormSource.objects.count(), 1) @@ -144,8 +144,9 @@ class DocumentUploadIssueTestCase(GenericViewTestCase): # Upload the test document with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_descriptor: self.post( - 'sources:upload_interactive', data={ - 'document-language': 'eng', 'source-file': file_descriptor, + viewname='sources:upload_interactive', data={ + 'document-language': 'eng', + 'source-file': file_descriptor, 'document_type_id': self.document_type.pk } ) @@ -163,7 +164,7 @@ class DocumentUploadIssueTestCase(GenericViewTestCase): # Test for issue 25 during editing self.post( - 'documents:document_edit', args=(document.pk,), data={ + viewname='documents:document_edit', args=(document.pk,), data={ 'description': TEST_DOCUMENT_DESCRIPTION, 'language': document.language, 'label': document.label } @@ -189,7 +190,7 @@ class NewDocumentVersionViewTestCase(GenericDocumentViewTestCase): NewVersionBlock.objects.block(self.document) response = self.post( - 'sources:upload_version', args=(self.document.pk,), + viewname='sources:upload_version', args=(self.document.pk,), follow=True ) @@ -222,6 +223,13 @@ class StagingFolderViewTestCase(GenericViewTestCase): fs_cleanup(self.temporary_directory) super(StagingFolderViewTestCase, self).tearDown() + def _request_staging_file_delete_view(self, staging_folder, staging_file): + return self.post( + viewname='sources:staging_file_delete', args=( + staging_folder.pk, staging_file.encoded_filename + ) + ) + def test_staging_folder_delete_no_permission(self): self.login_user() @@ -236,12 +244,9 @@ class StagingFolderViewTestCase(GenericViewTestCase): staging_file = list(staging_folder.get_files())[0] - response = self.post( - 'sources:staging_file_delete', args=( - staging_folder.pk, staging_file.encoded_filename - ), follow=True + response = self._request_staging_file_delete_view( + staging_folder=staging_folder, staging_file=staging_file ) - self.assertEqual(response.status_code, 403) self.assertEqual(len(list(staging_folder.get_files())), 1) @@ -261,13 +266,10 @@ class StagingFolderViewTestCase(GenericViewTestCase): staging_file = list(staging_folder.get_files())[0] - response = self.post( - 'sources:staging_file_delete', args=( - staging_folder.pk, staging_file.encoded_filename - ), follow=True + response = self._request_staging_file_delete_view( + staging_folder=staging_folder, staging_file=staging_file ) - - self.assertContains(response, 'deleted', status_code=200) + self.assertEqual(response.status_code, 302) self.assertEqual(len(list(staging_folder.get_files())), 0) @@ -278,6 +280,17 @@ class SourcesTestCase(GenericDocumentViewTestCase): uncompress=TEST_SOURCE_UNCOMPRESS_N ) + def _request_setup_source_list_view(self): + return self.get(viewname='sources:setup_source_list') + + def test_source_list_view_no_permission(self): + self._create_web_source() + + self.login_user() + + response = self._request_setup_source_list_view() + self.assertEqual(response.status_code, 403) + def test_source_list_view_with_permission(self): self._create_web_source() @@ -285,56 +298,49 @@ class SourcesTestCase(GenericDocumentViewTestCase): self.grant_permission(permission=permission_sources_setup_view) - response = self.get(viewname='sources:setup_source_list') + response = self._request_setup_source_list_view() + self.assertContains( + response=response, text=self.source.label, status_code=200 + ) - self.assertContains(response, text=self.source.label, status_code=200) - - def test_source_list_view_no_permission(self): - self._create_web_source() + def _request_setup_source_create_view(self): + return self.post( + args=(SOURCE_CHOICE_WEB_FORM,), + viewname='sources:setup_source_create', data={ + 'enabled': True, 'label': TEST_SOURCE_LABEL, + 'uncompress': TEST_SOURCE_UNCOMPRESS_N + } + ) + def test_source_create_view_no_permission(self): self.login_user() - response = self.get(viewname='sources:setup_source_list') + self.grant_permission(permission=permission_sources_setup_view) + response = self._request_setup_source_create_view() self.assertEqual(response.status_code, 403) + self.assertEqual(WebFormSource.objects.count(), 0) + def test_source_create_view_with_permission(self): self.login_user() self.grant_permission(permission=permission_sources_setup_create) self.grant_permission(permission=permission_sources_setup_view) - response = self.post( - args=(SOURCE_CHOICE_WEB_FORM,), follow=True, - viewname='sources:setup_source_create', data={ - 'enabled': True, 'label': TEST_SOURCE_LABEL, - 'uncompress': TEST_SOURCE_UNCOMPRESS_N - } - ) + response = self._request_setup_source_create_view() + self.assertEquals(response.status_code, 302) webform_source = WebFormSource.objects.first() - self.assertEqual(webform_source.label, TEST_SOURCE_LABEL) self.assertEqual(webform_source.uncompress, TEST_SOURCE_UNCOMPRESS_N) - self.assertEquals(response.status_code, 200) - - def test_source_create_view_no_permission(self): - self.login_user() - - self.grant_permission(permission=permission_sources_setup_view) - - response = self.post( - args=(SOURCE_CHOICE_WEB_FORM,), follow=True, - viewname='sources:setup_source_create', data={ - 'enabled': True, 'label': TEST_SOURCE_LABEL, - 'uncompress': TEST_SOURCE_UNCOMPRESS_N - } + def _request_setup_source_delete_view(self): + return self.post( + args=(self.source.pk,), + viewname='sources:setup_source_delete' ) - self.assertEqual(response.status_code, 403) - self.assertEqual(WebFormSource.objects.count(), 0) - def test_source_delete_view_with_permission(self): self._create_web_source() @@ -343,12 +349,8 @@ class SourcesTestCase(GenericDocumentViewTestCase): self.grant_permission(permission=permission_sources_setup_delete) self.grant_permission(permission=permission_sources_setup_view) - response = self.post( - args=(self.source.pk,), follow=True, - viewname='sources:setup_source_delete' - ) - - self.assertEqual(response.status_code, 200) + response = self._request_setup_source_delete_view() + self.assertEqual(response.status_code, 302) self.assertEqual(WebFormSource.objects.count(), 0) def test_source_delete_view_no_permission(self): @@ -358,10 +360,6 @@ class SourcesTestCase(GenericDocumentViewTestCase): self.grant_permission(permission=permission_sources_setup_view) - response = self.post( - args=(self.source.pk,), follow=True, - viewname='sources:setup_source_delete' - ) - + response = self._request_setup_source_delete_view() self.assertEqual(response.status_code, 403) self.assertEqual(WebFormSource.objects.count(), 1) diff --git a/mayan/apps/tags/tests/test_api.py b/mayan/apps/tags/tests/test_api.py index 065b37d5fa..a0f1d1e084 100644 --- a/mayan/apps/tags/tests/test_api.py +++ b/mayan/apps/tags/tests/test_api.py @@ -29,17 +29,12 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): super(TagAPITestCase, self).setUp() self.login_user() - def tearDown(self): - if hasattr(self, 'document_type'): - self.document_type.delete() - super(TagAPITestCase, self).tearDown() - def _create_tag(self): return Tag.objects.create( color=TEST_TAG_COLOR, label=TEST_TAG_LABEL ) - def _request_tag_create(self): + def _request_tag_create_view(self): return self.post( viewname='rest_api:tag-list', data={ 'label': TEST_TAG_LABEL, 'color': TEST_TAG_COLOR @@ -47,13 +42,13 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): ) def test_tag_create_view_no_permission(self): - response = self._request_tag_create() + response = self._request_tag_create_view() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(Tag.objects.count(), 0) def test_tag_create_view_with_permission(self): self.grant_permission(permission=permission_tag_create) - response = self._request_tag_create() + response = self._request_tag_create_view() self.assertEqual(response.status_code, status.HTTP_201_CREATED) tag = Tag.objects.first() @@ -65,19 +60,21 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertEqual(tag.label, TEST_TAG_LABEL) self.assertEqual(tag.color, TEST_TAG_COLOR) - def _request_tag_delete(self): - return self.delete(viewname='rest_api:tag-detail', args=(self.tag.pk,)) + def _request_tag_delete_view(self): + return self.delete( + viewname='rest_api:tag-detail', args=(self.tag.pk,) + ) def test_tag_delete_view_no_access(self): self.tag = self._create_tag() - response = self._request_tag_delete() + response = self._request_tag_delete_view() self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertTrue(self.tag in Tag.objects.all()) def test_tag_delete_view_with_access(self): self.tag = self._create_tag() self.grant_access(permission=permission_tag_delete, obj=self.tag) - response = self._request_tag_delete() + response = self._request_tag_delete_view() self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertFalse(self.tag in Tag.objects.all()) @@ -127,7 +124,7 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): force_text(self.document.uuid) ) - def _request_tag_edit_via_patch(self): + def _request_tag_edit_via_patch_view(self): return self.patch( viewname='rest_api:tag-detail', args=(self.tag.pk,), data={ 'label': TEST_TAG_LABEL_EDITED, @@ -137,7 +134,7 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): def test_tag_edit_via_patch_no_access(self): self.tag = self._create_tag() - response = self._request_tag_edit_via_patch() + response = self._request_tag_edit_via_patch_view() self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.tag.refresh_from_db() self.assertEqual(self.tag.label, TEST_TAG_LABEL) @@ -146,13 +143,13 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): def test_tag_edit_via_patch_with_access(self): self.tag = self._create_tag() self.grant_access(permission=permission_tag_edit, obj=self.tag) - response = self._request_tag_edit_via_patch() + response = self._request_tag_edit_via_patch_view() self.assertEqual(response.status_code, status.HTTP_200_OK) self.tag.refresh_from_db() self.assertEqual(self.tag.label, TEST_TAG_LABEL_EDITED) self.assertEqual(self.tag.color, TEST_TAG_COLOR_EDITED) - def _request_tag_edit_via_put(self): + def _request_tag_edit_via_put_view(self): return self.put( viewname='rest_api:tag-detail', args=(self.tag.pk,), data={ 'label': TEST_TAG_LABEL_EDITED, @@ -162,7 +159,7 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): def test_tag_edit_via_put_no_access(self): self.tag = self._create_tag() - response = self._request_tag_edit_via_put() + response = self._request_tag_edit_via_put_view() self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.tag.refresh_from_db() self.assertEqual(self.tag.label, TEST_TAG_LABEL) @@ -171,13 +168,13 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): def test_tag_edit_via_put_with_access(self): self.tag = self._create_tag() self.grant_access(permission=permission_tag_edit, obj=self.tag) - response = self._request_tag_edit_via_put() + response = self._request_tag_edit_via_put_view() self.assertEqual(response.status_code, status.HTTP_200_OK) self.tag.refresh_from_db() self.assertEqual(self.tag.label, TEST_TAG_LABEL_EDITED) self.assertEqual(self.tag.color, TEST_TAG_COLOR_EDITED) - def _request_document_attach_tag(self): + def _request_document_attach_tag_view(self): return self.post( viewname='rest_api:document-tag-list', args=(self.document.pk,), data={'tag_pk': self.tag.pk} @@ -186,7 +183,7 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): def test_document_attach_tag_view_no_access(self): self.tag = self._create_tag() self.document = self.upload_document() - response = self._request_document_attach_tag() + response = self._request_document_attach_tag_view() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertFalse(self.tag in self.document.tags.all()) @@ -194,7 +191,7 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): self.tag = self._create_tag() self.document = self.upload_document() self.grant_access(permission=permission_tag_attach, obj=self.document) - response = self._request_document_attach_tag() + response = self._request_document_attach_tag_view() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertFalse(self.tag in self.document.tags.all()) @@ -202,7 +199,7 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): self.tag = self._create_tag() self.document = self.upload_document() self.grant_access(permission=permission_tag_attach, obj=self.tag) - response = self._request_document_attach_tag() + response = self._request_document_attach_tag_view() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue(self.tag in self.document.tags.all()) @@ -211,7 +208,7 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): self.document = self.upload_document() self.grant_access(permission=permission_tag_attach, obj=self.document) self.grant_access(permission=permission_tag_attach, obj=self.tag) - response = self._request_document_attach_tag() + response = self._request_document_attach_tag_view() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue(self.tag in self.document.tags.all()) @@ -294,7 +291,7 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['results'][0]['label'], self.tag.label) - def _request_document_tag_remove(self): + def _request_document_tag_remove_view(self): return self.delete( viewname='rest_api:document-tag-detail', args=( self.document.pk, self.tag.pk @@ -305,7 +302,7 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): self.tag = self._create_tag() self.document = self.upload_document() self.tag.documents.add(self.document) - response = self._request_document_tag_remove() + response = self._request_document_tag_remove_view() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertTrue(self.tag in self.document.tags.all()) @@ -314,7 +311,7 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): self.document = self.upload_document() self.tag.documents.add(self.document) self.grant_access(permission=permission_tag_remove, obj=self.document) - response = self._request_document_tag_remove() + response = self._request_document_tag_remove_view() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertTrue(self.tag in self.document.tags.all()) @@ -323,7 +320,7 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): self.document = self.upload_document() self.tag.documents.add(self.document) self.grant_access(permission=permission_tag_remove, obj=self.tag) - response = self._request_document_tag_remove() + response = self._request_document_tag_remove_view() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertTrue(self.tag in self.document.tags.all()) @@ -333,6 +330,6 @@ class TagAPITestCase(DocumentTestMixin, BaseAPITestCase): self.tag.documents.add(self.document) self.grant_access(permission=permission_document_view, obj=self.document) self.grant_access(permission=permission_tag_remove, obj=self.tag) - response = self._request_document_tag_remove() + response = self._request_document_tag_remove_view() self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertFalse(self.tag in self.document.tags.all()) diff --git a/mayan/apps/tags/tests/test_models.py b/mayan/apps/tags/tests/test_models.py index e6d5b25acd..fd94ecc8ed 100644 --- a/mayan/apps/tags/tests/test_models.py +++ b/mayan/apps/tags/tests/test_models.py @@ -14,19 +14,6 @@ from .literals import TEST_TAG_COLOR, TEST_TAG_LABEL class TagTestCase(DocumentTestMixin, BaseTestCase): auto_upload_document = False - def setUp(self): - super(TagTestCase, self).setUp() - - def tearDown(self): - self.document.delete() - self.document_type.delete() - super(TagTestCase, self).tearDown() - - def runTest(self): - tag = Tag.objects.create(color=TEST_TAG_COLOR, label=TEST_TAG_LABEL) - self.assertEqual(tag.label, TEST_TAG_LABEL) - self.assertEqual(tag.get_color_code(), 'red') - def test_addition_and_deletion_of_documents(self): tag = Tag.objects.create(color=TEST_TAG_COLOR, label=TEST_TAG_LABEL) self.document = self.upload_document() diff --git a/mayan/apps/tags/tests/test_views.py b/mayan/apps/tags/tests/test_views.py index 4286b8de9b..4c123fa8ae 100644 --- a/mayan/apps/tags/tests/test_views.py +++ b/mayan/apps/tags/tests/test_views.py @@ -21,48 +21,45 @@ class TagViewTestCase(GenericDocumentViewTestCase): color=TEST_TAG_COLOR, label=TEST_TAG_LABEL ) - def _request_create_tag(self): + def _request_create_tag_view(self): return self.post( - 'tags:tag_create', data={ + viewname='tags:tag_create', data={ 'label': TEST_TAG_LABEL, 'color': TEST_TAG_COLOR - }, follow=True + } ) def test_tag_create_view_no_permissions(self): self.login_user() - response = self._request_create_tag() - + response = self._request_create_tag_view() self.assertEqual(response.status_code, 403) + self.assertEqual(Tag.objects.count(), 0) def test_tag_create_view_with_permissions(self): self.login_user() self.grant_permission(permission=permission_tag_create) - - response = self._request_create_tag() - - self.assertEqual(response.status_code, 200) + response = self._request_create_tag_view() + self.assertEqual(response.status_code, 302) self.assertEqual(Tag.objects.count(), 1) tag = Tag.objects.first() self.assertEqual(tag.label, TEST_TAG_LABEL) self.assertEqual(tag.color, TEST_TAG_COLOR) - def _request_delete_tag(self): + def _request_delete_tag_view(self): return self.post( - 'tags:tag_delete', args=(self.tag.pk,), follow=True + viewname='tags:tag_delete', args=(self.tag.pk,) ) def test_tag_delete_view_no_permissions(self): self.login_user() self._create_tag() - response = self._request_delete_tag() - - self.assertEqual(response.status_code, 200) + response = self._request_delete_tag_view() + self.assertEqual(response.status_code, 302) self.assertEqual(Tag.objects.count(), 1) def test_tag_delete_view_with_access(self): @@ -71,25 +68,23 @@ class TagViewTestCase(GenericDocumentViewTestCase): self.grant_access(obj=self.tag, permission=permission_tag_delete) - response = self._request_delete_tag() - - self.assertEqual(response.status_code, 200) + response = self._request_delete_tag_view() + self.assertEqual(response.status_code, 302) self.assertEqual(Tag.objects.count(), 0) - def _request_multiple_delete(self): + def _request_multiple_delete_view(self): return self.post( - 'tags:tag_multiple_delete', data={'id_list': self.tag.pk}, - follow=True + viewname='tags:tag_multiple_delete', + data={'id_list': self.tag.pk}, ) def test_tag_multiple_delete_view_no_permissions(self): self.login_user() self._create_tag() - response = self._request_multiple_delete() - - self.assertEqual(response.status_code, 200) + response = self._request_multiple_delete_view() + self.assertEqual(response.status_code, 302) self.assertEqual(Tag.objects.count(), 1) def test_tag_multiple_delete_view_with_access(self): @@ -98,25 +93,23 @@ class TagViewTestCase(GenericDocumentViewTestCase): self.grant_access(obj=self.tag, permission=permission_tag_delete) - response = self._request_multiple_delete() - - self.assertEqual(response.status_code, 200) + response = self._request_multiple_delete_view() + self.assertEqual(response.status_code, 302) self.assertEqual(Tag.objects.count(), 0) - def _request_edit_tag(self): + def _request_edit_tag_view(self): return self.post( - 'tags:tag_edit', args=(self.tag.pk,), data={ + viewname='tags:tag_edit', args=(self.tag.pk,), data={ 'label': TEST_TAG_LABEL_EDITED, 'color': TEST_TAG_COLOR_EDITED - }, follow=True + } ) def test_tag_edit_view_no_permissions(self): self.login_user() self._create_tag() - response = self._request_edit_tag() - + response = self._request_edit_tag_view() self.assertEqual(response.status_code, 403) tag = Tag.objects.get(pk=self.tag.pk) self.assertEqual(tag.label, TEST_TAG_LABEL) @@ -128,23 +121,24 @@ class TagViewTestCase(GenericDocumentViewTestCase): self.grant_access(obj=self.tag, permission=permission_tag_edit) - response = self._request_edit_tag() - - self.assertEqual(response.status_code, 200) + response = self._request_edit_tag_view() + self.assertEqual(response.status_code, 302) tag = Tag.objects.get(pk=self.tag.pk) self.assertEqual(tag.label, TEST_TAG_LABEL_EDITED) self.assertEqual(tag.color, TEST_TAG_COLOR_EDITED) - def _request_document_list(self): - return self.get('documents:document_list') + def _request_document_list_view(self): + return self.get(viewname='documents:document_list') def test_document_tags_widget_no_permissions(self): self.login_user() self._create_tag() self.tag.documents.add(self.document) - response = self._request_document_list() - self.assertNotContains(response, text=TEST_TAG_LABEL, status_code=200) + response = self._request_document_list_view() + self.assertNotContains( + response=response, text=TEST_TAG_LABEL, status_code=200 + ) def test_document_tags_widget_with_access(self): self.login_user() @@ -157,13 +151,14 @@ class TagViewTestCase(GenericDocumentViewTestCase): obj=self.document, permission=permission_document_view ) - response = self._request_document_list() + response = self._request_document_list_view() + self.assertContains( + response=response, text=TEST_TAG_LABEL, status_code=200 + ) - self.assertContains(response, text=TEST_TAG_LABEL, status_code=200) - - def _request_attach_tag(self): + def _request_attach_tag_view(self): return self.post( - 'tags:tag_attach', args=(self.document.pk,), data={ + viewname='tags:tag_attach', args=(self.document.pk,), data={ 'tags': self.tag.pk, 'user': self.user.pk } @@ -177,8 +172,7 @@ class TagViewTestCase(GenericDocumentViewTestCase): self.grant_access(obj=self.tag, permission=permission_tag_attach) - response = self._request_attach_tag() - + response = self._request_attach_tag_view() # Redirect to previous URL and show warning message about having to # select at least one object. self.assertEqual(response.status_code, 302) @@ -196,17 +190,16 @@ class TagViewTestCase(GenericDocumentViewTestCase): # choices self.grant_access(obj=self.tag, permission=permission_tag_view) - response = self._request_attach_tag() - + response = self._request_attach_tag_view() self.assertEqual(response.status_code, 302) self.assertQuerysetEqual( self.document.tags.all(), (repr(self.tag),) ) - def _request_multiple_attach_tag(self): + def _request_multiple_attach_tag_view(self): return self.post( - 'tags:multiple_documents_tag_attach', data={ + viewname='tags:multiple_documents_tag_attach', data={ 'id_list': self.document.pk, 'tags': self.tag.pk, 'user': self.user.pk } @@ -218,8 +211,7 @@ class TagViewTestCase(GenericDocumentViewTestCase): self.grant_permission(permission=permission_tag_view) - response = self._request_multiple_attach_tag() - + response = self._request_multiple_attach_tag_view() self.assertEqual(response.status_code, 200) self.assertEqual(self.document.tags.count(), 0) @@ -234,17 +226,16 @@ class TagViewTestCase(GenericDocumentViewTestCase): # choices self.grant_access(obj=self.tag, permission=permission_tag_view) - response = self._request_multiple_attach_tag() - + response = self._request_multiple_attach_tag_view() self.assertEqual(response.status_code, 302) self.assertQuerysetEqual( self.document.tags.all(), (repr(self.tag),) ) - def _request_single_document_multiple_tag_remove(self): + def _request_single_document_multiple_tag_remove_view(self): return self.post( - 'tags:single_document_multiple_tag_remove', + viewname='tags:single_document_multiple_tag_remove', args=(self.document.pk,), data={ 'id_list': self.document.pk, 'tags': self.tag.pk, @@ -259,9 +250,9 @@ class TagViewTestCase(GenericDocumentViewTestCase): self.grant_access(obj=self.tag, permission=permission_tag_view) - response = self._request_single_document_multiple_tag_remove() - + response = self._request_single_document_multiple_tag_remove_view() self.assertEqual(response.status_code, 200) + self.assertQuerysetEqual(self.document.tags.all(), (repr(self.tag),)) def test_single_document_multiple_tag_remove_view_with_access(self): @@ -274,14 +265,14 @@ class TagViewTestCase(GenericDocumentViewTestCase): self.grant_access(obj=self.tag, permission=permission_tag_remove) self.grant_access(obj=self.tag, permission=permission_tag_view) - response = self._request_single_document_multiple_tag_remove() - + response = self._request_single_document_multiple_tag_remove_view() self.assertEqual(response.status_code, 302) + self.assertEqual(self.document.tags.count(), 0) - def _request_multiple_documents_selection_tag_remove(self): + def _request_multiple_documents_selection_tag_remove_view(self): return self.post( - 'tags:multiple_documents_selection_tag_remove', + viewname='tags:multiple_documents_selection_tag_remove', data={ 'id_list': self.document.pk, 'tags': self.tag.pk, @@ -296,9 +287,9 @@ class TagViewTestCase(GenericDocumentViewTestCase): self.grant_access(obj=self.tag, permission=permission_tag_view) - response = self._request_multiple_documents_selection_tag_remove() - + response = self._request_multiple_documents_selection_tag_remove_view() self.assertEqual(response.status_code, 200) + self.assertQuerysetEqual(self.document.tags.all(), (repr(self.tag),)) def test_multiple_documents_selection_tag_remove_view_with_access(self): @@ -311,7 +302,7 @@ class TagViewTestCase(GenericDocumentViewTestCase): self.grant_access(obj=self.tag, permission=permission_tag_remove) self.grant_access(obj=self.tag, permission=permission_tag_view) - response = self._request_multiple_documents_selection_tag_remove() - + response = self._request_multiple_documents_selection_tag_remove_view() self.assertEqual(response.status_code, 302) + self.assertEqual(self.document.tags.count(), 0) diff --git a/mayan/apps/tags/tests/test_wizard_steps.py b/mayan/apps/tags/tests/test_wizard_steps.py index f0f095f104..273381465c 100644 --- a/mayan/apps/tags/tests/test_wizard_steps.py +++ b/mayan/apps/tags/tests/test_wizard_steps.py @@ -27,7 +27,7 @@ class TaggedDocumentUploadTestCase(GenericDocumentViewTestCase): self.document.delete() def _request_upload_interactive_document_create_view(self): - with open(TEST_SMALL_DOCUMENT_PATH, 'rb') as file_object: + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: return self.post( viewname='sources:upload_interactive', args=(self.source.pk,), data={ @@ -48,6 +48,5 @@ class TaggedDocumentUploadTestCase(GenericDocumentViewTestCase): permission=permission_document_create, obj=self.document_type ) response = self._request_upload_interactive_document_create_view() - self.assertEqual(response.status_code, 302) self.assertTrue(self.tag in Document.objects.first().tags.all()) diff --git a/mayan/settings/base.py b/mayan/settings/base.py index 4d5203a02e..9a49c4fd1a 100644 --- a/mayan/settings/base.py +++ b/mayan/settings/base.py @@ -11,18 +11,13 @@ https://docs.djangoproject.com/en/1.10/ref/settings/ """ from __future__ import unicode_literals -import errno import os import sys -import yaml - from django.utils.translation import ugettext_lazy as _ import environ -import mayan - from .literals import ( CONFIGURATION_FILENAME, CONFIGURATION_LAST_GOOD_FILENAME, DEFAULT_SECRET_KEY, SECRET_KEY_FILENAME, SYSTEM_DIR @@ -359,7 +354,7 @@ CONFIGURATION_LAST_GOOD_FILEPATH = os.path.join( MEDIA_ROOT, CONFIGURATION_LAST_GOOD_FILENAME ) -if not 'revertsettings' in sys.argv: +if 'revertsettings' not in sys.argv: result = read_configuration_file(CONFIGURATION_FILEPATH) if result: globals().update(result) diff --git a/mayan/settings/literals.py b/mayan/settings/literals.py index 874921fb65..b30b20544b 100644 --- a/mayan/settings/literals.py +++ b/mayan/settings/literals.py @@ -1,7 +1,5 @@ from __future__ import unicode_literals -import os - DEFAULT_SECRET_KEY = 'secret-key-missing!' SECRET_KEY_FILENAME = 'SECRET_KEY' SYSTEM_DIR = 'system' diff --git a/mayan/settings/staging/docker.py b/mayan/settings/staging/docker.py index d4092d887a..464daabcf2 100644 --- a/mayan/settings/staging/docker.py +++ b/mayan/settings/staging/docker.py @@ -14,4 +14,4 @@ DATABASES = { BROKER_URL = 'redis://127.0.0.1:6379/0' CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/0' -DEBUG=True +DEBUG = True diff --git a/mayan/settings/testing/development.py b/mayan/settings/testing/development.py index a9804898f7..f1133c7916 100644 --- a/mayan/settings/testing/development.py +++ b/mayan/settings/testing/development.py @@ -4,4 +4,3 @@ from .base import * # NOQA DEBUG = False SIGNATURES_GPG_PATH = '/usr/bin/gpg1' - diff --git a/mayan/settings/testing/docker/base.py b/mayan/settings/testing/docker/base.py index 738748eb99..eb890b44b0 100644 --- a/mayan/settings/testing/docker/base.py +++ b/mayan/settings/testing/docker/base.py @@ -3,4 +3,3 @@ from __future__ import unicode_literals from ..base import * # NOQA SIGNATURES_GPG_PATH = '/usr/bin/gpg1' - diff --git a/mayan/settings/utils.py b/mayan/settings/utils.py index 7322f6b365..bb2a8da489 100644 --- a/mayan/settings/utils.py +++ b/mayan/settings/utils.py @@ -2,7 +2,6 @@ from __future__ import unicode_literals import errno import os -import sys import yaml