test_thread_merge_api.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import json
  2. from django.core.urlresolvers import reverse
  3. from django.utils.encoding import smart_str
  4. from misago.acl.testutils import override_acl
  5. from misago.categories.models import Category
  6. from .. import testutils
  7. from ..models import Thread
  8. from .test_threads_api import ThreadsApiTestCase
  9. class ThreadMergeApiTests(ThreadsApiTestCase):
  10. def setUp(self):
  11. super(ThreadMergeApiTests, self).setUp()
  12. Category(
  13. name='Category B',
  14. slug='category-b',
  15. ).insert_at(self.category, position='last-child', save=True)
  16. self.category_b = Category.objects.get(slug='category-b')
  17. self.api_link = reverse('misago:api:thread-merge', kwargs={'pk': self.thread.pk})
  18. def override_other_acl(self, acl=None):
  19. other_category_acl = self.user.acl['categories'][self.category.pk].copy()
  20. other_category_acl.update({
  21. 'can_see': 1,
  22. 'can_browse': 1,
  23. 'can_see_all_threads': 1,
  24. 'can_see_own_threads': 0,
  25. 'can_hide_threads': 0,
  26. 'can_approve_content': 0,
  27. 'can_edit_posts': 0,
  28. 'can_hide_posts': 0,
  29. 'can_hide_own_posts': 0,
  30. 'can_merge_threads': 0
  31. })
  32. if acl:
  33. other_category_acl.update(acl)
  34. categories_acl = self.user.acl['categories']
  35. categories_acl[self.category_b.pk] = other_category_acl
  36. visible_categories = [self.category.pk]
  37. if other_category_acl['can_see']:
  38. visible_categories.append(self.category_b.pk)
  39. override_acl(self.user, {
  40. 'visible_categories': visible_categories,
  41. 'categories': categories_acl,
  42. })
  43. def test_merge_no_permission(self):
  44. """api validates if thread can be merged with other one"""
  45. self.override_acl({
  46. 'can_merge_threads': 0
  47. })
  48. response = self.client.post(self.api_link)
  49. self.assertContains(response, "You don't have permission to merge this thread with others.", status_code=403)
  50. def test_merge_no_url(self):
  51. """api validates if thread url was given"""
  52. self.override_acl({
  53. 'can_merge_threads': 1
  54. })
  55. response = self.client.post(self.api_link)
  56. self.assertContains(response, "This is not a valid thread link.", status_code=400)
  57. def test_invalid_url(self):
  58. """api validates thread url"""
  59. self.override_acl({
  60. 'can_merge_threads': 1
  61. })
  62. response = self.client.post(self.api_link, {
  63. 'thread_url': self.user.get_absolute_url()
  64. })
  65. self.assertContains(response, "This is not a valid thread link.", status_code=400)
  66. def test_current_thread_url(self):
  67. """api validates if thread url given is to current thread"""
  68. self.override_acl({
  69. 'can_merge_threads': 1
  70. })
  71. response = self.client.post(self.api_link, {
  72. 'thread_url': self.thread.get_absolute_url()
  73. })
  74. self.assertContains(response, "You can't merge thread with itself.", status_code=400)
  75. def test_other_thread_exists(self):
  76. """api validates if other thread exists"""
  77. self.override_acl({
  78. 'can_merge_threads': 1
  79. })
  80. self.override_other_acl()
  81. other_thread = testutils.post_thread(self.category_b)
  82. other_thread_url = other_thread.get_absolute_url()
  83. other_thread.delete()
  84. response = self.client.post(self.api_link, {
  85. 'thread_url': other_thread_url
  86. })
  87. self.assertContains(response, "The thread you have entered link to doesn't exist", status_code=400)
  88. def test_other_thread_is_invisible(self):
  89. """api validates if other thread is visible"""
  90. self.override_acl({
  91. 'can_merge_threads': 1
  92. })
  93. self.override_other_acl({
  94. 'can_see': 0
  95. })
  96. other_thread = testutils.post_thread(self.category_b)
  97. response = self.client.post(self.api_link, {
  98. 'thread_url': other_thread.get_absolute_url()
  99. })
  100. self.assertContains(response, "The thread you have entered link to doesn't exist", status_code=400)
  101. def test_other_thread_isnt_mergeable(self):
  102. """api validates if other thread can be merged"""
  103. self.override_acl({
  104. 'can_merge_threads': 1
  105. })
  106. self.override_other_acl({
  107. 'can_merge_threads': 0
  108. })
  109. other_thread = testutils.post_thread(self.category_b)
  110. response = self.client.post(self.api_link, {
  111. 'thread_url': other_thread.get_absolute_url()
  112. })
  113. self.assertContains(response, "You don't have permission to merge this thread", status_code=400)
  114. def test_other_thread_isnt_replyable(self):
  115. """api validates if other thread can be replied, which is condition for merg"""
  116. self.override_acl({
  117. 'can_merge_threads': 1
  118. })
  119. self.override_other_acl({
  120. 'can_reply_threads': 0
  121. })
  122. other_thread = testutils.post_thread(self.category_b)
  123. response = self.client.post(self.api_link, {
  124. 'thread_url': other_thread.get_absolute_url()
  125. })
  126. self.assertContains(response, "You can't merge this thread into thread you can't reply.", status_code=400)
  127. def test_merge_threads(self):
  128. """api merges two threads successfully"""
  129. self.override_acl({
  130. 'can_merge_threads': 1
  131. })
  132. self.override_other_acl({
  133. 'can_merge_threads': 1
  134. })
  135. other_thread = testutils.post_thread(self.category_b)
  136. response = self.client.post(self.api_link, {
  137. 'thread_url': other_thread.get_absolute_url()
  138. })
  139. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  140. # other thread has two posts now
  141. self.assertEqual(other_thread.post_set.count(), 3)
  142. # first thread is gone
  143. with self.assertRaises(Thread.DoesNotExist):
  144. Thread.objects.get(pk=self.thread.pk)