From 2a5264bc2c2a8e2d495a372ffda4609f21af4819 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sun, 8 May 2016 01:13:54 -0400 Subject: [PATCH] Fix view context document resolution for the document smart link list view. Fixes GitLab issue #266. Thanks to Baptiste GAILLET @bat79a for the find. --- mayan/apps/linking/apps.py | 2 +- mayan/apps/linking/tests/literals.py | 5 ++ mayan/apps/linking/tests/test_models.py | 3 +- mayan/apps/linking/tests/test_views.py | 67 +++++++++++++++++++++++-- 4 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 mayan/apps/linking/tests/literals.py diff --git a/mayan/apps/linking/apps.py b/mayan/apps/linking/apps.py index e8d99160fc..54250c85e4 100644 --- a/mayan/apps/linking/apps.py +++ b/mayan/apps/linking/apps.py @@ -54,7 +54,7 @@ class LinkingApp(MayanAppConfig): SourceColumn( source=ResolvedSmartLink, label=_('Label'), func=lambda context: context['object'].get_dynamic_label( - context['resolved_object'] + context['document'] ) ) diff --git a/mayan/apps/linking/tests/literals.py b/mayan/apps/linking/tests/literals.py new file mode 100644 index 0000000000..279ea65564 --- /dev/null +++ b/mayan/apps/linking/tests/literals.py @@ -0,0 +1,5 @@ +from __future__ import unicode_literals + +TEST_SMART_LINK_DYNAMIC_LABEL = '{{ document.label }}' +TEST_SMART_LINK_EDITED_LABEL = 'test edited label' +TEST_SMART_LINK_LABEL = 'test label' diff --git a/mayan/apps/linking/tests/test_models.py b/mayan/apps/linking/tests/test_models.py index 41fba66f49..3d79855654 100644 --- a/mayan/apps/linking/tests/test_models.py +++ b/mayan/apps/linking/tests/test_models.py @@ -13,8 +13,7 @@ from user_management.tests.literals import ( from ..models import SmartLink -TEST_SMART_LINK_LABEL = 'test label' -TEST_SMART_LINK_DYNAMIC_LABEL = '{{ document.label }}' +from .literals import TEST_SMART_LINK_LABEL, TEST_SMART_LINK_DYNAMIC_LABEL @override_settings(OCR_AUTO_OCR=False) diff --git a/mayan/apps/linking/tests/test_views.py b/mayan/apps/linking/tests/test_views.py index 039e520a91..317e2450b5 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 documents.permissions import permission_document_view from documents.tests.test_views import GenericDocumentViewTestCase from user_management.tests import ( TEST_USER_USERNAME, TEST_USER_PASSWORD @@ -8,11 +9,13 @@ from user_management.tests import ( from ..models import SmartLink from ..permissions import ( permission_smart_link_create, permission_smart_link_delete, - permission_smart_link_edit + permission_smart_link_edit, permission_smart_link_view ) -TEST_SMART_LINK_LABEL = 'test label' -TEST_SMART_LINK_EDITED_LABEL = 'test edited label' +from .literals import ( + TEST_SMART_LINK_DYNAMIC_LABEL, TEST_SMART_LINK_EDITED_LABEL, + TEST_SMART_LINK_LABEL +) class SmartLinkViewTestCase(GenericDocumentViewTestCase): @@ -105,3 +108,61 @@ class SmartLinkViewTestCase(GenericDocumentViewTestCase): smart_link = SmartLink.objects.get(pk=smart_link.pk) self.assertContains(response, text='update', status_code=200) self.assertEqual(smart_link.label, TEST_SMART_LINK_EDITED_LABEL) + + def setup_smart_links(self): + smart_link = SmartLink.objects.create( + label=TEST_SMART_LINK_LABEL, + dynamic_label=TEST_SMART_LINK_DYNAMIC_LABEL + ) + smart_link.document_types.add(self.document_type) + + smart_link_2 = SmartLink.objects.create( + label=TEST_SMART_LINK_LABEL, + dynamic_label=TEST_SMART_LINK_DYNAMIC_LABEL + ) + smart_link_2.document_types.add(self.document_type) + + def test_document_smart_link_list_view_no_permission(self): + self.setup_smart_links() + + self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) + + self.role.permissions.add( + permission_document_view.stored_permission + ) + + response = self.get( + 'linking:smart_link_instances_for_document', + args=(self.document.pk,) + ) + # Text must appear 2 times, only for the windows title and template + # heading. The two smart links are not shown. + + self.assertContains( + response, text=self.document.label, count=2, status_code=200 + ) + + def test_document_smart_link_list_view_with_permission(self): + self.setup_smart_links() + + self.login(username=TEST_USER_USERNAME, password=TEST_USER_PASSWORD) + + self.role.permissions.add( + permission_smart_link_view.stored_permission + ) + + self.role.permissions.add( + permission_document_view.stored_permission + ) + + response = self.get( + 'linking:smart_link_instances_for_document', + args=(self.document.pk,) + ) + + # Text must appear 4 times: 2 for the windows title and template + # heading, plus 2 for the test. + + self.assertContains( + response, text=self.document.label, count=4, status_code=200 + )