From e43d0a052c867885447dcb1edc83ced347e1ec69 Mon Sep 17 00:00:00 2001 From: Humberto Rocha Date: Wed, 15 Feb 2023 18:05:48 -0500 Subject: [PATCH] Fix unliked post displaying in favourites endpoint (#512) --- activities/services/timeline.py | 9 ++++++++- api/schemas.py | 10 +++++----- tests/api/test_likes.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/activities/services/timeline.py b/activities/services/timeline.py index f034abe..8450dbf 100644 --- a/activities/services/timeline.py +++ b/activities/services/timeline.py @@ -1,6 +1,12 @@ from django.db import models -from activities.models import Hashtag, Post, PostInteraction, TimelineEvent +from activities.models import ( + Hashtag, + Post, + PostInteraction, + PostInteractionStates, + TimelineEvent, +) from activities.services import PostService from users.models import Identity @@ -92,6 +98,7 @@ class TimelineService: .filter( interactions__identity=self.identity, interactions__type=PostInteraction.Types.like, + interactions__state__in=PostInteractionStates.group_active(), ) .order_by("-id") ) diff --git a/api/schemas.py b/api/schemas.py index c40e00f..f96371e 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -154,11 +154,11 @@ class Status(Schema): language: None = Field(...) text: str | None = Field(...) edited_at: str | None - favourited: bool | None - reblogged: bool | None - muted: bool | None - bookmarked: bool | None - pinned: bool | None + favourited: bool = False + reblogged: bool = False + muted: bool = False + bookmarked: bool = False + pinned: bool = False @classmethod def from_post( diff --git a/tests/api/test_likes.py b/tests/api/test_likes.py index a7b689d..b20676f 100644 --- a/tests/api/test_likes.py +++ b/tests/api/test_likes.py @@ -23,3 +23,31 @@ def test_likes_flow(api_client): # Check if it's displaying at likes endpoint response = api_client.get("/api/v1/favourites").json() assert response[0]["id"] == status_id + + +@pytest.mark.django_db +def test_unlike(api_client): + # Add a post + response = api_client.post( + "/api/v1/statuses", + content_type="application/json", + data={ + "status": "Like test.", + "visibility": "public", + }, + ).json() + assert response["content"] == "

Like test.

" + + status_id = response["id"] + + # Like it + response = api_client.post(f"/api/v1/statuses/{status_id}/favourite").json() + assert response["favourited"] is True + + # Unlike it + response = api_client.post(f"/api/v1/statuses/{status_id}/unfavourite").json() + assert response["favourited"] is False + + # Unliked post should not display at the endpoint + response = api_client.get("/api/v1/favourites").json() + assert len(response) == 0