You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
7.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. from django.conf import settings
  2. from django.utils.http import urlencode
  3. from rest_framework import status
  4. from rest_framework.reverse import reverse
  5. from api.models import DOCUMENT_CLASSIFICATION
  6. from api.tests.api.utils import (CRUDMixin, assign_user_to_role, make_doc,
  7. make_example_state, make_user, prepare_project)
  8. class TestExampleListAPI(CRUDMixin):
  9. def setUp(self):
  10. self.project = prepare_project(task=DOCUMENT_CLASSIFICATION)
  11. self.non_member = make_user()
  12. self.example = make_doc(self.project.item)
  13. self.data = {'text': 'example'}
  14. self.url = reverse(viewname='example_list', args=[self.project.item.id])
  15. def test_allows_project_member_to_list_docs(self):
  16. for member in self.project.members:
  17. response = self.assert_fetch(member, status.HTTP_200_OK)
  18. self.assertEqual(response.data['count'], 1)
  19. self.assertIn('results', response.data)
  20. for item in response.data['results']:
  21. self.assertIn('text', item)
  22. def test_denies_non_project_member_to_list_docs(self):
  23. self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN)
  24. def test_denies_unauthenticated_user_to_list_docs(self):
  25. self.assert_fetch(expected=status.HTTP_403_FORBIDDEN)
  26. def test_allows_project_admin_to_create_doc(self):
  27. response = self.assert_create(self.project.admin, status.HTTP_201_CREATED)
  28. self.assertEqual(response.data['text'], self.data['text'])
  29. def test_denies_project_staff_to_create_doc(self):
  30. for member in self.project.staffs:
  31. self.assert_create(member, status.HTTP_403_FORBIDDEN)
  32. def test_denies_unauthenticated_user_to_create_doc(self):
  33. self.assert_create(expected=status.HTTP_403_FORBIDDEN)
  34. def test_is_confirmed(self):
  35. make_example_state(self.example, self.project.admin)
  36. response = self.assert_fetch(self.project.admin, status.HTTP_200_OK)
  37. self.assertTrue(response.data['results'][0]['is_confirmed'])
  38. def test_is_not_confirmed(self):
  39. response = self.assert_fetch(self.project.admin, status.HTTP_200_OK)
  40. self.assertFalse(response.data['results'][0]['is_confirmed'])
  41. def test_does_not_share_another_user_confirmed(self):
  42. make_example_state(self.example, self.project.admin)
  43. response = self.assert_fetch(self.project.annotator, status.HTTP_200_OK)
  44. self.assertFalse(response.data['results'][0]['is_confirmed'])
  45. class TestExampleListCollaborative(CRUDMixin):
  46. def setUp(self):
  47. self.project = prepare_project(task=DOCUMENT_CLASSIFICATION)
  48. self.example = make_doc(self.project.item)
  49. self.url = reverse(viewname='example_list', args=[self.project.item.id])
  50. def test_shares_confirmed_in_same_role(self):
  51. annotator1 = make_user()
  52. assign_user_to_role(annotator1, self.project.item, settings.ROLE_ANNOTATOR)
  53. annotator2 = make_user()
  54. assign_user_to_role(annotator2, self.project.item, settings.ROLE_ANNOTATOR)
  55. make_example_state(self.example, annotator1)
  56. response = self.assert_fetch(annotator1, status.HTTP_200_OK)
  57. self.assertTrue(response.data['results'][0]['is_confirmed'])
  58. response = self.assert_fetch(annotator2, status.HTTP_200_OK)
  59. self.assertTrue(response.data['results'][0]['is_confirmed'])
  60. def test_does_not_share_confirmed_in_other_role(self):
  61. admin = self.project.admin
  62. approver = self.project.approver
  63. make_example_state(self.example, admin)
  64. response = self.assert_fetch(admin, status.HTTP_200_OK)
  65. self.assertTrue(response.data['results'][0]['is_confirmed'])
  66. response = self.assert_fetch(approver, status.HTTP_200_OK)
  67. self.assertFalse(response.data['results'][0]['is_confirmed'])
  68. class TestExampleListFilter(CRUDMixin):
  69. def setUp(self):
  70. self.project = prepare_project(task=DOCUMENT_CLASSIFICATION)
  71. self.example = make_doc(self.project.item)
  72. make_example_state(self.example, self.project.admin)
  73. def reverse(self, query_kwargs=None):
  74. base_url = reverse(viewname='example_list', args=[self.project.item.id])
  75. self.url = '{}?{}'.format(base_url, urlencode(query_kwargs))
  76. def assert_filter(self, data, user, expected):
  77. self.reverse(query_kwargs=data)
  78. response = self.assert_fetch(user, status.HTTP_200_OK)
  79. self.assertEqual(response.data['count'], expected)
  80. def test_returns_example_if_confirmed_is_true(self):
  81. user = self.project.admin
  82. self.assert_filter(data={'confirmed': 'True'}, user=user, expected=1)
  83. def test_does_not_return_example_if_confirmed_is_false(self):
  84. user = self.project.admin
  85. self.assert_filter(data={'confirmed': 'False'}, user=user, expected=0)
  86. def test_returns_example_if_confirmed_is_empty(self):
  87. user = self.project.admin
  88. self.assert_filter(data={'confirmed': ''}, user=user, expected=1)
  89. def test_does_not_return_example_if_user_is_different(self):
  90. user = self.project.approver
  91. self.assert_filter(data={'confirmed': 'True'}, user=user, expected=0)
  92. def test_returns_example_if_user_is_different(self):
  93. user = self.project.approver
  94. self.assert_filter(data={'confirmed': 'False'}, user=user, expected=1)
  95. def test_returns_example_if_user_is_different_and_confirmed_is_empty(self):
  96. user = self.project.approver
  97. self.assert_filter(data={'confirmed': ''}, user=user, expected=1)
  98. class TestExampleDetail(CRUDMixin):
  99. def setUp(self):
  100. self.project = prepare_project(task=DOCUMENT_CLASSIFICATION)
  101. self.non_member = make_user()
  102. doc = make_doc(self.project.item)
  103. self.data = {'text': 'example'}
  104. self.url = reverse(viewname='example_detail', args=[self.project.item.id, doc.id])
  105. def test_allows_project_member_to_get_doc(self):
  106. for member in self.project.members:
  107. response = self.assert_fetch(member, status.HTTP_200_OK)
  108. self.assertIn('text', response.data)
  109. def test_denies_non_project_member_to_get_doc(self):
  110. self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN)
  111. def test_denies_unauthenticated_user_to_get_doc(self):
  112. self.assert_fetch(expected=status.HTTP_403_FORBIDDEN)
  113. def test_allows_project_admin_to_update_doc(self):
  114. response = self.assert_update(self.project.admin, status.HTTP_200_OK)
  115. self.assertEqual(response.data['text'], self.data['text'])
  116. def test_denies_project_staff_to_update_doc(self):
  117. for member in self.project.staffs:
  118. self.assert_update(member, status.HTTP_403_FORBIDDEN)
  119. def test_denies_non_project_member_to_update_doc(self):
  120. self.assert_update(self.non_member, status.HTTP_403_FORBIDDEN)
  121. def test_allows_project_admin_to_delete_doc(self):
  122. self.assert_delete(self.project.admin, status.HTTP_204_NO_CONTENT)
  123. def test_denies_project_staff_to_delete_doc(self):
  124. for member in self.project.staffs:
  125. self.assert_delete(member, status.HTTP_403_FORBIDDEN)
  126. def test_denies_non_project_member_to_delete_doc(self):
  127. self.assert_delete(self.non_member, status.HTTP_403_FORBIDDEN)