From 6ab8724bb7093fadb8bbb2a49c7fc839d13097ef Mon Sep 17 00:00:00 2001 From: Ben White Date: Tue, 12 Dec 2023 14:59:56 +0100 Subject: [PATCH] Fixes --- posthog/api/comments.py | 8 ++++-- posthog/api/test/test_comments.py | 47 ++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/posthog/api/comments.py b/posthog/api/comments.py index 108043e9ec74c..493c470691957 100644 --- a/posthog/api/comments.py +++ b/posthog/api/comments.py @@ -81,10 +81,14 @@ def get_queryset(self) -> QuerySet: if params.get("item_id"): queryset = queryset.filter(item_id=params.get("item_id")) + source_comment_id = params.get("source_comment_id") if self.action == "thread": # Filter based on the source_comment_id - object_id = self.kwargs.get("pk") - queryset = queryset.filter(source_comment_id=object_id) + source_comment_id = self.kwargs.get("pk") + + if source_comment_id: + # NOTE: Should we also return the source_comment ? + queryset = queryset.filter(source_comment_id=source_comment_id) return queryset diff --git a/posthog/api/test/test_comments.py b/posthog/api/test/test_comments.py index 525580b039d51..c621694d7b4fb 100644 --- a/posthog/api/test/test_comments.py +++ b/posthog/api/test/test_comments.py @@ -6,13 +6,17 @@ class TestComments(APIBaseTest, QueryMatchingTest): - def _create_comment(self, content: str, scope: str = "Notebook") -> None: + def _create_comment(self, data={}) -> None: + payload = { + "content": "my content", + "scope": "Notebook", + } + + payload.update(data) + return self.client.post( f"/api/projects/{self.team.id}/comments", - { - "content": content, - "scope": scope, - }, + payload, ).json() def test_creates_comment_with_validation_errors(self) -> None: @@ -91,10 +95,39 @@ def test_empty_comments_list(self) -> None: } def test_lists_comments(self) -> None: - self._create_comment("comment 1") - self._create_comment("comment 2") + self._create_comment({"content": "comment 1"}) + self._create_comment({"content": "comment 2"}) response = self.client.get(f"/api/projects/{self.team.id}/comments") assert len(response.json()["results"]) == 2 assert response.json()["results"][0]["content"] == "comment 2" assert response.json()["results"][1]["content"] == "comment 1" + + def test_lists_comments_filtering(self) -> None: + self._create_comment({"content": "comment notebook-1", "scope": "Notebook", "item_id": "1"}) + self._create_comment({"content": "comment notebook-2", "scope": "Notebook", "item_id": "2"}) + self._create_comment({"content": "comment dashboard-1", "scope": "Dashboard", "item_id": "1"}) + + response = self.client.get(f"/api/projects/{self.team.id}/comments?scope=Notebook") + assert len(response.json()["results"]) == 2 + assert response.json()["results"][0]["content"] == "comment notebook-2" + assert response.json()["results"][1]["content"] == "comment notebook-1" + + response = self.client.get(f"/api/projects/{self.team.id}/comments?scope=Notebook&item_id=2") + assert len(response.json()["results"]) == 1 + assert response.json()["results"][0]["content"] == "comment notebook-2" + + def test_lists_comments_thread(self) -> None: + initial_comment = self._create_comment({"content": "comment notebook-1", "scope": "Notebook", "item_id": "1"}) + self._create_comment({"content": "comment reply", "source_comment_id": initial_comment["id"]}) + self._create_comment({"content": "comment other reply", "source_comment_id": initial_comment["id"]}) + self._create_comment({"content": "comment elsewhere"}) + + for url in [ + f"/api/projects/{self.team.id}/comments/{initial_comment['id']}/thread", + f"/api/projects/{self.team.id}/comments/?source_comment_id={initial_comment['id']}", + ]: + response = self.client.get(url) + assert len(response.json()["results"]) == 2 + assert response.json()["results"][0]["content"] == "comment other reply" + assert response.json()["results"][1]["content"] == "comment reply"