12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040 |
- import json
- from datetime import timedelta
- from django.urls import reverse
- from django.utils import timezone
- from misago.categories.models import Category
- from misago.threads import test
- from misago.threads.models import Post, Thread
- from misago.threads.test import patch_category_acl
- from misago.users.test import AuthenticatedUserTestCase
- class ThreadPostPatchApiTestCase(AuthenticatedUserTestCase):
- def setUp(self):
- super().setUp()
- self.category = Category.objects.get(slug="first-category")
- self.thread = test.post_thread(category=self.category)
- self.post = test.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"
- )
- 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):
- @patch_category_acl({"can_edit_posts": 2, "can_protect_posts": True})
- def test_protect_post(self):
- """api makes it possible to protect post"""
- 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.post.refresh_from_db()
- self.assertTrue(self.post.is_protected)
- @patch_category_acl({"can_edit_posts": 2, "can_protect_posts": True})
- def test_unprotect_post(self):
- """api makes it possible to unprotect protected post"""
- self.post.is_protected = True
- self.post.save()
- 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.post.refresh_from_db()
- self.assertFalse(self.post.is_protected)
- @patch_category_acl({"can_edit_posts": 2, "can_protect_posts": True})
- 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)
- 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.post.refresh_from_db()
- self.assertTrue(self.post.is_protected)
- self.thread.refresh_from_db()
- self.assertTrue(self.thread.best_answer_is_protected)
- @patch_category_acl({"can_edit_posts": 2, "can_protect_posts": True})
- 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)
- 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.post.refresh_from_db()
- self.assertFalse(self.post.is_protected)
- self.thread.refresh_from_db()
- self.assertFalse(self.thread.best_answer_is_protected)
- @patch_category_acl({"can_edit_posts": 2, "can_protect_posts": False})
- def test_protect_post_no_permission(self):
- """api validates permission to protect post"""
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-protected", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "You can't protect posts in this category."
- )
- self.post.refresh_from_db()
- self.assertFalse(self.post.is_protected)
- @patch_category_acl({"can_edit_posts": 2, "can_protect_posts": False})
- def test_unprotect_post_no_permission(self):
- """api validates permission to unprotect post"""
- self.post.is_protected = True
- self.post.save()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-protected", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "You can't protect posts in this category."
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_protected)
- @patch_category_acl({"can_edit_posts": 0, "can_protect_posts": True})
- def test_protect_post_not_editable(self):
- """api validates if we can edit post we want to protect"""
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-protected", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "You can't protect posts you can't edit."
- )
- self.post.refresh_from_db()
- self.assertFalse(self.post.is_protected)
- @patch_category_acl({"can_edit_posts": 0, "can_protect_posts": True})
- 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()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-protected", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "You can't protect posts you can't edit."
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_protected)
- class PostApproveApiTests(ThreadPostPatchApiTestCase):
- @patch_category_acl({"can_approve_content": True})
- def test_approve_post(self):
- """api makes it possible to approve post"""
- self.post.is_unapproved = True
- self.post.save()
- 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.post.refresh_from_db()
- self.assertFalse(self.post.is_unapproved)
- @patch_category_acl({"can_approve_content": True})
- def test_unapprove_post(self):
- """unapproving posts is not supported by api"""
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-unapproved", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "Content approval can't be reversed."
- )
- self.post.refresh_from_db()
- self.assertFalse(self.post.is_unapproved)
- @patch_category_acl({"can_approve_content": False})
- def test_approve_post_no_permission(self):
- """api validates approval permission"""
- self.post.is_unapproved = True
- self.post.save()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-unapproved", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "You can't approve posts in this category."
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_unapproved)
- @patch_category_acl({"can_approve_content": True, "can_close_threads": False})
- 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()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-unapproved", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "This thread is closed. You can't approve posts in it.",
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_unapproved)
- @patch_category_acl({"can_approve_content": True, "can_close_threads": False})
- 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()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-unapproved", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "This category is closed. You can't approve posts in it.",
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_unapproved)
- @patch_category_acl({"can_approve_content": True})
- 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()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-unapproved", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "You can't approve thread's first post."
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_unapproved)
- @patch_category_acl({"can_approve_content": True})
- def test_approve_hidden_post(self):
- """api approve hidden post fails"""
- self.post.is_unapproved = True
- self.post.is_hidden = True
- self.post.save()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-unapproved", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "You can't approve posts the content you can't see.",
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_unapproved)
- class PostHideApiTests(ThreadPostPatchApiTestCase):
- @patch_category_acl({"can_hide_posts": 1})
- def test_hide_post(self):
- """api makes it possible to hide post"""
- 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.post.refresh_from_db()
- self.assertTrue(self.post.is_hidden)
- @patch_category_acl({"can_hide_posts": 1})
- def test_hide_own_post(self):
- """api makes it possible to hide owned post"""
- 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.post.refresh_from_db()
- self.assertTrue(self.post.is_hidden)
- @patch_category_acl({"can_hide_posts": 0})
- def test_hide_post_no_permission(self):
- """api hide post with no permission fails"""
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "You can't hide posts in this category."
- )
- self.post.refresh_from_db()
- self.assertFalse(self.post.is_hidden)
- @patch_category_acl({"can_hide_own_posts": 1, "can_protect_posts": False})
- 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()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "This post is protected. You can't hide it."
- )
- self.post.refresh_from_db()
- self.assertFalse(self.post.is_hidden)
- @patch_category_acl({"can_hide_own_posts": True})
- def test_hide_other_user_post(self):
- """api validates post ownership when hiding"""
- self.post.poster = None
- self.post.save()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "You can't hide other users posts in this category.",
- )
- self.post.refresh_from_db()
- self.assertFalse(self.post.is_hidden)
- @patch_category_acl({"post_edit_time": 1, "can_hide_own_posts": True})
- 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()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "You can't hide posts that are older than 1 minute.",
- )
- self.post.refresh_from_db()
- self.assertFalse(self.post.is_hidden)
- @patch_category_acl({"can_close_threads": False, "can_hide_own_posts": True})
- 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()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "This thread is closed. You can't hide posts in it.",
- )
- self.post.refresh_from_db()
- self.assertFalse(self.post.is_hidden)
- @patch_category_acl({"can_close_threads": False, "can_hide_own_posts": True})
- 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()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "This category is closed. You can't hide posts in it.",
- )
- self.post.refresh_from_db()
- self.assertFalse(self.post.is_hidden)
- @patch_category_acl({"can_hide_posts": 1})
- def test_hide_first_post(self):
- """api hide first post fails"""
- self.thread.set_first_post(self.post)
- self.thread.save()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "You can't hide thread's first post."
- )
- @patch_category_acl({"can_hide_posts": 1})
- def test_hide_best_answer(self):
- """api hide first post fails"""
- self.thread.set_best_answer(self.user, self.post)
- self.thread.save()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {
- "id": self.post.id,
- "detail": [
- "You can't hide this post because its marked as best answer."
- ],
- },
- )
- class PostUnhideApiTests(ThreadPostPatchApiTestCase):
- @patch_category_acl({"can_hide_posts": 1})
- def test_show_post(self):
- """api makes it possible to unhide post"""
- self.post.is_hidden = True
- self.post.save()
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_hidden)
- 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.post.refresh_from_db()
- self.assertFalse(self.post.is_hidden)
- @patch_category_acl({"can_hide_own_posts": 1})
- def test_show_own_post(self):
- """api makes it possible to unhide owned post"""
- self.post.is_hidden = True
- self.post.save()
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_hidden)
- 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.post.refresh_from_db()
- self.assertFalse(self.post.is_hidden)
- @patch_category_acl({"can_hide_posts": 0})
- def test_show_post_no_permission(self):
- """api unhide post with no permission fails"""
- self.post.is_hidden = True
- self.post.save()
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_hidden)
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "You can't reveal posts in this category."
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_hidden)
- @patch_category_acl({"can_protect_posts": 0, "can_hide_own_posts": 1})
- 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.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, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "This post is protected. You can't reveal it."
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_hidden)
- @patch_category_acl({"can_hide_own_posts": 1})
- 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()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "You can't reveal other users posts in this category.",
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_hidden)
- @patch_category_acl({"post_edit_time": 1, "can_hide_own_posts": 1})
- 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()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "You can't reveal posts that are older than 1 minute.",
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_hidden)
- @patch_category_acl({"can_close_threads": False, "can_hide_own_posts": 1})
- 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()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "This thread is closed. You can't reveal posts in it.",
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_hidden)
- @patch_category_acl({"can_close_threads": False, "can_hide_own_posts": 1})
- 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()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "This category is closed. You can't reveal posts in it.",
- )
- self.post.refresh_from_db()
- self.assertTrue(self.post.is_hidden)
- @patch_category_acl({"can_hide_posts": 1})
- def test_show_first_post(self):
- """api unhide first post fails"""
- self.thread.set_first_post(self.post)
- self.thread.save()
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": False}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "You can't reveal thread's first post."
- )
- class PostLikeApiTests(ThreadPostPatchApiTestCase):
- @patch_category_acl({"can_see_posts_likes": 0})
- def test_like_no_see_permission(self):
- """api validates user's permission to see posts likes"""
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-liked", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"id": self.post.id, "detail": ["You can't like posts in this category."]},
- )
- @patch_category_acl({"can_like_posts": False})
- def test_like_no_like_permission(self):
- """api validates user's permission to see posts likes"""
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-liked", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- self.assertEqual(
- response.json(),
- {"id": self.post.id, "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"""
- test.like_post(self.post, username="Myo")
- test.like_post(self.post, username="Mugi")
- test.like_post(self.post, username="Bob")
- test.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"""
- test.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"""
- test.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().setUp()
- self.event = test.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):
- @patch_category_acl({"can_hide_events": 1})
- def test_hide_event(self):
- """api makes it possible to hide event"""
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": True}]
- )
- self.assertEqual(response.status_code, 200)
- self.event.refresh_from_db()
- self.assertTrue(self.event.is_hidden)
- @patch_category_acl({"can_hide_events": 1})
- def test_show_event(self):
- """api makes it possible to unhide event"""
- self.event.is_hidden = True
- self.event.save()
- self.event.refresh_from_db()
- self.assertTrue(self.event.is_hidden)
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": False}]
- )
- self.assertEqual(response.status_code, 200)
- self.event.refresh_from_db()
- self.assertFalse(self.event.is_hidden)
- @patch_category_acl({"can_hide_events": 0})
- def test_hide_event_no_permission(self):
- """api hide event with no permission fails"""
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": True}]
- )
- self.assertEqual(response.status_code, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0], "You can't hide events in this category."
- )
- self.event.refresh_from_db()
- self.assertFalse(self.event.is_hidden)
- @patch_category_acl({"can_close_threads": False, "can_hide_events": 1})
- def test_hide_event_closed_thread_no_permission(self):
- """api hide event in closed thread with no permission fails"""
- 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, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "This thread is closed. You can't hide events in it.",
- )
- self.event.refresh_from_db()
- self.assertFalse(self.event.is_hidden)
- @patch_category_acl({"can_close_threads": False, "can_hide_events": 1})
- def test_hide_event_closed_category_no_permission(self):
- """api hide event in closed category with no permission fails"""
- 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, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "This category is closed. You can't hide events in it.",
- )
- self.event.refresh_from_db()
- self.assertFalse(self.event.is_hidden)
- @patch_category_acl({"can_hide_events": 0})
- def test_show_event_no_permission(self):
- """api unhide event with no permission fails"""
- self.event.is_hidden = True
- self.event.save()
- self.event.refresh_from_db()
- self.assertTrue(self.event.is_hidden)
- response = self.patch(
- self.api_link, [{"op": "replace", "path": "is-hidden", "value": False}]
- )
- self.assertEqual(response.status_code, 404)
- @patch_category_acl({"can_close_threads": False, "can_hide_events": 1})
- 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.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, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "This thread is closed. You can't reveal events in it.",
- )
- self.event.refresh_from_db()
- self.assertTrue(self.event.is_hidden)
- @patch_category_acl({"can_close_threads": False, "can_hide_events": 1})
- 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.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, 400)
- response_json = response.json()
- self.assertEqual(
- response_json["detail"][0],
- "This category is closed. You can't reveal events in it.",
- )
- self.event.refresh_from_db()
- self.assertTrue(self.event.is_hidden)
|