From 96f587478680230a5f0b9392d817b9bb29de1f6a Mon Sep 17 00:00:00 2001 From: imperosol Date: Thu, 23 Jan 2025 14:32:10 +0100 Subject: [PATCH] Set the moderator when moderating news --- com/api.py | 1 + com/tests/test_api.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/com/api.py b/com/api.py index b39beac4..4a4d0e17 100644 --- a/com/api.py +++ b/com/api.py @@ -45,6 +45,7 @@ class NewsController(ControllerBase): news = self.get_object_or_exception(News, id=news_id) if not news.is_moderated: news.is_moderated = True + news.moderator = self.context.request.user news.save() @route.delete( diff --git a/com/tests/test_api.py b/com/tests/test_api.py index 1bcb4d13..8e4b0089 100644 --- a/com/tests/test_api.py +++ b/com/tests/test_api.py @@ -136,13 +136,21 @@ class TestModerateNews: # The API call should work even if the news is initially moderated. # In the latter case, the result should be a noop, rather than an error. news = baker.make(News, is_moderated=news_is_moderated) + initial_moderator = news.moderator client.force_login(user) response = client.patch( reverse("api:moderate_news", kwargs={"news_id": news.id}) ) + # if it wasn't moderated, it should now be moderated and the moderator should + # be the user that made the request. + # If it was already moderated, it should be a no-op, but not an error assert response.status_code == 200 news.refresh_from_db() assert news.is_moderated + if not news_is_moderated: + assert news.moderator == user + else: + assert news.moderator == initial_moderator def test_moderation_forbidden(self, client: Client): user = baker.make(User)