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.4 KiB

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