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
6.4 KiB

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
2 years ago
2 years ago
2 years ago
  1. from django.conf import settings
  2. from django.test import TestCase
  3. from rest_framework import status
  4. from rest_framework.reverse import reverse
  5. from api.tests.utils import CRUDMixin
  6. from examples.tests.utils import make_doc
  7. from label_types.tests.utils import make_label
  8. from projects.models import Member, Project, ProjectType
  9. from projects.tests.utils import prepare_project
  10. from roles.tests.utils import create_default_roles
  11. from users.tests.utils import make_user
  12. class TestProjectList(CRUDMixin):
  13. @classmethod
  14. def setUpTestData(cls):
  15. cls.project = prepare_project()
  16. cls.non_member = make_user()
  17. cls.url = reverse(viewname="project_list")
  18. def test_return_projects_to_member(self):
  19. for member in self.project.members:
  20. response = self.assert_fetch(member, status.HTTP_200_OK)
  21. project = response.data["results"][0]
  22. self.assertEqual(response.data["count"], 1)
  23. self.assertEqual(project["id"], self.project.item.id)
  24. def test_does_not_return_project_to_non_member(self):
  25. response = self.assert_fetch(self.non_member, status.HTTP_200_OK)
  26. self.assertEqual(response.data["count"], 0)
  27. class TestProjectCreate(CRUDMixin):
  28. @classmethod
  29. def setUpTestData(cls):
  30. create_default_roles()
  31. cls.user = make_user()
  32. cls.url = reverse(viewname="project_list")
  33. cls.data = {
  34. "name": "example",
  35. "project_type": "DocumentClassification",
  36. "description": "example",
  37. "guideline": "example",
  38. "resourcetype": "TextClassificationProject",
  39. }
  40. def test_allows_staff_user_to_create_project(self):
  41. self.user.is_staff = True
  42. self.user.save()
  43. response = self.assert_create(self.user, status.HTTP_201_CREATED)
  44. self.assertEqual(response.data["name"], self.data["name"])
  45. def test_exists_project_administrator(self):
  46. self.user.is_staff = True
  47. self.user.save()
  48. response = self.assert_create(self.user, status.HTTP_201_CREATED)
  49. members = Member.objects.filter(project=response.data["id"])
  50. self.assertEqual(members.count(), 1)
  51. member = members.first()
  52. self.assertEqual(member.role.name, settings.ROLE_PROJECT_ADMIN)
  53. def test_denies_non_staff_user_to_create_project(self):
  54. self.assert_create(self.user, status.HTTP_403_FORBIDDEN)
  55. def test_denies_unauthenticated_user_to_create_project(self):
  56. self.assert_create(expected=status.HTTP_403_FORBIDDEN)
  57. class TestSequenceLabelingProjectCreation(CRUDMixin):
  58. @classmethod
  59. def setUpTestData(cls):
  60. create_default_roles()
  61. cls.user = make_user()
  62. cls.url = reverse(viewname="project_list")
  63. cls.data = {
  64. "name": "example",
  65. "project_type": "SequenceLabeling",
  66. "description": "example",
  67. "guideline": "example",
  68. "allow_overlapping": True,
  69. "grapheme_mode": True,
  70. "resourcetype": "SequenceLabelingProject",
  71. }
  72. def test_allows_staff_user_to_create_project(self):
  73. self.user.is_staff = True
  74. self.user.save()
  75. response = self.assert_create(self.user, status.HTTP_201_CREATED)
  76. self.assertEqual(response.data["allow_overlapping"], self.data["allow_overlapping"])
  77. self.assertEqual(response.data["grapheme_mode"], self.data["grapheme_mode"])
  78. class TestProjectDetailAPI(CRUDMixin):
  79. @classmethod
  80. def setUpTestData(cls):
  81. cls.project = prepare_project("SequenceLabeling")
  82. cls.non_member = make_user()
  83. cls.url = reverse(viewname="project_detail", args=[cls.project.item.id])
  84. cls.data = {"description": "lorem", "resourcetype": "SequenceLabelingProject"}
  85. def test_return_project_to_member(self):
  86. for member in self.project.members:
  87. response = self.assert_fetch(member, status.HTTP_200_OK)
  88. self.assertEqual(response.data["id"], self.project.item.id)
  89. def test_does_not_return_project_to_non_member(self):
  90. self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN)
  91. def test_allows_admin_to_update_project(self):
  92. response = self.assert_update(self.project.admin, status.HTTP_200_OK)
  93. self.assertEqual(response.data["description"], self.data["description"])
  94. def test_denies_project_staff_to_update_project(self):
  95. for member in self.project.staffs:
  96. self.assert_update(member, status.HTTP_403_FORBIDDEN)
  97. def test_denies_non_member_to_update_project(self):
  98. self.assert_update(self.non_member, status.HTTP_403_FORBIDDEN)
  99. def test_allows_admin_to_delete_project(self):
  100. self.assert_delete(self.project.admin, status.HTTP_204_NO_CONTENT)
  101. def test_denies_project_staff_to_delete_project(self):
  102. for member in self.project.staffs:
  103. self.assert_delete(member, status.HTTP_403_FORBIDDEN)
  104. def test_denies_non_member_to_delete_project(self):
  105. self.assert_delete(self.non_member, status.HTTP_403_FORBIDDEN)
  106. class TestProjectModel(TestCase):
  107. def setUp(self):
  108. self.project = prepare_project().item
  109. def test_clone_project(self):
  110. project = self.project.clone()
  111. self.assertNotEqual(project.id, self.project.id)
  112. self.assertEqual(project.name, self.project.name)
  113. self.assertEqual(project.role_mappings.count(), self.project.role_mappings.count())
  114. class TestCloneProject(CRUDMixin):
  115. @classmethod
  116. def setUpTestData(cls):
  117. project = prepare_project(task=ProjectType.DOCUMENT_CLASSIFICATION)
  118. cls.project = project.item
  119. cls.user = project.admin
  120. make_doc(cls.project)
  121. cls.category_type = make_label(cls.project)
  122. cls.url = reverse(viewname="clone_project", args=[cls.project.id])
  123. def test_clone_project(self):
  124. response = self.assert_create(self.user, status.HTTP_201_CREATED)
  125. project = Project.objects.get(id=response.data["id"])
  126. # assert project
  127. self.assertNotEqual(project.id, self.project.id)
  128. self.assertEqual(project.name, self.project.name)
  129. # assert category type
  130. category_type = project.categorytype_set.first()
  131. self.assertEqual(category_type.text, self.category_type.text)
  132. # assert example
  133. example = self.project.examples.first()
  134. cloned_example = project.examples.first()
  135. self.assertEqual(example.text, cloned_example.text)