test_threads_merge_api.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import json
  2. from django.core.urlresolvers import reverse
  3. from django.utils.encoding import smart_str
  4. from misago.acl import add_acl
  5. from misago.acl.testutils import override_acl
  6. from misago.categories.models import Category
  7. from .. import testutils
  8. from ..api.threadendpoints.merge import MERGE_LIMIT
  9. from ..models import Post, Thread
  10. from ..serializers import ThreadsListSerializer
  11. from .test_threads_api import ThreadsApiTestCase
  12. class ThreadsMergeApiTests(ThreadsApiTestCase):
  13. def setUp(self):
  14. super(ThreadsMergeApiTests, self).setUp()
  15. self.api_link = reverse('misago:api:thread-merge')
  16. Category(
  17. name='Category B',
  18. slug='category-b',
  19. ).insert_at(self.category, position='last-child', save=True)
  20. self.category_b = Category.objects.get(slug='category-b')
  21. def test_merge_no_threads(self):
  22. """api validates if we are trying to merge no threads"""
  23. response = self.client.post(self.api_link, content_type="application/json")
  24. self.assertEqual(response.status_code, 403)
  25. response_json = json.loads(smart_str(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(smart_str(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(smart_str(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(smart_str(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(smart_str(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(smart_str(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(smart_str(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(smart_str(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_too_many_threads(self):
  114. """api rejects too many threads to merge"""
  115. threads = []
  116. for i in xrange(MERGE_LIMIT + 1):
  117. threads.append(testutils.post_thread(category=self.category).pk)
  118. self.override_acl({
  119. 'can_merge_threads': True,
  120. 'can_close_threads': False,
  121. 'can_edit_threads': False,
  122. 'can_reply_threads': False,
  123. })
  124. response = self.client.post(self.api_link, json.dumps({
  125. 'threads': threads
  126. }), content_type="application/json")
  127. self.assertEqual(response.status_code, 403)
  128. response_json = json.loads(response.content)
  129. self.assertEqual(response_json, {
  130. 'detail': "No more than %s threads can be merged at single time." % MERGE_LIMIT
  131. })
  132. def test_merge_no_final_thread(self):
  133. """api rejects merge because no data to merge threads was specified"""
  134. self.override_acl({
  135. 'can_merge_threads': True,
  136. 'can_close_threads': False,
  137. 'can_edit_threads': False,
  138. 'can_reply_threads': False,
  139. })
  140. thread = testutils.post_thread(category=self.category)
  141. response = self.client.post(self.api_link, json.dumps({
  142. 'threads': [self.thread.id, thread.id]
  143. }), content_type="application/json")
  144. self.assertEqual(response.status_code, 400)
  145. response_json = json.loads(smart_str(response.content))
  146. self.assertEqual(response_json, {
  147. 'title': ['This field is required.'],
  148. 'category': ['This field is required.'],
  149. })
  150. def test_merge_invalid_final_title(self):
  151. """api rejects merge because final thread title 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': '$$$',
  162. 'category': self.category.id,
  163. }), content_type="application/json")
  164. self.assertEqual(response.status_code, 400)
  165. response_json = json.loads(smart_str(response.content))
  166. self.assertEqual(response_json, {
  167. 'title': ["Thread title should be at least 5 characters long."]
  168. })
  169. def test_merge_invalid_category(self):
  170. """api rejects merge because final category 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_b.id,
  182. }), content_type="application/json")
  183. self.assertEqual(response.status_code, 400)
  184. response_json = json.loads(smart_str(response.content))
  185. self.assertEqual(response_json, {
  186. 'category': ["Requested category could not be found."]
  187. })
  188. def test_merge_invalid_weight(self):
  189. """api rejects merge because final weight was invalid"""
  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': 4,
  202. }), content_type="application/json")
  203. self.assertEqual(response.status_code, 400)
  204. response_json = json.loads(smart_str(response.content))
  205. self.assertEqual(response_json, {
  206. 'weight': ["Ensure this value is less than or equal to 2."]
  207. })
  208. def test_merge_unallowed_global_weight(self):
  209. """api rejects merge because global weight was unallowed"""
  210. self.override_acl({
  211. 'can_merge_threads': True,
  212. 'can_close_threads': False,
  213. 'can_edit_threads': False,
  214. 'can_reply_threads': False,
  215. })
  216. thread = testutils.post_thread(category=self.category)
  217. response = self.client.post(self.api_link, json.dumps({
  218. 'threads': [self.thread.id, thread.id],
  219. 'title': 'Valid thread title',
  220. 'category': self.category.id,
  221. 'weight': 2,
  222. }), content_type="application/json")
  223. self.assertEqual(response.status_code, 400)
  224. response_json = json.loads(smart_str(response.content))
  225. self.assertEqual(response_json, {
  226. 'weight': [
  227. "You don't have permission to pin threads globally in this category."
  228. ]
  229. })
  230. def test_merge_unallowed_local_weight(self):
  231. """api rejects merge because local weight was unallowed"""
  232. self.override_acl({
  233. 'can_merge_threads': True,
  234. 'can_close_threads': False,
  235. 'can_edit_threads': False,
  236. 'can_reply_threads': False,
  237. })
  238. thread = testutils.post_thread(category=self.category)
  239. response = self.client.post(self.api_link, json.dumps({
  240. 'threads': [self.thread.id, thread.id],
  241. 'title': 'Valid thread title',
  242. 'category': self.category.id,
  243. 'weight': 1,
  244. }), content_type="application/json")
  245. self.assertEqual(response.status_code, 400)
  246. response_json = json.loads(smart_str(response.content))
  247. self.assertEqual(response_json, {
  248. 'weight': [
  249. "You don't have permission to pin threads in this category."
  250. ]
  251. })
  252. def test_merge_allowed_local_weight(self):
  253. """api allows local weight"""
  254. self.override_acl({
  255. 'can_merge_threads': True,
  256. 'can_close_threads': False,
  257. 'can_edit_threads': False,
  258. 'can_reply_threads': False,
  259. 'can_pin_threads': 1,
  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': '$$$',
  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. 'title': ["Thread title should be at least 5 characters long."]
  272. })
  273. def test_merge_allowed_global_weight(self):
  274. """api allows local weight"""
  275. self.override_acl({
  276. 'can_merge_threads': True,
  277. 'can_close_threads': False,
  278. 'can_edit_threads': False,
  279. 'can_reply_threads': False,
  280. 'can_pin_threads': 2,
  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': '$$$',
  286. 'category': self.category.id,
  287. 'weight': 2,
  288. }), content_type="application/json")
  289. self.assertEqual(response.status_code, 400)
  290. response_json = json.loads(smart_str(response.content))
  291. self.assertEqual(response_json, {
  292. 'title': ["Thread title should be at least 5 characters long."]
  293. })
  294. def test_merge_unallowed_close(self):
  295. """api rejects merge because closing thread was unallowed"""
  296. self.override_acl({
  297. 'can_merge_threads': True,
  298. 'can_close_threads': False,
  299. 'can_edit_threads': False,
  300. 'can_reply_threads': False,
  301. })
  302. thread = testutils.post_thread(category=self.category)
  303. response = self.client.post(self.api_link, json.dumps({
  304. 'threads': [self.thread.id, thread.id],
  305. 'title': 'Valid thread title',
  306. 'category': self.category.id,
  307. 'is_closed': True,
  308. }), content_type="application/json")
  309. self.assertEqual(response.status_code, 400)
  310. response_json = json.loads(smart_str(response.content))
  311. self.assertEqual(response_json, {
  312. 'is_closed': [
  313. "You don't have permission to close threads in this category."
  314. ]
  315. })
  316. def test_merge_with_close(self):
  317. """api allows for closing thread"""
  318. self.override_acl({
  319. 'can_merge_threads': True,
  320. 'can_close_threads': False,
  321. 'can_edit_threads': False,
  322. 'can_reply_threads': False,
  323. 'can_close_threads': True,
  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': '$$$',
  329. 'category': self.category.id,
  330. 'weight': 0,
  331. 'is_closed': True,
  332. }), content_type="application/json")
  333. self.assertEqual(response.status_code, 400)
  334. response_json = json.loads(smart_str(response.content))
  335. self.assertEqual(response_json, {
  336. 'title': ["Thread title should be at least 5 characters long."]
  337. })
  338. def test_merge(self):
  339. """api performs basic merge"""
  340. posts_ids = [p.id for p in Post.objects.all()]
  341. self.override_acl({
  342. 'can_merge_threads': True,
  343. 'can_close_threads': False,
  344. 'can_edit_threads': False,
  345. 'can_reply_threads': False,
  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': 'Merged thread!',
  351. 'category': self.category.id,
  352. }), content_type="application/json")
  353. self.assertEqual(response.status_code, 200)
  354. # is response json with new thread?
  355. response_json = json.loads(smart_str(response.content))
  356. new_thread = Thread.objects.get(pk=response_json['id'])
  357. new_thread.is_read = False
  358. new_thread.subscription = None
  359. new_thread.top_category = None
  360. add_acl(self.user, new_thread.category)
  361. add_acl(self.user, new_thread)
  362. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  363. # did posts move to new thread?
  364. for post in Post.objects.filter(id__in=posts_ids):
  365. self.assertEqual(post.thread_id, new_thread.id)
  366. # are old threads gone?
  367. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])
  368. def test_merge_with_top_category(self):
  369. """api performs merge with top category"""
  370. posts_ids = [p.id for p in Post.objects.all()]
  371. self.override_acl({
  372. 'can_merge_threads': True,
  373. 'can_close_threads': False,
  374. 'can_edit_threads': False,
  375. 'can_reply_threads': False,
  376. })
  377. thread = testutils.post_thread(category=self.category)
  378. response = self.client.post(self.api_link, json.dumps({
  379. 'top_category': self.root.id,
  380. 'threads': [self.thread.id, thread.id],
  381. 'title': 'Merged thread!',
  382. 'category': self.category.id,
  383. }), content_type="application/json")
  384. self.assertEqual(response.status_code, 200)
  385. # is response json with new thread?
  386. response_json = json.loads(smart_str(response.content))
  387. new_thread = Thread.objects.get(pk=response_json['id'])
  388. new_thread.is_read = False
  389. new_thread.subscription = None
  390. new_thread.top_category = self.category
  391. add_acl(self.user, new_thread.category)
  392. add_acl(self.user, new_thread)
  393. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  394. # did posts move to new thread?
  395. for post in Post.objects.filter(id__in=posts_ids):
  396. self.assertEqual(post.thread_id, new_thread.id)
  397. # are old threads gone?
  398. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])