test_threads_merge_api.py 16 KB

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