Backport test improvements

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2019-04-20 02:39:57 -04:00
parent bfcad278aa
commit 0267c79b07
108 changed files with 5434 additions and 4685 deletions

View File

@@ -1,7 +1,5 @@
from __future__ import unicode_literals
from django.test import override_settings
from rest_framework import status
from mayan.apps.documents.tests import DocumentTestMixin
@@ -16,21 +14,17 @@ from ..permissions import (
from .literals import TEST_COMMENT_TEXT
@override_settings(OCR_AUTO_OCR=False)
class CommentAPITestCase(DocumentTestMixin, BaseAPITestCase):
def setUp(self):
super(CommentAPITestCase, self).setUp()
self.login_user()
def _create_comment(self):
return self.document.comments.create(
comment=TEST_COMMENT_TEXT, user=self.admin_user
def _create_test_comment(self):
return self.test_document.comments.create(
comment=TEST_COMMENT_TEXT, user=self._test_case_user
)
def _request_comment_create_view(self):
return self.post(
viewname='rest_api:comment-list', args=(self.document.pk,),
data={
viewname='rest_api:comment-list', kwargs={
'document_pk': self.test_document.pk
}, data={
'comment': TEST_COMMENT_TEXT
}
)
@@ -38,76 +32,94 @@ class CommentAPITestCase(DocumentTestMixin, BaseAPITestCase):
def test_comment_create_view_no_access(self):
response = self._request_comment_create_view()
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(Comment.objects.count(), 0)
def test_comment_create_view_with_access(self):
self.grant_access(permission=permission_comment_create, obj=self.document)
self.grant_access(
obj=self.test_document, permission=permission_comment_create
)
response = self._request_comment_create_view()
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
comment = Comment.objects.first()
self.assertEqual(Comment.objects.count(), 1)
self.assertEqual(response.data['id'], comment.pk)
def _request_comment_delete_view(self):
return self.delete(
viewname='rest_api:comment-detail', args=(
self.document.pk, self.comment.pk,
)
viewname='rest_api:comment-detail', kwargs={
'document_pk': self.test_document.pk,
'comment_pk': self.test_comment.pk,
}
)
def test_comment_delete_view_no_access(self):
self.comment = self._create_comment()
self.test_comment = self._create_test_comment()
response = self._request_comment_delete_view()
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertTrue(self.comment in Comment.objects.all())
self.assertTrue(self.test_comment in Comment.objects.all())
def test_comment_delete_view_with_access(self):
self.comment = self._create_comment()
self.test_comment = self._create_test_comment()
self.grant_access(
permission=permission_comment_delete, obj=self.document
obj=self.test_document, permission=permission_comment_delete
)
response = self._request_comment_delete_view()
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertFalse(self.comment in Comment.objects.all())
self.assertFalse(self.test_comment in Comment.objects.all())
def _request_comment_view(self):
return self.get(
viewname='rest_api:comment-detail', args=(
self.document.pk, self.comment.pk,
)
viewname='rest_api:comment-detail', kwargs={
'document_pk': self.test_document.pk,
'comment_pk': self.test_comment.pk
}
)
def test_comment_detail_view_no_access(self):
self.comment = self._create_comment()
self.test_comment = self._create_test_comment()
response = self._request_comment_view()
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_comment_detail_view_with_access(self):
self.comment = self._create_comment()
self.test_comment = self._create_test_comment()
self.grant_access(
permission=permission_comment_view, obj=self.document
obj=self.test_document, permission=permission_comment_view
)
response = self._request_comment_view()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['comment'], self.comment.comment)
self.assertEqual(response.data['comment'], self.test_comment.comment)
def _request_comment_list_view(self):
return self.get(
viewname='rest_api:comment-list', args=(self.document.pk,)
viewname='rest_api:comment-list', kwargs={
'document_pk': self.test_document.pk
}
)
def test_comment_list_view_no_access(self):
self.comment = self._create_comment()
self.test_comment = self._create_test_comment()
response = self._request_comment_list_view()
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_comment_list_view_with_access(self):
self.comment = self._create_comment()
self.test_comment = self._create_test_comment()
self.grant_access(
permission=permission_comment_view, obj=self.document
obj=self.test_document, permission=permission_comment_view
)
response = self._request_comment_list_view()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data['results'][0]['comment'], self.comment.comment
response.data['results'][0]['comment'], self.test_comment.comment
)