test_thread_merge_api.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. final_acl = {
  20. 'can_see': 1,
  21. 'can_browse': 1,
  22. 'can_see_all_threads': 1,
  23. 'can_see_own_threads': 0,
  24. 'can_hide_threads': 0,
  25. 'can_approve_content': 0,
  26. 'can_edit_posts': 0,
  27. 'can_hide_posts': 0,
  28. 'can_hide_own_posts': 0,
  29. 'can_merge_threads': 0
  30. }
  31. final_acl.update(acl or {})
  32. categories_acl = self.user.acl['categories']
  33. categories_acl[self.category_b.pk] = final_acl
  34. visible_categories = [self.category.pk]
  35. if final_acl['can_see']:
  36. visible_categories.append(self.category_b.pk)
  37. override_acl(self.user, {
  38. 'visible_categories': visible_categories,
  39. 'categories': categories_acl,
  40. })
  41. def test_merge_no_permission(self):
  42. """api validates if thread can be merged with other one"""
  43. self.override_acl({
  44. 'can_merge_threads': 0
  45. })
  46. response = self.client.post(self.api_link)
  47. self.assertContains(response, "You don't have permission to merge this thread with others.", status_code=403)
  48. def test_merge_no_url(self):
  49. """api validates if thread url was given"""
  50. self.override_acl({
  51. 'can_merge_threads': 1
  52. })
  53. response = self.client.post(self.api_link)
  54. self.assertContains(response, "This is not a valid thread link.", status_code=400)
  55. def test_invalid_url(self):
  56. """api validates thread url"""
  57. self.override_acl({
  58. 'can_merge_threads': 1
  59. })
  60. response = self.client.post(self.api_link, {
  61. 'thread_url': self.user.get_absolute_url()
  62. })
  63. self.assertContains(response, "This is not a valid thread link.", status_code=400)
  64. def test_current_thread_url(self):
  65. """api validates if thread url given is to current thread"""
  66. self.override_acl({
  67. 'can_merge_threads': 1
  68. })
  69. response = self.client.post(self.api_link, {
  70. 'thread_url': self.thread.get_absolute_url()
  71. })
  72. self.assertContains(response, "You can't merge thread with itself.", status_code=400)
  73. def test_other_thread_exists(self):
  74. """api validates if other thread exists"""
  75. self.override_acl({
  76. 'can_merge_threads': 1
  77. })
  78. self.override_other_acl()
  79. other_thread = testutils.post_thread(self.category_b)
  80. other_thread_url = other_thread.get_absolute_url()
  81. other_thread.delete()
  82. response = self.client.post(self.api_link, {
  83. 'thread_url': other_thread_url
  84. })
  85. self.assertContains(response, "The thread you have entered link to doesn't exist", status_code=400)
  86. def test_other_thread_is_invisible(self):
  87. """api validates if other thread is visible"""
  88. self.override_acl({
  89. 'can_merge_threads': 1
  90. })
  91. self.override_other_acl({
  92. 'can_see': 0
  93. })
  94. other_thread = testutils.post_thread(self.category_b)
  95. response = self.client.post(self.api_link, {
  96. 'thread_url': other_thread.get_absolute_url()
  97. })
  98. self.assertContains(response, "The thread you have entered link to doesn't exist", status_code=400)
  99. def test_other_thread_isnt_mergeable(self):
  100. """api validates if other thread can be merged"""
  101. self.override_acl({
  102. 'can_merge_threads': 1
  103. })
  104. self.override_other_acl({
  105. 'can_merge_threads': 0
  106. })
  107. other_thread = testutils.post_thread(self.category_b)
  108. response = self.client.post(self.api_link, {
  109. 'thread_url': other_thread.get_absolute_url()
  110. })
  111. self.assertContains(response, "You don't have permission to merge this thread", status_code=400)
  112. def test_merge_threads(self):
  113. """api merges two threads successfully"""
  114. self.override_acl({
  115. 'can_merge_threads': 1
  116. })
  117. self.override_other_acl({
  118. 'can_merge_threads': 1
  119. })
  120. other_thread = testutils.post_thread(self.category_b)
  121. response = self.client.post(self.api_link, {
  122. 'thread_url': other_thread.get_absolute_url()
  123. })
  124. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  125. # other thread has two posts now
  126. self.assertEqual(other_thread.post_set.count(), 3)
  127. # first thread is gone
  128. with self.assertRaises(Thread.DoesNotExist):
  129. Thread.objects.get(pk=self.thread.pk)