|
| 1 | +import pytest |
| 2 | +from django.contrib.auth import get_user_model |
| 3 | +from django.contrib.gis.geos import Point |
| 4 | +from django.test import override_settings |
| 5 | + |
| 6 | +from libraries.models import Library |
| 7 | + |
| 8 | +User = get_user_model() |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def countries_user(db): |
| 13 | + """Create a user for countries endpoint test fixtures. |
| 14 | + Provides a baseline creator for library objects.""" |
| 15 | + return User.objects.create_user( |
| 16 | + username="countriesuser", |
| 17 | + password="countriespass123", |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +def _create_library(*, user, country="IT", city="Florence", status=Library.Status.APPROVED): |
| 22 | + """Create a library with minimal required fields. |
| 23 | + Reduces boilerplate across countries test cases.""" |
| 24 | + return Library.objects.create( |
| 25 | + name=f"Lib in {city}", |
| 26 | + photo="libraries/photos/2026/02/test.jpg", |
| 27 | + location=Point(x=11.2558, y=43.7696, srid=4326), |
| 28 | + address="Via Rosina 15", |
| 29 | + city=city, |
| 30 | + country=country, |
| 31 | + status=status, |
| 32 | + created_by=user, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.django_db |
| 37 | +class TestCountriesEndpoint: |
| 38 | + """Tests for the GET /api/v1/libraries/countries/ endpoint.""" |
| 39 | + |
| 40 | + def test_returns_all_countries_with_approved_libraries(self, client, countries_user): |
| 41 | + """Verify the endpoint returns every country that has approved libraries. |
| 42 | + Confirms no top-N limit is applied.""" |
| 43 | + countries = ["IT", "DE", "FR", "ES", "GB", "US", "NL", "BE", "AT", "CH", "PT", "SE"] |
| 44 | + for code in countries: |
| 45 | + _create_library(user=countries_user, country=code, city=f"City-{code}") |
| 46 | + |
| 47 | + response = client.get("/api/v1/libraries/countries/") |
| 48 | + |
| 49 | + assert response.status_code == 200 |
| 50 | + data = response.json() |
| 51 | + assert len(data["items"]) == 12 |
| 52 | + |
| 53 | + def test_empty_db_returns_empty_list(self, client, db): |
| 54 | + """Verify the endpoint returns an empty items list when no libraries exist. |
| 55 | + Confirms graceful handling of the zero-data case.""" |
| 56 | + response = client.get("/api/v1/libraries/countries/") |
| 57 | + |
| 58 | + assert response.status_code == 200 |
| 59 | + assert response.json()["items"] == [] |
| 60 | + |
| 61 | + def test_excludes_pending_and_rejected_libraries(self, client, countries_user): |
| 62 | + """Verify only approved libraries contribute to country counts. |
| 63 | + Confirms non-approved statuses are filtered out.""" |
| 64 | + _create_library(user=countries_user, country="IT", city="Florence") |
| 65 | + _create_library(user=countries_user, country="DE", city="Berlin", status=Library.Status.PENDING) |
| 66 | + _create_library(user=countries_user, country="FR", city="Paris", status=Library.Status.REJECTED) |
| 67 | + |
| 68 | + response = client.get("/api/v1/libraries/countries/") |
| 69 | + |
| 70 | + data = response.json() |
| 71 | + assert len(data["items"]) == 1 |
| 72 | + assert data["items"][0]["country_code"] == "IT" |
| 73 | + |
| 74 | + def test_ordered_by_count_descending(self, client, countries_user): |
| 75 | + """Verify countries are ordered by library count, most first. |
| 76 | + Confirms the descending sort contract.""" |
| 77 | + for _ in range(3): |
| 78 | + _create_library(user=countries_user, country="DE", city="Berlin") |
| 79 | + _create_library(user=countries_user, country="IT", city="Florence") |
| 80 | + |
| 81 | + response = client.get("/api/v1/libraries/countries/") |
| 82 | + |
| 83 | + items = response.json()["items"] |
| 84 | + assert items[0]["country_code"] == "DE" |
| 85 | + assert items[0]["count"] == 3 |
| 86 | + assert items[1]["country_code"] == "IT" |
| 87 | + assert items[1]["count"] == 1 |
| 88 | + |
| 89 | + def test_country_entry_has_expected_fields(self, client, countries_user): |
| 90 | + """Verify each country entry contains code, name, flag, and count. |
| 91 | + Confirms the schema contract for the country list.""" |
| 92 | + _create_library(user=countries_user, country="FR", city="Paris") |
| 93 | + |
| 94 | + response = client.get("/api/v1/libraries/countries/") |
| 95 | + |
| 96 | + item = response.json()["items"][0] |
| 97 | + assert item["country_code"] == "FR" |
| 98 | + assert item["country_name"] == "France" |
| 99 | + assert item["flag_emoji"] == "\U0001F1EB\U0001F1F7" |
| 100 | + assert item["count"] == 1 |
| 101 | + |
| 102 | + def test_public_access_without_authentication(self, client, db): |
| 103 | + """Verify the endpoint is publicly accessible without auth. |
| 104 | + Confirms no JWT or session is required.""" |
| 105 | + response = client.get("/api/v1/libraries/countries/") |
| 106 | + |
| 107 | + assert response.status_code == 200 |
| 108 | + |
| 109 | + @override_settings(API_RATE_LIMIT_ENABLED=True, API_RATE_LIMIT_READ_REQUESTS=1, API_RATE_LIMIT_WINDOW_SECONDS=60) |
| 110 | + def test_rate_limiting_returns_429(self, client, countries_user): |
| 111 | + """Verify the endpoint enforces rate limiting. |
| 112 | + Confirms excessive requests receive a 429 response.""" |
| 113 | + client.get("/api/v1/libraries/countries/") |
| 114 | + |
| 115 | + response = client.get("/api/v1/libraries/countries/") |
| 116 | + |
| 117 | + assert response.status_code == 429 |
| 118 | + |
| 119 | + def test_cache_control_header(self, client, countries_user): |
| 120 | + """Verify GET /libraries/countries/ returns 1-hour public cache headers. |
| 121 | + Confirms the country list has a long TTL since it changes infrequently.""" |
| 122 | + _create_library(user=countries_user, country="IT", city="Florence") |
| 123 | + |
| 124 | + response = client.get("/api/v1/libraries/countries/") |
| 125 | + |
| 126 | + assert response.status_code == 200 |
| 127 | + cc = response["Cache-Control"] |
| 128 | + assert "public" in cc |
| 129 | + assert "max-age=3600" in cc |
| 130 | + assert "s-maxage=3600" in cc |
0 commit comments