test_thread_patch_api.py 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529
  1. import json
  2. from datetime import timedelta
  3. from django.utils import 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 import testutils
  8. from misago.threads.models import Thread
  9. from .test_threads_api import ThreadsApiTestCase
  10. class ThreadPatchApiTestCase(ThreadsApiTestCase):
  11. def patch(self, api_link, ops):
  12. return self.client.patch(api_link, json.dumps(ops), content_type="application/json")
  13. class ThreadAddAclApiTests(ThreadPatchApiTestCase):
  14. def test_add_acl_true(self):
  15. """api adds current thread's acl to response"""
  16. response = self.patch(self.api_link, [
  17. {
  18. 'op': 'add',
  19. 'path': 'acl',
  20. 'value': True,
  21. },
  22. ])
  23. self.assertEqual(response.status_code, 200)
  24. response_json = response.json()
  25. self.assertTrue(response_json['acl'])
  26. def test_add_acl_false(self):
  27. """if value is false, api won't add acl to the response, but will set empty key"""
  28. response = self.patch(self.api_link, [
  29. {
  30. 'op': 'add',
  31. 'path': 'acl',
  32. 'value': False,
  33. },
  34. ])
  35. self.assertEqual(response.status_code, 200)
  36. response_json = response.json()
  37. self.assertIsNone(response_json['acl'])
  38. class ThreadChangeTitleApiTests(ThreadPatchApiTestCase):
  39. def test_change_thread_title(self):
  40. """api makes it possible to change thread title"""
  41. self.override_acl({'can_edit_threads': 2})
  42. response = self.patch(
  43. self.api_link, [
  44. {
  45. 'op': 'replace',
  46. 'path': 'title',
  47. 'value': "Lorem ipsum change!",
  48. },
  49. ]
  50. )
  51. self.assertEqual(response.status_code, 200)
  52. response_json = response.json()
  53. self.assertEqual(response_json['title'], "Lorem ipsum change!")
  54. thread_json = self.get_thread_json()
  55. self.assertEqual(thread_json['title'], "Lorem ipsum change!")
  56. def test_change_thread_title_no_permission(self):
  57. """api validates permission to change title"""
  58. self.override_acl({'can_edit_threads': 0})
  59. response = self.patch(
  60. self.api_link, [
  61. {
  62. 'op': 'replace',
  63. 'path': 'title',
  64. 'value': "Lorem ipsum change!",
  65. },
  66. ]
  67. )
  68. self.assertEqual(response.status_code, 400)
  69. response_json = response.json()
  70. self.assertEqual(response_json['detail'][0], "You can't edit threads in this category.")
  71. def test_change_thread_title_closed_category_no_permission(self):
  72. """api test permission to edit thread title in closed category"""
  73. self.override_acl({
  74. 'can_edit_threads': 2,
  75. 'can_close_threads': 0
  76. })
  77. self.category.is_closed = True
  78. self.category.save()
  79. response = self.patch(
  80. self.api_link, [
  81. {
  82. 'op': 'replace',
  83. 'path': 'title',
  84. 'value': "Lorem ipsum change!",
  85. },
  86. ]
  87. )
  88. self.assertEqual(response.status_code, 400)
  89. response_json = response.json()
  90. self.assertEqual(
  91. response_json['detail'][0], "This category is closed. You can't edit threads in it."
  92. )
  93. def test_change_thread_title_closed_thread_no_permission(self):
  94. """api test permission to edit closed thread title"""
  95. self.override_acl({
  96. 'can_edit_threads': 2,
  97. 'can_close_threads': 0
  98. })
  99. self.thread.is_closed = True
  100. self.thread.save()
  101. response = self.patch(
  102. self.api_link, [
  103. {
  104. 'op': 'replace',
  105. 'path': 'title',
  106. 'value': "Lorem ipsum change!",
  107. },
  108. ]
  109. )
  110. self.assertEqual(response.status_code, 400)
  111. response_json = response.json()
  112. self.assertEqual(
  113. response_json['detail'][0], "This thread is closed. You can't edit it."
  114. )
  115. def test_change_thread_title_after_edit_time(self):
  116. """api cleans, validates and rejects too short title"""
  117. self.override_acl({'thread_edit_time': 1, 'can_edit_threads': 1})
  118. self.thread.started_on = timezone.now() - timedelta(minutes=10)
  119. self.thread.starter = self.user
  120. self.thread.save()
  121. response = self.patch(
  122. self.api_link, [
  123. {
  124. 'op': 'replace',
  125. 'path': 'title',
  126. 'value': "Lorem ipsum change!",
  127. },
  128. ]
  129. )
  130. self.assertEqual(response.status_code, 400)
  131. response_json = response.json()
  132. self.assertEqual(
  133. response_json['detail'][0], "You can't edit threads that are older than 1 minute."
  134. )
  135. def test_change_thread_title_invalid(self):
  136. """api cleans, validates and rejects too short title"""
  137. self.override_acl({'can_edit_threads': 2})
  138. response = self.patch(
  139. self.api_link, [
  140. {
  141. 'op': 'replace',
  142. 'path': 'title',
  143. 'value': 12,
  144. },
  145. ]
  146. )
  147. self.assertEqual(response.status_code, 400)
  148. response_json = response.json()
  149. self.assertEqual(
  150. response_json['detail'][0],
  151. "Thread title should be at least 5 characters long (it has 2)."
  152. )
  153. class ThreadPinGloballyApiTests(ThreadPatchApiTestCase):
  154. def test_pin_thread(self):
  155. """api makes it possible to pin globally thread"""
  156. self.override_acl({'can_pin_threads': 2})
  157. response = self.patch(
  158. self.api_link, [
  159. {
  160. 'op': 'replace',
  161. 'path': 'weight',
  162. 'value': 2,
  163. },
  164. ]
  165. )
  166. self.assertEqual(response.status_code, 200)
  167. response_json = response.json()
  168. self.assertEqual(response_json['weight'], 2)
  169. thread_json = self.get_thread_json()
  170. self.assertEqual(thread_json['weight'], 2)
  171. def test_pin_thread_closed_category_no_permission(self):
  172. """api checks if category is closed"""
  173. self.override_acl({
  174. 'can_pin_threads': 2,
  175. 'can_close_threads': 0,
  176. })
  177. self.category.is_closed = True
  178. self.category.save()
  179. response = self.patch(
  180. self.api_link, [
  181. {
  182. 'op': 'replace',
  183. 'path': 'weight',
  184. 'value': 2,
  185. },
  186. ]
  187. )
  188. self.assertEqual(response.status_code, 400)
  189. response_json = response.json()
  190. self.assertEqual(
  191. response_json['detail'][0], "This category is closed. You can't change threads weights in it."
  192. )
  193. def test_pin_thread_closed_no_permission(self):
  194. """api checks if thread is closed"""
  195. self.override_acl({
  196. 'can_pin_threads': 2,
  197. 'can_close_threads': 0,
  198. })
  199. self.thread.is_closed = True
  200. self.thread.save()
  201. response = self.patch(
  202. self.api_link, [
  203. {
  204. 'op': 'replace',
  205. 'path': 'weight',
  206. 'value': 2,
  207. },
  208. ]
  209. )
  210. self.assertEqual(response.status_code, 400)
  211. response_json = response.json()
  212. self.assertEqual(
  213. response_json['detail'][0], "This thread is closed. You can't change its weight."
  214. )
  215. def test_unpin_thread(self):
  216. """api makes it possible to unpin thread"""
  217. self.thread.weight = 2
  218. self.thread.save()
  219. thread_json = self.get_thread_json()
  220. self.assertEqual(thread_json['weight'], 2)
  221. self.override_acl({'can_pin_threads': 2})
  222. response = self.patch(
  223. self.api_link, [
  224. {
  225. 'op': 'replace',
  226. 'path': 'weight',
  227. 'value': 0,
  228. },
  229. ]
  230. )
  231. self.assertEqual(response.status_code, 200)
  232. response_json = response.json()
  233. self.assertEqual(response_json['weight'], 0)
  234. thread_json = self.get_thread_json()
  235. self.assertEqual(thread_json['weight'], 0)
  236. def test_pin_thread_no_permission(self):
  237. """api pin thread globally with no permission fails"""
  238. self.override_acl({'can_pin_threads': 1})
  239. response = self.patch(
  240. self.api_link, [
  241. {
  242. 'op': 'replace',
  243. 'path': 'weight',
  244. 'value': 2,
  245. },
  246. ]
  247. )
  248. self.assertEqual(response.status_code, 400)
  249. response_json = response.json()
  250. self.assertEqual(
  251. response_json['detail'][0], "You can't pin threads globally in this category."
  252. )
  253. thread_json = self.get_thread_json()
  254. self.assertEqual(thread_json['weight'], 0)
  255. def test_unpin_thread_no_permission(self):
  256. """api unpin thread with no permission fails"""
  257. self.thread.weight = 2
  258. self.thread.save()
  259. thread_json = self.get_thread_json()
  260. self.assertEqual(thread_json['weight'], 2)
  261. self.override_acl({'can_pin_threads': 1})
  262. response = self.patch(
  263. self.api_link, [
  264. {
  265. 'op': 'replace',
  266. 'path': 'weight',
  267. 'value': 1,
  268. },
  269. ]
  270. )
  271. self.assertEqual(response.status_code, 400)
  272. response_json = response.json()
  273. self.assertEqual(
  274. response_json['detail'][0], "You can't change globally pinned threads weights in this category."
  275. )
  276. thread_json = self.get_thread_json()
  277. self.assertEqual(thread_json['weight'], 2)
  278. class ThreadPinLocallyApiTests(ThreadPatchApiTestCase):
  279. def test_pin_thread(self):
  280. """api makes it possible to pin locally thread"""
  281. self.override_acl({'can_pin_threads': 1})
  282. response = self.patch(
  283. self.api_link, [
  284. {
  285. 'op': 'replace',
  286. 'path': 'weight',
  287. 'value': 1,
  288. },
  289. ]
  290. )
  291. self.assertEqual(response.status_code, 200)
  292. response_json = response.json()
  293. self.assertEqual(response_json['weight'], 1)
  294. thread_json = self.get_thread_json()
  295. self.assertEqual(thread_json['weight'], 1)
  296. def test_unpin_thread(self):
  297. """api makes it possible to unpin thread"""
  298. self.thread.weight = 1
  299. self.thread.save()
  300. thread_json = self.get_thread_json()
  301. self.assertEqual(thread_json['weight'], 1)
  302. self.override_acl({'can_pin_threads': 1})
  303. response = self.patch(
  304. self.api_link, [
  305. {
  306. 'op': 'replace',
  307. 'path': 'weight',
  308. 'value': 0,
  309. },
  310. ]
  311. )
  312. self.assertEqual(response.status_code, 200)
  313. response_json = response.json()
  314. self.assertEqual(response_json['weight'], 0)
  315. thread_json = self.get_thread_json()
  316. self.assertEqual(thread_json['weight'], 0)
  317. def test_pin_thread_no_permission(self):
  318. """api pin thread locally with no permission fails"""
  319. self.override_acl({'can_pin_threads': 0})
  320. response = self.patch(
  321. self.api_link, [
  322. {
  323. 'op': 'replace',
  324. 'path': 'weight',
  325. 'value': 1,
  326. },
  327. ]
  328. )
  329. self.assertEqual(response.status_code, 400)
  330. response_json = response.json()
  331. self.assertEqual(
  332. response_json['detail'][0], "You can't change threads weights in this category."
  333. )
  334. thread_json = self.get_thread_json()
  335. self.assertEqual(thread_json['weight'], 0)
  336. def test_unpin_thread_no_permission(self):
  337. """api unpin thread with no permission fails"""
  338. self.thread.weight = 1
  339. self.thread.save()
  340. thread_json = self.get_thread_json()
  341. self.assertEqual(thread_json['weight'], 1)
  342. self.override_acl({'can_pin_threads': 0})
  343. response = self.patch(
  344. self.api_link, [
  345. {
  346. 'op': 'replace',
  347. 'path': 'weight',
  348. 'value': 0,
  349. },
  350. ]
  351. )
  352. self.assertEqual(response.status_code, 400)
  353. response_json = response.json()
  354. self.assertEqual(
  355. response_json['detail'][0], "You can't change threads weights in this category."
  356. )
  357. thread_json = self.get_thread_json()
  358. self.assertEqual(thread_json['weight'], 1)
  359. class ThreadMoveApiTests(ThreadPatchApiTestCase):
  360. def setUp(self):
  361. super().setUp()
  362. Category(
  363. name='Category B',
  364. slug='category-b',
  365. ).insert_at(
  366. self.category,
  367. position='last-child',
  368. save=True,
  369. )
  370. self.category_b = Category.objects.get(slug='category-b')
  371. def override_other_acl(self, acl):
  372. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  373. other_category_acl.update({
  374. 'can_see': 1,
  375. 'can_browse': 1,
  376. 'can_see_all_threads': 1,
  377. 'can_see_own_threads': 0,
  378. 'can_hide_threads': 0,
  379. 'can_approve_content': 0,
  380. })
  381. other_category_acl.update(acl)
  382. categories_acl = self.user.acl_cache['categories']
  383. categories_acl[self.category_b.pk] = other_category_acl
  384. visible_categories = [self.category.pk]
  385. if other_category_acl['can_see']:
  386. visible_categories.append(self.category_b.pk)
  387. override_acl(
  388. self.user, {
  389. 'visible_categories': visible_categories,
  390. 'categories': categories_acl,
  391. }
  392. )
  393. def test_move_thread_no_top(self):
  394. """api moves thread to other category, sets no top category"""
  395. self.override_acl({'can_move_threads': True})
  396. self.override_other_acl({'can_start_threads': 2})
  397. response = self.patch(
  398. self.api_link, [
  399. {
  400. 'op': 'replace',
  401. 'path': 'category',
  402. 'value': self.category_b.pk,
  403. },
  404. {
  405. 'op': 'add',
  406. 'path': 'top-category',
  407. 'value': self.category_b.pk,
  408. },
  409. {
  410. 'op': 'replace',
  411. 'path': 'flatten-categories',
  412. 'value': None,
  413. },
  414. ]
  415. )
  416. self.assertEqual(response.status_code, 200)
  417. reponse_json = response.json()
  418. self.assertEqual(reponse_json['category'], self.category_b.pk)
  419. self.override_other_acl({})
  420. thread_json = self.get_thread_json()
  421. self.assertEqual(thread_json['category']['id'], self.category_b.pk)
  422. def test_move_thread_with_top(self):
  423. """api moves thread to other category, sets top"""
  424. self.override_acl({'can_move_threads': True})
  425. self.override_other_acl({'can_start_threads': 2})
  426. response = self.patch(
  427. self.api_link, [
  428. {
  429. 'op': 'replace',
  430. 'path': 'category',
  431. 'value': self.category_b.pk,
  432. },
  433. {
  434. 'op': 'add',
  435. 'path': 'top-category',
  436. 'value': Category.objects.root_category().pk,
  437. },
  438. {
  439. 'op': 'replace',
  440. 'path': 'flatten-categories',
  441. 'value': None,
  442. },
  443. ]
  444. )
  445. self.assertEqual(response.status_code, 200)
  446. reponse_json = response.json()
  447. self.assertEqual(reponse_json['category'], self.category_b.pk)
  448. self.override_other_acl({})
  449. thread_json = self.get_thread_json()
  450. self.assertEqual(thread_json['category']['id'], self.category_b.pk)
  451. def test_move_thread_reads(self):
  452. """api moves thread reads together with thread"""
  453. self.override_acl({'can_move_threads': True})
  454. self.override_other_acl({'can_start_threads': 2})
  455. poststracker.save_read(self.user, self.thread.first_post)
  456. self.assertEqual(self.user.postread_set.count(), 1)
  457. self.user.postread_set.get(category=self.category)
  458. response = self.patch(
  459. self.api_link, [
  460. {
  461. 'op': 'replace',
  462. 'path': 'category',
  463. 'value': self.category_b.pk,
  464. },
  465. {
  466. 'op': 'add',
  467. 'path': 'top-category',
  468. 'value': self.category_b.pk,
  469. },
  470. {
  471. 'op': 'replace',
  472. 'path': 'flatten-categories',
  473. 'value': None,
  474. },
  475. ]
  476. )
  477. self.assertEqual(response.status_code, 200)
  478. # thread read was moved to new category
  479. postreads = self.user.postread_set.filter(post__is_event=False).order_by('id')
  480. self.assertEqual(postreads.count(), 1)
  481. postreads.get(category=self.category_b)
  482. def test_move_thread_subscriptions(self):
  483. """api moves thread subscriptions together with thread"""
  484. self.override_acl({'can_move_threads': True})
  485. self.override_other_acl({'can_start_threads': 2})
  486. self.user.subscription_set.create(
  487. thread=self.thread,
  488. category=self.thread.category,
  489. last_read_on=self.thread.last_post_on,
  490. send_email=False,
  491. )
  492. self.assertEqual(self.user.subscription_set.count(), 1)
  493. self.user.subscription_set.get(category=self.category)
  494. response = self.patch(
  495. self.api_link, [
  496. {
  497. 'op': 'replace',
  498. 'path': 'category',
  499. 'value': self.category_b.pk,
  500. },
  501. {
  502. 'op': 'add',
  503. 'path': 'top-category',
  504. 'value': self.category_b.pk,
  505. },
  506. {
  507. 'op': 'replace',
  508. 'path': 'flatten-categories',
  509. 'value': None,
  510. },
  511. ]
  512. )
  513. self.assertEqual(response.status_code, 200)
  514. # thread read was moved to new category
  515. self.assertEqual(self.user.subscription_set.count(), 1)
  516. self.user.subscription_set.get(category=self.category_b)
  517. def test_move_thread_no_permission(self):
  518. """api move thread to other category with no permission fails"""
  519. self.override_acl({'can_move_threads': False})
  520. self.override_other_acl({})
  521. response = self.patch(
  522. self.api_link, [
  523. {
  524. 'op': 'replace',
  525. 'path': 'category',
  526. 'value': self.category_b.pk,
  527. },
  528. ]
  529. )
  530. self.assertEqual(response.status_code, 400)
  531. response_json = response.json()
  532. self.assertEqual(
  533. response_json['detail'][0], "You can't move threads in this category."
  534. )
  535. self.override_other_acl({})
  536. thread_json = self.get_thread_json()
  537. self.assertEqual(thread_json['category']['id'], self.category.pk)
  538. def test_move_thread_closed_category_no_permission(self):
  539. """api move thread from closed category with no permission fails"""
  540. self.override_acl({
  541. 'can_move_threads': True,
  542. 'can_close_threads': False,
  543. })
  544. self.override_other_acl({})
  545. self.category.is_closed = True
  546. self.category.save()
  547. response = self.patch(
  548. self.api_link, [
  549. {
  550. 'op': 'replace',
  551. 'path': 'category',
  552. 'value': self.category_b.pk,
  553. },
  554. ]
  555. )
  556. self.assertEqual(response.status_code, 400)
  557. response_json = response.json()
  558. self.assertEqual(
  559. response_json['detail'][0], "This category is closed. You can't move it's threads."
  560. )
  561. def test_move_closed_thread_no_permission(self):
  562. """api move closed thread with no permission fails"""
  563. self.override_acl({
  564. 'can_move_threads': True,
  565. 'can_close_threads': False,
  566. })
  567. self.override_other_acl({})
  568. self.thread.is_closed = True
  569. self.thread.save()
  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], "This thread is closed. You can't move it."
  583. )
  584. def test_move_thread_no_category_access(self):
  585. """api move thread to category with no access fails"""
  586. self.override_acl({'can_move_threads': True})
  587. self.override_other_acl({'can_see': False})
  588. response = self.patch(
  589. self.api_link, [
  590. {
  591. 'op': 'replace',
  592. 'path': 'category',
  593. 'value': self.category_b.pk,
  594. },
  595. ]
  596. )
  597. self.assertEqual(response.status_code, 400)
  598. response_json = response.json()
  599. self.assertEqual(response_json['detail'][0], 'NOT FOUND')
  600. self.override_other_acl({})
  601. thread_json = self.get_thread_json()
  602. self.assertEqual(thread_json['category']['id'], self.category.pk)
  603. def test_move_thread_no_category_browse(self):
  604. """api move thread to category with no browsing access fails"""
  605. self.override_acl({'can_move_threads': True})
  606. self.override_other_acl({'can_browse': False})
  607. response = self.patch(
  608. self.api_link, [
  609. {
  610. 'op': 'replace',
  611. 'path': 'category',
  612. 'value': self.category_b.pk,
  613. },
  614. ]
  615. )
  616. self.assertEqual(response.status_code, 400)
  617. response_json = response.json()
  618. self.assertEqual(
  619. response_json['detail'][0],
  620. 'You don\'t have permission to browse "Category B" contents.'
  621. )
  622. self.override_other_acl({})
  623. thread_json = self.get_thread_json()
  624. self.assertEqual(thread_json['category']['id'], self.category.pk)
  625. def test_move_thread_no_category_start_threads(self):
  626. """api move thread to category with no posting access fails"""
  627. self.override_acl({'can_move_threads': True})
  628. self.override_other_acl({'can_start_threads': False})
  629. response = self.patch(
  630. self.api_link, [
  631. {
  632. 'op': 'replace',
  633. 'path': 'category',
  634. 'value': self.category_b.pk,
  635. },
  636. ]
  637. )
  638. self.assertEqual(response.status_code, 400)
  639. response_json = response.json()
  640. self.assertEqual(
  641. response_json['detail'][0],
  642. "You don't have permission to start new threads in this category."
  643. )
  644. self.override_other_acl({})
  645. thread_json = self.get_thread_json()
  646. self.assertEqual(thread_json['category']['id'], self.category.pk)
  647. def test_move_thread_same_category(self):
  648. """api move thread to category it's already in fails"""
  649. self.override_acl({'can_move_threads': True})
  650. self.override_other_acl({'can_start_threads': 2})
  651. response = self.patch(
  652. self.api_link, [
  653. {
  654. 'op': 'replace',
  655. 'path': 'category',
  656. 'value': self.thread.category_id,
  657. },
  658. ]
  659. )
  660. self.assertEqual(response.status_code, 400)
  661. response_json = response.json()
  662. self.assertEqual(
  663. response_json['detail'][0], "You can't move thread to the category it's already in."
  664. )
  665. self.override_other_acl({})
  666. thread_json = self.get_thread_json()
  667. self.assertEqual(thread_json['category']['id'], self.category.pk)
  668. def test_thread_flatten_categories(self):
  669. """api flatten thread categories"""
  670. response = self.patch(
  671. self.api_link, [
  672. {
  673. 'op': 'replace',
  674. 'path': 'flatten-categories',
  675. 'value': None,
  676. },
  677. ]
  678. )
  679. self.assertEqual(response.status_code, 200)
  680. response_json = response.json()
  681. self.assertEqual(response_json['category'], self.category.pk)
  682. class ThreadCloseApiTests(ThreadPatchApiTestCase):
  683. def test_close_thread(self):
  684. """api makes it possible to close thread"""
  685. self.override_acl({'can_close_threads': True})
  686. response = self.patch(
  687. self.api_link, [
  688. {
  689. 'op': 'replace',
  690. 'path': 'is-closed',
  691. 'value': True,
  692. },
  693. ]
  694. )
  695. self.assertEqual(response.status_code, 200)
  696. response_json = response.json()
  697. self.assertTrue(response_json['is_closed'])
  698. thread_json = self.get_thread_json()
  699. self.assertTrue(thread_json['is_closed'])
  700. def test_open_thread(self):
  701. """api makes it possible to open thread"""
  702. self.thread.is_closed = True
  703. self.thread.save()
  704. thread_json = self.get_thread_json()
  705. self.assertTrue(thread_json['is_closed'])
  706. self.override_acl({'can_close_threads': True})
  707. response = self.patch(
  708. self.api_link, [
  709. {
  710. 'op': 'replace',
  711. 'path': 'is-closed',
  712. 'value': False,
  713. },
  714. ]
  715. )
  716. self.assertEqual(response.status_code, 200)
  717. response_json = response.json()
  718. self.assertFalse(response_json['is_closed'])
  719. thread_json = self.get_thread_json()
  720. self.assertFalse(thread_json['is_closed'])
  721. def test_close_thread_no_permission(self):
  722. """api close thread with no permission fails"""
  723. self.override_acl({'can_close_threads': False})
  724. response = self.patch(
  725. self.api_link, [
  726. {
  727. 'op': 'replace',
  728. 'path': 'is-closed',
  729. 'value': True,
  730. },
  731. ]
  732. )
  733. self.assertEqual(response.status_code, 400)
  734. response_json = response.json()
  735. self.assertEqual(
  736. response_json['detail'][0], "You don't have permission to close this thread."
  737. )
  738. thread_json = self.get_thread_json()
  739. self.assertFalse(thread_json['is_closed'])
  740. def test_open_thread_no_permission(self):
  741. """api open thread with no permission fails"""
  742. self.thread.is_closed = True
  743. self.thread.save()
  744. thread_json = self.get_thread_json()
  745. self.assertTrue(thread_json['is_closed'])
  746. self.override_acl({'can_close_threads': False})
  747. response = self.patch(
  748. self.api_link, [
  749. {
  750. 'op': 'replace',
  751. 'path': 'is-closed',
  752. 'value': False,
  753. },
  754. ]
  755. )
  756. self.assertEqual(response.status_code, 400)
  757. response_json = response.json()
  758. self.assertEqual(
  759. response_json['detail'][0], "You don't have permission to open this thread."
  760. )
  761. thread_json = self.get_thread_json()
  762. self.assertTrue(thread_json['is_closed'])
  763. class ThreadApproveApiTests(ThreadPatchApiTestCase):
  764. def test_approve_thread(self):
  765. """api makes it possible to approve thread"""
  766. self.thread.first_post.is_unapproved = True
  767. self.thread.first_post.save()
  768. self.thread.synchronize()
  769. self.thread.save()
  770. self.assertTrue(self.thread.is_unapproved)
  771. self.assertTrue(self.thread.has_unapproved_posts)
  772. self.override_acl({'can_approve_content': 1})
  773. response = self.patch(
  774. self.api_link, [
  775. {
  776. 'op': 'replace',
  777. 'path': 'is-unapproved',
  778. 'value': False,
  779. },
  780. ]
  781. )
  782. self.assertEqual(response.status_code, 200)
  783. response_json = response.json()
  784. self.assertFalse(response_json['is_unapproved'])
  785. self.assertFalse(response_json['has_unapproved_posts'])
  786. thread_json = self.get_thread_json()
  787. self.assertFalse(thread_json['is_unapproved'])
  788. self.assertFalse(thread_json['has_unapproved_posts'])
  789. thread = Thread.objects.get(pk=self.thread.pk)
  790. self.assertFalse(thread.is_unapproved)
  791. self.assertFalse(thread.has_unapproved_posts)
  792. def test_approve_thread_category_closed_no_permission(self):
  793. """api checks permission for approving threads in closed categories"""
  794. self.thread.first_post.is_unapproved = True
  795. self.thread.first_post.save()
  796. self.thread.synchronize()
  797. self.thread.save()
  798. self.assertTrue(self.thread.is_unapproved)
  799. self.assertTrue(self.thread.has_unapproved_posts)
  800. self.category.is_closed = True
  801. self.category.save()
  802. self.override_acl({
  803. 'can_approve_content': 1,
  804. 'can_close_threads': 0,
  805. })
  806. response = self.patch(
  807. self.api_link, [
  808. {
  809. 'op': 'replace',
  810. 'path': 'is-unapproved',
  811. 'value': False,
  812. },
  813. ]
  814. )
  815. self.assertEqual(response.status_code, 400)
  816. response_json = response.json()
  817. self.assertEqual(response_json['detail'][0], "This category is closed. You can't approve threads in it.")
  818. def test_approve_thread_closed_no_permission(self):
  819. """api checks permission for approving posts in closed categories"""
  820. self.thread.first_post.is_unapproved = True
  821. self.thread.first_post.save()
  822. self.thread.synchronize()
  823. self.thread.save()
  824. self.assertTrue(self.thread.is_unapproved)
  825. self.assertTrue(self.thread.has_unapproved_posts)
  826. self.thread.is_closed = True
  827. self.thread.save()
  828. self.override_acl({
  829. 'can_approve_content': 1,
  830. 'can_close_threads': 0,
  831. })
  832. response = self.patch(
  833. self.api_link, [
  834. {
  835. 'op': 'replace',
  836. 'path': 'is-unapproved',
  837. 'value': False,
  838. },
  839. ]
  840. )
  841. self.assertEqual(response.status_code, 400)
  842. response_json = response.json()
  843. self.assertEqual(response_json['detail'][0], "This thread is closed. You can't approve it.")
  844. def test_unapprove_thread(self):
  845. """api returns permission error on approval removal"""
  846. self.override_acl({'can_approve_content': 1})
  847. response = self.patch(
  848. self.api_link, [
  849. {
  850. 'op': 'replace',
  851. 'path': 'is-unapproved',
  852. 'value': True,
  853. },
  854. ]
  855. )
  856. self.assertEqual(response.status_code, 400)
  857. response_json = response.json()
  858. self.assertEqual(response_json['detail'][0], "Content approval can't be reversed.")
  859. class ThreadHideApiTests(ThreadPatchApiTestCase):
  860. def test_hide_thread(self):
  861. """api makes it possible to hide thread"""
  862. self.override_acl({'can_hide_threads': 1})
  863. response = self.patch(
  864. self.api_link, [
  865. {
  866. 'op': 'replace',
  867. 'path': 'is-hidden',
  868. 'value': True,
  869. },
  870. ]
  871. )
  872. self.assertEqual(response.status_code, 200)
  873. reponse_json = response.json()
  874. self.assertTrue(reponse_json['is_hidden'])
  875. self.override_acl({'can_hide_threads': 1})
  876. thread_json = self.get_thread_json()
  877. self.assertTrue(thread_json['is_hidden'])
  878. def test_hide_thread_no_permission(self):
  879. """api hide thread with no permission fails"""
  880. self.override_acl({'can_hide_threads': 0})
  881. response = self.patch(
  882. self.api_link, [
  883. {
  884. 'op': 'replace',
  885. 'path': 'is-hidden',
  886. 'value': True,
  887. },
  888. ]
  889. )
  890. self.assertEqual(response.status_code, 400)
  891. response_json = response.json()
  892. self.assertEqual(
  893. response_json['detail'][0], "You can't hide threads in this category."
  894. )
  895. thread_json = self.get_thread_json()
  896. self.assertFalse(thread_json['is_hidden'])
  897. def test_hide_non_owned_thread(self):
  898. """api forbids non-moderator from hiding other users threads"""
  899. self.override_acl({
  900. 'can_hide_own_threads': 1,
  901. 'can_hide_threads': 0
  902. })
  903. response = self.patch(
  904. self.api_link, [
  905. {
  906. 'op': 'replace',
  907. 'path': 'is-hidden',
  908. 'value': True,
  909. },
  910. ]
  911. )
  912. self.assertEqual(response.status_code, 400)
  913. response_json = response.json()
  914. self.assertEqual(
  915. response_json['detail'][0], "You can't hide other users theads in this category."
  916. )
  917. def test_hide_owned_thread_no_time(self):
  918. """api forbids non-moderator from hiding other users threads"""
  919. self.override_acl({
  920. 'can_hide_own_threads': 1,
  921. 'can_hide_threads': 0,
  922. 'thread_edit_time': 1,
  923. })
  924. self.thread.started_on = timezone.now() - timedelta(minutes=5)
  925. self.thread.starter = self.user
  926. self.thread.save()
  927. response = self.patch(
  928. self.api_link, [
  929. {
  930. 'op': 'replace',
  931. 'path': 'is-hidden',
  932. 'value': True,
  933. },
  934. ]
  935. )
  936. self.assertEqual(response.status_code, 400)
  937. response_json = response.json()
  938. self.assertEqual(
  939. response_json['detail'][0], "You can't hide threads that are older than 1 minute."
  940. )
  941. def test_hide_closed_category_no_permission(self):
  942. """api test permission to hide thread in closed category"""
  943. self.override_acl({
  944. 'can_hide_threads': 1,
  945. 'can_close_threads': 0
  946. })
  947. self.category.is_closed = True
  948. self.category.save()
  949. response = self.patch(
  950. self.api_link, [
  951. {
  952. 'op': 'replace',
  953. 'path': 'is-hidden',
  954. 'value': True,
  955. },
  956. ]
  957. )
  958. self.assertEqual(response.status_code, 400)
  959. response_json = response.json()
  960. self.assertEqual(
  961. response_json['detail'][0], "This category is closed. You can't hide threads in it."
  962. )
  963. def test_hide_closed_thread_no_permission(self):
  964. """api test permission to hide closed thread"""
  965. self.override_acl({
  966. 'can_hide_threads': 1,
  967. 'can_close_threads': 0
  968. })
  969. self.thread.is_closed = True
  970. self.thread.save()
  971. response = self.patch(
  972. self.api_link, [
  973. {
  974. 'op': 'replace',
  975. 'path': 'is-hidden',
  976. 'value': True,
  977. },
  978. ]
  979. )
  980. self.assertEqual(response.status_code, 400)
  981. response_json = response.json()
  982. self.assertEqual(
  983. response_json['detail'][0], "This thread is closed. You can't hide it."
  984. )
  985. class ThreadUnhideApiTests(ThreadPatchApiTestCase):
  986. def setUp(self):
  987. super().setUp()
  988. self.thread.is_hidden = True
  989. self.thread.save()
  990. def test_unhide_thread(self):
  991. """api makes it possible to unhide thread"""
  992. self.override_acl({'can_hide_threads': 1})
  993. response = self.patch(
  994. self.api_link, [
  995. {
  996. 'op': 'replace',
  997. 'path': 'is-hidden',
  998. 'value': False,
  999. },
  1000. ]
  1001. )
  1002. self.assertEqual(response.status_code, 200)
  1003. reponse_json = response.json()
  1004. self.assertFalse(reponse_json['is_hidden'])
  1005. self.override_acl({'can_hide_threads': 1})
  1006. thread_json = self.get_thread_json()
  1007. self.assertFalse(thread_json['is_hidden'])
  1008. def test_unhide_thread_no_permission(self):
  1009. """api unhide thread with no permission fails as thread is invisible"""
  1010. self.override_acl({'can_hide_threads': 0})
  1011. response = self.patch(
  1012. self.api_link, [
  1013. {
  1014. 'op': 'replace',
  1015. 'path': 'is-hidden',
  1016. 'value': True,
  1017. },
  1018. ]
  1019. )
  1020. self.assertEqual(response.status_code, 404)
  1021. def test_unhide_closed_category_no_permission(self):
  1022. """api test permission to unhide thread in closed category"""
  1023. self.override_acl({
  1024. 'can_hide_threads': 1,
  1025. 'can_close_threads': 0
  1026. })
  1027. self.category.is_closed = True
  1028. self.category.save()
  1029. response = self.patch(
  1030. self.api_link, [
  1031. {
  1032. 'op': 'replace',
  1033. 'path': 'is-hidden',
  1034. 'value': False,
  1035. },
  1036. ]
  1037. )
  1038. self.assertEqual(response.status_code, 400)
  1039. response_json = response.json()
  1040. self.assertEqual(
  1041. response_json['detail'][0], "This category is closed. You can't reveal threads in it."
  1042. )
  1043. def test_unhide_closed_thread_no_permission(self):
  1044. """api test permission to unhide closed thread"""
  1045. self.override_acl({
  1046. 'can_hide_threads': 1,
  1047. 'can_close_threads': 0
  1048. })
  1049. self.thread.is_closed = True
  1050. self.thread.save()
  1051. response = self.patch(
  1052. self.api_link, [
  1053. {
  1054. 'op': 'replace',
  1055. 'path': 'is-hidden',
  1056. 'value': False,
  1057. },
  1058. ]
  1059. )
  1060. self.assertEqual(response.status_code, 400)
  1061. response_json = response.json()
  1062. self.assertEqual(
  1063. response_json['detail'][0], "This thread is closed. You can't reveal it."
  1064. )
  1065. class ThreadSubscribeApiTests(ThreadPatchApiTestCase):
  1066. def test_subscribe_thread(self):
  1067. """api makes it possible to subscribe thread"""
  1068. response = self.patch(
  1069. self.api_link, [
  1070. {
  1071. 'op': 'replace',
  1072. 'path': 'subscription',
  1073. 'value': 'notify',
  1074. },
  1075. ]
  1076. )
  1077. self.assertEqual(response.status_code, 200)
  1078. reponse_json = response.json()
  1079. self.assertFalse(reponse_json['subscription'])
  1080. thread_json = self.get_thread_json()
  1081. self.assertFalse(thread_json['subscription'])
  1082. subscription = self.user.subscription_set.get(thread=self.thread)
  1083. self.assertFalse(subscription.send_email)
  1084. def test_subscribe_thread_with_email(self):
  1085. """api makes it possible to subscribe thread with emails"""
  1086. response = self.patch(
  1087. self.api_link, [
  1088. {
  1089. 'op': 'replace',
  1090. 'path': 'subscription',
  1091. 'value': 'email',
  1092. },
  1093. ]
  1094. )
  1095. self.assertEqual(response.status_code, 200)
  1096. reponse_json = response.json()
  1097. self.assertTrue(reponse_json['subscription'])
  1098. thread_json = self.get_thread_json()
  1099. self.assertTrue(thread_json['subscription'])
  1100. subscription = self.user.subscription_set.get(thread=self.thread)
  1101. self.assertTrue(subscription.send_email)
  1102. def test_unsubscribe_thread(self):
  1103. """api makes it possible to unsubscribe thread"""
  1104. response = self.patch(
  1105. self.api_link, [
  1106. {
  1107. 'op': 'replace',
  1108. 'path': 'subscription',
  1109. 'value': 'remove',
  1110. },
  1111. ]
  1112. )
  1113. self.assertEqual(response.status_code, 200)
  1114. reponse_json = response.json()
  1115. self.assertIsNone(reponse_json['subscription'])
  1116. thread_json = self.get_thread_json()
  1117. self.assertIsNone(thread_json['subscription'])
  1118. self.assertEqual(self.user.subscription_set.count(), 0)
  1119. def test_subscribe_as_guest(self):
  1120. """api makes it impossible to subscribe thread"""
  1121. self.logout_user()
  1122. response = self.patch(
  1123. self.api_link, [
  1124. {
  1125. 'op': 'replace',
  1126. 'path': 'subscription',
  1127. 'value': 'email',
  1128. },
  1129. ]
  1130. )
  1131. self.assertEqual(response.status_code, 403)
  1132. def test_subscribe_nonexistant_thread(self):
  1133. """api makes it impossible to subscribe nonexistant thread"""
  1134. bad_api_link = self.api_link.replace(
  1135. str(self.thread.pk), str(self.thread.pk + 9)
  1136. )
  1137. response = self.patch(
  1138. bad_api_link, [
  1139. {
  1140. 'op': 'replace',
  1141. 'path': 'subscription',
  1142. 'value': 'email',
  1143. },
  1144. ]
  1145. )
  1146. self.assertEqual(response.status_code, 404)
  1147. class ThreadMarkBestAnswerApiTests(ThreadPatchApiTestCase):
  1148. def test_mark_best_answer(self):
  1149. """api makes it possible to mark best answer"""
  1150. self.override_acl({'can_mark_best_answers': 2})
  1151. best_answer = testutils.reply_thread(self.thread)
  1152. response = self.patch(
  1153. self.api_link, [
  1154. {
  1155. 'op': 'replace',
  1156. 'path': 'best-answer',
  1157. 'value': best_answer.id,
  1158. },
  1159. ]
  1160. )
  1161. self.assertEqual(response.status_code, 200)
  1162. self.assertEqual(response.json(), {
  1163. 'id': self.thread.id,
  1164. 'detail': ['ok'],
  1165. 'best_answer': best_answer.id,
  1166. 'best_answer_is_protected': False,
  1167. 'best_answer_marked_on': response.json()['best_answer_marked_on'],
  1168. 'best_answer_marked_by': self.user.id,
  1169. 'best_answer_marked_by_name': self.user.username,
  1170. 'best_answer_marked_by_slug': self.user.slug,
  1171. })
  1172. thread_json = self.get_thread_json()
  1173. self.assertEqual(thread_json['best_answer'], best_answer.id)
  1174. self.assertEqual(thread_json['best_answer_is_protected'], False)
  1175. self.assertEqual(
  1176. thread_json['best_answer_marked_on'], response.json()['best_answer_marked_on'])
  1177. self.assertEqual(thread_json['best_answer_marked_by'], self.user.id)
  1178. self.assertEqual(thread_json['best_answer_marked_by_name'], self.user.username)
  1179. self.assertEqual(thread_json['best_answer_marked_by_slug'], self.user.slug)
  1180. def test_mark_best_answer_anonymous(self):
  1181. """api validates that user is authenticated before marking best answer"""
  1182. self.logout_user()
  1183. self.override_acl({'can_mark_best_answers': 2})
  1184. best_answer = testutils.reply_thread(self.thread)
  1185. response = self.patch(
  1186. self.api_link, [
  1187. {
  1188. 'op': 'replace',
  1189. 'path': 'best-answer',
  1190. 'value': best_answer.id,
  1191. },
  1192. ]
  1193. )
  1194. self.assertEqual(response.status_code, 403)
  1195. self.assertEqual(response.json(), {
  1196. 'detail': "This action is not available to guests.",
  1197. })
  1198. thread_json = self.get_thread_json()
  1199. self.assertIsNone(thread_json['best_answer'])
  1200. def test_mark_best_answer_no_permission(self):
  1201. """api validates permission to mark best answers"""
  1202. self.override_acl({'can_mark_best_answers': 0})
  1203. best_answer = testutils.reply_thread(self.thread)
  1204. response = self.patch(
  1205. self.api_link, [
  1206. {
  1207. 'op': 'replace',
  1208. 'path': 'best-answer',
  1209. 'value': best_answer.id,
  1210. },
  1211. ]
  1212. )
  1213. self.assertEqual(response.status_code, 400)
  1214. self.assertEqual(response.json(), {
  1215. 'id': self.thread.id,
  1216. 'detail': [
  1217. 'You don\'t have permission to mark best answers in the "First category" category.'
  1218. ],
  1219. })
  1220. thread_json = self.get_thread_json()
  1221. self.assertIsNone(thread_json['best_answer'])
  1222. def test_mark_best_answer_not_thread_starter(self):
  1223. """api validates permission to mark best answers in owned thread"""
  1224. self.override_acl({'can_mark_best_answers': 1})
  1225. best_answer = testutils.reply_thread(self.thread)
  1226. response = self.patch(
  1227. self.api_link, [
  1228. {
  1229. 'op': 'replace',
  1230. 'path': 'best-answer',
  1231. 'value': best_answer.id,
  1232. },
  1233. ]
  1234. )
  1235. self.assertEqual(response.status_code, 400)
  1236. self.assertEqual(response.json(), {
  1237. 'id': self.thread.id,
  1238. 'detail': [
  1239. "You don't have permission to mark best answer in this thread because you didn't "
  1240. "start it."
  1241. ],
  1242. })
  1243. thread_json = self.get_thread_json()
  1244. self.assertIsNone(thread_json['best_answer'])
  1245. # passing scenario is possible
  1246. self.thread.starter = self.user
  1247. self.thread.save()
  1248. self.override_acl({'can_mark_best_answers': 1})
  1249. response = self.patch(
  1250. self.api_link, [
  1251. {
  1252. 'op': 'replace',
  1253. 'path': 'best-answer',
  1254. 'value': best_answer.id,
  1255. },
  1256. ]
  1257. )
  1258. self.assertEqual(response.status_code, 200)
  1259. def test_mark_best_answer_category_closed(self):
  1260. """api validates permission to mark best answers in closed category"""
  1261. self.override_acl({'can_mark_best_answers': 2, 'can_close_threads': 0})
  1262. best_answer = testutils.reply_thread(self.thread)
  1263. self.category.is_closed = True
  1264. self.category.save()
  1265. response = self.patch(
  1266. self.api_link, [
  1267. {
  1268. 'op': 'replace',
  1269. 'path': 'best-answer',
  1270. 'value': best_answer.id,
  1271. },
  1272. ]
  1273. )
  1274. self.assertEqual(response.status_code, 400)
  1275. self.assertEqual(response.json(), {
  1276. 'id': self.thread.id,
  1277. 'detail': [
  1278. 'You don\'t have permission to mark best answer in this thread because its '
  1279. 'category "First category" is closed.'
  1280. ],
  1281. })
  1282. thread_json = self.get_thread_json()
  1283. self.assertIsNone(thread_json['best_answer'])
  1284. # passing scenario is possible
  1285. self.override_acl({'can_mark_best_answers': 2, 'can_close_threads': 1})
  1286. response = self.patch(
  1287. self.api_link, [
  1288. {
  1289. 'op': 'replace',
  1290. 'path': 'best-answer',
  1291. 'value': best_answer.id,
  1292. },
  1293. ]
  1294. )
  1295. self.assertEqual(response.status_code, 200)
  1296. def test_mark_best_answer_thread_closed(self):
  1297. """api validates permission to mark best answers in closed thread"""
  1298. self.override_acl({'can_mark_best_answers': 2, 'can_close_threads': 0})
  1299. best_answer = testutils.reply_thread(self.thread)
  1300. self.thread.is_closed = True
  1301. self.thread.save()
  1302. response = self.patch(
  1303. self.api_link, [
  1304. {
  1305. 'op': 'replace',
  1306. 'path': 'best-answer',
  1307. 'value': best_answer.id,
  1308. },
  1309. ]
  1310. )
  1311. self.assertEqual(response.status_code, 400)
  1312. self.assertEqual(response.json(), {
  1313. 'id': self.thread.id,
  1314. 'detail': [
  1315. "You can't mark best answer in this thread because it's closed and you don't have "
  1316. "permission to open it."
  1317. ],
  1318. })
  1319. thread_json = self.get_thread_json()
  1320. self.assertIsNone(thread_json['best_answer'])
  1321. # passing scenario is possible
  1322. self.override_acl({'can_mark_best_answers': 2, 'can_close_threads': 1})
  1323. response = self.patch(
  1324. self.api_link, [
  1325. {
  1326. 'op': 'replace',
  1327. 'path': 'best-answer',
  1328. 'value': best_answer.id,
  1329. },
  1330. ]
  1331. )
  1332. self.assertEqual(response.status_code, 200)
  1333. def test_mark_best_answer_invalid_post_id(self):
  1334. """api validates that post id is int"""
  1335. self.override_acl({'can_mark_best_answers': 2})
  1336. response = self.patch(
  1337. self.api_link, [
  1338. {
  1339. 'op': 'replace',
  1340. 'path': 'best-answer',
  1341. 'value': 'd7sd89a7d98sa',
  1342. },
  1343. ]
  1344. )
  1345. self.assertEqual(response.status_code, 400)
  1346. self.assertEqual(response.json(), {
  1347. 'id': self.thread.id,
  1348. 'detail': ["A valid integer is required."],
  1349. })
  1350. thread_json = self.get_thread_json()
  1351. self.assertIsNone(thread_json['best_answer'])
  1352. def test_mark_best_answer_post_not_found(self):
  1353. """api validates that post exists"""
  1354. self.override_acl({'can_mark_best_answers': 2})
  1355. response = self.patch(
  1356. self.api_link, [
  1357. {
  1358. 'op': 'replace',
  1359. 'path': 'best-answer',
  1360. 'value': self.thread.last_post_id + 1,
  1361. },
  1362. ]
  1363. )
  1364. self.assertEqual(response.status_code, 400)
  1365. self.assertEqual(response.json(), {
  1366. 'id': self.thread.id,
  1367. 'detail': ["NOT FOUND"],
  1368. })
  1369. thread_json = self.get_thread_json()
  1370. self.assertIsNone(thread_json['best_answer'])
  1371. def test_mark_best_answer_post_invisible(self):
  1372. """api validates post visibility to action author"""
  1373. self.override_acl({'can_mark_best_answers': 2})
  1374. unapproved_post = testutils.reply_thread(self.thread, is_unapproved=True)
  1375. response = self.patch(
  1376. self.api_link, [
  1377. {
  1378. 'op': 'replace',
  1379. 'path': 'best-answer',
  1380. 'value': unapproved_post.id,
  1381. },
  1382. ]
  1383. )
  1384. self.assertEqual(response.status_code, 400)
  1385. self.assertEqual(response.json(), {
  1386. 'id': self.thread.id,
  1387. 'detail': ["NOT FOUND"],
  1388. })
  1389. thread_json = self.get_thread_json()
  1390. self.assertIsNone(thread_json['best_answer'])
  1391. def test_mark_best_answer_post_other_thread(self):
  1392. """api validates post belongs to same thread"""
  1393. self.override_acl({'can_mark_best_answers': 2})
  1394. other_thread = testutils.post_thread(self.category)
  1395. response = self.patch(
  1396. self.api_link, [
  1397. {
  1398. 'op': 'replace',
  1399. 'path': 'best-answer',
  1400. 'value': other_thread.first_post_id,
  1401. },
  1402. ]
  1403. )
  1404. self.assertEqual(response.status_code, 400)
  1405. self.assertEqual(response.json(), {
  1406. 'id': self.thread.id,
  1407. 'detail': ["NOT FOUND"],
  1408. })
  1409. thread_json = self.get_thread_json()
  1410. self.assertIsNone(thread_json['best_answer'])
  1411. def test_mark_best_answer_event_id(self):
  1412. """api validates that post is not an event"""
  1413. self.override_acl({'can_mark_best_answers': 2})
  1414. best_answer = testutils.reply_thread(self.thread)
  1415. best_answer.is_event = True
  1416. best_answer.save()
  1417. response = self.patch(
  1418. self.api_link, [
  1419. {
  1420. 'op': 'replace',
  1421. 'path': 'best-answer',
  1422. 'value': best_answer.id,
  1423. },
  1424. ]
  1425. )
  1426. self.assertEqual(response.status_code, 400)
  1427. self.assertEqual(response.json(), {
  1428. 'id': self.thread.id,
  1429. 'detail': ["Events can't be marked as best answers."],
  1430. })
  1431. thread_json = self.get_thread_json()
  1432. self.assertIsNone(thread_json['best_answer'])
  1433. def test_mark_best_answer_first_post(self):
  1434. """api validates that post is not a first post in thread"""
  1435. self.override_acl({'can_mark_best_answers': 2})
  1436. response = self.patch(
  1437. self.api_link, [
  1438. {
  1439. 'op': 'replace',
  1440. 'path': 'best-answer',
  1441. 'value': self.thread.first_post_id,
  1442. },
  1443. ]
  1444. )
  1445. self.assertEqual(response.status_code, 400)
  1446. self.assertEqual(response.json(), {
  1447. 'id': self.thread.id,
  1448. 'detail': ["First post in a thread can't be marked as best answer."],
  1449. })
  1450. thread_json = self.get_thread_json()
  1451. self.assertIsNone(thread_json['best_answer'])
  1452. def test_mark_best_answer_hidden_post(self):
  1453. """api validates that post is not hidden"""
  1454. self.override_acl({'can_mark_best_answers': 2})
  1455. best_answer = testutils.reply_thread(self.thread, is_hidden=True)
  1456. response = self.patch(
  1457. self.api_link, [
  1458. {
  1459. 'op': 'replace',
  1460. 'path': 'best-answer',
  1461. 'value': best_answer.id,
  1462. },
  1463. ]
  1464. )
  1465. self.assertEqual(response.status_code, 400)
  1466. self.assertEqual(response.json(), {
  1467. 'id': self.thread.id,
  1468. 'detail': ["Hidden posts can't be marked as best answers."],
  1469. })
  1470. thread_json = self.get_thread_json()
  1471. self.assertIsNone(thread_json['best_answer'])
  1472. def test_mark_best_answer_unapproved_post(self):
  1473. """api validates that post is not unapproved"""
  1474. self.override_acl({'can_mark_best_answers': 2})
  1475. best_answer = testutils.reply_thread(self.thread, poster=self.user, is_unapproved=True)
  1476. response = self.patch(
  1477. self.api_link, [
  1478. {
  1479. 'op': 'replace',
  1480. 'path': 'best-answer',
  1481. 'value': best_answer.id,
  1482. },
  1483. ]
  1484. )
  1485. self.assertEqual(response.status_code, 400)
  1486. self.assertEqual(response.json(), {
  1487. 'id': self.thread.id,
  1488. 'detail': ["Unapproved posts can't be marked as best answers."],
  1489. })
  1490. thread_json = self.get_thread_json()
  1491. self.assertIsNone(thread_json['best_answer'])
  1492. def test_mark_best_answer_protected_post(self):
  1493. """api respects post protection"""
  1494. self.override_acl({'can_mark_best_answers': 2, 'can_protect_posts': 0})
  1495. best_answer = testutils.reply_thread(self.thread, is_protected=True)
  1496. response = self.patch(
  1497. self.api_link, [
  1498. {
  1499. 'op': 'replace',
  1500. 'path': 'best-answer',
  1501. 'value': best_answer.id,
  1502. },
  1503. ]
  1504. )
  1505. self.assertEqual(response.status_code, 400)
  1506. self.assertEqual(response.json(), {
  1507. 'id': self.thread.id,
  1508. 'detail': [
  1509. "You don't have permission to mark this post as best answer because a moderator "
  1510. "has protected it."
  1511. ],
  1512. })
  1513. thread_json = self.get_thread_json()
  1514. self.assertIsNone(thread_json['best_answer'])
  1515. # passing scenario is possible
  1516. self.override_acl({'can_mark_best_answers': 2, 'can_protect_posts': 1})
  1517. response = self.patch(
  1518. self.api_link, [
  1519. {
  1520. 'op': 'replace',
  1521. 'path': 'best-answer',
  1522. 'value': best_answer.id,
  1523. },
  1524. ]
  1525. )
  1526. self.assertEqual(response.status_code, 200)
  1527. class ThreadChangeBestAnswerApiTests(ThreadPatchApiTestCase):
  1528. def setUp(self):
  1529. super().setUp()
  1530. self.best_answer = testutils.reply_thread(self.thread)
  1531. self.thread.set_best_answer(self.user, self.best_answer)
  1532. self.thread.save()
  1533. def test_change_best_answer(self):
  1534. """api makes it possible to change best answer"""
  1535. self.override_acl({'can_mark_best_answers': 2, 'can_change_marked_answers': 2})
  1536. best_answer = testutils.reply_thread(self.thread)
  1537. response = self.patch(
  1538. self.api_link, [
  1539. {
  1540. 'op': 'replace',
  1541. 'path': 'best-answer',
  1542. 'value': best_answer.id,
  1543. },
  1544. ]
  1545. )
  1546. self.assertEqual(response.status_code, 200)
  1547. self.assertEqual(response.json(), {
  1548. 'id': self.thread.id,
  1549. 'detail': ['ok'],
  1550. 'best_answer': best_answer.id,
  1551. 'best_answer_is_protected': False,
  1552. 'best_answer_marked_on': response.json()['best_answer_marked_on'],
  1553. 'best_answer_marked_by': self.user.id,
  1554. 'best_answer_marked_by_name': self.user.username,
  1555. 'best_answer_marked_by_slug': self.user.slug,
  1556. })
  1557. thread_json = self.get_thread_json()
  1558. self.assertEqual(thread_json['best_answer'], best_answer.id)
  1559. self.assertEqual(thread_json['best_answer_is_protected'], False)
  1560. self.assertEqual(
  1561. thread_json['best_answer_marked_on'], response.json()['best_answer_marked_on'])
  1562. self.assertEqual(thread_json['best_answer_marked_by'], self.user.id)
  1563. self.assertEqual(thread_json['best_answer_marked_by_name'], self.user.username)
  1564. self.assertEqual(thread_json['best_answer_marked_by_slug'], self.user.slug)
  1565. def test_change_best_answer_same_post(self):
  1566. """api validates if new best answer is same as current one"""
  1567. self.override_acl({'can_mark_best_answers': 2, 'can_change_marked_answers': 2})
  1568. response = self.patch(
  1569. self.api_link, [
  1570. {
  1571. 'op': 'replace',
  1572. 'path': 'best-answer',
  1573. 'value': self.best_answer.id,
  1574. },
  1575. ]
  1576. )
  1577. self.assertEqual(response.status_code, 400)
  1578. self.assertEqual(response.json(), {
  1579. 'id': self.thread.id,
  1580. 'detail': ["This post is already marked as thread's best answer."],
  1581. })
  1582. thread_json = self.get_thread_json()
  1583. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1584. def test_change_best_answer_no_permission_to_mark(self):
  1585. """api validates permission to mark best answers before allowing answer change"""
  1586. self.override_acl({'can_mark_best_answers': 0, 'can_change_marked_answers': 2})
  1587. best_answer = testutils.reply_thread(self.thread)
  1588. response = self.patch(
  1589. self.api_link, [
  1590. {
  1591. 'op': 'replace',
  1592. 'path': 'best-answer',
  1593. 'value': best_answer.id,
  1594. },
  1595. ]
  1596. )
  1597. self.assertEqual(response.status_code, 400)
  1598. self.assertEqual(response.json(), {
  1599. 'id': self.thread.id,
  1600. 'detail': [
  1601. 'You don\'t have permission to mark best answers in the "First category" category.'
  1602. ],
  1603. })
  1604. thread_json = self.get_thread_json()
  1605. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1606. def test_change_best_answer_no_permission(self):
  1607. """api validates permission to change best answers"""
  1608. self.override_acl({'can_mark_best_answers': 2, 'can_change_marked_answers': 0})
  1609. best_answer = testutils.reply_thread(self.thread)
  1610. response = self.patch(
  1611. self.api_link, [
  1612. {
  1613. 'op': 'replace',
  1614. 'path': 'best-answer',
  1615. 'value': best_answer.id,
  1616. },
  1617. ]
  1618. )
  1619. self.assertEqual(response.status_code, 400)
  1620. self.assertEqual(response.json(), {
  1621. 'id': self.thread.id,
  1622. 'detail': [
  1623. 'You don\'t have permission to change this thread\'s marked answer because it\'s '
  1624. 'in the "First category" category.'
  1625. ],
  1626. })
  1627. thread_json = self.get_thread_json()
  1628. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1629. def test_change_best_answer_not_starter(self):
  1630. """api validates permission to change best answers"""
  1631. self.override_acl({'can_mark_best_answers': 2, 'can_change_marked_answers': 1})
  1632. best_answer = testutils.reply_thread(self.thread)
  1633. response = self.patch(
  1634. self.api_link, [
  1635. {
  1636. 'op': 'replace',
  1637. 'path': 'best-answer',
  1638. 'value': best_answer.id,
  1639. },
  1640. ]
  1641. )
  1642. self.assertEqual(response.status_code, 400)
  1643. self.assertEqual(response.json(), {
  1644. 'id': self.thread.id,
  1645. 'detail': [
  1646. "You don't have permission to change this thread's marked answer because you are "
  1647. "not a thread starter."
  1648. ],
  1649. })
  1650. thread_json = self.get_thread_json()
  1651. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1652. # passing scenario is possible
  1653. self.override_acl({'can_mark_best_answers': 2, 'can_change_marked_answers': 1})
  1654. self.thread.starter = self.user
  1655. self.thread.save()
  1656. response = self.patch(
  1657. self.api_link, [
  1658. {
  1659. 'op': 'replace',
  1660. 'path': 'best-answer',
  1661. 'value': best_answer.id,
  1662. },
  1663. ]
  1664. )
  1665. self.assertEqual(response.status_code, 200)
  1666. def test_change_best_answer_timelimit(self):
  1667. """api validates permission for starter to change best answers within timelimit"""
  1668. self.override_acl({
  1669. 'can_mark_best_answers': 2,
  1670. 'can_change_marked_answers': 1,
  1671. 'best_answer_change_time': 5,
  1672. })
  1673. best_answer = testutils.reply_thread(self.thread)
  1674. self.thread.best_answer_marked_on = timezone.now() - timedelta(minutes=6)
  1675. self.thread.starter = self.user
  1676. self.thread.save()
  1677. response = self.patch(
  1678. self.api_link, [
  1679. {
  1680. 'op': 'replace',
  1681. 'path': 'best-answer',
  1682. 'value': best_answer.id,
  1683. },
  1684. ]
  1685. )
  1686. self.assertEqual(response.status_code, 400)
  1687. self.assertEqual(response.json(), {
  1688. 'id': self.thread.id,
  1689. 'detail': [
  1690. "You don't have permission to change best answer that was marked for more than "
  1691. "5 minutes."
  1692. ],
  1693. })
  1694. thread_json = self.get_thread_json()
  1695. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1696. # passing scenario is possible
  1697. self.override_acl({
  1698. 'can_mark_best_answers': 2,
  1699. 'can_change_marked_answers': 1,
  1700. 'best_answer_change_time': 10,
  1701. })
  1702. response = self.patch(
  1703. self.api_link, [
  1704. {
  1705. 'op': 'replace',
  1706. 'path': 'best-answer',
  1707. 'value': best_answer.id,
  1708. },
  1709. ]
  1710. )
  1711. self.assertEqual(response.status_code, 200)
  1712. def test_change_best_answer_protected(self):
  1713. """api validates permission to change protected best answers"""
  1714. self.override_acl({
  1715. 'can_mark_best_answers': 2,
  1716. 'can_change_marked_answers': 2,
  1717. 'can_protect_posts': 0,
  1718. })
  1719. best_answer = testutils.reply_thread(self.thread)
  1720. self.thread.best_answer_is_protected = True
  1721. self.thread.save()
  1722. response = self.patch(
  1723. self.api_link, [
  1724. {
  1725. 'op': 'replace',
  1726. 'path': 'best-answer',
  1727. 'value': best_answer.id,
  1728. },
  1729. ]
  1730. )
  1731. self.assertEqual(response.status_code, 400)
  1732. self.assertEqual(response.json(), {
  1733. 'id': self.thread.id,
  1734. 'detail': [
  1735. "You don't have permission to change this thread's best answer because a "
  1736. "moderator has protected it."
  1737. ],
  1738. })
  1739. thread_json = self.get_thread_json()
  1740. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1741. # passing scenario is possible
  1742. self.override_acl({
  1743. 'can_mark_best_answers': 2,
  1744. 'can_change_marked_answers': 2,
  1745. 'can_protect_posts': 1,
  1746. })
  1747. response = self.patch(
  1748. self.api_link, [
  1749. {
  1750. 'op': 'replace',
  1751. 'path': 'best-answer',
  1752. 'value': best_answer.id,
  1753. },
  1754. ]
  1755. )
  1756. self.assertEqual(response.status_code, 200)
  1757. def test_change_best_answer_post_validation(self):
  1758. """api validates new post'"""
  1759. self.override_acl({
  1760. 'can_mark_best_answers': 2,
  1761. 'can_change_marked_answers': 2,
  1762. })
  1763. best_answer = testutils.reply_thread(self.thread, is_hidden=True)
  1764. response = self.patch(
  1765. self.api_link, [
  1766. {
  1767. 'op': 'replace',
  1768. 'path': 'best-answer',
  1769. 'value': best_answer.id,
  1770. },
  1771. ]
  1772. )
  1773. self.assertEqual(response.status_code, 400)
  1774. thread_json = self.get_thread_json()
  1775. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1776. class ThreadUnmarkBestAnswerApiTests(ThreadPatchApiTestCase):
  1777. def setUp(self):
  1778. super().setUp()
  1779. self.best_answer = testutils.reply_thread(self.thread)
  1780. self.thread.set_best_answer(self.user, self.best_answer)
  1781. self.thread.save()
  1782. def test_unmark_best_answer(self):
  1783. """api makes it possible to unmark best answer"""
  1784. self.override_acl({'can_mark_best_answers': 0, 'can_change_marked_answers': 2})
  1785. response = self.patch(
  1786. self.api_link, [
  1787. {
  1788. 'op': 'remove',
  1789. 'path': 'best-answer',
  1790. 'value': self.best_answer.id,
  1791. },
  1792. ]
  1793. )
  1794. self.assertEqual(response.status_code, 200)
  1795. self.assertEqual(response.json(), {
  1796. 'id': self.thread.id,
  1797. 'detail': ['ok'],
  1798. 'best_answer': None,
  1799. 'best_answer_is_protected': False,
  1800. 'best_answer_marked_on': None,
  1801. 'best_answer_marked_by': None,
  1802. 'best_answer_marked_by_name': None,
  1803. 'best_answer_marked_by_slug': None,
  1804. })
  1805. thread_json = self.get_thread_json()
  1806. self.assertIsNone(thread_json['best_answer'])
  1807. self.assertFalse(thread_json['best_answer_is_protected'])
  1808. self.assertIsNone(thread_json['best_answer_marked_on'])
  1809. self.assertIsNone(thread_json['best_answer_marked_by'])
  1810. self.assertIsNone(thread_json['best_answer_marked_by_name'])
  1811. self.assertIsNone(thread_json['best_answer_marked_by_slug'])
  1812. def test_unmark_best_answer_invalid_post_id(self):
  1813. """api validates that post id is int"""
  1814. self.override_acl({'can_mark_best_answers': 0, 'can_change_marked_answers': 2})
  1815. response = self.patch(
  1816. self.api_link, [
  1817. {
  1818. 'op': 'remove',
  1819. 'path': 'best-answer',
  1820. 'value': 'd7sd89a7d98sa',
  1821. },
  1822. ]
  1823. )
  1824. self.assertEqual(response.status_code, 400)
  1825. self.assertEqual(response.json(), {
  1826. 'id': self.thread.id,
  1827. 'detail': ["A valid integer is required."],
  1828. })
  1829. thread_json = self.get_thread_json()
  1830. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1831. def test_unmark_best_answer_post_not_found(self):
  1832. """api validates that post to unmark exists"""
  1833. self.override_acl({'can_mark_best_answers': 0, 'can_change_marked_answers': 2})
  1834. response = self.patch(
  1835. self.api_link, [
  1836. {
  1837. 'op': 'remove',
  1838. 'path': 'best-answer',
  1839. 'value': self.best_answer.id + 1,
  1840. },
  1841. ]
  1842. )
  1843. self.assertEqual(response.status_code, 400)
  1844. self.assertEqual(response.json(), {
  1845. 'id': self.thread.id,
  1846. 'detail': ["NOT FOUND"],
  1847. })
  1848. thread_json = self.get_thread_json()
  1849. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1850. def test_unmark_best_answer_wrong_post(self):
  1851. """api validates if post given to unmark is best answer"""
  1852. self.override_acl({'can_mark_best_answers': 0, 'can_change_marked_answers': 2})
  1853. best_answer = testutils.reply_thread(self.thread)
  1854. response = self.patch(
  1855. self.api_link, [
  1856. {
  1857. 'op': 'remove',
  1858. 'path': 'best-answer',
  1859. 'value': best_answer.id,
  1860. },
  1861. ]
  1862. )
  1863. self.assertEqual(response.status_code, 400)
  1864. self.assertEqual(response.json(), {
  1865. 'id': self.thread.id,
  1866. 'detail': [
  1867. "This post can't be unmarked because it's not currently marked as best answer."
  1868. ],
  1869. })
  1870. thread_json = self.get_thread_json()
  1871. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1872. def test_unmark_best_answer_no_permission(self):
  1873. """api validates if user has permission to unmark best answers"""
  1874. self.override_acl({'can_mark_best_answers': 0, 'can_change_marked_answers': 0})
  1875. response = self.patch(
  1876. self.api_link, [
  1877. {
  1878. 'op': 'remove',
  1879. 'path': 'best-answer',
  1880. 'value': self.best_answer.id,
  1881. },
  1882. ]
  1883. )
  1884. self.assertEqual(response.status_code, 400)
  1885. self.assertEqual(response.json(), {
  1886. 'id': self.thread.id,
  1887. 'detail': [
  1888. 'You don\'t have permission to unmark threads answers in the "First category" '
  1889. 'category.'
  1890. ],
  1891. })
  1892. thread_json = self.get_thread_json()
  1893. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1894. def test_unmark_best_answer_not_starter(self):
  1895. """api validates if starter has permission to unmark best answers"""
  1896. self.override_acl({'can_mark_best_answers': 0, 'can_change_marked_answers': 1})
  1897. response = self.patch(
  1898. self.api_link, [
  1899. {
  1900. 'op': 'remove',
  1901. 'path': 'best-answer',
  1902. 'value': self.best_answer.id,
  1903. },
  1904. ]
  1905. )
  1906. self.assertEqual(response.status_code, 400)
  1907. self.assertEqual(response.json(), {
  1908. 'id': self.thread.id,
  1909. 'detail': [
  1910. "You don't have permission to unmark this best answer because you are not a "
  1911. "thread starter."
  1912. ],
  1913. })
  1914. thread_json = self.get_thread_json()
  1915. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1916. # passing scenario is possible
  1917. self.override_acl({'can_mark_best_answers': 0, 'can_change_marked_answers': 1})
  1918. self.thread.starter = self.user
  1919. self.thread.save()
  1920. response = self.patch(
  1921. self.api_link, [
  1922. {
  1923. 'op': 'remove',
  1924. 'path': 'best-answer',
  1925. 'value': self.best_answer.id,
  1926. },
  1927. ]
  1928. )
  1929. self.assertEqual(response.status_code, 200)
  1930. def test_unmark_best_answer_timelimit(self):
  1931. """api validates if starter has permission to unmark best answer within time limit"""
  1932. self.override_acl({
  1933. 'can_mark_best_answers': 0,
  1934. 'can_change_marked_answers': 1,
  1935. 'best_answer_change_time': 5,
  1936. })
  1937. self.thread.best_answer_marked_on = timezone.now() - timedelta(minutes=6)
  1938. self.thread.starter = self.user
  1939. self.thread.save()
  1940. response = self.patch(
  1941. self.api_link, [
  1942. {
  1943. 'op': 'remove',
  1944. 'path': 'best-answer',
  1945. 'value': self.best_answer.id,
  1946. },
  1947. ]
  1948. )
  1949. self.assertEqual(response.status_code, 400)
  1950. self.assertEqual(response.json(), {
  1951. 'id': self.thread.id,
  1952. 'detail': [
  1953. "You don't have permission to unmark best answer that was marked for more than "
  1954. "5 minutes."
  1955. ],
  1956. })
  1957. thread_json = self.get_thread_json()
  1958. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  1959. # passing scenario is possible
  1960. self.override_acl({
  1961. 'can_mark_best_answers': 0,
  1962. 'can_change_marked_answers': 1,
  1963. 'best_answer_change_time': 10,
  1964. })
  1965. response = self.patch(
  1966. self.api_link, [
  1967. {
  1968. 'op': 'remove',
  1969. 'path': 'best-answer',
  1970. 'value': self.best_answer.id,
  1971. },
  1972. ]
  1973. )
  1974. self.assertEqual(response.status_code, 200)
  1975. def test_unmark_best_answer_closed_category(self):
  1976. """api validates if user has permission to unmark best answer in closed category"""
  1977. self.override_acl({
  1978. 'can_mark_best_answers': 0,
  1979. 'can_change_marked_answers': 2,
  1980. 'can_close_threads': 0,
  1981. })
  1982. self.category.is_closed = True
  1983. self.category.save()
  1984. response = self.patch(
  1985. self.api_link, [
  1986. {
  1987. 'op': 'remove',
  1988. 'path': 'best-answer',
  1989. 'value': self.best_answer.id,
  1990. },
  1991. ]
  1992. )
  1993. self.assertEqual(response.status_code, 400)
  1994. self.assertEqual(response.json(), {
  1995. 'id': self.thread.id,
  1996. 'detail': [
  1997. 'You don\'t have permission to unmark this best answer because its category '
  1998. '"First category" is closed.'
  1999. ],
  2000. })
  2001. thread_json = self.get_thread_json()
  2002. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  2003. # passing scenario is possible
  2004. self.override_acl({
  2005. 'can_mark_best_answers': 0,
  2006. 'can_change_marked_answers': 2,
  2007. 'can_close_threads': 1,
  2008. })
  2009. response = self.patch(
  2010. self.api_link, [
  2011. {
  2012. 'op': 'remove',
  2013. 'path': 'best-answer',
  2014. 'value': self.best_answer.id,
  2015. },
  2016. ]
  2017. )
  2018. self.assertEqual(response.status_code, 200)
  2019. def test_unmark_best_answer_closed_thread(self):
  2020. """api validates if user has permission to unmark best answer in closed thread"""
  2021. self.override_acl({
  2022. 'can_mark_best_answers': 0,
  2023. 'can_change_marked_answers': 2,
  2024. 'can_close_threads': 0,
  2025. })
  2026. self.thread.is_closed = True
  2027. self.thread.save()
  2028. response = self.patch(
  2029. self.api_link, [
  2030. {
  2031. 'op': 'remove',
  2032. 'path': 'best-answer',
  2033. 'value': self.best_answer.id,
  2034. },
  2035. ]
  2036. )
  2037. self.assertEqual(response.status_code, 400)
  2038. self.assertEqual(response.json(), {
  2039. 'id': self.thread.id,
  2040. 'detail': [
  2041. "You can't unmark this thread's best answer because it's closed and you don't "
  2042. "have permission to open it."
  2043. ],
  2044. })
  2045. thread_json = self.get_thread_json()
  2046. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  2047. # passing scenario is possible
  2048. self.override_acl({
  2049. 'can_mark_best_answers': 0,
  2050. 'can_change_marked_answers': 2,
  2051. 'can_close_threads': 1,
  2052. })
  2053. response = self.patch(
  2054. self.api_link, [
  2055. {
  2056. 'op': 'remove',
  2057. 'path': 'best-answer',
  2058. 'value': self.best_answer.id,
  2059. },
  2060. ]
  2061. )
  2062. self.assertEqual(response.status_code, 200)
  2063. def test_unmark_best_answer_protected(self):
  2064. """api validates permission to unmark protected best answers"""
  2065. self.override_acl({
  2066. 'can_mark_best_answers': 0,
  2067. 'can_change_marked_answers': 2,
  2068. 'can_protect_posts': 0,
  2069. })
  2070. self.thread.best_answer_is_protected = True
  2071. self.thread.save()
  2072. response = self.patch(
  2073. self.api_link, [
  2074. {
  2075. 'op': 'remove',
  2076. 'path': 'best-answer',
  2077. 'value': self.best_answer.id,
  2078. },
  2079. ]
  2080. )
  2081. self.assertEqual(response.status_code, 400)
  2082. self.assertEqual(response.json(), {
  2083. 'id': self.thread.id,
  2084. 'detail': [
  2085. "You don't have permission to unmark this thread's best answer because a "
  2086. "moderator has protected it."
  2087. ],
  2088. })
  2089. thread_json = self.get_thread_json()
  2090. self.assertEqual(thread_json['best_answer'], self.best_answer.id)
  2091. # passing scenario is possible
  2092. self.override_acl({
  2093. 'can_mark_best_answers': 0,
  2094. 'can_change_marked_answers': 2,
  2095. 'can_protect_posts': 1,
  2096. })
  2097. response = self.patch(
  2098. self.api_link, [
  2099. {
  2100. 'op': 'remove',
  2101. 'path': 'best-answer',
  2102. 'value': self.best_answer.id,
  2103. },
  2104. ]
  2105. )
  2106. self.assertEqual(response.status_code, 200)