test_thread_patch_api.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. def test_move_thread_with_top(self):
  322. """api moves thread to other category, sets top"""
  323. self.override_acl({'can_move_threads': True})
  324. self.override_other_acl({'can_start_threads': 2})
  325. response = self.patch(
  326. self.api_link, [
  327. {
  328. 'op': 'replace',
  329. 'path': 'category',
  330. 'value': self.category_b.pk,
  331. },
  332. {
  333. 'op': 'add',
  334. 'path': 'top-category',
  335. 'value': Category.objects.root_category().pk,
  336. },
  337. {
  338. 'op': 'replace',
  339. 'path': 'flatten-categories',
  340. 'value': None,
  341. },
  342. ]
  343. )
  344. self.assertEqual(response.status_code, 200)
  345. self.override_other_acl({})
  346. thread_json = self.get_thread_json()
  347. self.assertEqual(thread_json['category']['id'], self.category_b.pk)
  348. reponse_json = response.json()
  349. self.assertEqual(reponse_json['category'], self.category_b.pk)
  350. def test_move_thread_no_permission(self):
  351. """api move thread to other category with no permission fails"""
  352. self.override_acl({'can_move_threads': False})
  353. self.override_other_acl({})
  354. response = self.patch(
  355. self.api_link, [
  356. {
  357. 'op': 'replace',
  358. 'path': 'category',
  359. 'value': self.category_b.pk,
  360. },
  361. ]
  362. )
  363. self.assertEqual(response.status_code, 400)
  364. response_json = response.json()
  365. self.assertEqual(
  366. response_json['detail'][0], "You don't have permission to move this thread."
  367. )
  368. self.override_other_acl({})
  369. thread_json = self.get_thread_json()
  370. self.assertEqual(thread_json['category']['id'], self.category.pk)
  371. def test_move_thread_no_category_access(self):
  372. """api move thread to category with no access fails"""
  373. self.override_acl({'can_move_threads': True})
  374. self.override_other_acl({'can_see': False})
  375. response = self.patch(
  376. self.api_link, [
  377. {
  378. 'op': 'replace',
  379. 'path': 'category',
  380. 'value': self.category_b.pk,
  381. },
  382. ]
  383. )
  384. self.assertEqual(response.status_code, 400)
  385. response_json = response.json()
  386. self.assertEqual(response_json['detail'][0], 'NOT FOUND')
  387. self.override_other_acl({})
  388. thread_json = self.get_thread_json()
  389. self.assertEqual(thread_json['category']['id'], self.category.pk)
  390. def test_move_thread_no_category_browse(self):
  391. """api move thread to category with no browsing access fails"""
  392. self.override_acl({'can_move_threads': True})
  393. self.override_other_acl({'can_browse': False})
  394. response = self.patch(
  395. self.api_link, [
  396. {
  397. 'op': 'replace',
  398. 'path': 'category',
  399. 'value': self.category_b.pk,
  400. },
  401. ]
  402. )
  403. self.assertEqual(response.status_code, 400)
  404. response_json = response.json()
  405. self.assertEqual(
  406. response_json['detail'][0],
  407. 'You don\'t have permission to browse "Category B" contents.'
  408. )
  409. self.override_other_acl({})
  410. thread_json = self.get_thread_json()
  411. self.assertEqual(thread_json['category']['id'], self.category.pk)
  412. def test_move_thread_same_category(self):
  413. """api move thread to category it's already in fails"""
  414. self.override_acl({'can_move_threads': True})
  415. self.override_other_acl({'can_start_threads': 2})
  416. response = self.patch(
  417. self.api_link, [
  418. {
  419. 'op': 'replace',
  420. 'path': 'category',
  421. 'value': self.thread.category_id,
  422. },
  423. ]
  424. )
  425. self.assertEqual(response.status_code, 400)
  426. response_json = response.json()
  427. self.assertEqual(
  428. response_json['detail'][0], "You can't move thread to the category it's already in."
  429. )
  430. self.override_other_acl({})
  431. thread_json = self.get_thread_json()
  432. self.assertEqual(thread_json['category']['id'], self.category.pk)
  433. def test_thread_flatten_categories(self):
  434. """api flatten thread categories"""
  435. response = self.patch(
  436. self.api_link, [
  437. {
  438. 'op': 'replace',
  439. 'path': 'flatten-categories',
  440. 'value': None,
  441. },
  442. ]
  443. )
  444. self.assertEqual(response.status_code, 200)
  445. response_json = response.json()
  446. self.assertEqual(response_json['category'], self.category.pk)
  447. class ThreadCloseApiTests(ThreadPatchApiTestCase):
  448. def test_close_thread(self):
  449. """api makes it possible to close thread"""
  450. self.override_acl({'can_close_threads': True})
  451. response = self.patch(
  452. self.api_link, [
  453. {
  454. 'op': 'replace',
  455. 'path': 'is-closed',
  456. 'value': True,
  457. },
  458. ]
  459. )
  460. self.assertEqual(response.status_code, 200)
  461. thread_json = self.get_thread_json()
  462. self.assertTrue(thread_json['is_closed'])
  463. def test_open_thread(self):
  464. """api makes it possible to open thread"""
  465. self.thread.is_closed = True
  466. self.thread.save()
  467. thread_json = self.get_thread_json()
  468. self.assertTrue(thread_json['is_closed'])
  469. self.override_acl({'can_close_threads': True})
  470. response = self.patch(
  471. self.api_link, [
  472. {
  473. 'op': 'replace',
  474. 'path': 'is-closed',
  475. 'value': False,
  476. },
  477. ]
  478. )
  479. self.assertEqual(response.status_code, 200)
  480. thread_json = self.get_thread_json()
  481. self.assertFalse(thread_json['is_closed'])
  482. def test_close_thread_no_permission(self):
  483. """api close thread with no permission fails"""
  484. self.override_acl({'can_close_threads': False})
  485. response = self.patch(
  486. self.api_link, [
  487. {
  488. 'op': 'replace',
  489. 'path': 'is-closed',
  490. 'value': True,
  491. },
  492. ]
  493. )
  494. self.assertEqual(response.status_code, 400)
  495. response_json = response.json()
  496. self.assertEqual(
  497. response_json['detail'][0], "You don't have permission to close this thread."
  498. )
  499. thread_json = self.get_thread_json()
  500. self.assertFalse(thread_json['is_closed'])
  501. def test_open_thread_no_permission(self):
  502. """api open thread with no permission fails"""
  503. self.thread.is_closed = True
  504. self.thread.save()
  505. thread_json = self.get_thread_json()
  506. self.assertTrue(thread_json['is_closed'])
  507. self.override_acl({'can_close_threads': False})
  508. response = self.patch(
  509. self.api_link, [
  510. {
  511. 'op': 'replace',
  512. 'path': 'is-closed',
  513. 'value': False,
  514. },
  515. ]
  516. )
  517. self.assertEqual(response.status_code, 400)
  518. response_json = response.json()
  519. self.assertEqual(
  520. response_json['detail'][0], "You don't have permission to open this thread."
  521. )
  522. thread_json = self.get_thread_json()
  523. self.assertTrue(thread_json['is_closed'])
  524. class ThreadApproveApiTests(ThreadPatchApiTestCase):
  525. def test_approve_thread(self):
  526. """api makes it possible to approve thread"""
  527. self.thread.is_unapproved = True
  528. self.thread.save()
  529. self.override_acl({'can_approve_content': 1})
  530. response = self.patch(
  531. self.api_link, [
  532. {
  533. 'op': 'replace',
  534. 'path': 'is-unapproved',
  535. 'value': False,
  536. },
  537. ]
  538. )
  539. self.assertEqual(response.status_code, 200)
  540. thread_json = self.get_thread_json()
  541. self.assertFalse(thread_json['is_unapproved'])
  542. def test_unapprove_thread(self):
  543. """api returns permission error on approval removal"""
  544. self.override_acl({'can_approve_content': 1})
  545. response = self.patch(
  546. self.api_link, [
  547. {
  548. 'op': 'replace',
  549. 'path': 'is-unapproved',
  550. 'value': True,
  551. },
  552. ]
  553. )
  554. self.assertEqual(response.status_code, 400)
  555. response_json = response.json()
  556. self.assertEqual(response_json['detail'][0], "Content approval can't be reversed.")
  557. class ThreadHideApiTests(ThreadPatchApiTestCase):
  558. def test_hide_thread(self):
  559. """api makes it possible to hide thread"""
  560. self.override_acl({'can_hide_threads': 1})
  561. response = self.patch(
  562. self.api_link, [
  563. {
  564. 'op': 'replace',
  565. 'path': 'is-hidden',
  566. 'value': True,
  567. },
  568. ]
  569. )
  570. self.assertEqual(response.status_code, 200)
  571. self.override_acl({'can_hide_threads': 1})
  572. thread_json = self.get_thread_json()
  573. self.assertTrue(thread_json['is_hidden'])
  574. def test_show_thread(self):
  575. """api makes it possible to unhide thread"""
  576. self.thread.is_hidden = True
  577. self.thread.save()
  578. self.override_acl({'can_hide_threads': 1})
  579. thread_json = self.get_thread_json()
  580. self.assertTrue(thread_json['is_hidden'])
  581. self.override_acl({'can_hide_threads': 1})
  582. response = self.patch(
  583. self.api_link, [
  584. {
  585. 'op': 'replace',
  586. 'path': 'is-hidden',
  587. 'value': False,
  588. },
  589. ]
  590. )
  591. self.assertEqual(response.status_code, 200)
  592. self.override_acl({'can_hide_threads': 1})
  593. thread_json = self.get_thread_json()
  594. self.assertFalse(thread_json['is_hidden'])
  595. def test_hide_thread_no_permission(self):
  596. """api hide thread with no permission fails"""
  597. self.override_acl({'can_hide_threads': 0})
  598. response = self.patch(
  599. self.api_link, [
  600. {
  601. 'op': 'replace',
  602. 'path': 'is-hidden',
  603. 'value': True,
  604. },
  605. ]
  606. )
  607. self.assertEqual(response.status_code, 400)
  608. response_json = response.json()
  609. self.assertEqual(
  610. response_json['detail'][0], "You don't have permission to hide this thread."
  611. )
  612. thread_json = self.get_thread_json()
  613. self.assertFalse(thread_json['is_hidden'])
  614. def test_show_thread_no_permission(self):
  615. """api unhide thread with no permission fails"""
  616. self.thread.is_hidden = True
  617. self.thread.save()
  618. self.override_acl({'can_hide_threads': 1})
  619. thread_json = self.get_thread_json()
  620. self.assertTrue(thread_json['is_hidden'])
  621. self.override_acl({'can_hide_threads': 0})
  622. response = self.patch(
  623. self.api_link, [
  624. {
  625. 'op': 'replace',
  626. 'path': 'is-hidden',
  627. 'value': False,
  628. },
  629. ]
  630. )
  631. self.assertEqual(response.status_code, 404)
  632. class ThreadSubscribeApiTests(ThreadPatchApiTestCase):
  633. def test_subscribe_thread(self):
  634. """api makes it possible to subscribe thread"""
  635. response = self.patch(
  636. self.api_link, [
  637. {
  638. 'op': 'replace',
  639. 'path': 'subscription',
  640. 'value': 'notify',
  641. },
  642. ]
  643. )
  644. self.assertEqual(response.status_code, 200)
  645. thread_json = self.get_thread_json()
  646. self.assertFalse(thread_json['subscription'])
  647. subscription = self.user.subscription_set.get(thread=self.thread)
  648. self.assertFalse(subscription.send_email)
  649. def test_subscribe_thread_with_email(self):
  650. """api makes it possible to subscribe thread with emails"""
  651. response = self.patch(
  652. self.api_link, [
  653. {
  654. 'op': 'replace',
  655. 'path': 'subscription',
  656. 'value': 'email',
  657. },
  658. ]
  659. )
  660. self.assertEqual(response.status_code, 200)
  661. thread_json = self.get_thread_json()
  662. self.assertTrue(thread_json['subscription'])
  663. subscription = self.user.subscription_set.get(thread=self.thread)
  664. self.assertTrue(subscription.send_email)
  665. def test_unsubscribe_thread(self):
  666. """api makes it possible to unsubscribe thread"""
  667. response = self.patch(
  668. self.api_link, [
  669. {
  670. 'op': 'replace',
  671. 'path': 'subscription',
  672. 'value': 'remove',
  673. },
  674. ]
  675. )
  676. self.assertEqual(response.status_code, 200)
  677. thread_json = self.get_thread_json()
  678. self.assertIsNone(thread_json['subscription'])
  679. self.assertEqual(self.user.subscription_set.count(), 0)
  680. def test_subscribe_as_guest(self):
  681. """api makes it impossible to subscribe thread"""
  682. self.logout_user()
  683. response = self.patch(
  684. self.api_link, [
  685. {
  686. 'op': 'replace',
  687. 'path': 'subscription',
  688. 'value': 'email',
  689. },
  690. ]
  691. )
  692. self.assertEqual(response.status_code, 403)
  693. def test_subscribe_nonexistant_thread(self):
  694. """api makes it impossible to subscribe nonexistant thread"""
  695. bad_api_link = self.api_link.replace(
  696. six.text_type(self.thread.pk), six.text_type(self.thread.pk + 9)
  697. )
  698. response = self.patch(
  699. bad_api_link, [
  700. {
  701. 'op': 'replace',
  702. 'path': 'subscription',
  703. 'value': 'email',
  704. },
  705. ]
  706. )
  707. self.assertEqual(response.status_code, 404)