From c156c02376dff9da66474974c86aa10ba2d694fe Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 3 Apr 2019 16:52:54 -0400 Subject: [PATCH] Fix index list API view Add index create, delete, detail API tests. Fixes GitLab issue #564. Signed-off-by: Roberto Rosario --- HISTORY.rst | 2 + docs/releases/3.1.10.rst | 9 ++ mayan/apps/document_indexing/serializers.py | 2 +- .../apps/document_indexing/tests/test_api.py | 107 ++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 mayan/apps/document_indexing/tests/test_api.py diff --git a/HISTORY.rst b/HISTORY.rst index 4da7df2e5e..3d84df35c1 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -27,6 +27,8 @@ * Reject emails attachments of size 0. Thanks to Robert Schoeftner (@robert.schoeftner)for the report and solution. GitLab issue #574. * Add missing document index API view create permission. +* Fix index list API view. Add index create, delete, detail API tests. + GitLab issue #564. 3.1.9 (2018-11-01) ================== diff --git a/docs/releases/3.1.10.rst b/docs/releases/3.1.10.rst index dbd2f78894..0020a9a5a5 100644 --- a/docs/releases/3.1.10.rst +++ b/docs/releases/3.1.10.rst @@ -80,6 +80,14 @@ don't contain at least one version, and don't contain a single page. All these are requirements for a valid document in Mayan EDMS. +Other changes +^^^^^^^^^^^^^ + +* Add missing document index API view create permission. +* Fix index list API view. Add index create, delete, detail API tests. + GitLab issue #564. + + Removals -------- @@ -151,6 +159,7 @@ Bugs fixed or issues closed * :gitlab-issue:`559` IndexTestCase.test_dual_level_dual_document_index failure * :gitlab-issue:`562` events.links.link_user_notifications_list should use reverse +* :gitlab-issue:`564` API REST /api/indexes/ generates an "Internal Server Error" * :gitlab-issue:`566` apps.common.tests.base.GenericViewTestCase doesn't work with a custom ROOT_URLCONF * :gitlab-issue:`568` Tornado 6.0 doesn't support Python 2.7 diff --git a/mayan/apps/document_indexing/serializers.py b/mayan/apps/document_indexing/serializers.py index dc1c2b9b6a..b5683b3ddf 100644 --- a/mayan/apps/document_indexing/serializers.py +++ b/mayan/apps/document_indexing/serializers.py @@ -7,7 +7,7 @@ from .models import Index, IndexInstanceNode, IndexTemplateNode class IndexInstanceNodeSerializer(serializers.ModelSerializer): - children = serializers.ListField(child=RecursiveField()) + children = RecursiveField(many=True, read_only=True) documents_count = serializers.SerializerMethodField() documents = serializers.HyperlinkedIdentityField( view_name='rest_api:index-node-documents' diff --git a/mayan/apps/document_indexing/tests/test_api.py b/mayan/apps/document_indexing/tests/test_api.py new file mode 100644 index 0000000000..9127ce497e --- /dev/null +++ b/mayan/apps/document_indexing/tests/test_api.py @@ -0,0 +1,107 @@ +from __future__ import unicode_literals + +from rest_framework import status + +from documents.tests import DocumentTestMixin +from rest_api.tests import BaseAPITestCase + +from ..models import Index +from ..permissions import ( + permission_document_indexing_create, permission_document_indexing_delete, + permission_document_indexing_view +) + +from .literals import TEST_INDEX_LABEL, TEST_INDEX_SLUG +from .mixins import DocumentIndexingTestMixin + + +class DocumentIndexingAPITestCase(DocumentIndexingTestMixin, DocumentTestMixin, BaseAPITestCase): + auto_upload_document = False + + def setUp(self): + super(DocumentIndexingAPITestCase, self).setUp() + self.login_user() + + def _request_index_create_api_view(self): + return self.post( + viewname='rest_api:index-list', data={ + 'label': TEST_INDEX_LABEL, 'slug': TEST_INDEX_SLUG, + 'document_types': self.document_type.pk + } + ) + + def test_index_create_api_view_no_permission(self): + response = self._request_index_create_api_view() + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + + self.assertEqual(Index.objects.count(), 0) + + def test_index_create_api_view_with_permission(self): + self.grant_permission(permission=permission_document_indexing_create) + + response = self._request_index_create_api_view() + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + index = Index.objects.first() + + self.assertEqual(response.data['id'], index.pk) + self.assertEqual(response.data['label'], index.label) + + self.assertEqual(Index.objects.count(), 1) + self.assertEqual(index.label, TEST_INDEX_LABEL) + + def _request_index_delete_api_view(self): + return self.delete( + viewname='rest_api:index-detail', kwargs={ + 'pk': self.index.pk + } + ) + + def test_index_delete_api_view_no_permission(self): + self._create_index() + + response = self._request_index_delete_api_view() + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + + self.assertTrue(self.index in Index.objects.all()) + + def test_index_delete_api_view_with_access(self): + self._create_index() + + self.grant_access( + obj=self.index, permission=permission_document_indexing_delete + ) + + response = self._request_index_delete_api_view() + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + + self.assertTrue(self.index not in Index.objects.all()) + + def _request_index_detail_api_view(self): + return self.get( + viewname='rest_api:index-detail', kwargs={ + 'pk': self.index.pk + } + ) + + def test_index_detail_api_view_no_access(self): + self._create_index() + + response = self._request_index_detail_api_view() + + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + self.assertTrue('id' not in response.data) + + def test_index_detail_api_view_with_access(self): + self._create_index() + + self.grant_access( + obj=self.index, permission=permission_document_indexing_view + ) + + response = self._request_index_detail_api_view() + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual( + response.data['id'], self.index.pk + )