test_threads_merge_api.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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."]
  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_invalid_weight(self):
  190. """api rejects merge because final weight was invalid"""
  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': 4,
  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. 'weight': ["Ensure this value is less than or equal to 2."]
  208. })
  209. def test_merge_unallowed_global_weight(self):
  210. """api rejects merge because global weight was unallowed"""
  211. self.override_acl({
  212. 'can_merge_threads': True,
  213. 'can_close_threads': False,
  214. 'can_edit_threads': False,
  215. 'can_reply_threads': False,
  216. })
  217. thread = testutils.post_thread(category=self.category)
  218. response = self.client.post(self.api_link, json.dumps({
  219. 'threads': [self.thread.id, thread.id],
  220. 'title': 'Valid thread title',
  221. 'category': self.category.id,
  222. 'weight': 2,
  223. }), content_type="application/json")
  224. self.assertEqual(response.status_code, 400)
  225. response_json = json.loads(smart_str(response.content))
  226. self.assertEqual(response_json, {
  227. 'weight': [
  228. "You don't have permission to pin threads globally in this category."
  229. ]
  230. })
  231. def test_merge_unallowed_local_weight(self):
  232. """api rejects merge because local 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': 1,
  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 in this category."
  251. ]
  252. })
  253. def test_merge_allowed_local_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': 1,
  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': 1,
  268. }), content_type="application/json")
  269. self.assertEqual(response.status_code, 400)
  270. response_json = json.loads(smart_str(response.content))
  271. self.assertEqual(response_json, {
  272. 'title': ["Thread title should be at least 5 characters long."]
  273. })
  274. def test_merge_allowed_global_weight(self):
  275. """api allows local weight"""
  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. 'can_pin_threads': 2,
  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': '$$$',
  287. 'category': self.category.id,
  288. 'weight': 2,
  289. }), content_type="application/json")
  290. self.assertEqual(response.status_code, 400)
  291. response_json = json.loads(smart_str(response.content))
  292. self.assertEqual(response_json, {
  293. 'title': ["Thread title should be at least 5 characters long."]
  294. })
  295. def test_merge_unallowed_close(self):
  296. """api rejects merge because closing thread was unallowed"""
  297. self.override_acl({
  298. 'can_merge_threads': True,
  299. 'can_close_threads': False,
  300. 'can_edit_threads': False,
  301. 'can_reply_threads': False,
  302. })
  303. thread = testutils.post_thread(category=self.category)
  304. response = self.client.post(self.api_link, json.dumps({
  305. 'threads': [self.thread.id, thread.id],
  306. 'title': 'Valid thread title',
  307. 'category': self.category.id,
  308. 'is_closed': True,
  309. }), content_type="application/json")
  310. self.assertEqual(response.status_code, 400)
  311. response_json = json.loads(smart_str(response.content))
  312. self.assertEqual(response_json, {
  313. 'is_closed': [
  314. "You don't have permission to close threads in this category."
  315. ]
  316. })
  317. def test_merge_with_close(self):
  318. """api allows for closing thread"""
  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. 'can_close_threads': True,
  325. })
  326. thread = testutils.post_thread(category=self.category)
  327. response = self.client.post(self.api_link, json.dumps({
  328. 'threads': [self.thread.id, thread.id],
  329. 'title': '$$$',
  330. 'category': self.category.id,
  331. 'weight': 0,
  332. 'is_closed': True,
  333. }), content_type="application/json")
  334. self.assertEqual(response.status_code, 400)
  335. response_json = json.loads(smart_str(response.content))
  336. self.assertEqual(response_json, {
  337. 'title': ["Thread title should be at least 5 characters long."]
  338. })
  339. def test_merge(self):
  340. """api performs basic merge"""
  341. posts_ids = [p.id for p in Post.objects.all()]
  342. self.override_acl({
  343. 'can_merge_threads': True,
  344. 'can_close_threads': False,
  345. 'can_edit_threads': False,
  346. 'can_reply_threads': False,
  347. })
  348. thread = testutils.post_thread(category=self.category)
  349. response = self.client.post(self.api_link, json.dumps({
  350. 'threads': [self.thread.id, thread.id],
  351. 'title': 'Merged thread!',
  352. 'category': self.category.id,
  353. }), content_type="application/json")
  354. self.assertEqual(response.status_code, 200)
  355. # is response json with new thread?
  356. response_json = json.loads(smart_str(response.content))
  357. new_thread = Thread.objects.get(pk=response_json['id'])
  358. new_thread.is_read = False
  359. new_thread.subscription = None
  360. new_thread.top_category = None
  361. add_acl(self.user, new_thread.category)
  362. add_acl(self.user, new_thread)
  363. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  364. # did posts move to new thread?
  365. for post in Post.objects.filter(id__in=posts_ids):
  366. self.assertEqual(post.thread_id, new_thread.id)
  367. # are old threads gone?
  368. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])
  369. def test_merge_with_top_category(self):
  370. """api performs merge with top category"""
  371. posts_ids = [p.id for p in Post.objects.all()]
  372. self.override_acl({
  373. 'can_merge_threads': True,
  374. 'can_close_threads': False,
  375. 'can_edit_threads': False,
  376. 'can_reply_threads': False,
  377. })
  378. thread = testutils.post_thread(category=self.category)
  379. response = self.client.post(self.api_link, json.dumps({
  380. 'top_category': self.root.id,
  381. 'threads': [self.thread.id, thread.id],
  382. 'title': 'Merged thread!',
  383. 'category': self.category.id,
  384. }), content_type="application/json")
  385. self.assertEqual(response.status_code, 200)
  386. # is response json with new thread?
  387. response_json = json.loads(smart_str(response.content))
  388. new_thread = Thread.objects.get(pk=response_json['id'])
  389. new_thread.is_read = False
  390. new_thread.subscription = None
  391. new_thread.top_category = self.category
  392. add_acl(self.user, new_thread.category)
  393. add_acl(self.user, new_thread)
  394. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  395. # did posts move to new thread?
  396. for post in Post.objects.filter(id__in=posts_ids):
  397. self.assertEqual(post.thread_id, new_thread.id)
  398. # are old threads gone?
  399. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])