Skip to content

Commit 430aa4e

Browse files
committed
feat: add pagination for GET notification and fix tests accordingly
1 parent 451eb34 commit 430aa4e

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

src/api/notifications/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class NotificationPagination(PageNumberPagination):
2626
class NotificationViewSet(viewsets.ReadOnlyModelViewSet):
2727
permission_classes = [IsAuthenticated]
2828
serializer_class = NotificationSerializer
29+
pagination_class = NotificationPagination
2930

3031
def get_queryset( # pyright: ignore[reportIncompatibleMethodOverride]
3132
self,

src/api/tests/test_notifications.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,29 @@ def test_list_notifications_authenticated(
1818
response = client.get(url)
1919

2020
assert response.status_code == 200
21-
assert len(response.data) == 1
22-
assert response.data[0]["title"] == (
21+
assert response.data["count"] == 1
22+
23+
api_data = response.data["results"]
24+
assert api_data[0]["title"] == (
2325
"CVE-2025-0001 was automatically matched to packages you subscribed to"
2426
)
25-
assert response.data[0]["is_read"] is False
26-
assert "id" in response.data[0]
27-
assert "created_at" in response.data[0]
27+
assert api_data[0]["is_read"] is False
28+
assert "id" in api_data[0]
29+
assert "created_at" in api_data[0]
30+
31+
32+
def test_list_notifications_are_paginated(
33+
user: User,
34+
client: APIClient,
35+
make_maintainer_notification: Callable[..., list[Notification]],
36+
):
37+
make_maintainer_notification(user)
38+
url = reverse("notification-list")
39+
40+
response = client.get(url)
41+
assert "previous" in response.data
42+
assert "next" in response.data
43+
assert "count" in response.data
2844

2945

3046
def test_list_notifications_excludes_other_users_notifications(
@@ -38,7 +54,7 @@ def test_list_notifications_excludes_other_users_notifications(
3854
response = client.get(url)
3955

4056
assert response.status_code == 200
41-
assert len(response.data) == 0
57+
assert response.data["count"] == 0
4258

4359

4460
def test_list_notifications_unauthenticated(
@@ -61,7 +77,7 @@ def test_list_notifications_empty(
6177
response = client.get(url)
6278

6379
assert response.status_code == 200
64-
assert len(response.data) == 0
80+
assert response.data["count"] == 0
6581

6682

6783
def test_list_notifications_includes_text_notifications(
@@ -74,9 +90,11 @@ def test_list_notifications_includes_text_notifications(
7490
response = client.get(url)
7591

7692
assert response.status_code == 200
77-
assert len(response.data) == 1
78-
assert response.data[0]["title"] == "Hello"
79-
assert response.data[0]["is_read"] is False
93+
assert response.data["count"] == 1
94+
95+
api_data = response.data["results"]
96+
assert api_data[0]["title"] == "Hello"
97+
assert api_data[0]["is_read"] is False
8098

8199

82100
def test_list_notifications_shows_both_types(
@@ -91,4 +109,4 @@ def test_list_notifications_shows_both_types(
91109
response = client.get(url)
92110

93111
assert response.status_code == 200
94-
assert len(response.data) == 2
112+
assert response.data["count"] == 2

0 commit comments

Comments
 (0)