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.

117 lines
5.0 KiB

  1. from rest_framework import status
  2. from rest_framework.reverse import reverse
  3. from .utils import make_assignment, make_doc
  4. from api.tests.utils import CRUDMixin
  5. from examples.models import Assignment
  6. from projects.models import Member
  7. from projects.tests.utils import prepare_project
  8. from users.tests.utils import make_user
  9. class TestAssignmentList(CRUDMixin):
  10. def setUp(self):
  11. self.project = prepare_project()
  12. self.non_member = make_user()
  13. self.example = make_doc(self.project.item)
  14. make_assignment(self.project.item, self.example, self.project.admin)
  15. self.data = {"example": self.example.id, "assignee": self.project.staffs[0].id}
  16. self.url = reverse(viewname="assignment_list", args=[self.project.item.id])
  17. def test_allow_project_member_to_list_assignments(self):
  18. for member in self.project.members:
  19. self.assert_fetch(member, status.HTTP_200_OK)
  20. def test_denies_non_project_member_to_list_assignments(self):
  21. self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN)
  22. def test_denies_unauthenticated_user_to_list_assignments(self):
  23. self.assert_fetch(expected=status.HTTP_403_FORBIDDEN)
  24. def test_allows_project_admin_to_assign(self):
  25. response = self.assert_create(self.project.admin, status.HTTP_201_CREATED)
  26. self.assertEqual(response.data["example"], self.data["example"])
  27. self.assertEqual(response.data["assignee"], self.data["assignee"])
  28. def test_denies_non_admin_to_assign(self):
  29. for member in self.project.staffs:
  30. self.assert_create(member, status.HTTP_403_FORBIDDEN)
  31. def test_denies_non_project_member_to_assign(self):
  32. self.assert_create(self.non_member, status.HTTP_403_FORBIDDEN)
  33. def test_denies_unauthenticated_user_to_assign(self):
  34. self.assert_create(expected=status.HTTP_403_FORBIDDEN)
  35. class TestAssignmentDetail(CRUDMixin):
  36. def setUp(self):
  37. self.project = prepare_project()
  38. self.non_member = make_user()
  39. example = make_doc(self.project.item)
  40. assignment = make_assignment(self.project.item, example, self.project.admin)
  41. self.data = {"assignee": self.project.staffs[0].id}
  42. self.url = reverse(viewname="assignment_detail", args=[self.project.item.id, assignment.id])
  43. def test_allows_project_member_to_get_assignment(self):
  44. for member in self.project.members:
  45. self.assert_fetch(member, status.HTTP_200_OK)
  46. def test_denies_non_project_member_to_get_assignment(self):
  47. self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN)
  48. def test_denies_unauthenticated_user_to_get_assignment(self):
  49. self.assert_fetch(expected=status.HTTP_403_FORBIDDEN)
  50. def test_allows_project_admin_to_reassign(self):
  51. response = self.assert_update(self.project.admin, status.HTTP_200_OK)
  52. self.assertEqual(response.data["assignee"], self.data["assignee"])
  53. def test_denies_non_admin_to_reassign(self):
  54. for member in self.project.staffs:
  55. self.assert_update(member, status.HTTP_403_FORBIDDEN)
  56. def test_denies_non_project_member_to_reassign(self):
  57. self.assert_update(self.non_member, status.HTTP_403_FORBIDDEN)
  58. def test_denies_unauthenticated_user_to_reassign(self):
  59. self.assert_update(expected=status.HTTP_403_FORBIDDEN)
  60. def test_allows_project_admin_to_unassign(self):
  61. self.assert_delete(self.project.admin, status.HTTP_204_NO_CONTENT)
  62. def test_denies_non_admin_to_unassign(self):
  63. for member in self.project.staffs:
  64. self.assert_delete(member, status.HTTP_403_FORBIDDEN)
  65. def test_denies_non_project_member_to_unassign(self):
  66. self.assert_delete(self.non_member, status.HTTP_403_FORBIDDEN)
  67. def test_denies_unauthenticated_user_to_unassign(self):
  68. self.assert_delete(expected=status.HTTP_403_FORBIDDEN)
  69. class TestAssignmentBulk(CRUDMixin):
  70. def setUp(self):
  71. self.project = prepare_project()
  72. self.non_member = make_user()
  73. self.example = make_doc(self.project.item)
  74. members = Member.objects.filter(project=self.project.item)
  75. workloads = [{"member_id": member.id, "weight": 100} for member in members]
  76. self.data = {"strategy_name": "sampling_without_replacement", "workloads": workloads}
  77. self.url = reverse(viewname="bulk_assignment", args=[self.project.item.id])
  78. def test_denies_non_admin_to_bulk_assign(self):
  79. for member in self.project.staffs:
  80. self.assert_create(member, status.HTTP_403_FORBIDDEN)
  81. def test_denies_non_project_member_to_bulk_assign(self):
  82. self.assert_create(self.non_member, status.HTTP_403_FORBIDDEN)
  83. def test_denies_unauthenticated_user_to_bulk_assign(self):
  84. self.assert_create(expected=status.HTTP_403_FORBIDDEN)
  85. def test_allows_project_admin_to_bulk_assign(self):
  86. self.assert_create(self.project.admin, status.HTTP_201_CREATED)
  87. expected = self.project.item.examples.count() * len(self.project.members)
  88. self.assertEqual(Assignment.objects.count(), expected)