test_threads_merge_api.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. import json
  2. from django.core.urlresolvers import reverse
  3. from django.utils.encoding import smart_str
  4. from django.utils.six.moves import range
  5. from misago.acl import add_acl
  6. from misago.acl.testutils import override_acl
  7. from misago.categories.models import Category
  8. from .. import testutils
  9. from ..api.threadendpoints.merge import MERGE_LIMIT
  10. from ..models import Post, Thread
  11. from ..serializers import ThreadsListSerializer
  12. from .test_threads_api import ThreadsApiTestCase
  13. class ThreadsMergeApiTests(ThreadsApiTestCase):
  14. def setUp(self):
  15. super(ThreadsMergeApiTests, self).setUp()
  16. self.api_link = reverse('misago:api:thread-merge')
  17. Category(
  18. name='Category B',
  19. slug='category-b',
  20. ).insert_at(self.category, position='last-child', save=True)
  21. self.category_b = Category.objects.get(slug='category-b')
  22. def test_merge_no_threads(self):
  23. """api validates if we are trying to merge no threads"""
  24. response = self.client.post(self.api_link, content_type="application/json")
  25. self.assertEqual(response.status_code, 403)
  26. response_json = json.loads(smart_str(response.content))
  27. self.assertEqual(response_json, {
  28. 'detail': "You have to select at least two threads to merge."
  29. })
  30. def test_merge_empty_threads(self):
  31. """api validates if we are trying to empty threads list"""
  32. response = self.client.post(self.api_link, json.dumps({
  33. 'threads': []
  34. }), content_type="application/json")
  35. self.assertEqual(response.status_code, 403)
  36. response_json = json.loads(smart_str(response.content))
  37. self.assertEqual(response_json, {
  38. 'detail': "You have to select at least two threads to merge."
  39. })
  40. def test_merge_invalid_threads(self):
  41. """api validates if we are trying to merge invalid thread ids"""
  42. response = self.client.post(self.api_link, json.dumps({
  43. 'threads': 'abcd'
  44. }), content_type="application/json")
  45. self.assertEqual(response.status_code, 403)
  46. response_json = json.loads(smart_str(response.content))
  47. self.assertEqual(response_json, {
  48. 'detail': "One or more thread ids received were invalid."
  49. })
  50. response = self.client.post(self.api_link, json.dumps({
  51. 'threads': ['a', '-', 'c']
  52. }), content_type="application/json")
  53. self.assertEqual(response.status_code, 403)
  54. response_json = json.loads(smart_str(response.content))
  55. self.assertEqual(response_json, {
  56. 'detail': "One or more thread ids received were invalid."
  57. })
  58. def test_merge_single_thread(self):
  59. """api validates if we are trying to merge single thread"""
  60. response = self.client.post(self.api_link, json.dumps({
  61. 'threads': [self.thread.id]
  62. }), content_type="application/json")
  63. self.assertEqual(response.status_code, 403)
  64. response_json = json.loads(smart_str(response.content))
  65. self.assertEqual(response_json, {
  66. 'detail': "You have to select at least two threads to merge."
  67. })
  68. def test_merge_with_nonexisting_thread(self):
  69. """api validates if we are trying to merge with invalid thread"""
  70. unaccesible_thread = testutils.post_thread(category=self.category_b)
  71. response = self.client.post(self.api_link, json.dumps({
  72. 'threads': [self.thread.id, self.thread.id + 1000]
  73. }), content_type="application/json")
  74. self.assertEqual(response.status_code, 403)
  75. response_json = json.loads(smart_str(response.content))
  76. self.assertEqual(response_json, {
  77. 'detail': "One or more threads to merge could not be found."
  78. })
  79. def test_merge_with_invisible_thread(self):
  80. """api validates if we are trying to merge with inaccesible thread"""
  81. unaccesible_thread = testutils.post_thread(category=self.category_b)
  82. response = self.client.post(self.api_link, json.dumps({
  83. 'threads': [self.thread.id, unaccesible_thread.id]
  84. }), content_type="application/json")
  85. self.assertEqual(response.status_code, 403)
  86. response_json = json.loads(smart_str(response.content))
  87. self.assertEqual(response_json, {
  88. 'detail': "One or more threads to merge could not be found."
  89. })
  90. def test_merge_no_permission(self):
  91. """api validates permission to merge threads"""
  92. thread = testutils.post_thread(category=self.category)
  93. response = self.client.post(self.api_link, json.dumps({
  94. 'threads': [self.thread.id, thread.id]
  95. }), content_type="application/json")
  96. self.assertEqual(response.status_code, 403)
  97. response_json = json.loads(smart_str(response.content))
  98. self.assertEqual(response_json, [
  99. {
  100. 'id': thread.pk,
  101. 'title': thread.title,
  102. 'errors': [
  103. "You don't have permission to merge this thread with others."
  104. ]
  105. },
  106. {
  107. 'id': self.thread.pk,
  108. 'title': self.thread.title,
  109. 'errors': [
  110. "You don't have permission to merge this thread with others."
  111. ]
  112. },
  113. ])
  114. def test_merge_too_many_threads(self):
  115. """api rejects too many threads to merge"""
  116. threads = []
  117. for i in range(MERGE_LIMIT + 1):
  118. threads.append(testutils.post_thread(category=self.category).pk)
  119. self.override_acl({
  120. 'can_merge_threads': True,
  121. 'can_close_threads': False,
  122. 'can_edit_threads': False,
  123. 'can_reply_threads': False,
  124. })
  125. response = self.client.post(self.api_link, json.dumps({
  126. 'threads': threads
  127. }), content_type="application/json")
  128. self.assertEqual(response.status_code, 403)
  129. response_json = json.loads(smart_str(response.content))
  130. self.assertEqual(response_json, {
  131. 'detail': "No more than %s threads can be merged at single time." % MERGE_LIMIT
  132. })
  133. def test_merge_no_final_thread(self):
  134. """api rejects merge because no data to merge threads was specified"""
  135. self.override_acl({
  136. 'can_merge_threads': True,
  137. 'can_close_threads': False,
  138. 'can_edit_threads': False,
  139. 'can_reply_threads': False,
  140. })
  141. thread = testutils.post_thread(category=self.category)
  142. response = self.client.post(self.api_link, json.dumps({
  143. 'threads': [self.thread.id, thread.id]
  144. }), content_type="application/json")
  145. self.assertEqual(response.status_code, 400)
  146. response_json = json.loads(smart_str(response.content))
  147. self.assertEqual(response_json, {
  148. 'title': ['This field is required.'],
  149. 'category': ['This field is required.'],
  150. })
  151. def test_merge_invalid_final_title(self):
  152. """api rejects merge because final thread title was invalid"""
  153. self.override_acl({
  154. 'can_merge_threads': True,
  155. 'can_close_threads': False,
  156. 'can_edit_threads': False,
  157. 'can_reply_threads': False,
  158. })
  159. thread = testutils.post_thread(category=self.category)
  160. response = self.client.post(self.api_link, json.dumps({
  161. 'threads': [self.thread.id, thread.id],
  162. 'title': '$$$',
  163. 'category': self.category.id,
  164. }), content_type="application/json")
  165. self.assertEqual(response.status_code, 400)
  166. response_json = json.loads(smart_str(response.content))
  167. self.assertEqual(response_json, {
  168. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  169. })
  170. def test_merge_invalid_category(self):
  171. """api rejects merge because final category was invalid"""
  172. self.override_acl({
  173. 'can_merge_threads': True,
  174. 'can_close_threads': False,
  175. 'can_edit_threads': False,
  176. 'can_reply_threads': False,
  177. })
  178. thread = testutils.post_thread(category=self.category)
  179. response = self.client.post(self.api_link, json.dumps({
  180. 'threads': [self.thread.id, thread.id],
  181. 'title': 'Valid thread title',
  182. 'category': self.category_b.id,
  183. }), content_type="application/json")
  184. self.assertEqual(response.status_code, 400)
  185. response_json = json.loads(smart_str(response.content))
  186. self.assertEqual(response_json, {
  187. 'category': ["Requested category could not be found."]
  188. })
  189. def test_merge_unallowed_start_thread(self):
  190. """api rejects merge because category isn't allowing starting threads"""
  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. 'can_start_threads': 0
  197. })
  198. thread = testutils.post_thread(category=self.category)
  199. response = self.client.post(self.api_link, json.dumps({
  200. 'threads': [self.thread.id, thread.id],
  201. 'title': 'Valid thread title',
  202. 'category': self.category.id
  203. }), content_type="application/json")
  204. self.assertEqual(response.status_code, 400)
  205. response_json = json.loads(smart_str(response.content))
  206. self.assertEqual(response_json, {
  207. 'category': [
  208. "You can't create new threads in selected category."
  209. ]
  210. })
  211. def test_merge_invalid_weight(self):
  212. """api rejects merge because final weight was invalid"""
  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': 4,
  225. }), content_type="application/json")
  226. self.assertEqual(response.status_code, 400)
  227. response_json = json.loads(smart_str(response.content))
  228. self.assertEqual(response_json, {
  229. 'weight': ["Ensure this value is less than or equal to 2."]
  230. })
  231. def test_merge_unallowed_global_weight(self):
  232. """api rejects merge because global weight was unallowed"""
  233. self.override_acl({
  234. 'can_merge_threads': True,
  235. 'can_close_threads': False,
  236. 'can_edit_threads': False,
  237. 'can_reply_threads': False,
  238. })
  239. thread = testutils.post_thread(category=self.category)
  240. response = self.client.post(self.api_link, json.dumps({
  241. 'threads': [self.thread.id, thread.id],
  242. 'title': 'Valid thread title',
  243. 'category': self.category.id,
  244. 'weight': 2,
  245. }), content_type="application/json")
  246. self.assertEqual(response.status_code, 400)
  247. response_json = json.loads(smart_str(response.content))
  248. self.assertEqual(response_json, {
  249. 'weight': [
  250. "You don't have permission to pin threads globally in this category."
  251. ]
  252. })
  253. def test_merge_unallowed_local_weight(self):
  254. """api rejects merge because local weight was unallowed"""
  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. })
  261. thread = testutils.post_thread(category=self.category)
  262. response = self.client.post(self.api_link, json.dumps({
  263. 'threads': [self.thread.id, thread.id],
  264. 'title': 'Valid thread title',
  265. 'category': self.category.id,
  266. 'weight': 1,
  267. }), content_type="application/json")
  268. self.assertEqual(response.status_code, 400)
  269. response_json = json.loads(smart_str(response.content))
  270. self.assertEqual(response_json, {
  271. 'weight': [
  272. "You don't have permission to pin threads in this category."
  273. ]
  274. })
  275. def test_merge_allowed_local_weight(self):
  276. """api allows local weight"""
  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. 'can_pin_threads': 1,
  283. })
  284. thread = testutils.post_thread(category=self.category)
  285. response = self.client.post(self.api_link, json.dumps({
  286. 'threads': [self.thread.id, thread.id],
  287. 'title': '$$$',
  288. 'category': self.category.id,
  289. 'weight': 1,
  290. }), content_type="application/json")
  291. self.assertEqual(response.status_code, 400)
  292. response_json = json.loads(smart_str(response.content))
  293. self.assertEqual(response_json, {
  294. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  295. })
  296. def test_merge_allowed_global_weight(self):
  297. """api allows global weight"""
  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_pin_threads': 2,
  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': 2,
  311. }), content_type="application/json")
  312. self.assertEqual(response.status_code, 400)
  313. response_json = json.loads(smart_str(response.content))
  314. self.assertEqual(response_json, {
  315. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  316. })
  317. def test_merge_unallowed_close(self):
  318. """api rejects merge because closing thread was unallowed"""
  319. self.override_acl({
  320. 'can_merge_threads': True,
  321. 'can_close_threads': False,
  322. 'can_edit_threads': False,
  323. 'can_reply_threads': False,
  324. })
  325. thread = testutils.post_thread(category=self.category)
  326. response = self.client.post(self.api_link, json.dumps({
  327. 'threads': [self.thread.id, thread.id],
  328. 'title': 'Valid thread title',
  329. 'category': self.category.id,
  330. 'is_closed': True,
  331. }), content_type="application/json")
  332. self.assertEqual(response.status_code, 400)
  333. response_json = json.loads(smart_str(response.content))
  334. self.assertEqual(response_json, {
  335. 'is_closed': [
  336. "You don't have permission to close threads in this category."
  337. ]
  338. })
  339. def test_merge_with_close(self):
  340. """api allows for closing thread"""
  341. self.override_acl({
  342. 'can_merge_threads': True,
  343. 'can_edit_threads': False,
  344. 'can_reply_threads': False,
  345. 'can_close_threads': True,
  346. })
  347. thread = testutils.post_thread(category=self.category)
  348. response = self.client.post(self.api_link, json.dumps({
  349. 'threads': [self.thread.id, thread.id],
  350. 'title': '$$$',
  351. 'category': self.category.id,
  352. 'weight': 0,
  353. 'is_closed': True,
  354. }), content_type="application/json")
  355. self.assertEqual(response.status_code, 400)
  356. response_json = json.loads(smart_str(response.content))
  357. self.assertEqual(response_json, {
  358. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  359. })
  360. def test_merge_unallowed_hidden(self):
  361. """api rejects merge because hidden thread was unallowed"""
  362. self.override_acl({
  363. 'can_merge_threads': True,
  364. 'can_close_threads': False,
  365. 'can_edit_threads': False,
  366. 'can_reply_threads': False,
  367. 'can_hide_threads': 0,
  368. })
  369. thread = testutils.post_thread(category=self.category)
  370. response = self.client.post(self.api_link, json.dumps({
  371. 'threads': [self.thread.id, thread.id],
  372. 'title': 'Valid thread title',
  373. 'category': self.category.id,
  374. 'is_hidden': True,
  375. }), content_type="application/json")
  376. self.assertEqual(response.status_code, 400)
  377. response_json = json.loads(smart_str(response.content))
  378. self.assertEqual(response_json, {
  379. 'is_hidden': [
  380. "You don't have permission to hide threads in this category."
  381. ]
  382. })
  383. def test_merge_with_hide(self):
  384. """api allows for hiding thread"""
  385. self.override_acl({
  386. 'can_merge_threads': True,
  387. 'can_close_threads': False,
  388. 'can_edit_threads': False,
  389. 'can_reply_threads': False,
  390. 'can_hide_threads': 1,
  391. })
  392. thread = testutils.post_thread(category=self.category)
  393. response = self.client.post(self.api_link, json.dumps({
  394. 'threads': [self.thread.id, thread.id],
  395. 'title': '$$$',
  396. 'category': self.category.id,
  397. 'weight': 0,
  398. 'is_hidden': True,
  399. }), content_type="application/json")
  400. self.assertEqual(response.status_code, 400)
  401. response_json = json.loads(smart_str(response.content))
  402. self.assertEqual(response_json, {
  403. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  404. })
  405. def test_merge(self):
  406. """api performs basic merge"""
  407. posts_ids = [p.id for p in Post.objects.all()]
  408. self.override_acl({
  409. 'can_merge_threads': True,
  410. 'can_close_threads': False,
  411. 'can_edit_threads': False,
  412. 'can_reply_threads': False,
  413. })
  414. thread = testutils.post_thread(category=self.category)
  415. response = self.client.post(self.api_link, json.dumps({
  416. 'threads': [self.thread.id, thread.id],
  417. 'title': 'Merged thread!',
  418. 'category': self.category.id,
  419. }), content_type="application/json")
  420. self.assertEqual(response.status_code, 200)
  421. # is response json with new thread?
  422. response_json = json.loads(smart_str(response.content))
  423. new_thread = Thread.objects.get(pk=response_json['id'])
  424. new_thread.is_read = False
  425. new_thread.subscription = None
  426. new_thread.top_category = None
  427. add_acl(self.user, new_thread.category)
  428. add_acl(self.user, new_thread)
  429. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  430. # did posts move to new thread?
  431. for post in Post.objects.filter(id__in=posts_ids):
  432. self.assertEqual(post.thread_id, new_thread.id)
  433. # are old threads gone?
  434. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])
  435. def test_merge_kitchensink(self):
  436. """api performs merge"""
  437. posts_ids = [p.id for p in Post.objects.all()]
  438. self.override_acl({
  439. 'can_merge_threads': True,
  440. 'can_close_threads': True,
  441. 'can_hide_threads': 1,
  442. 'can_pin_threads': 2
  443. })
  444. thread = testutils.post_thread(category=self.category)
  445. response = self.client.post(self.api_link, json.dumps({
  446. 'threads': [self.thread.id, thread.id],
  447. 'title': 'Merged thread!',
  448. 'category': self.category.id,
  449. 'is_closed': 1,
  450. 'is_hidden': 1,
  451. 'weight': 2
  452. }), content_type="application/json")
  453. self.assertEqual(response.status_code, 200)
  454. # is response json with new thread?
  455. response_json = json.loads(smart_str(response.content))
  456. new_thread = Thread.objects.get(pk=response_json['id'])
  457. new_thread.is_read = False
  458. new_thread.subscription = None
  459. new_thread.top_category = None
  460. self.assertEqual(new_thread.weight, 2)
  461. self.assertTrue(new_thread.is_closed)
  462. self.assertTrue(new_thread.is_hidden)
  463. add_acl(self.user, new_thread.category)
  464. add_acl(self.user, new_thread)
  465. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  466. # did posts move to new thread?
  467. for post in Post.objects.filter(id__in=posts_ids):
  468. self.assertEqual(post.thread_id, new_thread.id)
  469. # are old threads gone?
  470. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])
  471. def test_merge_with_top_category(self):
  472. """api performs merge with top category"""
  473. posts_ids = [p.id for p in Post.objects.all()]
  474. self.override_acl({
  475. 'can_merge_threads': True,
  476. 'can_close_threads': False,
  477. 'can_edit_threads': False,
  478. 'can_reply_threads': False,
  479. })
  480. thread = testutils.post_thread(category=self.category)
  481. response = self.client.post(self.api_link, json.dumps({
  482. 'top_category': self.root.id,
  483. 'threads': [self.thread.id, thread.id],
  484. 'title': 'Merged thread!',
  485. 'category': self.category.id,
  486. }), content_type="application/json")
  487. self.assertEqual(response.status_code, 200)
  488. # is response json with new thread?
  489. response_json = json.loads(smart_str(response.content))
  490. new_thread = Thread.objects.get(pk=response_json['id'])
  491. new_thread.is_read = False
  492. new_thread.subscription = None
  493. new_thread.top_category = self.category
  494. add_acl(self.user, new_thread.category)
  495. add_acl(self.user, new_thread)
  496. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  497. # did posts move to new thread?
  498. for post in Post.objects.filter(id__in=posts_ids):
  499. self.assertEqual(post.thread_id, new_thread.id)
  500. # are old threads gone?
  501. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])