test_threads_merge_api.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 .. import testutils
  7. from ..api.threadendpoints.merge import MERGE_LIMIT
  8. from ..models import Post, Thread
  9. from ..serializers import ThreadsListSerializer
  10. from .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(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_too_many_threads(self):
  113. """api rejects too many threads to merge"""
  114. threads = []
  115. for i in xrange(MERGE_LIMIT + 1):
  116. threads.append(testutils.post_thread(category=self.category).pk)
  117. self.override_acl({
  118. 'can_merge_threads': True,
  119. 'can_close_threads': False,
  120. 'can_edit_threads': False,
  121. 'can_reply_threads': False,
  122. })
  123. response = self.client.post(self.api_link, json.dumps({
  124. 'threads': threads
  125. }), content_type="application/json")
  126. self.assertEqual(response.status_code, 403)
  127. response_json = json.loads(response.content)
  128. self.assertEqual(response_json, {
  129. 'detail': "No more than %s threads can be merged at single time." % MERGE_LIMIT
  130. })
  131. def test_merge_no_final_thread(self):
  132. """api rejects merge because no data to merge threads was specified"""
  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. }), content_type="application/json")
  143. self.assertEqual(response.status_code, 400)
  144. response_json = json.loads(response.content)
  145. self.assertEqual(response_json, {
  146. 'title': ['This field is required.'],
  147. 'category': ['This field is required.'],
  148. })
  149. def test_merge_invalid_final_title(self):
  150. """api rejects merge because final thread title 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': '$$$',
  161. 'category': self.category.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. 'title': ["Thread title should be at least 5 characters long."]
  167. })
  168. def test_merge_invalid_category(self):
  169. """api rejects merge because final category 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_b.id,
  181. }), content_type="application/json")
  182. self.assertEqual(response.status_code, 400)
  183. response_json = json.loads(response.content)
  184. self.assertEqual(response_json, {
  185. 'category': ["Requested category could not be found."]
  186. })
  187. def test_merge_invalid_weight(self):
  188. """api rejects merge because final weight was invalid"""
  189. self.override_acl({
  190. 'can_merge_threads': True,
  191. 'can_close_threads': False,
  192. 'can_edit_threads': False,
  193. 'can_reply_threads': False,
  194. })
  195. thread = testutils.post_thread(category=self.category)
  196. response = self.client.post(self.api_link, json.dumps({
  197. 'threads': [self.thread.id, thread.id],
  198. 'title': 'Valid thread title',
  199. 'category': self.category.id,
  200. 'weight': 4,
  201. }), content_type="application/json")
  202. self.assertEqual(response.status_code, 400)
  203. response_json = json.loads(response.content)
  204. self.assertEqual(response_json, {
  205. 'weight': ["Ensure this value is less than or equal to 2."]
  206. })
  207. def test_merge_unallowed_global_weight(self):
  208. """api rejects merge because global weight was unallowed"""
  209. self.override_acl({
  210. 'can_merge_threads': True,
  211. 'can_close_threads': False,
  212. 'can_edit_threads': False,
  213. 'can_reply_threads': False,
  214. })
  215. thread = testutils.post_thread(category=self.category)
  216. response = self.client.post(self.api_link, json.dumps({
  217. 'threads': [self.thread.id, thread.id],
  218. 'title': 'Valid thread title',
  219. 'category': self.category.id,
  220. 'weight': 2,
  221. }), content_type="application/json")
  222. self.assertEqual(response.status_code, 400)
  223. response_json = json.loads(response.content)
  224. self.assertEqual(response_json, {
  225. 'weight': [
  226. "You don't have permission to pin threads globally in this category."
  227. ]
  228. })
  229. def test_merge_unallowed_local_weight(self):
  230. """api rejects merge because local weight was unallowed"""
  231. self.override_acl({
  232. 'can_merge_threads': True,
  233. 'can_close_threads': False,
  234. 'can_edit_threads': False,
  235. 'can_reply_threads': False,
  236. })
  237. thread = testutils.post_thread(category=self.category)
  238. response = self.client.post(self.api_link, json.dumps({
  239. 'threads': [self.thread.id, thread.id],
  240. 'title': 'Valid thread title',
  241. 'category': self.category.id,
  242. 'weight': 1,
  243. }), content_type="application/json")
  244. self.assertEqual(response.status_code, 400)
  245. response_json = json.loads(response.content)
  246. self.assertEqual(response_json, {
  247. 'weight': [
  248. "You don't have permission to pin threads in this category."
  249. ]
  250. })
  251. def test_merge_allowed_local_weight(self):
  252. """api allows local weight"""
  253. self.override_acl({
  254. 'can_merge_threads': True,
  255. 'can_close_threads': False,
  256. 'can_edit_threads': False,
  257. 'can_reply_threads': False,
  258. 'can_pin_threads': 1,
  259. })
  260. thread = testutils.post_thread(category=self.category)
  261. response = self.client.post(self.api_link, json.dumps({
  262. 'threads': [self.thread.id, thread.id],
  263. 'title': '$$$',
  264. 'category': self.category.id,
  265. 'weight': 1,
  266. }), content_type="application/json")
  267. self.assertEqual(response.status_code, 400)
  268. response_json = json.loads(response.content)
  269. self.assertEqual(response_json, {
  270. 'title': ["Thread title should be at least 5 characters long."]
  271. })
  272. def test_merge_allowed_global_weight(self):
  273. """api allows local weight"""
  274. self.override_acl({
  275. 'can_merge_threads': True,
  276. 'can_close_threads': False,
  277. 'can_edit_threads': False,
  278. 'can_reply_threads': False,
  279. 'can_pin_threads': 2,
  280. })
  281. thread = testutils.post_thread(category=self.category)
  282. response = self.client.post(self.api_link, json.dumps({
  283. 'threads': [self.thread.id, thread.id],
  284. 'title': '$$$',
  285. 'category': self.category.id,
  286. 'weight': 2,
  287. }), content_type="application/json")
  288. self.assertEqual(response.status_code, 400)
  289. response_json = json.loads(response.content)
  290. self.assertEqual(response_json, {
  291. 'title': ["Thread title should be at least 5 characters long."]
  292. })
  293. def test_merge_unallowed_close(self):
  294. """api rejects merge because closing thread was unallowed"""
  295. self.override_acl({
  296. 'can_merge_threads': True,
  297. 'can_close_threads': False,
  298. 'can_edit_threads': False,
  299. 'can_reply_threads': False,
  300. })
  301. thread = testutils.post_thread(category=self.category)
  302. response = self.client.post(self.api_link, json.dumps({
  303. 'threads': [self.thread.id, thread.id],
  304. 'title': 'Valid thread title',
  305. 'category': self.category.id,
  306. 'is_closed': True,
  307. }), content_type="application/json")
  308. self.assertEqual(response.status_code, 400)
  309. response_json = json.loads(response.content)
  310. self.assertEqual(response_json, {
  311. 'is_closed': [
  312. "You don't have permission to close threads in this category."
  313. ]
  314. })
  315. def test_merge_with_close(self):
  316. """api allows for closing thread"""
  317. self.override_acl({
  318. 'can_merge_threads': True,
  319. 'can_close_threads': False,
  320. 'can_edit_threads': False,
  321. 'can_reply_threads': False,
  322. 'can_close_threads': True,
  323. })
  324. thread = testutils.post_thread(category=self.category)
  325. response = self.client.post(self.api_link, json.dumps({
  326. 'threads': [self.thread.id, thread.id],
  327. 'title': '$$$',
  328. 'category': self.category.id,
  329. 'weight': 0,
  330. 'is_closed': True,
  331. }), content_type="application/json")
  332. self.assertEqual(response.status_code, 400)
  333. response_json = json.loads(response.content)
  334. self.assertEqual(response_json, {
  335. 'title': ["Thread title should be at least 5 characters long."]
  336. })
  337. def test_merge(self):
  338. """api performs basic merge"""
  339. posts_ids = [p.id for p in Post.objects.all()]
  340. self.override_acl({
  341. 'can_merge_threads': True,
  342. 'can_close_threads': False,
  343. 'can_edit_threads': False,
  344. 'can_reply_threads': False,
  345. })
  346. thread = testutils.post_thread(category=self.category)
  347. response = self.client.post(self.api_link, json.dumps({
  348. 'threads': [self.thread.id, thread.id],
  349. 'title': 'Merged thread!',
  350. 'category': self.category.id,
  351. }), content_type="application/json")
  352. self.assertEqual(response.status_code, 200)
  353. # is response json with new thread?
  354. response_json = json.loads(response.content)
  355. new_thread = Thread.objects.get(pk=response_json['id'])
  356. new_thread.is_read = False
  357. new_thread.subscription = None
  358. new_thread.top_category = None
  359. add_acl(self.user, new_thread.category)
  360. add_acl(self.user, new_thread)
  361. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  362. # did posts move to new thread?
  363. for post in Post.objects.filter(id__in=posts_ids):
  364. self.assertEqual(post.thread_id, new_thread.id)
  365. # are old threads gone?
  366. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])
  367. def test_merge_with_top_category(self):
  368. """api performs merge with top category"""
  369. posts_ids = [p.id for p in Post.objects.all()]
  370. self.override_acl({
  371. 'can_merge_threads': True,
  372. 'can_close_threads': False,
  373. 'can_edit_threads': False,
  374. 'can_reply_threads': False,
  375. })
  376. thread = testutils.post_thread(category=self.category)
  377. response = self.client.post(self.api_link, json.dumps({
  378. 'top_category': self.root.id,
  379. 'threads': [self.thread.id, thread.id],
  380. 'title': 'Merged thread!',
  381. 'category': self.category.id,
  382. }), content_type="application/json")
  383. self.assertEqual(response.status_code, 200)
  384. # is response json with new thread?
  385. response_json = json.loads(response.content)
  386. new_thread = Thread.objects.get(pk=response_json['id'])
  387. new_thread.is_read = False
  388. new_thread.subscription = None
  389. new_thread.top_category = self.category
  390. add_acl(self.user, new_thread.category)
  391. add_acl(self.user, new_thread)
  392. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  393. # did posts move to new thread?
  394. for post in Post.objects.filter(id__in=posts_ids):
  395. self.assertEqual(post.thread_id, new_thread.id)
  396. # are old threads gone?
  397. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])