test_threads_merge_api.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. import json
  2. from django.core.urlresolvers import reverse
  3. from misago.acl import add_acl
  4. from misago.acl.testutils import override_acl
  5. from misago.categories.models import Category
  6. from misago.threads import testutils
  7. from misago.threads.api.threadendpoints.merge import MERGE_LIMIT
  8. from misago.threads.models import Thread, Post
  9. from misago.threads.serializers import ThreadListSerializer
  10. from misago.threads.tests.test_threads_api import ThreadsApiTestCase
  11. class ThreadsMergeApiTests(ThreadsApiTestCase):
  12. def setUp(self):
  13. super(ThreadsMergeApiTests, self).setUp()
  14. self.api_link = reverse('misago:api:thread-merge')
  15. Category(
  16. name='Category B',
  17. slug='category-b',
  18. ).insert_at(self.category, position='last-child', save=True)
  19. self.category_b = Category.objects.get(slug='category-b')
  20. def test_merge_no_threads(self):
  21. """api validates if we are trying to merge no threads"""
  22. response = self.client.post(
  23. self.api_link, content_type="application/json")
  24. self.assertEqual(response.status_code, 403)
  25. response_json = json.loads(response.content)
  26. self.assertEqual(response_json, {
  27. 'detail': "You have to select at least two threads to merge."
  28. })
  29. def test_merge_empty_threads(self):
  30. """api validates if we are trying to empty threads list"""
  31. response = self.client.post(self.api_link, json.dumps({
  32. 'threads': []
  33. }), content_type="application/json")
  34. self.assertEqual(response.status_code, 403)
  35. response_json = json.loads(response.content)
  36. self.assertEqual(response_json, {
  37. 'detail': "You have to select at least two threads to merge."
  38. })
  39. def test_merge_invalid_threads(self):
  40. """api validates if we are trying to merge invalid thread ids"""
  41. response = self.client.post(self.api_link, json.dumps({
  42. 'threads': 'abcd'
  43. }), content_type="application/json")
  44. self.assertEqual(response.status_code, 403)
  45. response_json = json.loads(response.content)
  46. self.assertEqual(response_json, {
  47. 'detail': "One or more thread ids received were invalid."
  48. })
  49. response = self.client.post(self.api_link, json.dumps({
  50. 'threads': ['a', '-', 'c']
  51. }), content_type="application/json")
  52. self.assertEqual(response.status_code, 403)
  53. response_json = json.loads(response.content)
  54. self.assertEqual(response_json, {
  55. 'detail': "One or more thread ids received were invalid."
  56. })
  57. def test_merge_single_thread(self):
  58. """api validates if we are trying to merge single thread"""
  59. response = self.client.post(self.api_link, json.dumps({
  60. 'threads': [self.thread.id]
  61. }), content_type="application/json")
  62. self.assertEqual(response.status_code, 403)
  63. response_json = json.loads(response.content)
  64. self.assertEqual(response_json, {
  65. 'detail': "You have to select at least two threads to merge."
  66. })
  67. def test_merge_with_nonexisting_thread(self):
  68. """api validates if we are trying to merge with invalid thread"""
  69. unaccesible_thread = testutils.post_thread(category=self.category_b)
  70. response = self.client.post(self.api_link, json.dumps({
  71. 'threads': [self.thread.id, self.thread.id + 1000]
  72. }), content_type="application/json")
  73. self.assertEqual(response.status_code, 403)
  74. response_json = json.loads(response.content)
  75. self.assertEqual(response_json, {
  76. 'detail': "One or more threads to merge could not be found."
  77. })
  78. def test_merge_with_invisible_thread(self):
  79. """api validates if we are trying to merge with inaccesible thread"""
  80. unaccesible_thread = testutils.post_thread(category=self.category_b)
  81. response = self.client.post(self.api_link, json.dumps({
  82. 'threads': [self.thread.id, unaccesible_thread.id]
  83. }), content_type="application/json")
  84. self.assertEqual(response.status_code, 403)
  85. response_json = json.loads(response.content)
  86. self.assertEqual(response_json, {
  87. 'detail': "One or more threads to merge could not be found."
  88. })
  89. def test_merge_no_permission(self):
  90. """api validates permission to merge threads"""
  91. thread = testutils.post_thread(category=self.category)
  92. response = self.client.post(self.api_link, json.dumps({
  93. 'threads': [self.thread.id, thread.id]
  94. }), content_type="application/json")
  95. self.assertEqual(response.status_code, 403)
  96. response_json = json.loads(response.content)
  97. self.assertEqual(response_json, [
  98. {
  99. 'id': thread.pk,
  100. 'title': thread.title,
  101. 'errors': [
  102. "You don't have permission to merge this thread with others."
  103. ]
  104. },
  105. {
  106. 'id': self.thread.pk,
  107. 'title': self.thread.title,
  108. 'errors': [
  109. "You don't have permission to merge this thread with others."
  110. ]
  111. },
  112. ])
  113. def test_merge_no_final_thread(self):
  114. """api rejects merge because no data to merge threads was specified"""
  115. self.override_acl({
  116. 'can_merge_threads': True,
  117. 'can_close_threads': False,
  118. 'can_edit_threads': False,
  119. 'can_reply_threads': False,
  120. })
  121. thread = testutils.post_thread(category=self.category)
  122. response = self.client.post(self.api_link, json.dumps({
  123. 'threads': [self.thread.id, thread.id]
  124. }), content_type="application/json")
  125. self.assertEqual(response.status_code, 400)
  126. response_json = json.loads(response.content)
  127. self.assertEqual(response_json, {
  128. 'title': ['This field is required.'],
  129. 'category': ['This field is required.'],
  130. })
  131. def test_merge_invalid_final_title(self):
  132. """api rejects merge because final thread title was invalid"""
  133. self.override_acl({
  134. 'can_merge_threads': True,
  135. 'can_close_threads': False,
  136. 'can_edit_threads': False,
  137. 'can_reply_threads': False,
  138. })
  139. thread = testutils.post_thread(category=self.category)
  140. response = self.client.post(self.api_link, json.dumps({
  141. 'threads': [self.thread.id, thread.id],
  142. 'title': '$$$',
  143. 'category': self.category.id,
  144. }), content_type="application/json")
  145. self.assertEqual(response.status_code, 400)
  146. response_json = json.loads(response.content)
  147. self.assertEqual(response_json, {
  148. 'title': ["Thread title should be at least 5 characters long."]
  149. })
  150. def test_merge_invalid_category(self):
  151. """api rejects merge because final category was invalid"""
  152. self.override_acl({
  153. 'can_merge_threads': True,
  154. 'can_close_threads': False,
  155. 'can_edit_threads': False,
  156. 'can_reply_threads': False,
  157. })
  158. thread = testutils.post_thread(category=self.category)
  159. response = self.client.post(self.api_link, json.dumps({
  160. 'threads': [self.thread.id, thread.id],
  161. 'title': 'Valid thread title',
  162. 'category': self.category_b.id,
  163. }), content_type="application/json")
  164. self.assertEqual(response.status_code, 400)
  165. response_json = json.loads(response.content)
  166. self.assertEqual(response_json, {
  167. 'category': ["Requested category could not be found."]
  168. })
  169. def test_merge_invalid_weight(self):
  170. """api rejects merge because final weight was invalid"""
  171. self.override_acl({
  172. 'can_merge_threads': True,
  173. 'can_close_threads': False,
  174. 'can_edit_threads': False,
  175. 'can_reply_threads': False,
  176. })
  177. thread = testutils.post_thread(category=self.category)
  178. response = self.client.post(self.api_link, json.dumps({
  179. 'threads': [self.thread.id, thread.id],
  180. 'title': 'Valid thread title',
  181. 'category': self.category.id,
  182. 'weight': 4,
  183. }), content_type="application/json")
  184. self.assertEqual(response.status_code, 400)
  185. response_json = json.loads(response.content)
  186. self.assertEqual(response_json, {
  187. 'weight': ["Ensure this value is less than or equal to 2."]
  188. })
  189. def test_merge_unallowed_global_weight(self):
  190. """api rejects merge because global weight was unallowed"""
  191. self.override_acl({
  192. 'can_merge_threads': True,
  193. 'can_close_threads': False,
  194. 'can_edit_threads': False,
  195. 'can_reply_threads': False,
  196. })
  197. thread = testutils.post_thread(category=self.category)
  198. response = self.client.post(self.api_link, json.dumps({
  199. 'threads': [self.thread.id, thread.id],
  200. 'title': 'Valid thread title',
  201. 'category': self.category.id,
  202. 'weight': 2,
  203. }), content_type="application/json")
  204. self.assertEqual(response.status_code, 400)
  205. response_json = json.loads(response.content)
  206. self.assertEqual(response_json, {
  207. 'weight': [
  208. "You don't have permission to pin threads globally in this category."
  209. ]
  210. })
  211. def test_merge_unallowed_local_weight(self):
  212. """api rejects merge because local weight was unallowed"""
  213. self.override_acl({
  214. 'can_merge_threads': True,
  215. 'can_close_threads': False,
  216. 'can_edit_threads': False,
  217. 'can_reply_threads': False,
  218. })
  219. thread = testutils.post_thread(category=self.category)
  220. response = self.client.post(self.api_link, json.dumps({
  221. 'threads': [self.thread.id, thread.id],
  222. 'title': 'Valid thread title',
  223. 'category': self.category.id,
  224. 'weight': 1,
  225. }), content_type="application/json")
  226. self.assertEqual(response.status_code, 400)
  227. response_json = json.loads(response.content)
  228. self.assertEqual(response_json, {
  229. 'weight': [
  230. "You don't have permission to pin threads in this category."
  231. ]
  232. })
  233. def test_merge_allowed_local_weight(self):
  234. """api allows local weight"""
  235. self.override_acl({
  236. 'can_merge_threads': True,
  237. 'can_close_threads': False,
  238. 'can_edit_threads': False,
  239. 'can_reply_threads': False,
  240. 'can_pin_threads': 1,
  241. })
  242. thread = testutils.post_thread(category=self.category)
  243. response = self.client.post(self.api_link, json.dumps({
  244. 'threads': [self.thread.id, thread.id],
  245. 'title': '$$$',
  246. 'category': self.category.id,
  247. 'weight': 1,
  248. }), content_type="application/json")
  249. self.assertEqual(response.status_code, 400)
  250. response_json = json.loads(response.content)
  251. self.assertEqual(response_json, {
  252. 'title': ["Thread title should be at least 5 characters long."]
  253. })
  254. def test_merge_allowed_global_weight(self):
  255. """api allows local weight"""
  256. self.override_acl({
  257. 'can_merge_threads': True,
  258. 'can_close_threads': False,
  259. 'can_edit_threads': False,
  260. 'can_reply_threads': False,
  261. 'can_pin_threads': 2,
  262. })
  263. thread = testutils.post_thread(category=self.category)
  264. response = self.client.post(self.api_link, json.dumps({
  265. 'threads': [self.thread.id, thread.id],
  266. 'title': '$$$',
  267. 'category': self.category.id,
  268. 'weight': 2,
  269. }), content_type="application/json")
  270. self.assertEqual(response.status_code, 400)
  271. response_json = json.loads(response.content)
  272. self.assertEqual(response_json, {
  273. 'title': ["Thread title should be at least 5 characters long."]
  274. })
  275. def test_merge_unallowed_close(self):
  276. """api rejects merge because closing thread was unallowed"""
  277. self.override_acl({
  278. 'can_merge_threads': True,
  279. 'can_close_threads': False,
  280. 'can_edit_threads': False,
  281. 'can_reply_threads': False,
  282. })
  283. thread = testutils.post_thread(category=self.category)
  284. response = self.client.post(self.api_link, json.dumps({
  285. 'threads': [self.thread.id, thread.id],
  286. 'title': 'Valid thread title',
  287. 'category': self.category.id,
  288. 'is_closed': True,
  289. }), content_type="application/json")
  290. self.assertEqual(response.status_code, 400)
  291. response_json = json.loads(response.content)
  292. self.assertEqual(response_json, {
  293. 'is_closed': [
  294. "You don't have permission to close threads in this category."
  295. ]
  296. })
  297. def test_merge_with_close(self):
  298. """api allows for closing thread"""
  299. self.override_acl({
  300. 'can_merge_threads': True,
  301. 'can_close_threads': False,
  302. 'can_edit_threads': False,
  303. 'can_reply_threads': False,
  304. 'can_close_threads': True,
  305. })
  306. thread = testutils.post_thread(category=self.category)
  307. response = self.client.post(self.api_link, json.dumps({
  308. 'threads': [self.thread.id, thread.id],
  309. 'title': '$$$',
  310. 'category': self.category.id,
  311. 'weight': 0,
  312. 'is_closed': True,
  313. }), content_type="application/json")
  314. self.assertEqual(response.status_code, 400)
  315. response_json = json.loads(response.content)
  316. self.assertEqual(response_json, {
  317. 'title': ["Thread title should be at least 5 characters long."]
  318. })
  319. def test_merge(self):
  320. """api performs basic merge"""
  321. posts_ids = [p.id for p in Post.objects.all()]
  322. self.override_acl({
  323. 'can_merge_threads': True,
  324. 'can_close_threads': False,
  325. 'can_edit_threads': False,
  326. 'can_reply_threads': False,
  327. })
  328. thread = testutils.post_thread(category=self.category)
  329. response = self.client.post(self.api_link, json.dumps({
  330. 'threads': [self.thread.id, thread.id],
  331. 'title': 'Merged thread!',
  332. 'category': self.category.id,
  333. }), content_type="application/json")
  334. self.assertEqual(response.status_code, 200)
  335. # is response json with new thread?
  336. response_json = json.loads(response.content)
  337. new_thread = Thread.objects.get(pk=response_json['id'])
  338. new_thread.is_read = False
  339. new_thread.subscription = None
  340. new_thread.top_category = None
  341. add_acl(self.user, new_thread.category)
  342. add_acl(self.user, new_thread)
  343. self.assertEqual(response_json, ThreadListSerializer(new_thread).data)
  344. # did posts move to new thread?
  345. for post in Post.objects.filter(id__in=posts_ids):
  346. self.assertEqual(post.thread_id, new_thread.id)
  347. # are old threads gone?
  348. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])
  349. def test_merge_with_top_category(self):
  350. """api performs merge with top category"""
  351. posts_ids = [p.id for p in Post.objects.all()]
  352. self.override_acl({
  353. 'can_merge_threads': True,
  354. 'can_close_threads': False,
  355. 'can_edit_threads': False,
  356. 'can_reply_threads': False,
  357. })
  358. thread = testutils.post_thread(category=self.category)
  359. response = self.client.post(self.api_link, json.dumps({
  360. 'top_category': self.root.id,
  361. 'threads': [self.thread.id, thread.id],
  362. 'title': 'Merged thread!',
  363. 'category': self.category.id,
  364. }), content_type="application/json")
  365. self.assertEqual(response.status_code, 200)
  366. # is response json with new thread?
  367. response_json = json.loads(response.content)
  368. new_thread = Thread.objects.get(pk=response_json['id'])
  369. new_thread.is_read = False
  370. new_thread.subscription = None
  371. new_thread.top_category = self.category
  372. add_acl(self.user, new_thread.category)
  373. add_acl(self.user, new_thread)
  374. self.assertEqual(response_json, ThreadListSerializer(new_thread).data)
  375. # did posts move to new thread?
  376. for post in Post.objects.filter(id__in=posts_ids):
  377. self.assertEqual(post.thread_id, new_thread.id)
  378. # are old threads gone?
  379. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])