test_thread_patch_api.py 25 KB

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