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.

126 lines
4.8 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 rest_framework import status
  3. from rest_framework.reverse import reverse
  4. from api.tests.utils import CRUDMixin
  5. from projects.models import Member
  6. from projects.tests.utils import prepare_project
  7. from roles.tests.utils import create_default_roles
  8. from users.tests.utils import make_user
  9. class TestProjectList(CRUDMixin):
  10. @classmethod
  11. def setUpTestData(cls):
  12. cls.project = prepare_project()
  13. cls.non_member = make_user()
  14. cls.url = reverse(viewname="project_list")
  15. def test_return_projects_to_member(self):
  16. for member in self.project.members:
  17. response = self.assert_fetch(member, status.HTTP_200_OK)
  18. project = response.data["results"][0]
  19. self.assertEqual(response.data["count"], 1)
  20. self.assertEqual(project["id"], self.project.item.id)
  21. def test_does_not_return_project_to_non_member(self):
  22. response = self.assert_fetch(self.non_member, status.HTTP_200_OK)
  23. self.assertEqual(response.data["count"], 0)
  24. class TestProjectCreate(CRUDMixin):
  25. @classmethod
  26. def setUpTestData(cls):
  27. create_default_roles()
  28. cls.user = make_user()
  29. cls.url = reverse(viewname="project_list")
  30. cls.data = {
  31. "name": "example",
  32. "project_type": "DocumentClassification",
  33. "description": "example",
  34. "guideline": "example",
  35. "resourcetype": "TextClassificationProject",
  36. }
  37. def test_allows_staff_user_to_create_project(self):
  38. self.user.is_staff = True
  39. self.user.save()
  40. response = self.assert_create(self.user, status.HTTP_201_CREATED)
  41. self.assertEqual(response.data["name"], self.data["name"])
  42. def test_exists_project_administrator(self):
  43. self.user.is_staff = True
  44. self.user.save()
  45. response = self.assert_create(self.user, status.HTTP_201_CREATED)
  46. members = Member.objects.filter(project=response.data["id"])
  47. self.assertEqual(members.count(), 1)
  48. member = members.first()
  49. self.assertEqual(member.role.name, settings.ROLE_PROJECT_ADMIN)
  50. def test_denies_non_staff_user_to_create_project(self):
  51. self.assert_create(self.user, status.HTTP_403_FORBIDDEN)
  52. def test_denies_unauthenticated_user_to_create_project(self):
  53. self.assert_create(expected=status.HTTP_403_FORBIDDEN)
  54. class TestSequenceLabelingProjectCreation(CRUDMixin):
  55. @classmethod
  56. def setUpTestData(cls):
  57. create_default_roles()
  58. cls.user = make_user()
  59. cls.url = reverse(viewname="project_list")
  60. cls.data = {
  61. "name": "example",
  62. "project_type": "SequenceLabeling",
  63. "description": "example",
  64. "guideline": "example",
  65. "allow_overlapping": True,
  66. "grapheme_mode": True,
  67. "resourcetype": "SequenceLabelingProject",
  68. }
  69. def test_allows_staff_user_to_create_project(self):
  70. self.user.is_staff = True
  71. self.user.save()
  72. response = self.assert_create(self.user, status.HTTP_201_CREATED)
  73. self.assertEqual(response.data["allow_overlapping"], self.data["allow_overlapping"])
  74. self.assertEqual(response.data["grapheme_mode"], self.data["grapheme_mode"])
  75. class TestProjectDetailAPI(CRUDMixin):
  76. @classmethod
  77. def setUpTestData(cls):
  78. cls.project = prepare_project("SequenceLabeling")
  79. cls.non_member = make_user()
  80. cls.url = reverse(viewname="project_detail", args=[cls.project.item.id])
  81. cls.data = {"description": "lorem", "resourcetype": "SequenceLabelingProject"}
  82. def test_return_project_to_member(self):
  83. for member in self.project.members:
  84. response = self.assert_fetch(member, status.HTTP_200_OK)
  85. self.assertEqual(response.data["id"], self.project.item.id)
  86. def test_does_not_return_project_to_non_member(self):
  87. self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN)
  88. def test_allows_admin_to_update_project(self):
  89. response = self.assert_update(self.project.admin, status.HTTP_200_OK)
  90. self.assertEqual(response.data["description"], self.data["description"])
  91. def test_denies_project_staff_to_update_project(self):
  92. for member in self.project.staffs:
  93. self.assert_update(member, status.HTTP_403_FORBIDDEN)
  94. def test_denies_non_member_to_update_project(self):
  95. self.assert_update(self.non_member, status.HTTP_403_FORBIDDEN)
  96. def test_allows_admin_to_delete_project(self):
  97. self.assert_delete(self.project.admin, status.HTTP_204_NO_CONTENT)
  98. def test_denies_project_staff_to_delete_project(self):
  99. for member in self.project.staffs:
  100. self.assert_delete(member, status.HTTP_403_FORBIDDEN)
  101. def test_denies_non_member_to_delete_project(self):
  102. self.assert_delete(self.non_member, status.HTTP_403_FORBIDDEN)