Skip to content

Commit

Permalink
Set the moderator when moderating news
Browse files Browse the repository at this point in the history
  • Loading branch information
imperosol committed Jan 23, 2025
1 parent 2ca3286 commit 976cfad
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions com/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def moderate_news(self, news_id: int):
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(
Expand Down
8 changes: 8 additions & 0 deletions com/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,21 @@ def test_moderation_ok(self, client: Client, news_is_moderated: bool): # noqa F
# 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)
Expand Down

0 comments on commit 976cfad

Please sign in to comment.