test_thread_postsplit_api.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import json
  4. from django.urls import reverse
  5. from django.utils.six.moves import range
  6. from misago.acl.testutils import override_acl
  7. from misago.categories.models import Category
  8. from misago.threads import testutils
  9. from misago.threads.api.postendpoints.split import SPLIT_LIMIT
  10. from misago.threads.models import Thread
  11. from misago.users.testutils import AuthenticatedUserTestCase
  12. class ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
  13. def setUp(self):
  14. super(ThreadPostSplitApiTestCase, self).setUp()
  15. self.category = Category.objects.get(slug='first-category')
  16. self.thread = testutils.post_thread(category=self.category)
  17. self.posts = [
  18. testutils.reply_thread(self.thread).pk,
  19. testutils.reply_thread(self.thread).pk
  20. ]
  21. self.api_link = reverse('misago:api:thread-post-split', kwargs={
  22. 'thread_pk': self.thread.pk
  23. })
  24. Category(
  25. name='Category B',
  26. slug='category-b',
  27. ).insert_at(self.category, position='last-child', save=True)
  28. self.category_b = Category.objects.get(slug='category-b')
  29. self.override_acl()
  30. self.override_other_acl()
  31. def refresh_thread(self):
  32. self.thread = Thread.objects.get(pk=self.thread.pk)
  33. def override_acl(self, extra_acl=None):
  34. new_acl = self.user.acl_cache
  35. new_acl['categories'][self.category.pk].update({
  36. 'can_see': 1,
  37. 'can_browse': 1,
  38. 'can_start_threads': 1,
  39. 'can_reply_threads': 1,
  40. 'can_edit_posts': 1,
  41. 'can_approve_content': 0,
  42. 'can_move_posts': 1
  43. })
  44. if extra_acl:
  45. new_acl['categories'][self.category.pk].update(extra_acl)
  46. override_acl(self.user, new_acl)
  47. def override_other_acl(self, acl=None):
  48. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  49. other_category_acl.update({
  50. 'can_see': 1,
  51. 'can_browse': 1,
  52. 'can_start_threads': 0,
  53. 'can_reply_threads': 0,
  54. 'can_edit_posts': 1,
  55. 'can_approve_content': 0,
  56. 'can_move_posts': 1
  57. })
  58. if acl:
  59. other_category_acl.update(acl)
  60. categories_acl = self.user.acl_cache['categories']
  61. categories_acl[self.category_b.pk] = other_category_acl
  62. visible_categories = [self.category.pk]
  63. if other_category_acl['can_see']:
  64. visible_categories.append(self.category_b.pk)
  65. override_acl(self.user, {
  66. 'visible_categories': visible_categories,
  67. 'categories': categories_acl,
  68. })
  69. def test_anonymous_user(self):
  70. """you need to authenticate to split posts"""
  71. self.logout_user()
  72. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  73. self.assertEqual(response.status_code, 403)
  74. def test_no_permission(self):
  75. """api validates permission to split"""
  76. self.override_acl({
  77. 'can_move_posts': 0
  78. })
  79. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  80. self.assertContains(response, "You can't split posts from this thread.", status_code=403)
  81. def test_empty_data(self):
  82. """api handles empty data"""
  83. response = self.client.post(self.api_link)
  84. self.assertContains(response, "You have to specify at least one post to split.", status_code=400)
  85. def test_no_posts_ids(self):
  86. """api rejects no posts ids"""
  87. response = self.client.post(self.api_link, json.dumps({
  88. 'posts': []
  89. }), content_type="application/json")
  90. self.assertContains(response, "You have to specify at least one post to split.", status_code=400)
  91. def test_invalid_posts_data(self):
  92. """api handles invalid data"""
  93. response = self.client.post(self.api_link, json.dumps({
  94. 'posts': 'string'
  95. }), content_type="application/json")
  96. self.assertContains(response, "One or more post ids received were invalid.", status_code=400)
  97. def test_invalid_posts_ids(self):
  98. """api handles invalid post id"""
  99. response = self.client.post(self.api_link, json.dumps({
  100. 'posts': [1, 2, 'string']
  101. }), content_type="application/json")
  102. self.assertContains(response, "One or more post ids received were invalid.", status_code=400)
  103. def test_split_limit(self):
  104. """api rejects more posts than split limit"""
  105. response = self.client.post(self.api_link, json.dumps({
  106. 'posts': list(range(SPLIT_LIMIT + 1))
  107. }), content_type="application/json")
  108. self.assertContains(response, "No more than {} posts can be split".format(SPLIT_LIMIT), status_code=400)
  109. def test_split_invisible(self):
  110. """api validates posts visibility"""
  111. response = self.client.post(self.api_link, json.dumps({
  112. 'posts': [
  113. testutils.reply_thread(self.thread, is_unapproved=True).pk
  114. ]
  115. }), content_type="application/json")
  116. self.assertContains(response, "One or more posts to split could not be found.", status_code=400)
  117. def test_split_event(self):
  118. """api rejects events split"""
  119. response = self.client.post(self.api_link, json.dumps({
  120. 'posts': [
  121. testutils.reply_thread(self.thread, is_event=True).pk
  122. ]
  123. }), content_type="application/json")
  124. self.assertContains(response, "Events can't be split.", status_code=400)
  125. def test_split_first_post(self):
  126. """api rejects first post split"""
  127. response = self.client.post(self.api_link, json.dumps({
  128. 'posts': [
  129. self.thread.first_post_id
  130. ]
  131. }), content_type="application/json")
  132. self.assertContains(response, "You can't split thread's first post.", status_code=400)
  133. def test_split_hidden_posts(self):
  134. """api recjects attempt to split urneadable hidden post"""
  135. response = self.client.post(self.api_link, json.dumps({
  136. 'posts': [
  137. testutils.reply_thread(self.thread, is_hidden=True).pk
  138. ]
  139. }), content_type="application/json")
  140. self.assertContains(response, "You can't split posts the content you can't see.", status_code=400)
  141. def test_split_other_thread_posts(self):
  142. """api recjects attempt to split other thread's post"""
  143. other_thread = testutils.post_thread(self.category)
  144. response = self.client.post(self.api_link, json.dumps({
  145. 'posts': [
  146. testutils.reply_thread(other_thread, is_hidden=True).pk
  147. ]
  148. }), content_type="application/json")
  149. self.assertContains(response, "One or more posts to split could not be found.", status_code=400)
  150. def test_split_empty_new_thread_data(self):
  151. """api handles empty form data"""
  152. response = self.client.post(self.api_link, json.dumps({
  153. 'posts': self.posts
  154. }), content_type="application/json")
  155. self.assertEqual(response.status_code, 400)
  156. response_json = response.json()
  157. self.assertEqual(response_json, {
  158. 'title': ['This field is required.'],
  159. 'category': ['This field is required.'],
  160. })
  161. def test_split_invalid_final_title(self):
  162. """api rejects split because final thread title was invalid"""
  163. response = self.client.post(self.api_link, json.dumps({
  164. 'posts': self.posts,
  165. 'title': '$$$',
  166. 'category': self.category.id
  167. }), content_type="application/json")
  168. self.assertEqual(response.status_code, 400)
  169. response_json = response.json()
  170. self.assertEqual(response_json, {
  171. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  172. })
  173. def test_split_invalid_category(self):
  174. """api rejects split because final category was invalid"""
  175. self.override_other_acl({
  176. 'can_see': 0
  177. })
  178. response = self.client.post(self.api_link, json.dumps({
  179. 'posts': self.posts,
  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 = response.json()
  185. self.assertEqual(response_json, {
  186. 'category': ["Requested category could not be found."]
  187. })
  188. def test_split_unallowed_start_thread(self):
  189. """api rejects split because category isn't allowing starting threads"""
  190. self.override_acl({
  191. 'can_start_threads': 0
  192. })
  193. response = self.client.post(self.api_link, json.dumps({
  194. 'posts': self.posts,
  195. 'title': 'Valid thread title',
  196. 'category': self.category.id
  197. }), content_type="application/json")
  198. self.assertEqual(response.status_code, 400)
  199. response_json = response.json()
  200. self.assertEqual(response_json, {
  201. 'category': [
  202. "You can't create new threads in selected category."
  203. ]
  204. })
  205. def test_split_invalid_weight(self):
  206. """api rejects split because final weight was invalid"""
  207. response = self.client.post(self.api_link, json.dumps({
  208. 'posts': self.posts,
  209. 'title': 'Valid thread title',
  210. 'category': self.category.id,
  211. 'weight': 4,
  212. }), content_type="application/json")
  213. self.assertEqual(response.status_code, 400)
  214. response_json = response.json()
  215. self.assertEqual(response_json, {
  216. 'weight': ["Ensure this value is less than or equal to 2."]
  217. })
  218. def test_split_unallowed_global_weight(self):
  219. """api rejects split because global weight was unallowed"""
  220. response = self.client.post(self.api_link, json.dumps({
  221. 'posts': self.posts,
  222. 'title': 'Valid thread title',
  223. 'category': self.category.id,
  224. 'weight': 2,
  225. }), content_type="application/json")
  226. self.assertEqual(response.status_code, 400)
  227. response_json = response.json()
  228. self.assertEqual(response_json, {
  229. 'weight': [
  230. "You don't have permission to pin threads globally in this category."
  231. ]
  232. })
  233. def test_split_unallowed_local_weight(self):
  234. """api rejects split because local weight was unallowed"""
  235. response = self.client.post(self.api_link, json.dumps({
  236. 'posts': self.posts,
  237. 'title': 'Valid thread title',
  238. 'category': self.category.id,
  239. 'weight': 1,
  240. }), content_type="application/json")
  241. self.assertEqual(response.status_code, 400)
  242. response_json = response.json()
  243. self.assertEqual(response_json, {
  244. 'weight': [
  245. "You don't have permission to pin threads in this category."
  246. ]
  247. })
  248. def test_split_allowed_local_weight(self):
  249. """api allows local weight"""
  250. self.override_acl({
  251. 'can_pin_threads': 1
  252. })
  253. response = self.client.post(self.api_link, json.dumps({
  254. 'posts': self.posts,
  255. 'title': '$$$',
  256. 'category': self.category.id,
  257. 'weight': 1,
  258. }), content_type="application/json")
  259. self.assertEqual(response.status_code, 400)
  260. response_json = response.json()
  261. self.assertEqual(response_json, {
  262. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  263. })
  264. def test_split_allowed_global_weight(self):
  265. """api allows global weight"""
  266. self.override_acl({
  267. 'can_pin_threads': 2
  268. })
  269. response = self.client.post(self.api_link, json.dumps({
  270. 'posts': self.posts,
  271. 'title': '$$$',
  272. 'category': self.category.id,
  273. 'weight': 2,
  274. }), content_type="application/json")
  275. self.assertEqual(response.status_code, 400)
  276. response_json = response.json()
  277. self.assertEqual(response_json, {
  278. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  279. })
  280. def test_split_unallowed_close(self):
  281. """api rejects split because closing thread was unallowed"""
  282. response = self.client.post(self.api_link, json.dumps({
  283. 'posts': self.posts,
  284. 'title': 'Valid thread title',
  285. 'category': self.category.id,
  286. 'is_closed': True,
  287. }), content_type="application/json")
  288. self.assertEqual(response.status_code, 400)
  289. response_json = response.json()
  290. self.assertEqual(response_json, {
  291. 'is_closed': [
  292. "You don't have permission to close threads in this category."
  293. ]
  294. })
  295. def test_split_with_close(self):
  296. """api allows for closing thread"""
  297. self.override_acl({
  298. 'can_close_threads': True
  299. })
  300. response = self.client.post(self.api_link, json.dumps({
  301. 'posts': self.posts,
  302. 'title': '$$$',
  303. 'category': self.category.id,
  304. 'weight': 0,
  305. 'is_closed': True,
  306. }), content_type="application/json")
  307. self.assertEqual(response.status_code, 400)
  308. response_json = response.json()
  309. self.assertEqual(response_json, {
  310. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  311. })
  312. def test_split_unallowed_hidden(self):
  313. """api rejects split because hidden thread was unallowed"""
  314. response = self.client.post(self.api_link, json.dumps({
  315. 'posts': self.posts,
  316. 'title': 'Valid thread title',
  317. 'category': self.category.id,
  318. 'is_hidden': True,
  319. }), content_type="application/json")
  320. self.assertEqual(response.status_code, 400)
  321. response_json = response.json()
  322. self.assertEqual(response_json, {
  323. 'is_hidden': [
  324. "You don't have permission to hide threads in this category."
  325. ]
  326. })
  327. def test_split_with_hide(self):
  328. """api allows for hiding thread"""
  329. self.override_acl({
  330. 'can_hide_threads': True
  331. })
  332. response = self.client.post(self.api_link, json.dumps({
  333. 'posts': self.posts,
  334. 'title': '$$$',
  335. 'category': self.category.id,
  336. 'weight': 0,
  337. 'is_hidden': True,
  338. }), content_type="application/json")
  339. self.assertEqual(response.status_code, 400)
  340. response_json = response.json()
  341. self.assertEqual(response_json, {
  342. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  343. })
  344. def test_split(self):
  345. """api splits posts to new thread"""
  346. self.refresh_thread()
  347. self.assertEqual(self.thread.replies, 2)
  348. response = self.client.post(self.api_link, json.dumps({
  349. 'posts': self.posts,
  350. 'title': 'Split thread.',
  351. 'category': self.category.id
  352. }), content_type="application/json")
  353. self.assertEqual(response.status_code, 200)
  354. # thread was created
  355. split_thread = self.category.thread_set.get(slug='split-thread')
  356. self.assertEqual(split_thread.replies, 1)
  357. # posts were removed from old thread
  358. self.refresh_thread()
  359. self.assertEqual(self.thread.replies, 0)
  360. # posts were moved to new thread
  361. self.assertEqual(split_thread.post_set.filter(pk__in=self.posts).count(), 2)
  362. def test_split_kitchensink(self):
  363. """api splits posts with kitchensink"""
  364. self.refresh_thread()
  365. self.assertEqual(self.thread.replies, 2)
  366. self.override_other_acl({
  367. 'can_start_threads': 2,
  368. 'can_close_threads': True,
  369. 'can_hide_threads': True,
  370. 'can_pin_threads': 2
  371. })
  372. response = self.client.post(self.api_link, json.dumps({
  373. 'posts': self.posts,
  374. 'title': 'Split thread',
  375. 'category': self.category_b.id,
  376. 'weight': 2,
  377. 'is_closed': 1,
  378. 'is_hidden': 1
  379. }), content_type="application/json")
  380. self.assertEqual(response.status_code, 200)
  381. # thread was created
  382. split_thread = self.category_b.thread_set.get(slug='split-thread')
  383. self.assertEqual(split_thread.replies, 1)
  384. self.assertEqual(split_thread.weight, 2)
  385. self.assertTrue(split_thread.is_closed)
  386. self.assertTrue(split_thread.is_hidden)
  387. # posts were removed from old thread
  388. self.refresh_thread()
  389. self.assertEqual(self.thread.replies, 0)
  390. # posts were moved to new thread
  391. self.assertEqual(split_thread.post_set.filter(pk__in=self.posts).count(), 2)