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.

155 lines
6.2 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
  1. from django.conf import settings
  2. from django.core.exceptions import ValidationError
  3. from django.test import TestCase
  4. from model_mommy import mommy
  5. from rest_framework import status
  6. from rest_framework.reverse import reverse
  7. from api.tests.utils import CRUDMixin
  8. from projects.models import Member
  9. from projects.tests.utils import prepare_project
  10. from roles.models import Role
  11. from users.tests.utils import make_user
  12. class TestMemberListAPI(CRUDMixin):
  13. def setUp(self):
  14. self.project = prepare_project()
  15. self.non_member = make_user()
  16. admin_role = Role.objects.get(name=settings.ROLE_PROJECT_ADMIN)
  17. self.data = {"user": self.non_member.id, "role": admin_role.id, "project": self.project.item.id}
  18. self.url = reverse(viewname="member_list", args=[self.project.item.id])
  19. def test_allows_project_admin_to_know_members(self):
  20. self.assert_fetch(self.project.admin, status.HTTP_200_OK)
  21. def test_denies_project_staff_to_know_members(self):
  22. for member in self.project.staffs:
  23. self.assert_fetch(member, status.HTTP_403_FORBIDDEN)
  24. def test_denies_non_project_member_to_know_members(self):
  25. self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN)
  26. def test_denies_unauthenticated_user_to_known_members(self):
  27. self.assert_fetch(expected=status.HTTP_403_FORBIDDEN)
  28. def test_allows_project_admin_to_add_member(self):
  29. self.assert_create(self.project.admin, status.HTTP_201_CREATED)
  30. def test_denies_project_staff_to_add_member(self):
  31. for member in self.project.staffs:
  32. self.assert_create(member, status.HTTP_403_FORBIDDEN)
  33. def test_denies_non_project_member_to_add_member(self):
  34. self.assert_create(self.non_member, status.HTTP_403_FORBIDDEN)
  35. def test_denies_unauthenticated_user_to_add_member(self):
  36. self.assert_create(expected=status.HTTP_403_FORBIDDEN)
  37. def assert_bulk_delete(self, user=None, expected=status.HTTP_403_FORBIDDEN):
  38. if user:
  39. self.client.force_login(user)
  40. ids = [item.id for item in self.project.item.role_mappings.all()]
  41. response = self.client.delete(self.url, data={"ids": ids}, format="json")
  42. self.assertEqual(response.status_code, expected)
  43. def test_allows_project_admin_to_remove_members(self):
  44. self.assert_bulk_delete(self.project.admin, status.HTTP_204_NO_CONTENT)
  45. response = self.client.get(self.url)
  46. self.assertEqual(len(response.data), 1)
  47. def test_denies_project_staff_to_remove_members(self):
  48. for member in self.project.staffs:
  49. self.assert_bulk_delete(member, status.HTTP_403_FORBIDDEN)
  50. def test_denies_non_project_member_to_remove_members(self):
  51. self.assert_bulk_delete(self.non_member, status.HTTP_403_FORBIDDEN)
  52. def test_denies_unauthenticated_user_to_remove_members(self):
  53. self.assert_bulk_delete(expected=status.HTTP_403_FORBIDDEN)
  54. class TestMemberRoleDetailAPI(CRUDMixin):
  55. def setUp(self):
  56. self.project = prepare_project()
  57. self.non_member = make_user()
  58. admin_role = Role.objects.get(name=settings.ROLE_PROJECT_ADMIN)
  59. member = Member.objects.get(user=self.project.approver)
  60. self.url = reverse(viewname="member_detail", args=[self.project.item.id, member.id])
  61. self.data = {"role": admin_role.id}
  62. def test_allows_project_admin_to_known_member(self):
  63. self.assert_fetch(self.project.admin, status.HTTP_200_OK)
  64. def test_denies_project_staff_to_know_member(self):
  65. for member in self.project.staffs:
  66. self.assert_fetch(member, status.HTTP_403_FORBIDDEN)
  67. def test_denies_non_project_member_to_know_member(self):
  68. self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN)
  69. def test_denies_unauthenticated_user_to_know_member(self):
  70. self.assert_fetch(expected=status.HTTP_403_FORBIDDEN)
  71. def test_allows_project_admin_to_change_member_role(self):
  72. self.assert_update(self.project.admin, status.HTTP_200_OK)
  73. def test_denies_project_staff_to_change_member_role(self):
  74. for member in self.project.staffs:
  75. self.assert_update(member, status.HTTP_403_FORBIDDEN)
  76. def test_denies_non_project_member_to_change_member_role(self):
  77. self.assert_update(self.non_member, status.HTTP_403_FORBIDDEN)
  78. def test_denies_unauthenticated_user_to_change_member_role(self):
  79. self.assert_update(expected=status.HTTP_403_FORBIDDEN)
  80. class TestMemberFilter(CRUDMixin):
  81. def setUp(self):
  82. self.project = prepare_project()
  83. self.url = reverse(viewname="member_list", args=[self.project.item.id])
  84. self.url += f"?user={self.project.admin.id}"
  85. def test_filter_role_by_user_id(self):
  86. response = self.assert_fetch(self.project.admin, status.HTTP_200_OK)
  87. self.assertEqual(len(response.data), 1)
  88. class TestMyRole(CRUDMixin):
  89. def setUp(self):
  90. self.project = prepare_project()
  91. self.url = reverse(viewname="my_role", args=[self.project.item.id])
  92. def test_admin(self):
  93. response = self.assert_fetch(self.project.admin, status.HTTP_200_OK)
  94. self.assertEqual(response.data["rolename"], settings.ROLE_PROJECT_ADMIN)
  95. def test_approver(self):
  96. response = self.assert_fetch(self.project.approver, status.HTTP_200_OK)
  97. self.assertEqual(response.data["rolename"], settings.ROLE_ANNOTATION_APPROVER)
  98. def test_annotator(self):
  99. response = self.assert_fetch(self.project.annotator, status.HTTP_200_OK)
  100. self.assertEqual(response.data["rolename"], settings.ROLE_ANNOTATOR)
  101. class TestMemberManager(CRUDMixin):
  102. def test_has_role(self):
  103. project = prepare_project()
  104. admin = project.admin
  105. expected = [
  106. (settings.ROLE_PROJECT_ADMIN, True),
  107. (settings.ROLE_ANNOTATION_APPROVER, False),
  108. (settings.ROLE_ANNOTATOR, False),
  109. ]
  110. for role, expect in expected:
  111. self.assertEqual(Member.objects.has_role(project.item, admin, role), expect)
  112. class TestMember(TestCase):
  113. def test_clean(self):
  114. member = mommy.make("Member")
  115. same_user = Member(project=member.project, user=member.user, role=member.role)
  116. with self.assertRaises(ValidationError):
  117. same_user.clean()