|
| 1 | +from collections.abc import Callable |
| 2 | + |
| 3 | +from django.contrib.auth.models import User |
| 4 | +from rest_framework.reverse import reverse |
| 5 | +from rest_framework.test import APIClient |
| 6 | + |
| 7 | +from webview.models import Notification |
| 8 | + |
| 9 | + |
| 10 | +def test_list_notifications_authenticated( |
| 11 | + client: APIClient, |
| 12 | + user: User, |
| 13 | + make_maintainer_notification: Callable[..., list[Notification]], |
| 14 | +) -> None: |
| 15 | + make_maintainer_notification(user) |
| 16 | + |
| 17 | + url = reverse("notification-list") |
| 18 | + response = client.get(url) |
| 19 | + |
| 20 | + assert response.status_code == 200 |
| 21 | + assert len(response.data) == 1 |
| 22 | + assert response.data[0]["title"] == ( |
| 23 | + "CVE-2025-0001 was automatically matched to packages you subscribed to" |
| 24 | + ) |
| 25 | + assert response.data[0]["is_read"] is False |
| 26 | + assert "id" in response.data[0] |
| 27 | + assert "created_at" in response.data[0] |
| 28 | + |
| 29 | + |
| 30 | +def test_list_notifications_excludes_other_users_notifications( |
| 31 | + client: APIClient, |
| 32 | + staff: User, |
| 33 | + make_maintainer_notification: Callable[..., list[Notification]], |
| 34 | +) -> None: |
| 35 | + make_maintainer_notification(staff) |
| 36 | + |
| 37 | + url = reverse("notification-list") |
| 38 | + response = client.get(url) |
| 39 | + |
| 40 | + assert response.status_code == 200 |
| 41 | + assert len(response.data) == 0 |
| 42 | + |
| 43 | + |
| 44 | +def test_list_notifications_unauthenticated( |
| 45 | + user: User, |
| 46 | + make_maintainer_notification: Callable[..., list[Notification]], |
| 47 | +) -> None: |
| 48 | + make_maintainer_notification(user) |
| 49 | + |
| 50 | + client = APIClient() |
| 51 | + url = reverse("notification-list") |
| 52 | + response = client.get(url) |
| 53 | + |
| 54 | + assert response.status_code == 401 |
| 55 | + |
| 56 | + |
| 57 | +def test_list_notifications_empty( |
| 58 | + client: APIClient, |
| 59 | +) -> None: |
| 60 | + url = reverse("notification-list") |
| 61 | + response = client.get(url) |
| 62 | + |
| 63 | + assert response.status_code == 200 |
| 64 | + assert len(response.data) == 0 |
| 65 | + |
| 66 | + |
| 67 | +def test_list_notifications_includes_text_notifications( |
| 68 | + client: APIClient, |
| 69 | + user: User, |
| 70 | +) -> None: |
| 71 | + user.profile.create_text_notification("Hello", "World") |
| 72 | + |
| 73 | + url = reverse("notification-list") |
| 74 | + response = client.get(url) |
| 75 | + |
| 76 | + 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 |
| 80 | + |
| 81 | + |
| 82 | +def test_list_notifications_shows_both_types( |
| 83 | + client: APIClient, |
| 84 | + user: User, |
| 85 | + make_maintainer_notification: Callable[..., list[Notification]], |
| 86 | +) -> None: |
| 87 | + user.profile.create_text_notification("Hello", "World") |
| 88 | + make_maintainer_notification(user) |
| 89 | + |
| 90 | + url = reverse("notification-list") |
| 91 | + response = client.get(url) |
| 92 | + |
| 93 | + assert response.status_code == 200 |
| 94 | + assert len(response.data) == 2 |
0 commit comments