test_thread_patch_api.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 .test_threads_api import ThreadsApiTestCase
  7. class ThreadPatchApiTestCase(ThreadsApiTestCase):
  8. def patch(self, api_link, ops):
  9. return self.client.patch(api_link, json.dumps(ops), content_type="application/json")
  10. class ThreadAddAclApiTests(ThreadPatchApiTestCase):
  11. def test_add_acl_true(self):
  12. """api adds current thread's acl to response"""
  13. response = self.patch(self.api_link, [
  14. {
  15. 'op': 'add',
  16. 'path': 'acl',
  17. 'value': True,
  18. },
  19. ])
  20. self.assertEqual(response.status_code, 200)
  21. response_json = response.json()
  22. self.assertTrue(response_json['acl'])
  23. def test_add_acl_false(self):
  24. """if value is false, api won't add acl to the response, but will set empty key"""
  25. response = self.patch(self.api_link, [
  26. {
  27. 'op': 'add',
  28. 'path': 'acl',
  29. 'value': False,
  30. },
  31. ])
  32. self.assertEqual(response.status_code, 200)
  33. response_json = response.json()
  34. self.assertIsNone(response_json['acl'])
  35. class ThreadChangeTitleApiTests(ThreadPatchApiTestCase):
  36. def test_change_thread_title(self):
  37. """api makes it possible to change thread title"""
  38. self.override_acl({'can_edit_threads': 2})
  39. response = self.patch(
  40. self.api_link, [
  41. {
  42. 'op': 'replace',
  43. 'path': 'title',
  44. 'value': "Lorem ipsum change!",
  45. },
  46. ]
  47. )
  48. self.assertEqual(response.status_code, 200)
  49. thread_json = self.get_thread_json()
  50. self.assertEqual(thread_json['title'], "Lorem ipsum change!")
  51. def test_change_thread_title_no_permission(self):
  52. """api validates permission to change title"""
  53. self.override_acl({'can_edit_threads': 0})
  54. response = self.patch(
  55. self.api_link, [
  56. {
  57. 'op': 'replace',
  58. 'path': 'title',
  59. 'value': "Lorem ipsum change!",
  60. },
  61. ]
  62. )
  63. self.assertEqual(response.status_code, 400)
  64. response_json = response.json()
  65. self.assertEqual(response_json['detail'][0], "You can't edit threads in this category.")
  66. def test_change_thread_title_after_edit_time(self):
  67. """api cleans, validates and rejects too short title"""
  68. self.override_acl({'thread_edit_time': 1, 'can_edit_threads': 1})
  69. self.thread.starter = self.user
  70. self.thread.started_on = timezone.now() - timedelta(minutes=10)
  71. self.thread.save()
  72. response = self.patch(
  73. self.api_link, [
  74. {
  75. 'op': 'replace',
  76. 'path': 'title',
  77. 'value': "Lorem ipsum change!",
  78. },
  79. ]
  80. )
  81. self.assertEqual(response.status_code, 400)
  82. response_json = response.json()
  83. self.assertEqual(
  84. response_json['detail'][0], "You can't edit threads that are older than 1 minute."
  85. )
  86. def test_change_thread_title_invalid(self):
  87. """api cleans, validates and rejects too short title"""
  88. self.override_acl({'can_edit_threads': 2})
  89. response = self.patch(
  90. self.api_link, [
  91. {
  92. 'op': 'replace',
  93. 'path': 'title',
  94. 'value': 12,
  95. },
  96. ]
  97. )
  98. self.assertEqual(response.status_code, 400)
  99. response_json = response.json()
  100. self.assertEqual(
  101. response_json['detail'][0],
  102. "Thread title should be at least 5 characters long (it has 2)."
  103. )
  104. class ThreadPinGloballyApiTests(ThreadPatchApiTestCase):
  105. def test_pin_thread(self):
  106. """api makes it possible to pin globally thread"""
  107. self.override_acl({'can_pin_threads': 2})
  108. response = self.patch(
  109. self.api_link, [
  110. {
  111. 'op': 'replace',
  112. 'path': 'weight',
  113. 'value': 2,
  114. },
  115. ]
  116. )
  117. self.assertEqual(response.status_code, 200)
  118. thread_json = self.get_thread_json()
  119. self.assertEqual(thread_json['weight'], 2)
  120. def test_unpin_thread(self):
  121. """api makes it possible to unpin thread"""
  122. self.thread.weight = 2
  123. self.thread.save()
  124. thread_json = self.get_thread_json()
  125. self.assertEqual(thread_json['weight'], 2)
  126. self.override_acl({'can_pin_threads': 2})
  127. response = self.patch(
  128. self.api_link, [
  129. {
  130. 'op': 'replace',
  131. 'path': 'weight',
  132. 'value': 0,
  133. },
  134. ]
  135. )
  136. self.assertEqual(response.status_code, 200)
  137. thread_json = self.get_thread_json()
  138. self.assertEqual(thread_json['weight'], 0)
  139. def test_pin_thread_no_permission(self):
  140. """api pin thread globally with no permission fails"""
  141. self.override_acl({'can_pin_threads': 1})
  142. response = self.patch(
  143. self.api_link, [
  144. {
  145. 'op': 'replace',
  146. 'path': 'weight',
  147. 'value': 2,
  148. },
  149. ]
  150. )
  151. self.assertEqual(response.status_code, 400)
  152. response_json = response.json()
  153. self.assertEqual(
  154. response_json['detail'][0], "You don't have permission to pin this thread globally."
  155. )
  156. thread_json = self.get_thread_json()
  157. self.assertEqual(thread_json['weight'], 0)
  158. def test_unpin_thread_no_permission(self):
  159. """api unpin thread with no permission fails"""
  160. self.thread.weight = 2
  161. self.thread.save()
  162. thread_json = self.get_thread_json()
  163. self.assertEqual(thread_json['weight'], 2)
  164. self.override_acl({'can_pin_threads': 1})
  165. response = self.patch(
  166. self.api_link, [
  167. {
  168. 'op': 'replace',
  169. 'path': 'weight',
  170. 'value': 1,
  171. },
  172. ]
  173. )
  174. self.assertEqual(response.status_code, 400)
  175. response_json = response.json()
  176. self.assertEqual(
  177. response_json['detail'][0], "You don't have permission to change this thread's weight."
  178. )
  179. thread_json = self.get_thread_json()
  180. self.assertEqual(thread_json['weight'], 2)
  181. class ThreadPinLocallyApiTests(ThreadPatchApiTestCase):
  182. def test_pin_thread(self):
  183. """api makes it possible to pin locally thread"""
  184. self.override_acl({'can_pin_threads': 1})
  185. response = self.patch(
  186. self.api_link, [
  187. {
  188. 'op': 'replace',
  189. 'path': 'weight',
  190. 'value': 1,
  191. },
  192. ]
  193. )
  194. self.assertEqual(response.status_code, 200)
  195. thread_json = self.get_thread_json()
  196. self.assertEqual(thread_json['weight'], 1)
  197. def test_unpin_thread(self):
  198. """api makes it possible to unpin thread"""
  199. self.thread.weight = 1
  200. self.thread.save()
  201. thread_json = self.get_thread_json()
  202. self.assertEqual(thread_json['weight'], 1)
  203. self.override_acl({'can_pin_threads': 1})
  204. response = self.patch(
  205. self.api_link, [
  206. {
  207. 'op': 'replace',
  208. 'path': 'weight',
  209. 'value': 0,
  210. },
  211. ]
  212. )
  213. self.assertEqual(response.status_code, 200)
  214. thread_json = self.get_thread_json()
  215. self.assertEqual(thread_json['weight'], 0)
  216. def test_pin_thread_no_permission(self):
  217. """api pin thread locally with no permission fails"""
  218. self.override_acl({'can_pin_threads': 0})
  219. response = self.patch(
  220. self.api_link, [
  221. {
  222. 'op': 'replace',
  223. 'path': 'weight',
  224. 'value': 1,
  225. },
  226. ]
  227. )
  228. self.assertEqual(response.status_code, 400)
  229. response_json = response.json()
  230. self.assertEqual(
  231. response_json['detail'][0], "You don't have permission to change this thread's weight."
  232. )
  233. thread_json = self.get_thread_json()
  234. self.assertEqual(thread_json['weight'], 0)
  235. def test_unpin_thread_no_permission(self):
  236. """api unpin thread with no permission fails"""
  237. self.thread.weight = 1
  238. self.thread.save()
  239. thread_json = self.get_thread_json()
  240. self.assertEqual(thread_json['weight'], 1)
  241. self.override_acl({'can_pin_threads': 0})
  242. response = self.patch(
  243. self.api_link, [
  244. {
  245. 'op': 'replace',
  246. 'path': 'weight',
  247. 'value': 0,
  248. },
  249. ]
  250. )
  251. self.assertEqual(response.status_code, 400)
  252. response_json = response.json()
  253. self.assertEqual(
  254. response_json['detail'][0], "You don't have permission to change this thread's weight."
  255. )
  256. thread_json = self.get_thread_json()
  257. self.assertEqual(thread_json['weight'], 1)
  258. class ThreadMoveApiTests(ThreadPatchApiTestCase):
  259. def setUp(self):
  260. super(ThreadMoveApiTests, self).setUp()
  261. Category(
  262. name='Category B',
  263. slug='category-b',
  264. ).insert_at(
  265. self.category,
  266. position='last-child',
  267. save=True,
  268. )
  269. self.category_b = Category.objects.get(slug='category-b')
  270. def override_other_acl(self, acl):
  271. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  272. other_category_acl.update({
  273. 'can_see': 1,
  274. 'can_browse': 1,
  275. 'can_see_all_threads': 1,
  276. 'can_see_own_threads': 0,
  277. 'can_hide_threads': 0,
  278. 'can_approve_content': 0,
  279. })
  280. other_category_acl.update(acl)
  281. categories_acl = self.user.acl_cache['categories']
  282. categories_acl[self.category_b.pk] = other_category_acl
  283. visible_categories = [self.category.pk]
  284. if other_category_acl['can_see']:
  285. visible_categories.append(self.category_b.pk)
  286. override_acl(
  287. self.user, {
  288. 'visible_categories': visible_categories,
  289. 'categories': categories_acl,
  290. }
  291. )
  292. def test_move_thread_no_top(self):
  293. """api moves thread to other category, sets no top category"""
  294. self.override_acl({'can_move_threads': True})
  295. self.override_other_acl({'can_start_threads': 2})
  296. response = self.patch(
  297. self.api_link, [
  298. {
  299. 'op': 'replace',
  300. 'path': 'category',
  301. 'value': self.category_b.pk,
  302. },
  303. {
  304. 'op': 'add',
  305. 'path': 'top-category',
  306. 'value': self.category_b.pk,
  307. },
  308. {
  309. 'op': 'replace',
  310. 'path': 'flatten-categories',
  311. 'value': None,
  312. },
  313. ]
  314. )
  315. self.assertEqual(response.status_code, 200)
  316. self.override_other_acl({})
  317. thread_json = self.get_thread_json()
  318. self.assertEqual(thread_json['category']['id'], self.category_b.pk)
  319. reponse_json = response.json()
  320. self.assertEqual(reponse_json['category'], self.category_b.pk)
  321. self.assertEqual(reponse_json['top_category'], None)
  322. def test_move_thread_with_top(self):
  323. """api moves thread to other category, sets top"""
  324. self.override_acl({'can_move_threads': True})
  325. self.override_other_acl({'can_start_threads': 2})
  326. response = self.patch(
  327. self.api_link, [
  328. {
  329. 'op': 'replace',
  330. 'path': 'category',
  331. 'value': self.category_b.pk,
  332. },
  333. {
  334. 'op': 'add',
  335. 'path': 'top-category',
  336. 'value': Category.objects.root_category().pk,
  337. },
  338. {
  339. 'op': 'replace',
  340. 'path': 'flatten-categories',
  341. 'value': None,
  342. },
  343. ]
  344. )
  345. self.assertEqual(response.status_code, 200)
  346. self.override_other_acl({})
  347. thread_json = self.get_thread_json()
  348. self.assertEqual(thread_json['category']['id'], self.category_b.pk)
  349. reponse_json = response.json()
  350. self.assertEqual(reponse_json['category'], self.category_b.pk)
  351. self.assertEqual(reponse_json['top_category'], self.category.pk)
  352. def test_move_thread_no_permission(self):
  353. """api move thread to other category with no permission fails"""
  354. self.override_acl({'can_move_threads': False})
  355. self.override_other_acl({})
  356. response = self.patch(
  357. self.api_link, [
  358. {
  359. 'op': 'replace',
  360. 'path': 'category',
  361. 'value': self.category_b.pk,
  362. },
  363. ]
  364. )
  365. self.assertEqual(response.status_code, 400)
  366. response_json = response.json()
  367. self.assertEqual(
  368. response_json['detail'][0], "You don't have permission to move this thread."
  369. )
  370. self.override_other_acl({})
  371. thread_json = self.get_thread_json()
  372. self.assertEqual(thread_json['category']['id'], self.category.pk)
  373. def test_move_thread_no_category_access(self):
  374. """api move thread to category with no access fails"""
  375. self.override_acl({'can_move_threads': True})
  376. self.override_other_acl({'can_see': False})
  377. response = self.patch(
  378. self.api_link, [
  379. {
  380. 'op': 'replace',
  381. 'path': 'category',
  382. 'value': self.category_b.pk,
  383. },
  384. ]
  385. )
  386. self.assertEqual(response.status_code, 400)
  387. response_json = response.json()
  388. self.assertEqual(response_json['detail'][0], 'NOT FOUND')
  389. self.override_other_acl({})
  390. thread_json = self.get_thread_json()
  391. self.assertEqual(thread_json['category']['id'], self.category.pk)
  392. def test_move_thread_no_category_browse(self):
  393. """api move thread to category with no browsing access fails"""
  394. self.override_acl({'can_move_threads': True})
  395. self.override_other_acl({'can_browse': False})
  396. response = self.patch(
  397. self.api_link, [
  398. {
  399. 'op': 'replace',
  400. 'path': 'category',
  401. 'value': self.category_b.pk,
  402. },
  403. ]
  404. )
  405. self.assertEqual(response.status_code, 400)
  406. response_json = response.json()
  407. self.assertEqual(
  408. response_json['detail'][0],
  409. 'You don\'t have permission to browse "Category B" contents.'
  410. )
  411. self.override_other_acl({})
  412. thread_json = self.get_thread_json()
  413. self.assertEqual(thread_json['category']['id'], self.category.pk)
  414. def test_move_thread_same_category(self):
  415. """api move thread to category it's already in fails"""
  416. self.override_acl({'can_move_threads': True})
  417. self.override_other_acl({'can_start_threads': 2})
  418. response = self.patch(
  419. self.api_link, [
  420. {
  421. 'op': 'replace',
  422. 'path': 'category',
  423. 'value': self.thread.category_id,
  424. },
  425. ]
  426. )
  427. self.assertEqual(response.status_code, 400)
  428. response_json = response.json()
  429. self.assertEqual(
  430. response_json['detail'][0], "You can't move thread to the category it's already in."
  431. )
  432. self.override_other_acl({})
  433. thread_json = self.get_thread_json()
  434. self.assertEqual(thread_json['category']['id'], self.category.pk)
  435. def test_thread_flatten_categories(self):
  436. """api flatten thread categories"""
  437. response = self.patch(
  438. self.api_link, [
  439. {
  440. 'op': 'replace',
  441. 'path': 'flatten-categories',
  442. 'value': None,
  443. },
  444. ]
  445. )
  446. self.assertEqual(response.status_code, 200)
  447. response_json = response.json()
  448. self.assertEqual(response_json['category'], self.category.pk)
  449. def test_thread_top_flatten_categories(self):
  450. """api flatten thread with top category"""
  451. self.thread.category = self.category_b
  452. self.thread.save()
  453. self.override_other_acl({})
  454. response = self.patch(
  455. self.api_link, [
  456. {
  457. 'op': 'add',
  458. 'path': 'top-category',
  459. 'value': Category.objects.root_category().pk,
  460. },
  461. {
  462. 'op': 'replace',
  463. 'path': 'flatten-categories',
  464. 'value': None,
  465. },
  466. ]
  467. )
  468. self.assertEqual(response.status_code, 200)
  469. response_json = response.json()
  470. self.assertEqual(response_json['top_category'], self.category.pk)
  471. self.assertEqual(response_json['category'], self.category_b.pk)
  472. class ThreadCloseApiTests(ThreadPatchApiTestCase):
  473. def test_close_thread(self):
  474. """api makes it possible to close thread"""
  475. self.override_acl({'can_close_threads': True})
  476. response = self.patch(
  477. self.api_link, [
  478. {
  479. 'op': 'replace',
  480. 'path': 'is-closed',
  481. 'value': True,
  482. },
  483. ]
  484. )
  485. self.assertEqual(response.status_code, 200)
  486. thread_json = self.get_thread_json()
  487. self.assertTrue(thread_json['is_closed'])
  488. def test_open_thread(self):
  489. """api makes it possible to open thread"""
  490. self.thread.is_closed = True
  491. self.thread.save()
  492. thread_json = self.get_thread_json()
  493. self.assertTrue(thread_json['is_closed'])
  494. self.override_acl({'can_close_threads': True})
  495. response = self.patch(
  496. self.api_link, [
  497. {
  498. 'op': 'replace',
  499. 'path': 'is-closed',
  500. 'value': False,
  501. },
  502. ]
  503. )
  504. self.assertEqual(response.status_code, 200)
  505. thread_json = self.get_thread_json()
  506. self.assertFalse(thread_json['is_closed'])
  507. def test_close_thread_no_permission(self):
  508. """api close thread with no permission fails"""
  509. self.override_acl({'can_close_threads': False})
  510. response = self.patch(
  511. self.api_link, [
  512. {
  513. 'op': 'replace',
  514. 'path': 'is-closed',
  515. 'value': True,
  516. },
  517. ]
  518. )
  519. self.assertEqual(response.status_code, 400)
  520. response_json = response.json()
  521. self.assertEqual(
  522. response_json['detail'][0], "You don't have permission to close this thread."
  523. )
  524. thread_json = self.get_thread_json()
  525. self.assertFalse(thread_json['is_closed'])
  526. def test_open_thread_no_permission(self):
  527. """api open thread with no permission fails"""
  528. self.thread.is_closed = True
  529. self.thread.save()
  530. thread_json = self.get_thread_json()
  531. self.assertTrue(thread_json['is_closed'])
  532. self.override_acl({'can_close_threads': False})
  533. response = self.patch(
  534. self.api_link, [
  535. {
  536. 'op': 'replace',
  537. 'path': 'is-closed',
  538. 'value': False,
  539. },
  540. ]
  541. )
  542. self.assertEqual(response.status_code, 400)
  543. response_json = response.json()
  544. self.assertEqual(
  545. response_json['detail'][0], "You don't have permission to open this thread."
  546. )
  547. thread_json = self.get_thread_json()
  548. self.assertTrue(thread_json['is_closed'])
  549. class ThreadApproveApiTests(ThreadPatchApiTestCase):
  550. def test_approve_thread(self):
  551. """api makes it possible to approve thread"""
  552. self.thread.is_unapproved = True
  553. self.thread.save()
  554. self.override_acl({'can_approve_content': 1})
  555. response = self.patch(
  556. self.api_link, [
  557. {
  558. 'op': 'replace',
  559. 'path': 'is-unapproved',
  560. 'value': False,
  561. },
  562. ]
  563. )
  564. self.assertEqual(response.status_code, 200)
  565. thread_json = self.get_thread_json()
  566. self.assertFalse(thread_json['is_unapproved'])
  567. def test_unapprove_thread(self):
  568. """api returns permission error on approval removal"""
  569. self.override_acl({'can_approve_content': 1})
  570. response = self.patch(
  571. self.api_link, [
  572. {
  573. 'op': 'replace',
  574. 'path': 'is-unapproved',
  575. 'value': True,
  576. },
  577. ]
  578. )
  579. self.assertEqual(response.status_code, 400)
  580. response_json = response.json()
  581. self.assertEqual(response_json['detail'][0], "Content approval can't be reversed.")
  582. class ThreadHideApiTests(ThreadPatchApiTestCase):
  583. def test_hide_thread(self):
  584. """api makes it possible to hide thread"""
  585. self.override_acl({'can_hide_threads': 1})
  586. response = self.patch(
  587. self.api_link, [
  588. {
  589. 'op': 'replace',
  590. 'path': 'is-hidden',
  591. 'value': True,
  592. },
  593. ]
  594. )
  595. self.assertEqual(response.status_code, 200)
  596. self.override_acl({'can_hide_threads': 1})
  597. thread_json = self.get_thread_json()
  598. self.assertTrue(thread_json['is_hidden'])
  599. def test_show_thread(self):
  600. """api makes it possible to unhide thread"""
  601. self.thread.is_hidden = True
  602. self.thread.save()
  603. self.override_acl({'can_hide_threads': 1})
  604. thread_json = self.get_thread_json()
  605. self.assertTrue(thread_json['is_hidden'])
  606. self.override_acl({'can_hide_threads': 1})
  607. response = self.patch(
  608. self.api_link, [
  609. {
  610. 'op': 'replace',
  611. 'path': 'is-hidden',
  612. 'value': False,
  613. },
  614. ]
  615. )
  616. self.assertEqual(response.status_code, 200)
  617. self.override_acl({'can_hide_threads': 1})
  618. thread_json = self.get_thread_json()
  619. self.assertFalse(thread_json['is_hidden'])
  620. def test_hide_thread_no_permission(self):
  621. """api hide thread with no permission fails"""
  622. self.override_acl({'can_hide_threads': 0})
  623. response = self.patch(
  624. self.api_link, [
  625. {
  626. 'op': 'replace',
  627. 'path': 'is-hidden',
  628. 'value': True,
  629. },
  630. ]
  631. )
  632. self.assertEqual(response.status_code, 400)
  633. response_json = response.json()
  634. self.assertEqual(
  635. response_json['detail'][0], "You don't have permission to hide this thread."
  636. )
  637. thread_json = self.get_thread_json()
  638. self.assertFalse(thread_json['is_hidden'])
  639. def test_show_thread_no_permission(self):
  640. """api unhide thread with no permission fails"""
  641. self.thread.is_hidden = True
  642. self.thread.save()
  643. self.override_acl({'can_hide_threads': 1})
  644. thread_json = self.get_thread_json()
  645. self.assertTrue(thread_json['is_hidden'])
  646. self.override_acl({'can_hide_threads': 0})
  647. response = self.patch(
  648. self.api_link, [
  649. {
  650. 'op': 'replace',
  651. 'path': 'is-hidden',
  652. 'value': False,
  653. },
  654. ]
  655. )
  656. self.assertEqual(response.status_code, 404)
  657. class ThreadSubscribeApiTests(ThreadPatchApiTestCase):
  658. def test_subscribe_thread(self):
  659. """api makes it possible to subscribe thread"""
  660. response = self.patch(
  661. self.api_link, [
  662. {
  663. 'op': 'replace',
  664. 'path': 'subscription',
  665. 'value': 'notify',
  666. },
  667. ]
  668. )
  669. self.assertEqual(response.status_code, 200)
  670. thread_json = self.get_thread_json()
  671. self.assertFalse(thread_json['subscription'])
  672. subscription = self.user.subscription_set.get(thread=self.thread)
  673. self.assertFalse(subscription.send_email)
  674. def test_subscribe_thread_with_email(self):
  675. """api makes it possible to subscribe thread with emails"""
  676. response = self.patch(
  677. self.api_link, [
  678. {
  679. 'op': 'replace',
  680. 'path': 'subscription',
  681. 'value': 'email',
  682. },
  683. ]
  684. )
  685. self.assertEqual(response.status_code, 200)
  686. thread_json = self.get_thread_json()
  687. self.assertTrue(thread_json['subscription'])
  688. subscription = self.user.subscription_set.get(thread=self.thread)
  689. self.assertTrue(subscription.send_email)
  690. def test_unsubscribe_thread(self):
  691. """api makes it possible to unsubscribe thread"""
  692. response = self.patch(
  693. self.api_link, [
  694. {
  695. 'op': 'replace',
  696. 'path': 'subscription',
  697. 'value': 'remove',
  698. },
  699. ]
  700. )
  701. self.assertEqual(response.status_code, 200)
  702. thread_json = self.get_thread_json()
  703. self.assertIsNone(thread_json['subscription'])
  704. self.assertEqual(self.user.subscription_set.count(), 0)
  705. def test_subscribe_as_guest(self):
  706. """api makes it impossible to subscribe thread"""
  707. self.logout_user()
  708. response = self.patch(
  709. self.api_link, [
  710. {
  711. 'op': 'replace',
  712. 'path': 'subscription',
  713. 'value': 'email',
  714. },
  715. ]
  716. )
  717. self.assertEqual(response.status_code, 403)
  718. def test_subscribe_nonexistant_thread(self):
  719. """api makes it impossible to subscribe nonexistant thread"""
  720. bad_api_link = self.api_link.replace(
  721. six.text_type(self.thread.pk), six.text_type(self.thread.pk + 9)
  722. )
  723. response = self.patch(
  724. bad_api_link, [
  725. {
  726. 'op': 'replace',
  727. 'path': 'subscription',
  728. 'value': 'email',
  729. },
  730. ]
  731. )
  732. self.assertEqual(response.status_code, 404)