Reorganize and improve checkouts tests

Signed-off-by: Roberto Rosario <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2019-08-21 04:25:56 -04:00
parent 8bcd2c247f
commit e7faece342
5 changed files with 178 additions and 150 deletions

View File

@@ -4,13 +4,37 @@ import datetime
from django.utils.timezone import now
from mayan.apps.common.literals import TIME_DELTA_UNIT_DAYS
from ..models import DocumentCheckout
class DocumentCheckoutsAPIViewTestMixin(object):
def _request_checkedout_document_view(self):
return self.get(
viewname='rest_api:checkedout-document-view',
kwargs={'pk': self.test_check_out.pk}
)
def _request_test_document_check_out_view(self):
return self.post(
viewname='rest_api:checkout-document-list', data={
'document_pk': self.test_document.pk,
'expiration_datetime': '2099-01-01T12:00'
}
)
def _request_checkout_list_view(self):
return self.get(viewname='rest_api:checkout-document-list')
class DocumentCheckoutTestMixin(object):
_test_document_check_out_seconds = 0.1
def _check_out_test_document(self, user=None):
def _check_out_test_document(self, document=None, user=None):
if not document:
document = self.test_document
if not user:
user = self._test_case_user
@@ -19,7 +43,44 @@ class DocumentCheckoutTestMixin(object):
)
self.test_check_out = DocumentCheckout.objects.check_out_document(
block_new_version=True, document=self.test_document,
block_new_version=True, document=document,
expiration_datetime=self._check_out_expiration_datetime,
user=user
)
class DocumentCheckoutViewTestMixin(object):
def _request_test_document_check_in_get_view(self):
return self.get(
viewname='checkouts:check_in_document', kwargs={
'pk': self.test_document.pk
}
)
def _request_test_document_check_in_post_view(self):
return self.post(
viewname='checkouts:check_in_document', kwargs={
'pk': self.test_document.pk
}
)
def _request_test_document_check_out_detail_view(self):
return self.get(
viewname='checkouts:check_out_info', kwargs={
'pk': self.test_document.pk
}
)
def _request_test_document_check_out_list_view(self):
return self.get(viewname='checkouts:check_out_list')
def _request_test_document_check_out_view(self):
return self.post(
viewname='checkouts:check_out_document', kwargs={
'pk': self.test_document.pk
}, data={
'expiration_datetime_unit': TIME_DELTA_UNIT_DAYS,
'expiration_datetime_amount': 99,
'block_new_version': True
}
)

View File

@@ -13,16 +13,15 @@ from ..permissions import (
permission_document_check_out, permission_document_check_out_detail_view
)
from .mixins import DocumentCheckoutTestMixin
from .mixins import (
DocumentCheckoutsAPIViewTestMixin, DocumentCheckoutTestMixin
)
class CheckoutsAPITestCase(DocumentCheckoutTestMixin, DocumentTestMixin, BaseAPITestCase):
def _request_checkedout_document_view(self):
return self.get(
viewname='rest_api:checkedout-document-view',
kwargs={'pk': self.test_check_out.pk}
)
class CheckoutsAPITestCase(
DocumentCheckoutsAPIViewTestMixin, DocumentCheckoutTestMixin,
DocumentTestMixin, BaseAPITestCase
):
def test_checkedout_document_view_no_access(self):
self._check_out_test_document()
@@ -65,16 +64,8 @@ class CheckoutsAPITestCase(DocumentCheckoutTestMixin, DocumentTestMixin, BaseAPI
force_text(self.test_document.uuid)
)
def _request_document_checkout_view(self):
return self.post(
viewname='rest_api:checkout-document-list', data={
'document_pk': self.test_document.pk,
'expiration_datetime': '2099-01-01T12:00'
}
)
def test_document_checkout_no_access(self):
response = self._request_document_checkout_view()
response = self._request_test_document_check_out_view()
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assertEqual(DocumentCheckout.objects.count(), 0)
@@ -82,16 +73,13 @@ class CheckoutsAPITestCase(DocumentCheckoutTestMixin, DocumentTestMixin, BaseAPI
def test_document_checkout_with_access(self):
self.grant_access(permission=permission_document_check_out, obj=self.test_document)
response = self._request_document_checkout_view()
response = self._request_test_document_check_out_view()
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(
DocumentCheckout.objects.first().document, self.test_document
)
def _request_checkout_list_view(self):
return self.get(viewname='rest_api:checkout-document-list')
def test_checkout_list_view_no_access(self):
self._check_out_test_document()

View File

@@ -10,38 +10,40 @@ from ..permissions import (
from .mixins import DocumentCheckoutTestMixin
class CheckoutLinksTestCase(DocumentCheckoutTestMixin, GenericDocumentViewTestCase):
def _resolve_checkout_link(self):
class CheckoutLinksTestCase(
DocumentCheckoutTestMixin, GenericDocumentViewTestCase
):
def _resolve_document_check_out_link(self):
self.add_test_view(test_object=self.test_document)
context = self.get_test_view()
context['user'] = self._test_case_user
return link_check_out_document.resolve(context=context)
def test_checkout_link_no_access(self):
resolved_link = self._resolve_checkout_link()
self.assertEqual(resolved_link, None)
def test_checkout_link_with_access(self):
self.grant_access(
obj=self.test_document, permission=permission_document_check_out
)
resolved_link = self._resolve_checkout_link()
self.assertNotEqual(resolved_link, None)
def _resolve_checkout_info_link(self):
def _resolve_document_check_out_info_link(self):
self.add_test_view(test_object=self.test_document)
context = self.get_test_view()
context['user'] = self._test_case_user
return link_check_out_info.resolve(context=context)
def test_checkout_info_link_no_access(self):
resolved_link = self._resolve_checkout_info_link()
def test_document_check_out_link_no_access(self):
resolved_link = self._resolve_document_check_out_link()
self.assertEqual(resolved_link, None)
def test_checkout_info_link_with_access(self):
def test_document_check_out_link_with_access(self):
self.grant_access(
obj=self.test_document, permission=permission_document_check_out
)
resolved_link = self._resolve_document_check_out_link()
self.assertNotEqual(resolved_link, None)
def test_document_check_out_info_link_no_access(self):
resolved_link = self._resolve_document_check_out_info_link()
self.assertEqual(resolved_link, None)
def test_document_check_out_info_link_with_access(self):
self.grant_access(
obj=self.test_document,
permission=permission_document_check_out_detail_view
)
resolved_link = self._resolve_checkout_info_link()
resolved_link = self._resolve_document_check_out_info_link()
self.assertNotEqual(resolved_link, None)

View File

@@ -3,7 +3,9 @@ from __future__ import unicode_literals
import time
from mayan.apps.common.tests import BaseTestCase
from mayan.apps.documents.tests import GenericDocumentTestCase, DocumentTestMixin
from mayan.apps.documents.tests import (
GenericDocumentTestCase, DocumentTestMixin
)
from mayan.apps.documents.tests.literals import TEST_SMALL_DOCUMENT_PATH
from ..exceptions import (
@@ -15,7 +17,9 @@ from ..models import DocumentCheckout, NewVersionBlock
from .mixins import DocumentCheckoutTestMixin
class DocumentCheckoutTestCase(DocumentCheckoutTestMixin, GenericDocumentTestCase):
class DocumentCheckoutTestCase(
DocumentCheckoutTestMixin, GenericDocumentTestCase
):
def test_document_check_out(self):
self._check_out_test_document()
@@ -25,7 +29,7 @@ class DocumentCheckoutTestCase(DocumentCheckoutTestMixin, GenericDocumentTestCas
)
)
def test_checkin_in(self):
def test_document_check_in(self):
self._check_out_test_document()
self.test_document.check_in()
@@ -37,7 +41,7 @@ class DocumentCheckoutTestCase(DocumentCheckoutTestMixin, GenericDocumentTestCas
)
)
def test_double_check_out(self):
def test_document_double_check_out(self):
self._create_test_case_superuser()
self._check_out_test_document()
@@ -49,11 +53,11 @@ class DocumentCheckoutTestCase(DocumentCheckoutTestMixin, GenericDocumentTestCas
block_new_version=True
)
def test_checkin_without_checkout(self):
def test_document_checkin_without_checkout(self):
with self.assertRaises(DocumentNotCheckedOut):
self.test_document.check_in()
def test_auto_check_in(self):
def test_document_auto_check_in(self):
self._check_out_test_document()
# Ensure we wait from longer than the document check out expiration
@@ -64,7 +68,9 @@ class DocumentCheckoutTestCase(DocumentCheckoutTestMixin, GenericDocumentTestCas
self.assertFalse(self.test_document.is_checked_out())
class NewVersionBlockTestCase(DocumentCheckoutTestMixin, DocumentTestMixin, BaseTestCase):
class NewVersionBlockTestCase(
DocumentCheckoutTestMixin, DocumentTestMixin, BaseTestCase
):
def test_blocking(self):
NewVersionBlock.objects.block(document=self.test_document)

View File

@@ -1,6 +1,5 @@
from __future__ import unicode_literals
from mayan.apps.common.literals import TIME_DELTA_UNIT_DAYS
from mayan.apps.documents.permissions import permission_document_view
from mayan.apps.documents.tests import GenericDocumentViewTestCase
from mayan.apps.sources.links import link_document_version_upload
@@ -12,64 +11,53 @@ from ..permissions import (
permission_document_check_out, permission_document_check_out_detail_view
)
from .mixins import DocumentCheckoutTestMixin
from .mixins import DocumentCheckoutTestMixin, DocumentCheckoutViewTestMixin
class DocumentCheckoutViewTestCase(DocumentCheckoutTestMixin, GenericDocumentViewTestCase):
def _request_document_check_in_get_view(self):
return self.get(
viewname='checkouts:check_in_document', kwargs={
'pk': self.test_document.pk
}
)
def test_check_in_document_get_view_no_permission(self):
class DocumentCheckoutViewTestCase(
DocumentCheckoutTestMixin, DocumentCheckoutViewTestMixin,
GenericDocumentViewTestCase
):
def test_document_check_in_get_view_no_permission(self):
self._check_out_test_document()
response = self._request_document_check_in_get_view()
response = self._request_test_document_check_in_get_view()
self.assertContains(
response=response, text=self.test_document.label, status_code=200
)
self.assertTrue(self.test_document.is_checked_out())
def test_check_in_document_get_view_with_access(self):
def test_document_check_in_get_view_with_access(self):
self._check_out_test_document()
self.grant_access(
obj=self.test_document, permission=permission_document_check_in
)
response = self._request_document_check_in_get_view()
response = self._request_test_document_check_in_get_view()
self.assertContains(
response=response, text=self.test_document.label, status_code=200
)
self.assertTrue(self.test_document.is_checked_out())
def _request_document_check_in_post_view(self):
return self.post(
viewname='checkouts:check_in_document', kwargs={
'pk': self.test_document.pk
}
)
def test_check_in_document_post_view_no_permission(self):
def test_document_check_in_post_view_no_permission(self):
self._check_out_test_document()
response = self._request_document_check_in_post_view()
response = self._request_test_document_check_in_post_view()
self.assertEqual(response.status_code, 403)
self.assertTrue(self.test_document.is_checked_out())
def test_check_in_document_post_view_with_access(self):
def test_document_check_in_post_view_with_access(self):
self._check_out_test_document()
self.grant_access(
obj=self.test_document, permission=permission_document_check_in
)
response = self._request_document_check_in_post_view()
response = self._request_test_document_check_in_post_view()
self.assertEqual(response.status_code, 302)
self.assertFalse(self.test_document.is_checked_out())
@@ -79,24 +67,13 @@ class DocumentCheckoutViewTestCase(DocumentCheckoutTestMixin, GenericDocumentVie
)
)
def _request_document_checkout_view(self):
return self.post(
viewname='checkouts:check_out_document', kwargs={
'pk': self.test_document.pk
}, data={
'expiration_datetime_0': 2,
'expiration_datetime_1': TIME_DELTA_UNIT_DAYS,
'block_new_version': True
}
)
def test_check_out_document_view_no_permission(self):
response = self._request_document_checkout_view()
def test_document_check_out_view_no_permission(self):
response = self._request_test_document_check_out_view()
self.assertEqual(response.status_code, 403)
self.assertFalse(self.test_document.is_checked_out())
def test_check_out_document_view_with_access(self):
def test_document_check_out_view_with_access(self):
self.grant_access(
obj=self.test_document, permission=permission_document_check_out
)
@@ -105,28 +82,21 @@ class DocumentCheckoutViewTestCase(DocumentCheckoutTestMixin, GenericDocumentVie
permission=permission_document_check_out_detail_view
)
response = self._request_document_checkout_view()
response = self._request_test_document_check_out_view()
self.assertEqual(response.status_code, 302)
self.assertTrue(self.test_document.is_checked_out())
def _request_check_out_detail_view(self):
return self.get(
viewname='checkouts:check_out_info', kwargs={
'pk': self.test_document.pk
}
)
def test_checkout_detail_view_no_permission(self):
def test_document_check_out_detail_view_no_permission(self):
self._check_out_test_document()
response = self._request_check_out_detail_view()
response = self._request_test_document_check_out_detail_view()
self.assertNotContains(
response, text=STATE_LABELS[STATE_CHECKED_OUT], status_code=404
)
def test_checkout_detail_view_with_access(self):
def test_document_check_out_detail_view_with_access(self):
self._check_out_test_document()
self.grant_access(
@@ -134,15 +104,12 @@ class DocumentCheckoutViewTestCase(DocumentCheckoutTestMixin, GenericDocumentVie
permission=permission_document_check_out_detail_view
)
response = self._request_check_out_detail_view()
response = self._request_test_document_check_out_detail_view()
self.assertContains(
response, text=STATE_LABELS[STATE_CHECKED_OUT], status_code=200
)
def _request_check_out_list_view(self):
return self.get(viewname='checkouts:check_out_list')
def test_checkout_list_view_no_permission(self):
def test_document_check_out_list_view_no_permission(self):
self._check_out_test_document()
self.grant_access(
@@ -150,12 +117,12 @@ class DocumentCheckoutViewTestCase(DocumentCheckoutTestMixin, GenericDocumentVie
permission=permission_document_view
)
response = self._request_check_out_list_view()
response = self._request_test_document_check_out_list_view()
self.assertNotContains(
response=response, text=self.test_document.label, status_code=200
)
def test_checkout_list_view_with_access(self):
def test_document_check_out_list_view_with_access(self):
self._check_out_test_document()
self.grant_access(
@@ -167,12 +134,59 @@ class DocumentCheckoutViewTestCase(DocumentCheckoutTestMixin, GenericDocumentVie
permission=permission_document_view
)
response = self._request_check_out_list_view()
response = self._request_test_document_check_out_list_view()
self.assertContains(
response=response, text=self.test_document.label, status_code=200
)
def test_document_new_version_after_check_out(self):
def test_document_check_in_forcefull_view_no_permission(self):
# Gitlab issue #237
# Forcefully checking in a document by a user without adequate
# permissions throws out an error
self._create_test_user()
# Check out document as test_user
self._check_out_test_document(user=self.test_user)
self.grant_access(
obj=self.test_document, permission=permission_document_check_in
)
response = self.post(
viewname='checkouts:check_in_document', kwargs={
'pk': self.test_document.pk
}
)
self.assertContains(
response=response, text='Insufficient permissions', status_code=403
)
self.assertTrue(self.test_document.is_checked_out())
def test_document_check_in_forcefull_view_with_permission(self):
self._create_test_user()
# Check out document as test_user
self._check_out_test_document(user=self.test_user)
self.grant_access(
obj=self.test_document, permission=permission_document_check_in_override
)
# Check in document as test_case_user
response = self.post(
viewname='checkouts:check_in_document', kwargs={
'pk': self.test_document.pk
}
)
self.assertEqual(response.status_code, 302)
self.assertFalse(self.test_document.is_checked_out())
class NewVersionBlockViewTestCase(
DocumentCheckoutTestMixin, DocumentCheckoutViewTestMixin,
GenericDocumentViewTestCase
):
def test_document_check_out_block_new_version(self):
"""
Gitlab issue #231
User shown option to upload new version of a document even though it
@@ -208,46 +222,3 @@ class DocumentCheckoutViewTestCase(DocumentCheckoutTestMixin, GenericDocumentVie
resolved_link = link_document_version_upload.resolve(context=response.context)
self.assertEqual(resolved_link, None)
def test_forcefull_check_in_document_view_no_permission(self):
# Gitlab issue #237
# Forcefully checking in a document by a user without adequate
# permissions throws out an error
self._create_test_case_superuser()
self._check_out_test_document(user=self._test_case_superuser)
self.grant_access(
obj=self.test_document, permission=permission_document_check_in
)
response = self.post(
viewname='checkouts:check_in_document', kwargs={
'pk': self.test_document.pk
}
)
self.assertContains(
response=response, text='Insufficient permissions', status_code=403
)
self.assertTrue(self.test_document.is_checked_out())
def test_forcefull_check_in_document_view_with_permission(self):
self._create_test_case_superuser()
self._check_out_test_document(user=self._test_case_superuser)
self.grant_access(
obj=self.test_document, permission=permission_document_check_in
)
self.grant_access(
obj=self.test_document, permission=permission_document_check_in_override
)
response = self.post(
viewname='checkouts:check_in_document', kwargs={
'pk': self.test_document.pk
}
)
self.assertEqual(response.status_code, 302)
self.assertFalse(self.test_document.is_checked_out())