test_thread_patch_api.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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. postreads = self.user.postread_set.filter(post__is_event=False).order_by('id')
  479. self.assertEqual(postreads.count(), 1)
  480. postreads.get(category=self.category_b)
  481. def test_move_thread_subscriptions(self):
  482. """api moves thread subscriptions together with thread"""
  483. self.override_acl({'can_move_threads': True})
  484. self.override_other_acl({'can_start_threads': 2})
  485. self.user.subscription_set.create(
  486. thread=self.thread,
  487. category=self.thread.category,
  488. last_read_on=self.thread.last_post_on,
  489. send_email=False,
  490. )
  491. self.assertEqual(self.user.subscription_set.count(), 1)
  492. self.user.subscription_set.get(category=self.category)
  493. response = self.patch(
  494. self.api_link, [
  495. {
  496. 'op': 'replace',
  497. 'path': 'category',
  498. 'value': self.category_b.pk,
  499. },
  500. {
  501. 'op': 'add',
  502. 'path': 'top-category',
  503. 'value': self.category_b.pk,
  504. },
  505. {
  506. 'op': 'replace',
  507. 'path': 'flatten-categories',
  508. 'value': None,
  509. },
  510. ]
  511. )
  512. self.assertEqual(response.status_code, 200)
  513. # thread read was moved to new category
  514. self.assertEqual(self.user.subscription_set.count(), 1)
  515. self.user.subscription_set.get(category=self.category_b)
  516. def test_move_thread_no_permission(self):
  517. """api move thread to other category with no permission fails"""
  518. self.override_acl({'can_move_threads': False})
  519. self.override_other_acl({})
  520. response = self.patch(
  521. self.api_link, [
  522. {
  523. 'op': 'replace',
  524. 'path': 'category',
  525. 'value': self.category_b.pk,
  526. },
  527. ]
  528. )
  529. self.assertEqual(response.status_code, 400)
  530. response_json = response.json()
  531. self.assertEqual(
  532. response_json['detail'][0], "You can't move threads in this category."
  533. )
  534. self.override_other_acl({})
  535. thread_json = self.get_thread_json()
  536. self.assertEqual(thread_json['category']['id'], self.category.pk)
  537. def test_move_thread_closed_category_no_permission(self):
  538. """api move thread from closed category with no permission fails"""
  539. self.override_acl({
  540. 'can_move_threads': True,
  541. 'can_close_threads': False,
  542. })
  543. self.override_other_acl({})
  544. self.category.is_closed = True
  545. self.category.save()
  546. response = self.patch(
  547. self.api_link, [
  548. {
  549. 'op': 'replace',
  550. 'path': 'category',
  551. 'value': self.category_b.pk,
  552. },
  553. ]
  554. )
  555. self.assertEqual(response.status_code, 400)
  556. response_json = response.json()
  557. self.assertEqual(
  558. response_json['detail'][0], "This category is closed. You can't move it's threads."
  559. )
  560. def test_move_closed_thread_no_permission(self):
  561. """api move closed thread with no permission fails"""
  562. self.override_acl({
  563. 'can_move_threads': True,
  564. 'can_close_threads': False,
  565. })
  566. self.override_other_acl({})
  567. self.thread.is_closed = True
  568. self.thread.save()
  569. response = self.patch(
  570. self.api_link, [
  571. {
  572. 'op': 'replace',
  573. 'path': 'category',
  574. 'value': self.category_b.pk,
  575. },
  576. ]
  577. )
  578. self.assertEqual(response.status_code, 400)
  579. response_json = response.json()
  580. self.assertEqual(
  581. response_json['detail'][0], "This thread is closed. You can't move it."
  582. )
  583. def test_move_thread_no_category_access(self):
  584. """api move thread to category with no access fails"""
  585. self.override_acl({'can_move_threads': True})
  586. self.override_other_acl({'can_see': False})
  587. response = self.patch(
  588. self.api_link, [
  589. {
  590. 'op': 'replace',
  591. 'path': 'category',
  592. 'value': self.category_b.pk,
  593. },
  594. ]
  595. )
  596. self.assertEqual(response.status_code, 400)
  597. response_json = response.json()
  598. self.assertEqual(response_json['detail'][0], 'NOT FOUND')
  599. self.override_other_acl({})
  600. thread_json = self.get_thread_json()
  601. self.assertEqual(thread_json['category']['id'], self.category.pk)
  602. def test_move_thread_no_category_browse(self):
  603. """api move thread to category with no browsing access fails"""
  604. self.override_acl({'can_move_threads': True})
  605. self.override_other_acl({'can_browse': False})
  606. response = self.patch(
  607. self.api_link, [
  608. {
  609. 'op': 'replace',
  610. 'path': 'category',
  611. 'value': self.category_b.pk,
  612. },
  613. ]
  614. )
  615. self.assertEqual(response.status_code, 400)
  616. response_json = response.json()
  617. self.assertEqual(
  618. response_json['detail'][0],
  619. 'You don\'t have permission to browse "Category B" contents.'
  620. )
  621. self.override_other_acl({})
  622. thread_json = self.get_thread_json()
  623. self.assertEqual(thread_json['category']['id'], self.category.pk)
  624. def test_move_thread_no_category_start_threads(self):
  625. """api move thread to category with no posting access fails"""
  626. self.override_acl({'can_move_threads': True})
  627. self.override_other_acl({'can_start_threads': False})
  628. response = self.patch(
  629. self.api_link, [
  630. {
  631. 'op': 'replace',
  632. 'path': 'category',
  633. 'value': self.category_b.pk,
  634. },
  635. ]
  636. )
  637. self.assertEqual(response.status_code, 400)
  638. response_json = response.json()
  639. self.assertEqual(
  640. response_json['detail'][0],
  641. "You don't have permission to start new threads in this category."
  642. )
  643. self.override_other_acl({})
  644. thread_json = self.get_thread_json()
  645. self.assertEqual(thread_json['category']['id'], self.category.pk)
  646. def test_move_thread_same_category(self):
  647. """api move thread to category it's already in fails"""
  648. self.override_acl({'can_move_threads': True})
  649. self.override_other_acl({'can_start_threads': 2})
  650. response = self.patch(
  651. self.api_link, [
  652. {
  653. 'op': 'replace',
  654. 'path': 'category',
  655. 'value': self.thread.category_id,
  656. },
  657. ]
  658. )
  659. self.assertEqual(response.status_code, 400)
  660. response_json = response.json()
  661. self.assertEqual(
  662. response_json['detail'][0], "You can't move thread to the category it's already in."
  663. )
  664. self.override_other_acl({})
  665. thread_json = self.get_thread_json()
  666. self.assertEqual(thread_json['category']['id'], self.category.pk)
  667. def test_thread_flatten_categories(self):
  668. """api flatten thread categories"""
  669. response = self.patch(
  670. self.api_link, [
  671. {
  672. 'op': 'replace',
  673. 'path': 'flatten-categories',
  674. 'value': None,
  675. },
  676. ]
  677. )
  678. self.assertEqual(response.status_code, 200)
  679. response_json = response.json()
  680. self.assertEqual(response_json['category'], self.category.pk)
  681. class ThreadCloseApiTests(ThreadPatchApiTestCase):
  682. def test_close_thread(self):
  683. """api makes it possible to close thread"""
  684. self.override_acl({'can_close_threads': True})
  685. response = self.patch(
  686. self.api_link, [
  687. {
  688. 'op': 'replace',
  689. 'path': 'is-closed',
  690. 'value': True,
  691. },
  692. ]
  693. )
  694. self.assertEqual(response.status_code, 200)
  695. response_json = response.json()
  696. self.assertTrue(response_json['is_closed'])
  697. thread_json = self.get_thread_json()
  698. self.assertTrue(thread_json['is_closed'])
  699. def test_open_thread(self):
  700. """api makes it possible to open thread"""
  701. self.thread.is_closed = True
  702. self.thread.save()
  703. thread_json = self.get_thread_json()
  704. self.assertTrue(thread_json['is_closed'])
  705. self.override_acl({'can_close_threads': True})
  706. response = self.patch(
  707. self.api_link, [
  708. {
  709. 'op': 'replace',
  710. 'path': 'is-closed',
  711. 'value': False,
  712. },
  713. ]
  714. )
  715. self.assertEqual(response.status_code, 200)
  716. response_json = response.json()
  717. self.assertFalse(response_json['is_closed'])
  718. thread_json = self.get_thread_json()
  719. self.assertFalse(thread_json['is_closed'])
  720. def test_close_thread_no_permission(self):
  721. """api close thread with no permission fails"""
  722. self.override_acl({'can_close_threads': False})
  723. response = self.patch(
  724. self.api_link, [
  725. {
  726. 'op': 'replace',
  727. 'path': 'is-closed',
  728. 'value': True,
  729. },
  730. ]
  731. )
  732. self.assertEqual(response.status_code, 400)
  733. response_json = response.json()
  734. self.assertEqual(
  735. response_json['detail'][0], "You don't have permission to close this thread."
  736. )
  737. thread_json = self.get_thread_json()
  738. self.assertFalse(thread_json['is_closed'])
  739. def test_open_thread_no_permission(self):
  740. """api open thread with no permission fails"""
  741. self.thread.is_closed = True
  742. self.thread.save()
  743. thread_json = self.get_thread_json()
  744. self.assertTrue(thread_json['is_closed'])
  745. self.override_acl({'can_close_threads': False})
  746. response = self.patch(
  747. self.api_link, [
  748. {
  749. 'op': 'replace',
  750. 'path': 'is-closed',
  751. 'value': False,
  752. },
  753. ]
  754. )
  755. self.assertEqual(response.status_code, 400)
  756. response_json = response.json()
  757. self.assertEqual(
  758. response_json['detail'][0], "You don't have permission to open this thread."
  759. )
  760. thread_json = self.get_thread_json()
  761. self.assertTrue(thread_json['is_closed'])
  762. class ThreadApproveApiTests(ThreadPatchApiTestCase):
  763. def test_approve_thread(self):
  764. """api makes it possible to approve thread"""
  765. self.thread.first_post.is_unapproved = True
  766. self.thread.first_post.save()
  767. self.thread.synchronize()
  768. self.thread.save()
  769. self.assertTrue(self.thread.is_unapproved)
  770. self.assertTrue(self.thread.has_unapproved_posts)
  771. self.override_acl({'can_approve_content': 1})
  772. response = self.patch(
  773. self.api_link, [
  774. {
  775. 'op': 'replace',
  776. 'path': 'is-unapproved',
  777. 'value': False,
  778. },
  779. ]
  780. )
  781. self.assertEqual(response.status_code, 200)
  782. response_json = response.json()
  783. self.assertFalse(response_json['is_unapproved'])
  784. self.assertFalse(response_json['has_unapproved_posts'])
  785. thread_json = self.get_thread_json()
  786. self.assertFalse(thread_json['is_unapproved'])
  787. self.assertFalse(thread_json['has_unapproved_posts'])
  788. thread = Thread.objects.get(pk=self.thread.pk)
  789. self.assertFalse(thread.is_unapproved)
  790. self.assertFalse(thread.has_unapproved_posts)
  791. def test_approve_thread_category_closed_no_permission(self):
  792. """api checks permission for approving threads in closed categories"""
  793. self.thread.first_post.is_unapproved = True
  794. self.thread.first_post.save()
  795. self.thread.synchronize()
  796. self.thread.save()
  797. self.assertTrue(self.thread.is_unapproved)
  798. self.assertTrue(self.thread.has_unapproved_posts)
  799. self.category.is_closed = True
  800. self.category.save()
  801. self.override_acl({
  802. 'can_approve_content': 1,
  803. 'can_close_threads': 0,
  804. })
  805. response = self.patch(
  806. self.api_link, [
  807. {
  808. 'op': 'replace',
  809. 'path': 'is-unapproved',
  810. 'value': False,
  811. },
  812. ]
  813. )
  814. self.assertEqual(response.status_code, 400)
  815. response_json = response.json()
  816. self.assertEqual(response_json['detail'][0], "This category is closed. You can't approve threads in it.")
  817. def test_approve_thread_closed_no_permission(self):
  818. """api checks permission for approving posts in closed categories"""
  819. self.thread.first_post.is_unapproved = True
  820. self.thread.first_post.save()
  821. self.thread.synchronize()
  822. self.thread.save()
  823. self.assertTrue(self.thread.is_unapproved)
  824. self.assertTrue(self.thread.has_unapproved_posts)
  825. self.thread.is_closed = True
  826. self.thread.save()
  827. self.override_acl({
  828. 'can_approve_content': 1,
  829. 'can_close_threads': 0,
  830. })
  831. response = self.patch(
  832. self.api_link, [
  833. {
  834. 'op': 'replace',
  835. 'path': 'is-unapproved',
  836. 'value': False,
  837. },
  838. ]
  839. )
  840. self.assertEqual(response.status_code, 400)
  841. response_json = response.json()
  842. self.assertEqual(response_json['detail'][0], "This thread is closed. You can't approve it.")
  843. def test_unapprove_thread(self):
  844. """api returns permission error on approval removal"""
  845. self.override_acl({'can_approve_content': 1})
  846. response = self.patch(
  847. self.api_link, [
  848. {
  849. 'op': 'replace',
  850. 'path': 'is-unapproved',
  851. 'value': True,
  852. },
  853. ]
  854. )
  855. self.assertEqual(response.status_code, 400)
  856. response_json = response.json()
  857. self.assertEqual(response_json['detail'][0], "Content approval can't be reversed.")
  858. class ThreadHideApiTests(ThreadPatchApiTestCase):
  859. def test_hide_thread(self):
  860. """api makes it possible to hide thread"""
  861. self.override_acl({'can_hide_threads': 1})
  862. response = self.patch(
  863. self.api_link, [
  864. {
  865. 'op': 'replace',
  866. 'path': 'is-hidden',
  867. 'value': True,
  868. },
  869. ]
  870. )
  871. self.assertEqual(response.status_code, 200)
  872. reponse_json = response.json()
  873. self.assertTrue(reponse_json['is_hidden'])
  874. self.override_acl({'can_hide_threads': 1})
  875. thread_json = self.get_thread_json()
  876. self.assertTrue(thread_json['is_hidden'])
  877. def test_hide_thread_no_permission(self):
  878. """api hide thread with no permission fails"""
  879. self.override_acl({'can_hide_threads': 0})
  880. response = self.patch(
  881. self.api_link, [
  882. {
  883. 'op': 'replace',
  884. 'path': 'is-hidden',
  885. 'value': True,
  886. },
  887. ]
  888. )
  889. self.assertEqual(response.status_code, 400)
  890. response_json = response.json()
  891. self.assertEqual(
  892. response_json['detail'][0], "You can't hide threads in this category."
  893. )
  894. thread_json = self.get_thread_json()
  895. self.assertFalse(thread_json['is_hidden'])
  896. def test_hide_non_owned_thread(self):
  897. """api forbids non-moderator from hiding other users threads"""
  898. self.override_acl({
  899. 'can_hide_own_threads': 1,
  900. 'can_hide_threads': 0
  901. })
  902. response = self.patch(
  903. self.api_link, [
  904. {
  905. 'op': 'replace',
  906. 'path': 'is-hidden',
  907. 'value': True,
  908. },
  909. ]
  910. )
  911. self.assertEqual(response.status_code, 400)
  912. response_json = response.json()
  913. self.assertEqual(
  914. response_json['detail'][0], "You can't hide other users theads in this category."
  915. )
  916. def test_hide_owned_thread_no_time(self):
  917. """api forbids non-moderator from hiding other users threads"""
  918. self.override_acl({
  919. 'can_hide_own_threads': 1,
  920. 'can_hide_threads': 0,
  921. 'thread_edit_time': 1,
  922. })
  923. self.thread.starter = self.user
  924. self.thread.started_on = timezone.now() - timedelta(minutes=5)
  925. self.thread.save()
  926. response = self.patch(
  927. self.api_link, [
  928. {
  929. 'op': 'replace',
  930. 'path': 'is-hidden',
  931. 'value': True,
  932. },
  933. ]
  934. )
  935. self.assertEqual(response.status_code, 400)
  936. response_json = response.json()
  937. self.assertEqual(
  938. response_json['detail'][0], "You can't hide threads that are older than 1 minute."
  939. )
  940. def test_hide_closed_category_no_permission(self):
  941. """api test permission to hide thread in closed category"""
  942. self.override_acl({
  943. 'can_hide_threads': 1,
  944. 'can_close_threads': 0
  945. })
  946. self.category.is_closed = True
  947. self.category.save()
  948. response = self.patch(
  949. self.api_link, [
  950. {
  951. 'op': 'replace',
  952. 'path': 'is-hidden',
  953. 'value': True,
  954. },
  955. ]
  956. )
  957. self.assertEqual(response.status_code, 400)
  958. response_json = response.json()
  959. self.assertEqual(
  960. response_json['detail'][0], "This category is closed. You can't hide threads in it."
  961. )
  962. def test_hide_closed_thread_no_permission(self):
  963. """api test permission to hide closed thread"""
  964. self.override_acl({
  965. 'can_hide_threads': 1,
  966. 'can_close_threads': 0
  967. })
  968. self.thread.is_closed = True
  969. self.thread.save()
  970. response = self.patch(
  971. self.api_link, [
  972. {
  973. 'op': 'replace',
  974. 'path': 'is-hidden',
  975. 'value': True,
  976. },
  977. ]
  978. )
  979. self.assertEqual(response.status_code, 400)
  980. response_json = response.json()
  981. self.assertEqual(
  982. response_json['detail'][0], "This thread is closed. You can't hide it."
  983. )
  984. class ThreadUnhideApiTests(ThreadPatchApiTestCase):
  985. def setUp(self):
  986. super(ThreadUnhideApiTests, self).setUp()
  987. self.thread.is_hidden = True
  988. self.thread.save()
  989. def test_unhide_thread(self):
  990. """api makes it possible to unhide thread"""
  991. self.override_acl({'can_hide_threads': 1})
  992. response = self.patch(
  993. self.api_link, [
  994. {
  995. 'op': 'replace',
  996. 'path': 'is-hidden',
  997. 'value': False,
  998. },
  999. ]
  1000. )
  1001. self.assertEqual(response.status_code, 200)
  1002. reponse_json = response.json()
  1003. self.assertFalse(reponse_json['is_hidden'])
  1004. self.override_acl({'can_hide_threads': 1})
  1005. thread_json = self.get_thread_json()
  1006. self.assertFalse(thread_json['is_hidden'])
  1007. def test_unhide_thread_no_permission(self):
  1008. """api unhide thread with no permission fails as thread is invisible"""
  1009. self.override_acl({'can_hide_threads': 0})
  1010. response = self.patch(
  1011. self.api_link, [
  1012. {
  1013. 'op': 'replace',
  1014. 'path': 'is-hidden',
  1015. 'value': True,
  1016. },
  1017. ]
  1018. )
  1019. self.assertEqual(response.status_code, 404)
  1020. def test_unhide_closed_category_no_permission(self):
  1021. """api test permission to unhide thread in closed category"""
  1022. self.override_acl({
  1023. 'can_hide_threads': 1,
  1024. 'can_close_threads': 0
  1025. })
  1026. self.category.is_closed = True
  1027. self.category.save()
  1028. response = self.patch(
  1029. self.api_link, [
  1030. {
  1031. 'op': 'replace',
  1032. 'path': 'is-hidden',
  1033. 'value': False,
  1034. },
  1035. ]
  1036. )
  1037. self.assertEqual(response.status_code, 400)
  1038. response_json = response.json()
  1039. self.assertEqual(
  1040. response_json['detail'][0], "This category is closed. You can't reveal threads in it."
  1041. )
  1042. def test_unhide_closed_thread_no_permission(self):
  1043. """api test permission to unhide closed thread"""
  1044. self.override_acl({
  1045. 'can_hide_threads': 1,
  1046. 'can_close_threads': 0
  1047. })
  1048. self.thread.is_closed = True
  1049. self.thread.save()
  1050. response = self.patch(
  1051. self.api_link, [
  1052. {
  1053. 'op': 'replace',
  1054. 'path': 'is-hidden',
  1055. 'value': False,
  1056. },
  1057. ]
  1058. )
  1059. self.assertEqual(response.status_code, 400)
  1060. response_json = response.json()
  1061. self.assertEqual(
  1062. response_json['detail'][0], "This thread is closed. You can't reveal it."
  1063. )
  1064. class ThreadSubscribeApiTests(ThreadPatchApiTestCase):
  1065. def test_subscribe_thread(self):
  1066. """api makes it possible to subscribe thread"""
  1067. response = self.patch(
  1068. self.api_link, [
  1069. {
  1070. 'op': 'replace',
  1071. 'path': 'subscription',
  1072. 'value': 'notify',
  1073. },
  1074. ]
  1075. )
  1076. self.assertEqual(response.status_code, 200)
  1077. reponse_json = response.json()
  1078. self.assertFalse(reponse_json['subscription'])
  1079. thread_json = self.get_thread_json()
  1080. self.assertFalse(thread_json['subscription'])
  1081. subscription = self.user.subscription_set.get(thread=self.thread)
  1082. self.assertFalse(subscription.send_email)
  1083. def test_subscribe_thread_with_email(self):
  1084. """api makes it possible to subscribe thread with emails"""
  1085. response = self.patch(
  1086. self.api_link, [
  1087. {
  1088. 'op': 'replace',
  1089. 'path': 'subscription',
  1090. 'value': 'email',
  1091. },
  1092. ]
  1093. )
  1094. self.assertEqual(response.status_code, 200)
  1095. reponse_json = response.json()
  1096. self.assertTrue(reponse_json['subscription'])
  1097. thread_json = self.get_thread_json()
  1098. self.assertTrue(thread_json['subscription'])
  1099. subscription = self.user.subscription_set.get(thread=self.thread)
  1100. self.assertTrue(subscription.send_email)
  1101. def test_unsubscribe_thread(self):
  1102. """api makes it possible to unsubscribe thread"""
  1103. response = self.patch(
  1104. self.api_link, [
  1105. {
  1106. 'op': 'replace',
  1107. 'path': 'subscription',
  1108. 'value': 'remove',
  1109. },
  1110. ]
  1111. )
  1112. self.assertEqual(response.status_code, 200)
  1113. reponse_json = response.json()
  1114. self.assertIsNone(reponse_json['subscription'])
  1115. thread_json = self.get_thread_json()
  1116. self.assertIsNone(thread_json['subscription'])
  1117. self.assertEqual(self.user.subscription_set.count(), 0)
  1118. def test_subscribe_as_guest(self):
  1119. """api makes it impossible to subscribe thread"""
  1120. self.logout_user()
  1121. response = self.patch(
  1122. self.api_link, [
  1123. {
  1124. 'op': 'replace',
  1125. 'path': 'subscription',
  1126. 'value': 'email',
  1127. },
  1128. ]
  1129. )
  1130. self.assertEqual(response.status_code, 403)
  1131. def test_subscribe_nonexistant_thread(self):
  1132. """api makes it impossible to subscribe nonexistant thread"""
  1133. bad_api_link = self.api_link.replace(
  1134. six.text_type(self.thread.pk), six.text_type(self.thread.pk + 9)
  1135. )
  1136. response = self.patch(
  1137. bad_api_link, [
  1138. {
  1139. 'op': 'replace',
  1140. 'path': 'subscription',
  1141. 'value': 'email',
  1142. },
  1143. ]
  1144. )
  1145. self.assertEqual(response.status_code, 404)