test_thread_merge_api.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 Poll, PollVote, 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)
  145. def test_merge_threads_kept_poll(self):
  146. """api merges two threads successfully, keeping poll from old thread"""
  147. self.override_acl({
  148. 'can_merge_threads': 1
  149. })
  150. self.override_other_acl({
  151. 'can_merge_threads': 1
  152. })
  153. other_thread = testutils.post_thread(self.category_b)
  154. poll = testutils.post_poll(other_thread, self.user)
  155. response = self.client.post(self.api_link, {
  156. 'thread_url': other_thread.get_absolute_url()
  157. })
  158. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  159. # other thread has two posts now
  160. self.assertEqual(other_thread.post_set.count(), 3)
  161. # first thread is gone
  162. with self.assertRaises(Thread.DoesNotExist):
  163. Thread.objects.get(pk=self.thread.pk)
  164. # poll and its votes were kept
  165. self.assertEqual(Poll.objects.filter(pk=poll.pk, thread=other_thread).count(), 1)
  166. self.assertEqual(PollVote.objects.filter(poll=poll, thread=other_thread).count(), 4)
  167. def test_merge_threads_moved_poll(self):
  168. """api merges two threads successfully, moving poll from other thread"""
  169. self.override_acl({
  170. 'can_merge_threads': 1
  171. })
  172. self.override_other_acl({
  173. 'can_merge_threads': 1
  174. })
  175. other_thread = testutils.post_thread(self.category_b)
  176. poll = testutils.post_poll(self.thread, self.user)
  177. response = self.client.post(self.api_link, {
  178. 'thread_url': other_thread.get_absolute_url()
  179. })
  180. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  181. # other thread has two posts now
  182. self.assertEqual(other_thread.post_set.count(), 3)
  183. # first thread is gone
  184. with self.assertRaises(Thread.DoesNotExist):
  185. Thread.objects.get(pk=self.thread.pk)
  186. # poll and its votes were moved
  187. self.assertEqual(Poll.objects.filter(pk=poll.pk, thread=other_thread).count(), 1)
  188. self.assertEqual(PollVote.objects.filter(poll=poll, thread=other_thread).count(), 4)
  189. def test_threads_merge_conflict(self):
  190. """api errors on merge conflict, returning list of available polls"""
  191. self.override_acl({
  192. 'can_merge_threads': 1
  193. })
  194. self.override_other_acl({
  195. 'can_merge_threads': 1
  196. })
  197. other_thread = testutils.post_thread(self.category_b)
  198. poll = testutils.post_poll(self.thread, self.user)
  199. other_poll = testutils.post_poll(other_thread, self.user)
  200. response = self.client.post(self.api_link, {
  201. 'thread_url': other_thread.get_absolute_url()
  202. })
  203. self.assertEqual(response.status_code, 400)
  204. self.assertEqual(response.json(), {
  205. 'polls': [
  206. [0, "Delete all polls"],
  207. [poll.pk, poll.question],
  208. [other_poll.pk, other_poll.question]
  209. ]
  210. })
  211. # polls and votes were untouched
  212. self.assertEqual(Poll.objects.count(), 2)
  213. self.assertEqual(PollVote.objects.count(), 8)
  214. def test_threads_merge_conflict_invalid_resolution(self):
  215. """api errors on invalid merge conflict resolution"""
  216. self.override_acl({
  217. 'can_merge_threads': 1
  218. })
  219. self.override_other_acl({
  220. 'can_merge_threads': 1
  221. })
  222. other_thread = testutils.post_thread(self.category_b)
  223. poll = testutils.post_poll(self.thread, self.user)
  224. other_poll = testutils.post_poll(other_thread, self.user)
  225. response = self.client.post(self.api_link, {
  226. 'thread_url': other_thread.get_absolute_url(),
  227. 'poll': 'jhdkajshdsak'
  228. })
  229. self.assertEqual(response.status_code, 400)
  230. self.assertEqual(response.json(), {
  231. 'detail': "Invalid choice."
  232. })
  233. # polls and votes were untouched
  234. self.assertEqual(Poll.objects.count(), 2)
  235. self.assertEqual(PollVote.objects.count(), 8)
  236. def test_threads_merge_conflict_delete_all(self):
  237. """api deletes all polls when delete all choice is selected"""
  238. self.override_acl({
  239. 'can_merge_threads': 1
  240. })
  241. self.override_other_acl({
  242. 'can_merge_threads': 1
  243. })
  244. other_thread = testutils.post_thread(self.category_b)
  245. poll = testutils.post_poll(self.thread, self.user)
  246. other_poll = testutils.post_poll(other_thread, self.user)
  247. response = self.client.post(self.api_link, {
  248. 'thread_url': other_thread.get_absolute_url(),
  249. 'poll': 0
  250. })
  251. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  252. # other thread has two posts now
  253. self.assertEqual(other_thread.post_set.count(), 3)
  254. # first thread is gone
  255. with self.assertRaises(Thread.DoesNotExist):
  256. Thread.objects.get(pk=self.thread.pk)
  257. # polls and votes are gone
  258. self.assertEqual(Poll.objects.count(), 0)
  259. self.assertEqual(PollVote.objects.count(), 0)
  260. def test_threads_merge_conflict_keep_first_poll(self):
  261. """api deletes other poll on merge"""
  262. self.override_acl({
  263. 'can_merge_threads': 1
  264. })
  265. self.override_other_acl({
  266. 'can_merge_threads': 1
  267. })
  268. other_thread = testutils.post_thread(self.category_b)
  269. poll = testutils.post_poll(self.thread, self.user)
  270. other_poll = testutils.post_poll(other_thread, self.user)
  271. response = self.client.post(self.api_link, {
  272. 'thread_url': other_thread.get_absolute_url(),
  273. 'poll': poll.pk
  274. })
  275. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  276. # other thread has two posts now
  277. self.assertEqual(other_thread.post_set.count(), 3)
  278. # first thread is gone
  279. with self.assertRaises(Thread.DoesNotExist):
  280. Thread.objects.get(pk=self.thread.pk)
  281. # other poll and its votes are gone
  282. self.assertEqual(Poll.objects.filter(thread=self.thread).count(), 0)
  283. self.assertEqual(PollVote.objects.filter(thread=self.thread).count(), 0)
  284. self.assertEqual(Poll.objects.filter(thread=other_thread).count(), 1)
  285. self.assertEqual(PollVote.objects.filter(thread=other_thread).count(), 4)
  286. Poll.objects.get(pk=poll.pk)
  287. with self.assertRaises(Poll.DoesNotExist):
  288. Poll.objects.get(pk=other_poll.pk)
  289. def test_threads_merge_conflict_keep_other_poll(self):
  290. """api deletes first poll on merge"""
  291. self.override_acl({
  292. 'can_merge_threads': 1
  293. })
  294. self.override_other_acl({
  295. 'can_merge_threads': 1
  296. })
  297. other_thread = testutils.post_thread(self.category_b)
  298. poll = testutils.post_poll(self.thread, self.user)
  299. other_poll = testutils.post_poll(other_thread, self.user)
  300. response = self.client.post(self.api_link, {
  301. 'thread_url': other_thread.get_absolute_url(),
  302. 'poll': other_poll.pk
  303. })
  304. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  305. # other thread has two posts now
  306. self.assertEqual(other_thread.post_set.count(), 3)
  307. # first thread is gone
  308. with self.assertRaises(Thread.DoesNotExist):
  309. Thread.objects.get(pk=self.thread.pk)
  310. # other poll and its votes are gone
  311. self.assertEqual(Poll.objects.filter(thread=self.thread).count(), 0)
  312. self.assertEqual(PollVote.objects.filter(thread=self.thread).count(), 0)
  313. self.assertEqual(Poll.objects.filter(thread=other_thread).count(), 1)
  314. self.assertEqual(PollVote.objects.filter(thread=other_thread).count(), 4)
  315. Poll.objects.get(pk=other_poll.pk)
  316. with self.assertRaises(Poll.DoesNotExist):
  317. Poll.objects.get(pk=poll.pk)