test_thread_patch_api.py 41 KB

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