Files
mayan-edms/mayan/apps/document_indexing/tests/test_api.py
Roberto Rosario a0d2000419 Test simplification
Unify usage of self.test_document and self.test_documents in
tests.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
2019-05-07 00:54:59 -04:00

104 lines
3.3 KiB
Python

from __future__ import unicode_literals
from rest_framework import status
from mayan.apps.documents.tests import DocumentTestMixin
from mayan.apps.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 IndexTestMixin
class DocumentIndexingAPITestCase(IndexTestMixin, DocumentTestMixin, BaseAPITestCase):
auto_upload_document = False
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.test_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.test_index.pk
}
)
def test_index_delete_api_view_no_permission(self):
self._create_test_index()
response = self._request_index_delete_api_view()
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertTrue(self.test_index in Index.objects.all())
def test_index_delete_api_view_with_access(self):
self._create_test_index()
self.grant_access(
obj=self.test_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.test_index not in Index.objects.all())
def _request_index_detail_api_view(self):
return self.get(
viewname='rest_api:index-detail', kwargs={
'pk': self.test_index.pk
}
)
def test_index_detail_api_view_no_access(self):
self._create_test_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_test_index()
self.grant_access(
obj=self.test_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.test_index.pk
)