Files
kmtnflower/backend/tests/test_sitemap_api.py
T
Hermes Agent 3687e91f7f
CI Pipeline / backend-test (push) Successful in 10m37s
CI Pipeline / frontend-test (push) Successful in 29s
CI Pipeline / frontend-build (push) Successful in 32s
CI Pipeline / backend-lint (push) Successful in 10s
CI Pipeline / backend-test (pull_request) Successful in 10m26s
CI Pipeline / frontend-test (pull_request) Successful in 28s
CI Pipeline / frontend-build (pull_request) Successful in 31s
CI Pipeline / backend-lint (pull_request) Successful in 11s
CI Pipeline / quality-gate (push) Successful in 3s
CI Pipeline / quality-gate (pull_request) Successful in 2s
test(seo): add unit tests for sitemap export and API endpoints
- test_sitemap_export.py: 19 tests covering SEOMAP_ROUTES constants,
  build_sitemap_xml() output structure, XML validity, trailing slash
  handling, and build_robots_txt() rules + sitemap reference
- test_sitemap_api.py: 7 tests covering /sitemap.xml and /robots.txt
  endpoint status codes, content types, content validation, and
  auth-gated /regenerate endpoint

Addresses reviewer REQUEST_CHANGES: 'This needs a unit test' and
'Unit test for this type too'
2026-08-04 22:01:22 +00:00

56 lines
1.9 KiB
Python

"""Tests for sitemap API endpoints — /api/sitemap/sitemap.xml, /api/sitemap/robots.txt, /api/sitemap/regenerate."""
import pytest
from httpx import AsyncClient
from starlette.testclient import TestClient
from app.main import app
class TestSitemapEndpoints:
"""Tests for sitemap API routes."""
def _client(self):
return TestClient(app)
def test_sitemap_xml_status(self):
client = self._client()
resp = client.get("/api/sitemap/sitemap.xml")
assert resp.status_code == 200
def test_sitemap_xml_content_type(self):
client = self._client()
resp = client.get("/api/sitemap/sitemap.xml")
assert resp.headers["content-type"].startswith("application/xml")
def test_sitemap_xml_contains_routes(self):
client = self._client()
resp = client.get("/api/sitemap/sitemap.xml")
body = resp.text
for path in ["/", "/about", "/schedule", "/events", "/contact"]:
assert f"<loc>https://kmountainflower.org{path}</loc>" in body
def test_robots_txt_status(self):
client = self._client()
resp = client.get("/api/sitemap/robots.txt")
assert resp.status_code == 200
def test_robots_txt_content_type(self):
client = self._client()
resp = client.get("/api/sitemap/robots.txt")
assert resp.headers["content-type"].startswith("text/plain")
def test_robots_txt_contains_rules(self):
client = self._client()
resp = client.get("/api/sitemap/robots.txt")
body = resp.text
assert "User-agent: *" in body
assert "Disallow: /admin" in body
assert "Disallow: /login" in body
assert "Sitemap: https://kmountainflower.org/sitemap.xml" in body
def test_regenerate_requires_auth(self):
client = self._client()
resp = client.post("/api/sitemap/regenerate")
assert resp.status_code in (401, 403)