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.

133 lines
5.5 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
  1. from rest_framework import status
  2. from rest_framework.reverse import reverse
  3. from .utils import make_comment, make_doc
  4. from api.tests.utils import CRUDMixin
  5. from projects.tests.utils import prepare_project
  6. from users.tests.utils import make_user
  7. class TestCommentListDocAPI(CRUDMixin):
  8. @classmethod
  9. def setUpTestData(cls):
  10. cls.project = prepare_project()
  11. cls.non_member = make_user()
  12. doc1 = make_doc(cls.project.item)
  13. doc2 = make_doc(cls.project.item)
  14. make_comment(doc1, cls.project.admin)
  15. make_comment(doc2, cls.project.admin)
  16. cls.data = {"text": "example"}
  17. cls.url = reverse(viewname="comment_list", args=[cls.project.item.id])
  18. cls.url += f"?example={doc1.id}"
  19. def test_allows_project_member_to_list_comments(self):
  20. for member in self.project.members:
  21. response = self.assert_fetch(member, status.HTTP_200_OK)
  22. self.assertEqual(response.data["count"], 1)
  23. def test_denies_non_project_member_to_list_comments(self):
  24. self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN)
  25. def test_denies_unauthenticated_user_to_list_comments(self):
  26. self.assert_fetch(expected=status.HTTP_403_FORBIDDEN)
  27. def test_allows_project_member_to_create_comment(self):
  28. for member in self.project.members:
  29. self.assert_create(member, status.HTTP_201_CREATED)
  30. def test_denies_non_project_member_to_create_comment(self):
  31. self.assert_create(self.non_member, status.HTTP_403_FORBIDDEN)
  32. def test_denies_unauthenticated_user_to_create_comment(self):
  33. self.assert_create(expected=status.HTTP_403_FORBIDDEN)
  34. class TestCommentListProjectAPI(CRUDMixin):
  35. def setUp(self):
  36. self.project = prepare_project()
  37. self.non_member = make_user()
  38. self.doc = make_doc(self.project.item)
  39. make_comment(self.doc, self.project.admin)
  40. self.url = reverse(viewname="comment_list", args=[self.project.item.id])
  41. def test_allows_project_member_to_list_comments(self):
  42. for member in self.project.members:
  43. response = self.assert_fetch(member, status.HTTP_200_OK)
  44. self.assertEqual(response.data["count"], 1)
  45. def test_denies_non_project_member_to_list_comments(self):
  46. self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN)
  47. def test_denies_unauthenticated_user_to_list_comments(self):
  48. self.assert_fetch(expected=status.HTTP_403_FORBIDDEN)
  49. def assert_bulk_delete(self, user=None, expected=status.HTTP_403_FORBIDDEN):
  50. ids = [item.id for item in self.doc.comments.all()]
  51. if user:
  52. self.client.force_login(user)
  53. response = self.client.delete(self.url, data={"ids": ids}, format="json")
  54. self.assertEqual(response.status_code, expected)
  55. def test_allows_project_member_to_delete_comments(self):
  56. # Todo: Disallow non admin to delete comments.
  57. for member in self.project.members:
  58. self.assert_bulk_delete(member, status.HTTP_204_NO_CONTENT)
  59. response = self.client.get(self.url)
  60. self.assertEqual(response.data["count"], 0)
  61. def test_denies_non_project_member_to_delete_comments(self):
  62. self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN)
  63. def test_denies_unauthenticated_user_to_delete_comments(self):
  64. self.assert_fetch(expected=status.HTTP_403_FORBIDDEN)
  65. class TestCommentDetailAPI(CRUDMixin):
  66. def setUp(self):
  67. self.project = prepare_project()
  68. self.non_member = make_user()
  69. doc = make_doc(self.project.item)
  70. comment = make_comment(doc, self.project.admin)
  71. self.data = {"text": "example"}
  72. self.url = reverse(viewname="comment_detail", args=[self.project.item.id, comment.id])
  73. def test_allows_comment_owner_to_get_comment(self):
  74. # Todo: Allows project member to get comment.
  75. self.assert_fetch(self.project.admin, status.HTTP_200_OK)
  76. def test_denies_non_comment_owner_to_get_comment(self):
  77. for member in self.project.staffs:
  78. self.assert_fetch(member, status.HTTP_403_FORBIDDEN)
  79. def test_denies_non_project_member_to_get_comment(self):
  80. self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN)
  81. def test_denies_unauthenticated_user_to_get_comment(self):
  82. self.assert_fetch(expected=status.HTTP_403_FORBIDDEN)
  83. def test_allows_comment_owner_to_update_comment(self):
  84. response = self.assert_update(self.project.admin, status.HTTP_200_OK)
  85. self.assertEqual(response.data["text"], self.data["text"])
  86. def test_denies_non_comment_owner_to_update_comment(self):
  87. for member in self.project.staffs:
  88. self.assert_update(member, status.HTTP_403_FORBIDDEN)
  89. def test_denies_non_project_member_to_update_comment(self):
  90. self.assert_update(self.non_member, status.HTTP_403_FORBIDDEN)
  91. def test_denies_unauthenticated_user_to_update_comment(self):
  92. self.assert_update(expected=status.HTTP_403_FORBIDDEN)
  93. def test_allows_comment_owner_to_delete_comment(self):
  94. self.assert_delete(self.project.admin, status.HTTP_204_NO_CONTENT)
  95. def test_denies_non_comment_owner_to_delete_comment(self):
  96. for member in self.project.staffs:
  97. self.assert_delete(member, status.HTTP_403_FORBIDDEN)
  98. def test_denies_non_project_member_to_delete_comment(self):
  99. self.assert_delete(self.non_member, status.HTTP_403_FORBIDDEN)
  100. def test_denies_unauthenticated_user_to_delete_comment(self):
  101. self.assert_delete(expected=status.HTTP_403_FORBIDDEN)