test_threads_bulkdelete_api.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import json
  2. from django.urls import reverse
  3. from misago.acl.testutils import override_acl
  4. from misago.categories import PRIVATE_THREADS_ROOT_NAME
  5. from misago.categories.models import Category
  6. from misago.threads import testutils
  7. from misago.threads.models import Thread
  8. from misago.threads.serializers.moderation import THREADS_LIMIT
  9. from misago.threads.threadtypes import trees_map
  10. from .test_threads_api import ThreadsApiTestCase
  11. class ThreadsBulkDeleteApiTests(ThreadsApiTestCase):
  12. def setUp(self):
  13. super(ThreadsBulkDeleteApiTests, self).setUp()
  14. self.api_link = reverse('misago:api:thread-list')
  15. self.threads = [
  16. testutils.post_thread(
  17. category=self.category,
  18. poster=self.user,
  19. ),
  20. testutils.post_thread(category=self.category),
  21. testutils.post_thread(
  22. category=self.category,
  23. poster=self.user,
  24. ),
  25. ]
  26. def delete(self, url, data=None):
  27. return self.client.delete(url, json.dumps(data), content_type="application/json")
  28. def test_delete_anonymous(self):
  29. """anonymous users can't bulk delete threads"""
  30. self.logout_user()
  31. response = self.delete(self.api_link)
  32. self.assertContains(response, "This action is not available to guests.", status_code=403)
  33. def test_delete_no_ids(self):
  34. """api requires ids to delete"""
  35. self.override_acl({
  36. 'can_hide_own_threads': 0,
  37. 'can_hide_threads': 0,
  38. })
  39. response = self.delete(self.api_link)
  40. self.assertContains(response, "You have to specify at least one thread to delete.", status_code=403)
  41. def test_validate_ids(self):
  42. """api validates that ids are list of ints"""
  43. self.override_acl({
  44. 'can_hide_own_threads': 2,
  45. 'can_hide_threads': 2,
  46. })
  47. response = self.delete(self.api_link, True)
  48. self.assertContains(response, "Expected a list of items", status_code=403)
  49. response = self.delete(self.api_link, 'abbss')
  50. self.assertContains(response, "Expected a list of items", status_code=403)
  51. response = self.delete(self.api_link, [1, 2, 3, 'a', 'b', 'x'])
  52. self.assertContains(response, "One or more thread ids received were invalid.", status_code=403)
  53. def test_validate_ids_length(self):
  54. """api validates that ids are list of ints"""
  55. self.override_acl({
  56. 'can_hide_own_threads': 2,
  57. 'can_hide_threads': 2,
  58. })
  59. response = self.delete(self.api_link, list(range(THREADS_LIMIT + 1)))
  60. self.assertContains(
  61. response,
  62. "No more than {} threads can be deleted at single time.".format(THREADS_LIMIT),
  63. status_code=403,
  64. )
  65. def test_validate_thread_visibility(self):
  66. """api valdiates if user can see deleted thread"""
  67. self.override_acl({
  68. 'can_hide_own_threads': 2,
  69. 'can_hide_threads': 2,
  70. })
  71. unapproved_thread = self.threads[1]
  72. unapproved_thread.is_unapproved = True
  73. unapproved_thread.save()
  74. threads_ids = [p.id for p in self.threads]
  75. response = self.delete(self.api_link, threads_ids)
  76. self.assertContains(response, "threads to delete could not be found", status_code=403)
  77. # no thread was deleted
  78. for thread in self.threads:
  79. Thread.objects.get(pk=thread.pk)
  80. def test_delete_other_user_thread_no_permission(self):
  81. """api valdiates if user can delete other users threads"""
  82. self.override_acl({
  83. 'can_hide_own_threads': 2,
  84. 'can_hide_threads': 0,
  85. })
  86. other_thread = self.threads[1]
  87. response = self.delete(self.api_link, [p.id for p in self.threads])
  88. self.assertEqual(response.status_code, 400)
  89. self.assertEqual(response.json(), [
  90. {
  91. 'thread': {
  92. 'id': other_thread.pk,
  93. 'title': other_thread.title
  94. },
  95. 'error': "You can't delete other users theads in this category."
  96. }
  97. ])
  98. # no threads are removed on failed attempt
  99. for thread in self.threads:
  100. Thread.objects.get(pk=thread.pk)
  101. def test_delete_thread_closed_category_no_permission(self):
  102. """api tests category's closed state"""
  103. self.category.is_closed = True
  104. self.category.save()
  105. self.override_acl({
  106. 'can_hide_threads': 2,
  107. 'can_hide_own_threads': 2,
  108. 'can_close_threads': False,
  109. })
  110. response = self.delete(self.api_link, [p.id for p in self.threads])
  111. self.assertEqual(response.status_code, 400)
  112. self.assertEqual(response.json(), [
  113. {
  114. 'thread': {
  115. 'id': thread.pk,
  116. 'title': thread.title
  117. },
  118. 'error': "This category is closed. You can't delete threads in it."
  119. } for thread in sorted(self.threads, key=lambda i: i.pk)
  120. ])
  121. def test_delete_thread_closed_no_permission(self):
  122. """api tests thread's closed state"""
  123. closed_thread = self.threads[1]
  124. closed_thread.is_closed = True
  125. closed_thread.save()
  126. self.override_acl({
  127. 'can_hide_threads': 2,
  128. 'can_hide_own_threads': 2,
  129. 'can_close_threads': False,
  130. })
  131. response = self.delete(self.api_link, [p.id for p in self.threads])
  132. self.assertEqual(response.status_code, 400)
  133. self.assertEqual(response.json(), [
  134. {
  135. 'thread': {
  136. 'id': closed_thread.pk,
  137. 'title': closed_thread.title
  138. },
  139. 'error': "This thread is closed. You can't delete it."
  140. }
  141. ])
  142. def test_delete_private_thread(self):
  143. """attempt to delete private thread fails"""
  144. private_thread = self.threads[0]
  145. private_thread.category = Category.objects.get(
  146. tree_id=trees_map.get_tree_id_for_root(PRIVATE_THREADS_ROOT_NAME),
  147. )
  148. private_thread.save()
  149. private_thread.threadparticipant_set.create(
  150. user=self.user,
  151. is_owner=True,
  152. )
  153. self.override_acl({
  154. 'can_hide_own_threads': 2,
  155. 'can_hide_threads': 2,
  156. })
  157. threads_ids = [p.id for p in self.threads]
  158. response = self.delete(self.api_link, threads_ids)
  159. self.assertEqual(response.status_code, 403)
  160. self.assertContains(response, "threads to delete could not be found", status_code=403)
  161. Thread.objects.get(pk=private_thread.pk)