123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386 |
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- import json
- from datetime import timedelta
- from django.urls import reverse
- from django.utils import timezone
- from misago.acl.testutils import override_acl
- from misago.categories.models import Category
- from misago.threads import testutils
- from misago.threads.models import Thread, Post
- from misago.users.testutils import AuthenticatedUserTestCase
- class ThreadPostPatchApiTestCase(AuthenticatedUserTestCase):
- def setUp(self):
- super(ThreadPostPatchApiTestCase, self).setUp()
- self.category = Category.objects.get(slug='first-category')
- self.thread = testutils.post_thread(category=self.category)
- self.post = testutils.reply_thread(self.thread, poster=self.user)
- self.api_link = reverse(
- 'misago:api:thread-post-detail',
- kwargs={
- 'thread_pk': self.thread.pk,
- 'pk': self.post.pk,
- },
- )
- def patch(self, api_link, ops):
- return self.client.patch(api_link, json.dumps(ops), content_type="application/json")
- def refresh_post(self):
- self.post = self.thread.post_set.get(pk=self.post.pk)
- def refresh_thread(self):
- self.thread = Thread.objects.get(pk=self.thread.pk)
- def override_acl(self, extra_acl=None):
- new_acl = self.user.acl_cache
- new_acl['categories'][self.category.pk].update({
- 'can_see': 1,
- 'can_browse': 1,
- 'can_start_threads': 0,
- 'can_reply_threads': 0,
- 'can_edit_posts': 1,
- })
- if extra_acl:
- new_acl['categories'][self.category.pk].update(extra_acl)
- override_acl(self.user, new_acl)
- class PostAddAclApiTests(ThreadPostPatchApiTestCase):
- def test_add_acl_true(self):
- """api adds current event's acl to response"""
- response = self.patch(self.api_link, [
- {
- 'op': 'add',
- 'path': 'acl',
- 'value': True,
- },
- ])
- self.assertEqual(response.status_code, 200)
- response_json = response.json()
- self.assertTrue(response_json['acl'])
- def test_add_acl_false(self):
- """if value is false, api won't add acl to the response, but will set empty key"""
- response = self.patch(self.api_link, [
- {
- 'op': 'add',
- 'path': 'acl',
- 'value': False,
- },
- ])
- self.assertEqual(response.status_code, 200)
- response_json = response.json()
- self.assertIsNone(response_json['acl'])
- class PostProtectApiTests(ThreadPostPatchApiTestCase):
- def test_protect_post(self):
- """api makes it possible to protect post"""
- self.override_acl({'can_protect_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-protected',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- reponse_json = response.json()
- self.assertTrue(reponse_json['is_protected'])
- self.refresh_post()
- self.assertTrue(self.post.is_protected)
- def test_unprotect_post(self):
- """api makes it possible to unprotect protected post"""
- self.post.is_protected = True
- self.post.save()
- self.override_acl({'can_protect_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-protected',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- reponse_json = response.json()
- self.assertFalse(reponse_json['is_protected'])
- self.refresh_post()
- self.assertFalse(self.post.is_protected)
- def test_protect_best_answer(self):
- """api makes it possible to protect post"""
- self.thread.set_best_answer(self.user, self.post)
- self.thread.save()
- self.assertFalse(self.thread.best_answer_is_protected)
-
- self.override_acl({'can_protect_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-protected',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- reponse_json = response.json()
- self.assertTrue(reponse_json['is_protected'])
- self.refresh_post()
- self.assertTrue(self.post.is_protected)
- self.refresh_thread()
- self.assertTrue(self.thread.best_answer_is_protected)
- def test_unprotect_best_answer(self):
- """api makes it possible to unprotect protected post"""
- self.post.is_protected = True
- self.post.save()
- self.thread.set_best_answer(self.user, self.post)
- self.thread.save()
- self.assertTrue(self.thread.best_answer_is_protected)
- self.override_acl({'can_protect_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-protected',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- reponse_json = response.json()
- self.assertFalse(reponse_json['is_protected'])
- self.refresh_post()
- self.assertFalse(self.post.is_protected)
- self.refresh_thread()
- self.assertFalse(self.thread.best_answer_is_protected)
- def test_protect_post_no_permission(self):
- """api validates permission to protect post"""
- self.override_acl({'can_protect_posts': 0})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-protected',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't protect posts in this category.",
- })
- self.refresh_post()
- self.assertFalse(self.post.is_protected)
- def test_unprotect_post_no_permission(self):
- """api validates permission to unprotect post"""
- self.post.is_protected = True
- self.post.save()
- self.override_acl({'can_protect_posts': 0})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-protected',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't protect posts in this category.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_protected)
- def test_protect_post_not_editable(self):
- """api validates if we can edit post we want to protect"""
- self.override_acl({'can_edit_posts': 0, 'can_protect_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-protected',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't protect posts you can't edit.",
- })
- self.refresh_post()
- self.assertFalse(self.post.is_protected)
- def test_unprotect_post_not_editable(self):
- """api validates if we can edit post we want to protect"""
- self.post.is_protected = True
- self.post.save()
- self.override_acl({'can_edit_posts': 0, 'can_protect_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-protected',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't protect posts you can't edit.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_protected)
- class PostApproveApiTests(ThreadPostPatchApiTestCase):
- def test_approve_post(self):
- """api makes it possible to approve post"""
- self.post.is_unapproved = True
- self.post.save()
- self.override_acl({'can_approve_content': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-unapproved',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- reponse_json = response.json()
- self.assertFalse(reponse_json['is_unapproved'])
- self.refresh_post()
- self.assertFalse(self.post.is_unapproved)
- def test_unapprove_post(self):
- """unapproving posts is not supported by api"""
- self.override_acl({'can_approve_content': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-unapproved',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "Content approval can't be reversed.",
- })
- self.refresh_post()
- self.assertFalse(self.post.is_unapproved)
- def test_approve_post_no_permission(self):
- """api validates approval permission"""
- self.post.is_unapproved = True
- self.post.save()
- self.override_acl({'can_approve_content': 0})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-unapproved',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't approve posts in this category.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_unapproved)
- def test_approve_post_closed_thread_no_permission(self):
- """api validates approval permission in closed threads"""
- self.post.is_unapproved = True
- self.post.save()
- self.thread.is_closed = True
- self.thread.save()
- self.override_acl({
- 'can_approve_content': 1,
- 'can_close_threads': 0,
- })
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-unapproved',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "This thread is closed. You can't approve posts in it.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_unapproved)
- def test_approve_post_closed_category_no_permission(self):
- """api validates approval permission in closed categories"""
- self.post.is_unapproved = True
- self.post.save()
- self.category.is_closed = True
- self.category.save()
- self.override_acl({
- 'can_approve_content': 1,
- 'can_close_threads': 0,
- })
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-unapproved',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "This category is closed. You can't approve posts in it.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_unapproved)
- def test_approve_first_post(self):
- """api approve first post fails"""
- self.post.is_unapproved = True
- self.post.save()
- self.thread.set_first_post(self.post)
- self.thread.save()
- self.override_acl({'can_approve_content': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-unapproved',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't approve thread's first post.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_unapproved)
- def test_approve_hidden_post(self):
- """api approve hidden post fails"""
- self.post.is_unapproved = True
- self.post.is_hidden = True
- self.post.save()
- self.override_acl({'can_approve_content': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-unapproved',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't approve posts the content you can't see.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_unapproved)
- class PostHideApiTests(ThreadPostPatchApiTestCase):
- def test_hide_post(self):
- """api makes it possible to hide post"""
- self.override_acl({'can_hide_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- reponse_json = response.json()
- self.assertTrue(reponse_json['is_hidden'])
- self.refresh_post()
- self.assertTrue(self.post.is_hidden)
- def test_hide_own_post(self):
- """api makes it possible to hide owned post"""
- self.override_acl({'can_hide_own_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- reponse_json = response.json()
- self.assertTrue(reponse_json['is_hidden'])
- self.refresh_post()
- self.assertTrue(self.post.is_hidden)
- def test_hide_post_no_permission(self):
- """api hide post with no permission fails"""
- self.override_acl({'can_hide_posts': 0})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't hide posts in this category.",
- })
- self.refresh_post()
- self.assertFalse(self.post.is_hidden)
- def test_hide_own_protected_post(self):
- """api validates if we are trying to hide protected post"""
- self.post.is_protected = True
- self.post.save()
- self.override_acl({'can_protect_posts': 0, 'can_hide_own_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "This post is protected. You can't hide it.",
- })
- self.refresh_post()
- self.assertFalse(self.post.is_hidden)
- def test_hide_other_user_post(self):
- """api validates post ownership when hiding"""
- self.post.poster = None
- self.post.save()
- self.override_acl({'can_hide_own_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't hide other users posts in this category.",
- })
- self.refresh_post()
- self.assertFalse(self.post.is_hidden)
- def test_hide_own_post_after_edit_time(self):
- """api validates if we are trying to hide post after edit time"""
- self.post.posted_on = timezone.now() - timedelta(minutes=10)
- self.post.save()
- self.override_acl({'post_edit_time': 1, 'can_hide_own_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't hide posts that are older than 1 minute.",
- })
- self.refresh_post()
- self.assertFalse(self.post.is_hidden)
- def test_hide_post_in_closed_thread(self):
- """api validates if we are trying to hide post in closed thread"""
- self.thread.is_closed = True
- self.thread.save()
- self.override_acl({'can_hide_own_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "This thread is closed. You can't hide posts in it.",
- })
- self.refresh_post()
- self.assertFalse(self.post.is_hidden)
- def test_hide_post_in_closed_category(self):
- """api validates if we are trying to hide post in closed category"""
- self.category.is_closed = True
- self.category.save()
- self.override_acl({'can_hide_own_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "This category is closed. You can't hide posts in it.",
- })
- self.refresh_post()
- self.assertFalse(self.post.is_hidden)
- def test_hide_first_post(self):
- """api hide first post fails"""
- self.thread.set_first_post(self.post)
- self.thread.save()
- self.override_acl({'can_hide_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't hide thread's first post.",
- })
- def test_hide_best_answer(self):
- """api hide first post fails"""
- self.thread.set_best_answer(self.user, self.post)
- self.thread.save()
- self.override_acl({'can_hide_posts': 2})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't hide this post because its marked as best answer.",
- })
- class PostUnhideApiTests(ThreadPostPatchApiTestCase):
- def test_show_post(self):
- """api makes it possible to unhide post"""
- self.post.is_hidden = True
- self.post.save()
- self.refresh_post()
- self.assertTrue(self.post.is_hidden)
- self.override_acl({'can_hide_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- reponse_json = response.json()
- self.assertFalse(reponse_json['is_hidden'])
- self.refresh_post()
- self.assertFalse(self.post.is_hidden)
- def test_show_own_post(self):
- """api makes it possible to unhide owned post"""
- self.post.is_hidden = True
- self.post.save()
- self.refresh_post()
- self.assertTrue(self.post.is_hidden)
- self.override_acl({'can_hide_own_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- reponse_json = response.json()
- self.assertFalse(reponse_json['is_hidden'])
- self.refresh_post()
- self.assertFalse(self.post.is_hidden)
- def test_show_post_no_permission(self):
- """api unhide post with no permission fails"""
- self.post.is_hidden = True
- self.post.save()
- self.refresh_post()
- self.assertTrue(self.post.is_hidden)
- self.override_acl({'can_hide_posts': 0})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't reveal posts in this category.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_hidden)
- def test_show_own_protected_post(self):
- """api validates if we are trying to reveal protected post"""
- self.post.is_hidden = True
- self.post.save()
- self.override_acl({'can_protect_posts': 0, 'can_hide_own_posts': 1})
- self.post.is_protected = True
- self.post.save()
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "This post is protected. You can't reveal it.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_hidden)
- def test_show_other_user_post(self):
- """api validates post ownership when revealing"""
- self.post.is_hidden = True
- self.post.poster = None
- self.post.save()
- self.override_acl({'can_hide_own_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't reveal other users posts in this category.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_hidden)
- def test_show_own_post_after_edit_time(self):
- """api validates if we are trying to reveal post after edit time"""
- self.post.is_hidden = True
- self.post.posted_on = timezone.now() - timedelta(minutes=10)
- self.post.save()
- self.override_acl({'post_edit_time': 1, 'can_hide_own_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't reveal posts that are older than 1 minute.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_hidden)
- def test_show_post_in_closed_thread(self):
- """api validates if we are trying to reveal post in closed thread"""
- self.thread.is_closed = True
- self.thread.save()
- self.post.is_hidden = True
- self.post.save()
- self.override_acl({'can_hide_own_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "This thread is closed. You can't reveal posts in it.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_hidden)
- def test_show_post_in_closed_category(self):
- """api validates if we are trying to reveal post in closed category"""
- self.category.is_closed = True
- self.category.save()
- self.post.is_hidden = True
- self.post.save()
- self.override_acl({'can_hide_own_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "This category is closed. You can't reveal posts in it.",
- })
- self.refresh_post()
- self.assertTrue(self.post.is_hidden)
- def test_show_first_post(self):
- """api unhide first post fails"""
- self.thread.set_first_post(self.post)
- self.thread.save()
- self.override_acl({'can_hide_posts': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't reveal thread's first post.",
- })
- class PostLikeApiTests(ThreadPostPatchApiTestCase):
- def test_like_no_see_permission(self):
- """api validates user's permission to see posts likes"""
- self.override_acl({'can_see_posts_likes': 0})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-liked',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't like posts in this category.",
- })
- def test_like_no_like_permission(self):
- """api validates user's permission to see posts likes"""
- self.override_acl({'can_like_posts': False})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-liked',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't like posts in this category.",
- })
- def test_like_post(self):
- """api adds user like to post"""
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-liked',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- response_json = response.json()
- self.assertEqual(response_json['likes'], 1)
- self.assertEqual(response_json['is_liked'], True)
- self.assertEqual(
- response_json['last_likes'], [
- {
- 'id': self.user.id,
- 'username': self.user.username,
- },
- ]
- )
- post = Post.objects.get(pk=self.post.pk)
- self.assertEqual(post.likes, response_json['likes'])
- self.assertEqual(post.last_likes, response_json['last_likes'])
- def test_like_liked_post(self):
- """api adds user like to post"""
- testutils.like_post(self.post, username='Myo')
- testutils.like_post(self.post, username='Mugi')
- testutils.like_post(self.post, username='Bob')
- testutils.like_post(self.post, username='Miku')
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-liked',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- response_json = response.json()
- self.assertEqual(response_json['likes'], 5)
- self.assertEqual(response_json['is_liked'], True)
- self.assertEqual(
- response_json['last_likes'], [
- {
- 'id': self.user.id,
- 'username': self.user.username
- },
- {
- 'id': None,
- 'username': 'Miku',
- },
- {
- 'id': None,
- 'username': 'Bob',
- },
- {
- 'id': None,
- 'username': 'Mugi',
- },
- ]
- )
- post = Post.objects.get(pk=self.post.pk)
- self.assertEqual(post.likes, response_json['likes'])
- self.assertEqual(post.last_likes, response_json['last_likes'])
- def test_unlike_post(self):
- """api removes user like from post"""
- testutils.like_post(self.post, self.user)
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-liked',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- response_json = response.json()
- self.assertEqual(response_json['likes'], 0)
- self.assertEqual(response_json['is_liked'], False)
- self.assertEqual(response_json['last_likes'], [])
- post = Post.objects.get(pk=self.post.pk)
- self.assertEqual(post.likes, response_json['likes'])
- self.assertEqual(post.last_likes, response_json['last_likes'])
- def test_like_post_no_change(self):
- """api does no state change if we are linking liked post"""
- testutils.like_post(self.post, self.user)
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-liked',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- response_json = response.json()
- self.assertEqual(response_json['likes'], 1)
- self.assertEqual(response_json['is_liked'], True)
- self.assertEqual(
- response_json['last_likes'], [
- {
- 'id': self.user.id,
- 'username': self.user.username,
- },
- ]
- )
- post = Post.objects.get(pk=self.post.pk)
- self.assertEqual(post.likes, response_json['likes'])
- self.assertEqual(post.last_likes, response_json['last_likes'])
- def test_unlike_post_no_change(self):
- """api does no state change if we are unlinking unliked post"""
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-liked',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- response_json = response.json()
- self.assertEqual(response_json['likes'], 0)
- self.assertEqual(response_json['is_liked'], False)
- self.assertEqual(response_json['last_likes'], [])
- class ThreadEventPatchApiTestCase(ThreadPostPatchApiTestCase):
- def setUp(self):
- super(ThreadEventPatchApiTestCase, self).setUp()
- self.event = testutils.reply_thread(self.thread, poster=self.user, is_event=True)
- self.api_link = reverse(
- 'misago:api:thread-post-detail',
- kwargs={
- 'thread_pk': self.thread.pk,
- 'pk': self.event.pk,
- }
- )
- def refresh_event(self):
- self.event = self.thread.post_set.get(pk=self.event.pk)
- class EventAnonPatchApiTests(ThreadEventPatchApiTestCase):
- def test_anonymous_user(self):
- """anonymous users can't change event state"""
- self.logout_user()
- response = self.patch(self.api_link, [
- {
- 'op': 'add',
- 'path': 'acl',
- 'value': True,
- },
- ])
- self.assertEqual(response.status_code, 403)
- class EventAddAclApiTests(ThreadEventPatchApiTestCase):
- def test_add_acl_true(self):
- """api adds current event's acl to response"""
- response = self.patch(self.api_link, [
- {
- 'op': 'add',
- 'path': 'acl',
- 'value': True,
- },
- ])
- self.assertEqual(response.status_code, 200)
- response_json = response.json()
- self.assertTrue(response_json['acl'])
- def test_add_acl_false(self):
- """if value is false, api won't add acl to the response, but will set empty key"""
- response = self.patch(self.api_link, [
- {
- 'op': 'add',
- 'path': 'acl',
- 'value': False,
- },
- ])
- self.assertEqual(response.status_code, 200)
- response_json = response.json()
- self.assertIsNone(response_json['acl'])
- response = self.patch(self.api_link, [
- {
- 'op': 'add',
- 'path': 'acl',
- 'value': True,
- },
- ])
- self.assertEqual(response.status_code, 200)
- class EventHideApiTests(ThreadEventPatchApiTestCase):
- def test_hide_event(self):
- """api makes it possible to hide event"""
- self.override_acl({'can_hide_events': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- self.refresh_event()
- self.assertTrue(self.event.is_hidden)
- def test_show_event(self):
- """api makes it possible to unhide event"""
- self.event.is_hidden = True
- self.event.save()
- self.refresh_event()
- self.assertTrue(self.event.is_hidden)
- self.override_acl({'can_hide_events': 1})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 200)
- self.refresh_event()
- self.assertFalse(self.event.is_hidden)
- def test_hide_event_no_permission(self):
- """api hide event with no permission fails"""
- self.override_acl({'can_hide_events': 0})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "You can't hide events in this category.",
- })
- self.refresh_event()
- self.assertFalse(self.event.is_hidden)
- def test_hide_event_closed_thread_no_permission(self):
- """api hide event in closed thread with no permission fails"""
- self.override_acl({
- 'can_hide_events': 1,
- 'can_close_threads': 0,
- })
- self.thread.is_closed = True
- self.thread.save()
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "This thread is closed. You can't hide events in it.",
- })
- self.refresh_event()
- self.assertFalse(self.event.is_hidden)
- def test_hide_event_closed_category_no_permission(self):
- """api hide event in closed category with no permission fails"""
- self.override_acl({
- 'can_hide_events': 1,
- 'can_close_threads': 0,
- })
- self.category.is_closed = True
- self.category.save()
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': True,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "This category is closed. You can't hide events in it.",
- })
- self.refresh_event()
- self.assertFalse(self.event.is_hidden)
- def test_show_event_no_permission(self):
- """api unhide event with no permission fails"""
- self.event.is_hidden = True
- self.event.save()
- self.refresh_event()
- self.assertTrue(self.event.is_hidden)
- self.override_acl({'can_hide_events': 0})
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 404)
- def test_show_event_closed_thread_no_permission(self):
- """api show event in closed thread with no permission fails"""
- self.event.is_hidden = True
- self.event.save()
- self.override_acl({
- 'can_hide_events': 1,
- 'can_close_threads': 0,
- })
- self.thread.is_closed = True
- self.thread.save()
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "This thread is closed. You can't reveal events in it.",
- })
- self.refresh_event()
- self.assertTrue(self.event.is_hidden)
- def test_show_event_closed_category_no_permission(self):
- """api show event in closed category with no permission fails"""
- self.event.is_hidden = True
- self.event.save()
- self.override_acl({
- 'can_hide_events': 1,
- 'can_close_threads': 0,
- })
- self.category.is_closed = True
- self.category.save()
- response = self.patch(
- self.api_link, [
- {
- 'op': 'replace',
- 'path': 'is-hidden',
- 'value': False,
- },
- ]
- )
- self.assertEqual(response.status_code, 403)
- self.assertEqual(response.json(), {
- 'detail': "This category is closed. You can't reveal events in it.",
- })
- self.refresh_event()
- self.assertTrue(self.event.is_hidden)
|