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.

168 lines
7.2 KiB

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