From fa441149e4424b953ba84985afb68856cb2471af Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sat, 20 Apr 2019 20:22:05 -0400 Subject: [PATCH] Fix and optimize smart link tests Signed-off-by: Roberto Rosario --- mayan/apps/linking/tests/literals.py | 2 +- mayan/apps/linking/tests/mixins.py | 28 ++++++ mayan/apps/linking/tests/test_api.py | 111 ++++++++---------------- mayan/apps/linking/tests/test_models.py | 18 ++-- mayan/apps/linking/tests/test_views.py | 11 ++- 5 files changed, 78 insertions(+), 92 deletions(-) create mode 100644 mayan/apps/linking/tests/mixins.py diff --git a/mayan/apps/linking/tests/literals.py b/mayan/apps/linking/tests/literals.py index 6c00f08366..c0ae613142 100644 --- a/mayan/apps/linking/tests/literals.py +++ b/mayan/apps/linking/tests/literals.py @@ -1,7 +1,7 @@ from __future__ import unicode_literals TEST_SMART_LINK_CONDITION_FOREIGN_DOCUMENT_DATA = 'label' -TEST_SMART_LINK_CONDITION_EXPRESSION = 'sample' +TEST_SMART_LINK_CONDITION_EXPRESSION = 'title' TEST_SMART_LINK_CONDITION_EXPRESSION_EDITED = '\'test edited\'' TEST_SMART_LINK_CONDITION_OPERATOR = 'icontains' TEST_SMART_LINK_DYNAMIC_LABEL = '{{ document.label }}' diff --git a/mayan/apps/linking/tests/mixins.py b/mayan/apps/linking/tests/mixins.py new file mode 100644 index 0000000000..700d051f31 --- /dev/null +++ b/mayan/apps/linking/tests/mixins.py @@ -0,0 +1,28 @@ +from __future__ import unicode_literals + +from ..models import SmartLink, SmartLinkCondition + +from .literals import ( + TEST_SMART_LINK_CONDITION_FOREIGN_DOCUMENT_DATA, + TEST_SMART_LINK_CONDITION_EXPRESSION, + TEST_SMART_LINK_CONDITION_OPERATOR, TEST_SMART_LINK_DYNAMIC_LABEL, + TEST_SMART_LINK_LABEL +) + + +class SmartLinkTestMixin(object): + def _create_test_smart_link(self, add_test_document_type=False): + self.test_smart_link = SmartLink.objects.create( + label=TEST_SMART_LINK_LABEL, + dynamic_label=TEST_SMART_LINK_DYNAMIC_LABEL + ) + if add_test_document_type: + self.test_smart_link.document_types.add(self.test_document_type) + + def _create_test_smart_link_condition(self): + self.test_smart_link_condition = SmartLinkCondition.objects.create( + smart_link=self.test_smart_link, + foreign_document_data=TEST_SMART_LINK_CONDITION_FOREIGN_DOCUMENT_DATA, + expression=TEST_SMART_LINK_CONDITION_EXPRESSION, + operator=TEST_SMART_LINK_CONDITION_OPERATOR + ) diff --git a/mayan/apps/linking/tests/test_api.py b/mayan/apps/linking/tests/test_api.py index 941cb36ad5..5e78646e23 100644 --- a/mayan/apps/linking/tests/test_api.py +++ b/mayan/apps/linking/tests/test_api.py @@ -16,21 +16,16 @@ from .literals import ( TEST_SMART_LINK_CONDITION_FOREIGN_DOCUMENT_DATA, TEST_SMART_LINK_CONDITION_EXPRESSION, TEST_SMART_LINK_CONDITION_EXPRESSION_EDITED, - TEST_SMART_LINK_CONDITION_OPERATOR, TEST_SMART_LINK_DYNAMIC_LABEL, - TEST_SMART_LINK_LABEL_EDITED, TEST_SMART_LINK_LABEL + TEST_SMART_LINK_CONDITION_OPERATOR, TEST_SMART_LINK_LABEL_EDITED, + TEST_SMART_LINK_LABEL ) +from .mixins import SmartLinkTestMixin -class SmartLinkAPITestCase(DocumentTestMixin, BaseAPITestCase): +class SmartLinkAPITestCase(DocumentTestMixin, SmartLinkTestMixin, BaseAPITestCase): auto_create_document_type = False auto_upload_document = False - def _create_smart_link(self): - return SmartLink.objects.create( - label=TEST_SMART_LINK_LABEL, - dynamic_label=TEST_SMART_LINK_DYNAMIC_LABEL - ) - def _request_smartlink_create_view(self): return self.post( viewname='rest_api:smartlink-list', data={ @@ -98,7 +93,7 @@ class SmartLinkAPITestCase(DocumentTestMixin, BaseAPITestCase): ) def test_smart_link_delete_view_no_permission(self): - self.test_smart_link = self._create_smart_link() + self._create_test_smart_link() response = self._request_smart_link_delete_view() self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) @@ -106,7 +101,7 @@ class SmartLinkAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertEqual(SmartLink.objects.count(), 1) def test_smart_link_delete_view_with_access(self): - self.test_smart_link = self._create_smart_link() + self._create_test_smart_link() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_delete ) @@ -124,7 +119,7 @@ class SmartLinkAPITestCase(DocumentTestMixin, BaseAPITestCase): ) def test_smart_link_detail_view_no_permission(self): - self.test_smart_link = self._create_smart_link() + self._create_test_smart_link() response = self._request_smart_link_detail_view() self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) @@ -132,7 +127,7 @@ class SmartLinkAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertFalse('label' in response.data) def test_smart_link_detail_view_with_access(self): - self.test_smart_link = self._create_smart_link() + self._create_test_smart_link() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_view ) @@ -155,7 +150,7 @@ class SmartLinkAPITestCase(DocumentTestMixin, BaseAPITestCase): def test_smart_link_edit_view_via_patch_no_permission(self): self._create_document_type() - self.test_smart_link = self._create_smart_link() + self._create_test_smart_link() response = self._request_test_smart_link_edit_patch_api_view() self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) @@ -165,7 +160,7 @@ class SmartLinkAPITestCase(DocumentTestMixin, BaseAPITestCase): def test_smart_link_edit_view_via_patch_with_access(self): self._create_document_type() - self.test_smart_link = self._create_smart_link() + self._create_test_smart_link() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_edit ) @@ -187,7 +182,7 @@ class SmartLinkAPITestCase(DocumentTestMixin, BaseAPITestCase): def test_smart_link_edit_view_via_put_no_permission(self): self._create_document_type() - self.test_smart_link = self._create_smart_link() + self._create_test_smart_link() response = self._request_test_smart_link_edit_put_api_view() self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) @@ -197,7 +192,7 @@ class SmartLinkAPITestCase(DocumentTestMixin, BaseAPITestCase): def test_smart_link_edit_view_via_put_with_access(self): self._create_document_type() - self.test_smart_link = self._create_smart_link() + self._create_test_smart_link() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_edit ) @@ -209,21 +204,10 @@ class SmartLinkAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertEqual(self.test_smart_link.label, TEST_SMART_LINK_LABEL_EDITED) -class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): - def _create_smart_link(self): - self.test_smart_link = SmartLink.objects.create( - label=TEST_SMART_LINK_LABEL, - dynamic_label=TEST_SMART_LINK_DYNAMIC_LABEL - ) - self.test_smart_link.document_types.add(self.test_document_type) - - def _create_smart_link_condition(self): - self.test_smart_link_condition = SmartLinkCondition.objects.create( - smart_link=self.test_smart_link, - foreign_document_data=TEST_SMART_LINK_CONDITION_FOREIGN_DOCUMENT_DATA, - expression=TEST_SMART_LINK_CONDITION_EXPRESSION, - operator=TEST_SMART_LINK_CONDITION_OPERATOR - ) +class SmartLinkConditionAPITestCase(DocumentTestMixin, SmartLinkTestMixin, BaseAPITestCase): + def setUp(self): + super(SmartLinkConditionAPITestCase, self).setUp() + self._create_test_smart_link(add_test_document_type=True) def _request_resolved_smart_link_detail_view(self): return self.get( @@ -233,17 +217,16 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): 'smart_link_pk': self.test_smart_link.pk } ) + self._create_test_smart_link(add_test_document_type=True) def test_resolved_smart_link_detail_view_no_permission(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() response = self._request_resolved_smart_link_detail_view() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_resolved_smart_link_detail_view_with_document_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_document, permission=permission_document_view @@ -253,8 +236,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_resolved_smart_link_detail_view_with_smart_link_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_view @@ -264,8 +246,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_resolved_smart_link_detail_view_with_full_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_view @@ -289,8 +270,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): ) def test_resolved_smart_link_list_view_no_permission(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() response = self._request_resolved_smart_link_list_view() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) @@ -298,8 +278,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertFalse('results' in response.data) def test_resolved_smart_link_list_view_with_document_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_document, permission=permission_document_view @@ -311,8 +290,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertEqual(response.data['count'], 0) def test_resolved_smart_link_list_view_with_smart_link_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_view @@ -323,8 +301,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertFalse('results' in response.data) def test_resolved_smart_link_list_view_with_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_view @@ -349,15 +326,13 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): ) def test_resolved_smart_link_document_list_view_no_permission(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() response = self._request_resolved_smart_link_document_list_view() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_resolved_smart_link_document_list_view_with_smart_link_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_view @@ -367,8 +342,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_resolved_smart_link_document_list_view_with_document_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_document, permission=permission_document_view @@ -378,8 +352,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_resolved_smart_link_document_list_view_with_full_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_document, permission=permission_document_view @@ -405,14 +378,12 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): ) def test_smart_link_condition_create_view_no_permission(self): - self._create_smart_link() response = self._request_smart_link_condition_create_view() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertFalse('id' in response.data) def test_smart_link_condition_create_view_with_access(self): - self._create_smart_link() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_edit ) @@ -441,8 +412,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): ) def test_smart_link_condition_delete_view_no_permission(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() response = self._request_smart_link_condition_delete_view() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) @@ -450,8 +420,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertEqual(SmartLinkCondition.objects.count(), 1) def test_smart_link_condition_delete_view_with_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_edit @@ -472,8 +441,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): ) def test_smart_link_condition_detail_view_no_permission(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() response = self._request_smart_link_condition_detail_view() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) @@ -481,8 +449,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): self.assertFalse('id' in response.data) def test_smart_link_condition_detail_view_with_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_view @@ -506,8 +473,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): ) def test_smart_link_condition_patch_view_no_permission(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() response = self._request_smart_link_condition_edit_view_via_patch() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) @@ -519,8 +485,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): ) def test_smart_link_condition_patch_view_with_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_edit @@ -550,8 +515,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): ) def test_smart_link_condition_put_view_no_permission(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() response = self._request_smart_link_condition_edit_view_via_put() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) @@ -563,8 +527,7 @@ class SmartLinkConditionAPITestCase(DocumentTestMixin, BaseAPITestCase): ) def test_smart_link_condition_put_view_with_access(self): - self._create_smart_link() - self._create_smart_link_condition() + self._create_test_smart_link_condition() self.grant_access( obj=self.test_smart_link, permission=permission_smart_link_edit diff --git a/mayan/apps/linking/tests/test_models.py b/mayan/apps/linking/tests/test_models.py index 2fd85d6d3e..3c726323b6 100644 --- a/mayan/apps/linking/tests/test_models.py +++ b/mayan/apps/linking/tests/test_models.py @@ -2,20 +2,16 @@ from __future__ import unicode_literals from mayan.apps.documents.tests import GenericDocumentTestCase -from ..models import SmartLink - -from .literals import TEST_SMART_LINK_LABEL, TEST_SMART_LINK_DYNAMIC_LABEL +from .mixins import SmartLinkTestMixin -class SmartLinkTestCase(GenericDocumentTestCase): - def test_dynamic_label(self): - smart_link = SmartLink.objects.create( - label=TEST_SMART_LINK_LABEL, - dynamic_label=TEST_SMART_LINK_DYNAMIC_LABEL - ) - smart_link.document_types.add(self.test_document_type) +class SmartLinkTestCase(SmartLinkTestMixin, GenericDocumentTestCase): + def setUp(self): + super(SmartLinkTestCase, self).setUp() + self._create_test_smart_link(add_test_document_type=True) + def test_smart_link_dynamic_label(self): self.assertEqual( - smart_link.get_dynamic_label(document=self.test_document), + self.test_smart_link.get_dynamic_label(document=self.test_document), self.test_document.label ) diff --git a/mayan/apps/linking/tests/test_views.py b/mayan/apps/linking/tests/test_views.py index 54eb17c94e..ab06c5bf6f 100644 --- a/mayan/apps/linking/tests/test_views.py +++ b/mayan/apps/linking/tests/test_views.py @@ -1,5 +1,6 @@ from __future__ import absolute_import, unicode_literals +from mayan.apps.common.tests import GenericViewTestCase from mayan.apps.documents.permissions import permission_document_view from mayan.apps.documents.tests import GenericDocumentViewTestCase @@ -13,9 +14,10 @@ from .literals import ( TEST_SMART_LINK_DYNAMIC_LABEL, TEST_SMART_LINK_LABEL_EDITED, TEST_SMART_LINK_LABEL ) +from .mixins import SmartLinkTestMixin -class SmartLinkViewTestCase(GenericDocumentViewTestCase): +class SmartLinkViewTestCase(SmartLinkTestMixin, GenericViewTestCase): def _request_test_smart_link_create_view(self): return self.post( viewname='linking:smart_link_create', data={ @@ -47,11 +49,6 @@ class SmartLinkViewTestCase(GenericDocumentViewTestCase): } ) - def _create_test_smart_link(self): - self.test_smart_link = SmartLink.objects.create( - label=TEST_SMART_LINK_LABEL - ) - def test_smart_link_delete_view_no_permission(self): self._create_test_smart_link() @@ -98,6 +95,8 @@ class SmartLinkViewTestCase(GenericDocumentViewTestCase): self.test_smart_link.refresh_from_db() self.assertEqual(self.test_smart_link.label, TEST_SMART_LINK_LABEL_EDITED) + +class SmartLinkDocumentViewTestCase(SmartLinkTestMixin, GenericDocumentViewTestCase): def setup_smart_links(self): self.test_smart_link = SmartLink.objects.create( label=TEST_SMART_LINK_LABEL,