test_thread_patch_api.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. import json
  2. from datetime import timedelta
  3. from django.utils import six, timezone
  4. from misago.acl.testutils import override_acl
  5. from misago.categories.models import Category
  6. from misago.readtracker import poststracker
  7. from misago.threads.models import Thread
  8. from .test_threads_api import ThreadsApiTestCase
  9. class ThreadPatchApiTestCase(ThreadsApiTestCase):
  10. def patch(self, api_link, ops):
  11. return self.client.patch(api_link, json.dumps(ops), content_type="application/json")
  12. class ThreadAddAclApiTests(ThreadPatchApiTestCase):
  13. def test_add_acl_true(self):
  14. """api adds current thread's acl to response"""
  15. response = self.patch(self.api_link, [
  16. {
  17. 'op': 'add',
  18. 'path': 'acl',
  19. 'value': True,
  20. },
  21. ])
  22. self.assertEqual(response.status_code, 200)
  23. response_json = response.json()
  24. self.assertTrue(response_json['acl'])
  25. def test_add_acl_false(self):
  26. """if value is false, api won't add acl to the response, but will set empty key"""
  27. response = self.patch(self.api_link, [
  28. {
  29. 'op': 'add',
  30. 'path': 'acl',
  31. 'value': False,
  32. },
  33. ])
  34. self.assertEqual(response.status_code, 200)
  35. response_json = response.json()
  36. self.assertIsNone(response_json['acl'])
  37. class ThreadChangeTitleApiTests(ThreadPatchApiTestCase):
  38. def test_change_thread_title(self):
  39. """api makes it possible to change thread title"""
  40. self.override_acl({'can_edit_threads': 2})
  41. response = self.patch(
  42. self.api_link, [
  43. {
  44. 'op': 'replace',
  45. 'path': 'title',
  46. 'value': "Lorem ipsum change!",
  47. },
  48. ]
  49. )
  50. self.assertEqual(response.status_code, 200)
  51. response_json = response.json()
  52. self.assertEqual(response_json['title'], "Lorem ipsum change!")
  53. thread_json = self.get_thread_json()
  54. self.assertEqual(thread_json['title'], "Lorem ipsum change!")
  55. def test_change_thread_title_no_permission(self):
  56. """api validates permission to change title"""
  57. self.override_acl({'can_edit_threads': 0})
  58. response = self.patch(
  59. self.api_link, [
  60. {
  61. 'op': 'replace',
  62. 'path': 'title',
  63. 'value': "Lorem ipsum change!",
  64. },
  65. ]
  66. )
  67. self.assertEqual(response.status_code, 400)
  68. response_json = response.json()
  69. self.assertEqual(response_json['detail'][0], "You can't edit threads in this category.")
  70. def test_change_thread_title_closed_category_no_permission(self):
  71. """api test permission to edit thread title in closed category"""
  72. self.override_acl({
  73. 'can_edit_threads': 2,
  74. 'can_close_threads': 0
  75. })
  76. self.category.is_closed = True
  77. self.category.save()
  78. response = self.patch(
  79. self.api_link, [
  80. {
  81. 'op': 'replace',
  82. 'path': 'title',
  83. 'value': "Lorem ipsum change!",
  84. },
  85. ]
  86. )
  87. self.assertEqual(response.status_code, 400)
  88. response_json = response.json()
  89. self.assertEqual(
  90. response_json['detail'][0], "This category is closed. You can't edit threads in it."
  91. )
  92. def test_change_thread_title_closed_thread_no_permission(self):
  93. """api test permission to edit closed thread title"""
  94. self.override_acl({
  95. 'can_edit_threads': 2,
  96. 'can_close_threads': 0
  97. })
  98. self.thread.is_closed = True
  99. self.thread.save()
  100. response = self.patch(
  101. self.api_link, [
  102. {
  103. 'op': 'replace',
  104. 'path': 'title',
  105. 'value': "Lorem ipsum change!",
  106. },
  107. ]
  108. )
  109. self.assertEqual(response.status_code, 400)
  110. response_json = response.json()
  111. self.assertEqual(
  112. response_json['detail'][0], "This thread is closed. You can't edit it."
  113. )
  114. def test_change_thread_title_after_edit_time(self):
  115. """api cleans, validates and rejects too short title"""
  116. self.override_acl({'thread_edit_time': 1, 'can_edit_threads': 1})
  117. self.thread.starter = self.user
  118. self.thread.started_on = timezone.now() - timedelta(minutes=10)
  119. self.thread.save()
  120. response = self.patch(
  121. self.api_link, [
  122. {
  123. 'op': 'replace',
  124. 'path': 'title',
  125. 'value': "Lorem ipsum change!",
  126. },
  127. ]
  128. )
  129. self.assertEqual(response.status_code, 400)
  130. response_json = response.json()
  131. self.assertEqual(
  132. response_json['detail'][0], "You can't edit threads that are older than 1 minute."
  133. )
  134. def test_change_thread_title_invalid(self):
  135. """api cleans, validates and rejects too short title"""
  136. self.override_acl({'can_edit_threads': 2})
  137. response = self.patch(
  138. self.api_link, [
  139. {
  140. 'op': 'replace',
  141. 'path': 'title',
  142. 'value': 12,
  143. },
  144. ]
  145. )
  146. self.assertEqual(response.status_code, 400)
  147. response_json = response.json()
  148. self.assertEqual(
  149. response_json['detail'][0],
  150. "Thread title should be at least 5 characters long (it has 2)."
  151. )
  152. class ThreadPinGloballyApiTests(ThreadPatchApiTestCase):
  153. def test_pin_thread(self):
  154. """api makes it possible to pin globally thread"""
  155. self.override_acl({'can_pin_threads': 2})
  156. response = self.patch(
  157. self.api_link, [
  158. {
  159. 'op': 'replace',
  160. 'path': 'weight',
  161. 'value': 2,
  162. },
  163. ]
  164. )
  165. self.assertEqual(response.status_code, 200)
  166. response_json = response.json()
  167. self.assertEqual(response_json['weight'], 2)
  168. thread_json = self.get_thread_json()
  169. self.assertEqual(thread_json['weight'], 2)
  170. def test_pin_thread_closed_category_no_permission(self):
  171. """api checks if category is closed"""
  172. self.override_acl({
  173. 'can_pin_threads': 2,
  174. 'can_close_threads': 0,
  175. })
  176. self.category.is_closed = True
  177. self.category.save()
  178. response = self.patch(
  179. self.api_link, [
  180. {
  181. 'op': 'replace',
  182. 'path': 'weight',
  183. 'value': 2,
  184. },
  185. ]
  186. )
  187. self.assertEqual(response.status_code, 400)
  188. response_json = response.json()
  189. self.assertEqual(
  190. response_json['detail'][0], "This category is closed. You can't change threads weights in it."
  191. )
  192. def test_pin_thread_closed_no_permission(self):
  193. """api checks if thread is closed"""
  194. self.override_acl({
  195. 'can_pin_threads': 2,
  196. 'can_close_threads': 0,
  197. })
  198. self.thread.is_closed = True
  199. self.thread.save()
  200. response = self.patch(
  201. self.api_link, [
  202. {
  203. 'op': 'replace',
  204. 'path': 'weight',
  205. 'value': 2,
  206. },
  207. ]
  208. )
  209. self.assertEqual(response.status_code, 400)
  210. response_json = response.json()
  211. self.assertEqual(
  212. response_json['detail'][0], "This thread is closed. You can't change its weight."
  213. )
  214. def test_unpin_thread(self):
  215. """api makes it possible to unpin thread"""
  216. self.thread.weight = 2
  217. self.thread.save()
  218. thread_json = self.get_thread_json()
  219. self.assertEqual(thread_json['weight'], 2)
  220. self.override_acl({'can_pin_threads': 2})
  221. response = self.patch(
  222. self.api_link, [
  223. {
  224. 'op': 'replace',
  225. 'path': 'weight',
  226. 'value': 0,
  227. },
  228. ]
  229. )
  230. self.assertEqual(response.status_code, 200)
  231. response_json = response.json()
  232. self.assertEqual(response_json['weight'], 0)
  233. thread_json = self.get_thread_json()
  234. self.assertEqual(thread_json['weight'], 0)
  235. def test_pin_thread_no_permission(self):
  236. """api pin thread globally with no permission fails"""
  237. self.override_acl({'can_pin_threads': 1})
  238. response = self.patch(
  239. self.api_link, [
  240. {
  241. 'op': 'replace',
  242. 'path': 'weight',
  243. 'value': 2,
  244. },
  245. ]
  246. )
  247. self.assertEqual(response.status_code, 400)
  248. response_json = response.json()
  249. self.assertEqual(
  250. response_json['detail'][0], "You can't pin threads globally in this category."
  251. )
  252. thread_json = self.get_thread_json()
  253. self.assertEqual(thread_json['weight'], 0)
  254. def test_unpin_thread_no_permission(self):
  255. """api unpin thread with no permission fails"""
  256. self.thread.weight = 2
  257. self.thread.save()
  258. thread_json = self.get_thread_json()
  259. self.assertEqual(thread_json['weight'], 2)
  260. self.override_acl({'can_pin_threads': 1})
  261. response = self.patch(
  262. self.api_link, [
  263. {
  264. 'op': 'replace',
  265. 'path': 'weight',
  266. 'value': 1,
  267. },
  268. ]
  269. )
  270. self.assertEqual(response.status_code, 400)
  271. response_json = response.json()
  272. self.assertEqual(
  273. response_json['detail'][0], "You can't change globally pinned threads weights in this category."
  274. )
  275. thread_json = self.get_thread_json()
  276. self.assertEqual(thread_json['weight'], 2)
  277. class ThreadPinLocallyApiTests(ThreadPatchApiTestCase):
  278. def test_pin_thread(self):
  279. """api makes it possible to pin locally thread"""
  280. self.override_acl({'can_pin_threads': 1})
  281. response = self.patch(
  282. self.api_link, [
  283. {
  284. 'op': 'replace',
  285. 'path': 'weight',
  286. 'value': 1,
  287. },
  288. ]
  289. )
  290. self.assertEqual(response.status_code, 200)
  291. response_json = response.json()
  292. self.assertEqual(response_json['weight'], 1)
  293. thread_json = self.get_thread_json()
  294. self.assertEqual(thread_json['weight'], 1)
  295. def test_unpin_thread(self):
  296. """api makes it possible to unpin thread"""
  297. self.thread.weight = 1
  298. self.thread.save()
  299. thread_json = self.get_thread_json()
  300. self.assertEqual(thread_json['weight'], 1)
  301. self.override_acl({'can_pin_threads': 1})
  302. response = self.patch(
  303. self.api_link, [
  304. {
  305. 'op': 'replace',
  306. 'path': 'weight',
  307. 'value': 0,
  308. },
  309. ]
  310. )
  311. self.assertEqual(response.status_code, 200)
  312. response_json = response.json()
  313. self.assertEqual(response_json['weight'], 0)
  314. thread_json = self.get_thread_json()
  315. self.assertEqual(thread_json['weight'], 0)
  316. def test_pin_thread_no_permission(self):
  317. """api pin thread locally with no permission fails"""
  318. self.override_acl({'can_pin_threads': 0})
  319. response = self.patch(
  320. self.api_link, [
  321. {
  322. 'op': 'replace',
  323. 'path': 'weight',
  324. 'value': 1,
  325. },
  326. ]
  327. )
  328. self.assertEqual(response.status_code, 400)
  329. response_json = response.json()
  330. self.assertEqual(
  331. response_json['detail'][0], "You can't change threads weights in this category."
  332. )
  333. thread_json = self.get_thread_json()
  334. self.assertEqual(thread_json['weight'], 0)
  335. def test_unpin_thread_no_permission(self):
  336. """api unpin thread with no permission fails"""
  337. self.thread.weight = 1
  338. self.thread.save()
  339. thread_json = self.get_thread_json()
  340. self.assertEqual(thread_json['weight'], 1)
  341. self.override_acl({'can_pin_threads': 0})
  342. response = self.patch(
  343. self.api_link, [
  344. {
  345. 'op': 'replace',
  346. 'path': 'weight',
  347. 'value': 0,
  348. },
  349. ]
  350. )
  351. self.assertEqual(response.status_code, 400)
  352. response_json = response.json()
  353. self.assertEqual(
  354. response_json['detail'][0], "You can't change threads weights in this category."
  355. )
  356. thread_json = self.get_thread_json()
  357. self.assertEqual(thread_json['weight'], 1)
  358. class ThreadMoveApiTests(ThreadPatchApiTestCase):
  359. def setUp(self):
  360. super(ThreadMoveApiTests, self).setUp()
  361. Category(
  362. name='Category B',
  363. slug='category-b',
  364. ).insert_at(
  365. self.category,
  366. position='last-child',
  367. save=True,
  368. )
  369. self.category_b = Category.objects.get(slug='category-b')
  370. def override_other_acl(self, acl):
  371. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  372. other_category_acl.update({
  373. 'can_see': 1,
  374. 'can_browse': 1,
  375. 'can_see_all_threads': 1,
  376. 'can_see_own_threads': 0,
  377. 'can_hide_threads': 0,
  378. 'can_approve_content': 0,
  379. })
  380. other_category_acl.update(acl)
  381. categories_acl = self.user.acl_cache['categories']
  382. categories_acl[self.category_b.pk] = other_category_acl
  383. visible_categories = [self.category.pk]
  384. if other_category_acl['can_see']:
  385. visible_categories.append(self.category_b.pk)
  386. override_acl(
  387. self.user, {
  388. 'visible_categories': visible_categories,
  389. 'categories': categories_acl,
  390. }
  391. )
  392. def test_move_thread_no_top(self):
  393. """api moves thread to other category, sets no top category"""
  394. self.override_acl({'can_move_threads': True})
  395. self.override_other_acl({'can_start_threads': 2})
  396. response = self.patch(
  397. self.api_link, [
  398. {
  399. 'op': 'replace',
  400. 'path': 'category',
  401. 'value': self.category_b.pk,
  402. },
  403. {
  404. 'op': 'add',
  405. 'path': 'top-category',
  406. 'value': self.category_b.pk,
  407. },
  408. {
  409. 'op': 'replace',
  410. 'path': 'flatten-categories',
  411. 'value': None,
  412. },
  413. ]
  414. )
  415. self.assertEqual(response.status_code, 200)
  416. reponse_json = response.json()
  417. self.assertEqual(reponse_json['category'], self.category_b.pk)
  418. self.override_other_acl({})
  419. thread_json = self.get_thread_json()
  420. self.assertEqual(thread_json['category']['id'], self.category_b.pk)
  421. def test_move_thread_with_top(self):
  422. """api moves thread to other category, sets top"""
  423. self.override_acl({'can_move_threads': True})
  424. self.override_other_acl({'can_start_threads': 2})
  425. response = self.patch(
  426. self.api_link, [
  427. {
  428. 'op': 'replace',
  429. 'path': 'category',
  430. 'value': self.category_b.pk,
  431. },
  432. {
  433. 'op': 'add',
  434. 'path': 'top-category',
  435. 'value': Category.objects.root_category().pk,
  436. },
  437. {
  438. 'op': 'replace',
  439. 'path': 'flatten-categories',
  440. 'value': None,
  441. },
  442. ]
  443. )
  444. self.assertEqual(response.status_code, 200)
  445. reponse_json = response.json()
  446. self.assertEqual(reponse_json['category'], self.category_b.pk)
  447. self.override_other_acl({})
  448. thread_json = self.get_thread_json()
  449. self.assertEqual(thread_json['category']['id'], self.category_b.pk)
  450. def test_move_thread_reads(self):
  451. """api moves thread reads together with thread"""
  452. self.override_acl({'can_move_threads': True})
  453. self.override_other_acl({'can_start_threads': 2})
  454. poststracker.save_read(self.user, self.thread.first_post)
  455. self.assertEqual(self.user.postread_set.count(), 1)
  456. self.user.postread_set.get(category=self.category)
  457. response = self.patch(
  458. self.api_link, [
  459. {
  460. 'op': 'replace',
  461. 'path': 'category',
  462. 'value': self.category_b.pk,
  463. },
  464. {
  465. 'op': 'add',
  466. 'path': 'top-category',
  467. 'value': self.category_b.pk,
  468. },
  469. {
  470. 'op': 'replace',
  471. 'path': 'flatten-categories',
  472. 'value': None,
  473. },
  474. ]
  475. )
  476. self.assertEqual(response.status_code, 200)
  477. # thread read was moved to new category
  478. self.assertEqual(self.user.postread_set.count(), 1)
  479. self.user.postread_set.get(category=self.category_b)
  480. def test_move_thread_subscriptions(self):
  481. """api moves thread subscriptions together with thread"""
  482. self.override_acl({'can_move_threads': True})
  483. self.override_other_acl({'can_start_threads': 2})
  484. self.user.subscription_set.create(
  485. thread=self.thread,
  486. category=self.thread.category,
  487. last_read_on=self.thread.last_post_on,
  488. send_email=False,
  489. )
  490. self.assertEqual(self.user.subscription_set.count(), 1)
  491. self.user.subscription_set.get(category=self.category)
  492. response = self.patch(
  493. self.api_link, [
  494. {
  495. 'op': 'replace',
  496. 'path': 'category',
  497. 'value': self.category_b.pk,
  498. },
  499. {
  500. 'op': 'add',
  501. 'path': 'top-category',
  502. 'value': self.category_b.pk,
  503. },
  504. {
  505. 'op': 'replace',
  506. 'path': 'flatten-categories',
  507. 'value': None,
  508. },
  509. ]
  510. )
  511. self.assertEqual(response.status_code, 200)
  512. # thread read was moved to new category
  513. self.assertEqual(self.user.subscription_set.count(), 1)
  514. self.user.subscription_set.get(category=self.category_b)
  515. def test_move_thread_no_permission(self):
  516. """api move thread to other category with no permission fails"""
  517. self.override_acl({'can_move_threads': False})
  518. self.override_other_acl({})
  519. response = self.patch(
  520. self.api_link, [
  521. {
  522. 'op': 'replace',
  523. 'path': 'category',
  524. 'value': self.category_b.pk,
  525. },
  526. ]
  527. )
  528. self.assertEqual(response.status_code, 400)
  529. response_json = response.json()
  530. self.assertEqual(
  531. response_json['detail'][0], "You can't move threads in this category."
  532. )
  533. self.override_other_acl({})
  534. thread_json = self.get_thread_json()
  535. self.assertEqual(thread_json['category']['id'], self.category.pk)
  536. def test_move_thread_closed_category_no_permission(self):
  537. """api move thread from closed category with no permission fails"""
  538. self.override_acl({
  539. 'can_move_threads': True,
  540. 'can_close_threads': False,
  541. })
  542. self.override_other_acl({})
  543. self.category.is_closed = True
  544. self.category.save()
  545. response = self.patch(
  546. self.api_link, [
  547. {
  548. 'op': 'replace',
  549. 'path': 'category',
  550. 'value': self.category_b.pk,
  551. },
  552. ]
  553. )
  554. self.assertEqual(response.status_code, 400)
  555. response_json = response.json()
  556. self.assertEqual(
  557. response_json['detail'][0], "This category is closed. You can't move it's threads."
  558. )
  559. def test_move_closed_thread_no_permission(self):
  560. """api move closed thread with no permission fails"""
  561. self.override_acl({
  562. 'can_move_threads': True,
  563. 'can_close_threads': False,
  564. })
  565. self.override_other_acl({})
  566. self.thread.is_closed = True
  567. self.thread.save()
  568. response = self.patch(
  569. self.api_link, [
  570. {
  571. 'op': 'replace',
  572. 'path': 'category',
  573. 'value': self.category_b.pk,
  574. },
  575. ]
  576. )
  577. self.assertEqual(response.status_code, 400)
  578. response_json = response.json()
  579. self.assertEqual(
  580. response_json['detail'][0], "This thread is closed. You can't move it."
  581. )
  582. def test_move_thread_no_category_access(self):
  583. """api move thread to category with no access fails"""
  584. self.override_acl({'can_move_threads': True})
  585. self.override_other_acl({'can_see': False})
  586. response = self.patch(
  587. self.api_link, [
  588. {
  589. 'op': 'replace',
  590. 'path': 'category',
  591. 'value': self.category_b.pk,
  592. },
  593. ]
  594. )
  595. self.assertEqual(response.status_code, 400)
  596. response_json = response.json()
  597. self.assertEqual(response_json['detail'][0], 'NOT FOUND')
  598. self.override_other_acl({})
  599. thread_json = self.get_thread_json()
  600. self.assertEqual(thread_json['category']['id'], self.category.pk)
  601. def test_move_thread_no_category_browse(self):
  602. """api move thread to category with no browsing access fails"""
  603. self.override_acl({'can_move_threads': True})
  604. self.override_other_acl({'can_browse': False})
  605. response = self.patch(
  606. self.api_link, [
  607. {
  608. 'op': 'replace',
  609. 'path': 'category',
  610. 'value': self.category_b.pk,
  611. },
  612. ]
  613. )
  614. self.assertEqual(response.status_code, 400)
  615. response_json = response.json()
  616. self.assertEqual(
  617. response_json['detail'][0],
  618. 'You don\'t have permission to browse "Category B" contents.'
  619. )
  620. self.override_other_acl({})
  621. thread_json = self.get_thread_json()
  622. self.assertEqual(thread_json['category']['id'], self.category.pk)
  623. def test_move_thread_no_category_start_threads(self):
  624. """api move thread to category with no posting access fails"""
  625. self.override_acl({'can_move_threads': True})
  626. self.override_other_acl({'can_start_threads': False})
  627. response = self.patch(
  628. self.api_link, [
  629. {
  630. 'op': 'replace',
  631. 'path': 'category',
  632. 'value': self.category_b.pk,
  633. },
  634. ]
  635. )
  636. self.assertEqual(response.status_code, 400)
  637. response_json = response.json()
  638. self.assertEqual(
  639. response_json['detail'][0],
  640. "You don't have permission to start new threads in this category."
  641. )
  642. self.override_other_acl({})
  643. thread_json = self.get_thread_json()
  644. self.assertEqual(thread_json['category']['id'], self.category.pk)
  645. def test_move_thread_same_category(self):
  646. """api move thread to category it's already in fails"""
  647. self.override_acl({'can_move_threads': True})
  648. self.override_other_acl({'can_start_threads': 2})
  649. response = self.patch(
  650. self.api_link, [
  651. {
  652. 'op': 'replace',
  653. 'path': 'category',
  654. 'value': self.thread.category_id,
  655. },
  656. ]
  657. )
  658. self.assertEqual(response.status_code, 400)
  659. response_json = response.json()
  660. self.assertEqual(
  661. response_json['detail'][0], "You can't move thread to the category it's already in."
  662. )
  663. self.override_other_acl({})
  664. thread_json = self.get_thread_json()
  665. self.assertEqual(thread_json['category']['id'], self.category.pk)
  666. def test_thread_flatten_categories(self):
  667. """api flatten thread categories"""
  668. response = self.patch(
  669. self.api_link, [
  670. {
  671. 'op': 'replace',
  672. 'path': 'flatten-categories',
  673. 'value': None,
  674. },
  675. ]
  676. )
  677. self.assertEqual(response.status_code, 200)
  678. response_json = response.json()
  679. self.assertEqual(response_json['category'], self.category.pk)
  680. class ThreadCloseApiTests(ThreadPatchApiTestCase):
  681. def test_close_thread(self):
  682. """api makes it possible to close thread"""
  683. self.override_acl({'can_close_threads': True})
  684. response = self.patch(
  685. self.api_link, [
  686. {
  687. 'op': 'replace',
  688. 'path': 'is-closed',
  689. 'value': True,
  690. },
  691. ]
  692. )
  693. self.assertEqual(response.status_code, 200)
  694. response_json = response.json()
  695. self.assertTrue(response_json['is_closed'])
  696. thread_json = self.get_thread_json()
  697. self.assertTrue(thread_json['is_closed'])
  698. def test_open_thread(self):
  699. """api makes it possible to open thread"""
  700. self.thread.is_closed = True
  701. self.thread.save()
  702. thread_json = self.get_thread_json()
  703. self.assertTrue(thread_json['is_closed'])
  704. self.override_acl({'can_close_threads': True})
  705. response = self.patch(
  706. self.api_link, [
  707. {
  708. 'op': 'replace',
  709. 'path': 'is-closed',
  710. 'value': False,
  711. },
  712. ]
  713. )
  714. self.assertEqual(response.status_code, 200)
  715. response_json = response.json()
  716. self.assertFalse(response_json['is_closed'])
  717. thread_json = self.get_thread_json()
  718. self.assertFalse(thread_json['is_closed'])
  719. def test_close_thread_no_permission(self):
  720. """api close thread with no permission fails"""
  721. self.override_acl({'can_close_threads': False})
  722. response = self.patch(
  723. self.api_link, [
  724. {
  725. 'op': 'replace',
  726. 'path': 'is-closed',
  727. 'value': True,
  728. },
  729. ]
  730. )
  731. self.assertEqual(response.status_code, 400)
  732. response_json = response.json()
  733. self.assertEqual(
  734. response_json['detail'][0], "You don't have permission to close this thread."
  735. )
  736. thread_json = self.get_thread_json()
  737. self.assertFalse(thread_json['is_closed'])
  738. def test_open_thread_no_permission(self):
  739. """api open thread with no permission fails"""
  740. self.thread.is_closed = True
  741. self.thread.save()
  742. thread_json = self.get_thread_json()
  743. self.assertTrue(thread_json['is_closed'])
  744. self.override_acl({'can_close_threads': False})
  745. response = self.patch(
  746. self.api_link, [
  747. {
  748. 'op': 'replace',
  749. 'path': 'is-closed',
  750. 'value': False,
  751. },
  752. ]
  753. )
  754. self.assertEqual(response.status_code, 400)
  755. response_json = response.json()
  756. self.assertEqual(
  757. response_json['detail'][0], "You don't have permission to open this thread."
  758. )
  759. thread_json = self.get_thread_json()
  760. self.assertTrue(thread_json['is_closed'])
  761. class ThreadApproveApiTests(ThreadPatchApiTestCase):
  762. def test_approve_thread(self):
  763. """api makes it possible to approve thread"""
  764. self.thread.first_post.is_unapproved = True
  765. self.thread.first_post.save()
  766. self.thread.synchronize()
  767. self.thread.save()
  768. self.assertTrue(self.thread.is_unapproved)
  769. self.assertTrue(self.thread.has_unapproved_posts)
  770. self.override_acl({'can_approve_content': 1})
  771. response = self.patch(
  772. self.api_link, [
  773. {
  774. 'op': 'replace',
  775. 'path': 'is-unapproved',
  776. 'value': False,
  777. },
  778. ]
  779. )
  780. self.assertEqual(response.status_code, 200)
  781. response_json = response.json()
  782. self.assertFalse(response_json['is_unapproved'])
  783. self.assertFalse(response_json['has_unapproved_posts'])
  784. thread_json = self.get_thread_json()
  785. self.assertFalse(thread_json['is_unapproved'])
  786. self.assertFalse(thread_json['has_unapproved_posts'])
  787. thread = Thread.objects.get(pk=self.thread.pk)
  788. self.assertFalse(thread.is_unapproved)
  789. self.assertFalse(thread.has_unapproved_posts)
  790. def test_approve_thread_category_closed_no_permission(self):
  791. """api checks permission for approving threads in closed categories"""
  792. self.thread.first_post.is_unapproved = True
  793. self.thread.first_post.save()
  794. self.thread.synchronize()
  795. self.thread.save()
  796. self.assertTrue(self.thread.is_unapproved)
  797. self.assertTrue(self.thread.has_unapproved_posts)
  798. self.category.is_closed = True
  799. self.category.save()
  800. self.override_acl({
  801. 'can_approve_content': 1,
  802. 'can_close_threads': 0,
  803. })
  804. response = self.patch(
  805. self.api_link, [
  806. {
  807. 'op': 'replace',
  808. 'path': 'is-unapproved',
  809. 'value': False,
  810. },
  811. ]
  812. )
  813. self.assertEqual(response.status_code, 400)
  814. response_json = response.json()
  815. self.assertEqual(response_json['detail'][0], "This category is closed. You can't approve threads in it.")
  816. def test_approve_thread_closed_no_permission(self):
  817. """api checks permission for approving posts in closed categories"""
  818. self.thread.first_post.is_unapproved = True
  819. self.thread.first_post.save()
  820. self.thread.synchronize()
  821. self.thread.save()
  822. self.assertTrue(self.thread.is_unapproved)
  823. self.assertTrue(self.thread.has_unapproved_posts)
  824. self.thread.is_closed = True
  825. self.thread.save()
  826. self.override_acl({
  827. 'can_approve_content': 1,
  828. 'can_close_threads': 0,
  829. })
  830. response = self.patch(
  831. self.api_link, [
  832. {
  833. 'op': 'replace',
  834. 'path': 'is-unapproved',
  835. 'value': False,
  836. },
  837. ]
  838. )
  839. self.assertEqual(response.status_code, 400)
  840. response_json = response.json()
  841. self.assertEqual(response_json['detail'][0], "This thread is closed. You can't approve it.")
  842. def test_unapprove_thread(self):
  843. """api returns permission error on approval removal"""
  844. self.override_acl({'can_approve_content': 1})
  845. response = self.patch(
  846. self.api_link, [
  847. {
  848. 'op': 'replace',
  849. 'path': 'is-unapproved',
  850. 'value': True,
  851. },
  852. ]
  853. )
  854. self.assertEqual(response.status_code, 400)
  855. response_json = response.json()
  856. self.assertEqual(response_json['detail'][0], "Content approval can't be reversed.")
  857. class ThreadHideApiTests(ThreadPatchApiTestCase):
  858. def test_hide_thread(self):
  859. """api makes it possible to hide thread"""
  860. self.override_acl({'can_hide_threads': 1})
  861. response = self.patch(
  862. self.api_link, [
  863. {
  864. 'op': 'replace',
  865. 'path': 'is-hidden',
  866. 'value': True,
  867. },
  868. ]
  869. )
  870. self.assertEqual(response.status_code, 200)
  871. reponse_json = response.json()
  872. self.assertTrue(reponse_json['is_hidden'])
  873. self.override_acl({'can_hide_threads': 1})
  874. thread_json = self.get_thread_json()
  875. self.assertTrue(thread_json['is_hidden'])
  876. def test_hide_thread_no_permission(self):
  877. """api hide thread with no permission fails"""
  878. self.override_acl({'can_hide_threads': 0})
  879. response = self.patch(
  880. self.api_link, [
  881. {
  882. 'op': 'replace',
  883. 'path': 'is-hidden',
  884. 'value': True,
  885. },
  886. ]
  887. )
  888. self.assertEqual(response.status_code, 400)
  889. response_json = response.json()
  890. self.assertEqual(
  891. response_json['detail'][0], "You can't hide threads in this category."
  892. )
  893. thread_json = self.get_thread_json()
  894. self.assertFalse(thread_json['is_hidden'])
  895. def test_hide_non_owned_thread(self):
  896. """api forbids non-moderator from hiding other users threads"""
  897. self.override_acl({
  898. 'can_hide_own_threads': 1,
  899. 'can_hide_threads': 0
  900. })
  901. response = self.patch(
  902. self.api_link, [
  903. {
  904. 'op': 'replace',
  905. 'path': 'is-hidden',
  906. 'value': True,
  907. },
  908. ]
  909. )
  910. self.assertEqual(response.status_code, 400)
  911. response_json = response.json()
  912. self.assertEqual(
  913. response_json['detail'][0], "You can't hide other users theads in this category."
  914. )
  915. def test_hide_owned_thread_no_time(self):
  916. """api forbids non-moderator from hiding other users threads"""
  917. self.override_acl({
  918. 'can_hide_own_threads': 1,
  919. 'can_hide_threads': 0,
  920. 'thread_edit_time': 1,
  921. })
  922. self.thread.starter = self.user
  923. self.thread.started_on = timezone.now() - timedelta(minutes=5)
  924. self.thread.save()
  925. response = self.patch(
  926. self.api_link, [
  927. {
  928. 'op': 'replace',
  929. 'path': 'is-hidden',
  930. 'value': True,
  931. },
  932. ]
  933. )
  934. self.assertEqual(response.status_code, 400)
  935. response_json = response.json()
  936. self.assertEqual(
  937. response_json['detail'][0], "You can't hide threads that are older than 1 minute."
  938. )
  939. def test_hide_closed_category_no_permission(self):
  940. """api test permission to hide thread in closed category"""
  941. self.override_acl({
  942. 'can_hide_threads': 1,
  943. 'can_close_threads': 0
  944. })
  945. self.category.is_closed = True
  946. self.category.save()
  947. response = self.patch(
  948. self.api_link, [
  949. {
  950. 'op': 'replace',
  951. 'path': 'is-hidden',
  952. 'value': True,
  953. },
  954. ]
  955. )
  956. self.assertEqual(response.status_code, 400)
  957. response_json = response.json()
  958. self.assertEqual(
  959. response_json['detail'][0], "This category is closed. You can't hide threads in it."
  960. )
  961. def test_hide_closed_thread_no_permission(self):
  962. """api test permission to hide closed thread"""
  963. self.override_acl({
  964. 'can_hide_threads': 1,
  965. 'can_close_threads': 0
  966. })
  967. self.thread.is_closed = True
  968. self.thread.save()
  969. response = self.patch(
  970. self.api_link, [
  971. {
  972. 'op': 'replace',
  973. 'path': 'is-hidden',
  974. 'value': True,
  975. },
  976. ]
  977. )
  978. self.assertEqual(response.status_code, 400)
  979. response_json = response.json()
  980. self.assertEqual(
  981. response_json['detail'][0], "This thread is closed. You can't hide it."
  982. )
  983. class ThreadUnhideApiTests(ThreadPatchApiTestCase):
  984. def setUp(self):
  985. super(ThreadUnhideApiTests, self).setUp()
  986. self.thread.is_hidden = True
  987. self.thread.save()
  988. def test_unhide_thread(self):
  989. """api makes it possible to unhide thread"""
  990. self.override_acl({'can_hide_threads': 1})
  991. response = self.patch(
  992. self.api_link, [
  993. {
  994. 'op': 'replace',
  995. 'path': 'is-hidden',
  996. 'value': False,
  997. },
  998. ]
  999. )
  1000. self.assertEqual(response.status_code, 200)
  1001. reponse_json = response.json()
  1002. self.assertFalse(reponse_json['is_hidden'])
  1003. self.override_acl({'can_hide_threads': 1})
  1004. thread_json = self.get_thread_json()
  1005. self.assertFalse(thread_json['is_hidden'])
  1006. def test_unhide_thread_no_permission(self):
  1007. """api unhide thread with no permission fails as thread is invisible"""
  1008. self.override_acl({'can_hide_threads': 0})
  1009. response = self.patch(
  1010. self.api_link, [
  1011. {
  1012. 'op': 'replace',
  1013. 'path': 'is-hidden',
  1014. 'value': True,
  1015. },
  1016. ]
  1017. )
  1018. self.assertEqual(response.status_code, 404)
  1019. def test_unhide_closed_category_no_permission(self):
  1020. """api test permission to unhide thread in closed category"""
  1021. self.override_acl({
  1022. 'can_hide_threads': 1,
  1023. 'can_close_threads': 0
  1024. })
  1025. self.category.is_closed = True
  1026. self.category.save()
  1027. response = self.patch(
  1028. self.api_link, [
  1029. {
  1030. 'op': 'replace',
  1031. 'path': 'is-hidden',
  1032. 'value': False,
  1033. },
  1034. ]
  1035. )
  1036. self.assertEqual(response.status_code, 400)
  1037. response_json = response.json()
  1038. self.assertEqual(
  1039. response_json['detail'][0], "This category is closed. You can't reveal threads in it."
  1040. )
  1041. def test_unhide_closed_thread_no_permission(self):
  1042. """api test permission to unhide closed thread"""
  1043. self.override_acl({
  1044. 'can_hide_threads': 1,
  1045. 'can_close_threads': 0
  1046. })
  1047. self.thread.is_closed = True
  1048. self.thread.save()
  1049. response = self.patch(
  1050. self.api_link, [
  1051. {
  1052. 'op': 'replace',
  1053. 'path': 'is-hidden',
  1054. 'value': False,
  1055. },
  1056. ]
  1057. )
  1058. self.assertEqual(response.status_code, 400)
  1059. response_json = response.json()
  1060. self.assertEqual(
  1061. response_json['detail'][0], "This thread is closed. You can't reveal it."
  1062. )
  1063. class ThreadSubscribeApiTests(ThreadPatchApiTestCase):
  1064. def test_subscribe_thread(self):
  1065. """api makes it possible to subscribe thread"""
  1066. response = self.patch(
  1067. self.api_link, [
  1068. {
  1069. 'op': 'replace',
  1070. 'path': 'subscription',
  1071. 'value': 'notify',
  1072. },
  1073. ]
  1074. )
  1075. self.assertEqual(response.status_code, 200)
  1076. reponse_json = response.json()
  1077. self.assertFalse(reponse_json['subscription'])
  1078. thread_json = self.get_thread_json()
  1079. self.assertFalse(thread_json['subscription'])
  1080. subscription = self.user.subscription_set.get(thread=self.thread)
  1081. self.assertFalse(subscription.send_email)
  1082. def test_subscribe_thread_with_email(self):
  1083. """api makes it possible to subscribe thread with emails"""
  1084. response = self.patch(
  1085. self.api_link, [
  1086. {
  1087. 'op': 'replace',
  1088. 'path': 'subscription',
  1089. 'value': 'email',
  1090. },
  1091. ]
  1092. )
  1093. self.assertEqual(response.status_code, 200)
  1094. reponse_json = response.json()
  1095. self.assertTrue(reponse_json['subscription'])
  1096. thread_json = self.get_thread_json()
  1097. self.assertTrue(thread_json['subscription'])
  1098. subscription = self.user.subscription_set.get(thread=self.thread)
  1099. self.assertTrue(subscription.send_email)
  1100. def test_unsubscribe_thread(self):
  1101. """api makes it possible to unsubscribe thread"""
  1102. response = self.patch(
  1103. self.api_link, [
  1104. {
  1105. 'op': 'replace',
  1106. 'path': 'subscription',
  1107. 'value': 'remove',
  1108. },
  1109. ]
  1110. )
  1111. self.assertEqual(response.status_code, 200)
  1112. reponse_json = response.json()
  1113. self.assertIsNone(reponse_json['subscription'])
  1114. thread_json = self.get_thread_json()
  1115. self.assertIsNone(thread_json['subscription'])
  1116. self.assertEqual(self.user.subscription_set.count(), 0)
  1117. def test_subscribe_as_guest(self):
  1118. """api makes it impossible to subscribe thread"""
  1119. self.logout_user()
  1120. response = self.patch(
  1121. self.api_link, [
  1122. {
  1123. 'op': 'replace',
  1124. 'path': 'subscription',
  1125. 'value': 'email',
  1126. },
  1127. ]
  1128. )
  1129. self.assertEqual(response.status_code, 403)
  1130. def test_subscribe_nonexistant_thread(self):
  1131. """api makes it impossible to subscribe nonexistant thread"""
  1132. bad_api_link = self.api_link.replace(
  1133. six.text_type(self.thread.pk), six.text_type(self.thread.pk + 9)
  1134. )
  1135. response = self.patch(
  1136. bad_api_link, [
  1137. {
  1138. 'op': 'replace',
  1139. 'path': 'subscription',
  1140. 'value': 'email',
  1141. },
  1142. ]
  1143. )
  1144. self.assertEqual(response.status_code, 404)