test(seo): add unit tests for sitemap export and API endpoints
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
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_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'
This commit is contained in:
1 parent
3dead2921a
commit
3687e91f7f
2 files changed
+168
No files matched your search
@@ -0,0 +1,55 @@
|
||||
"""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)
|
||||
@@ -0,0 +1,113 @@
|
||||
"""Tests for sitemap_export — sitemap.xml and robots.txt generation."""
|
||||
|
||||
import pytest
|
||||
from xml.etree import ElementTree as ET
|
||||
|
||||
from app.sitemap_export import (
|
||||
SEOMAP_ROUTES,
|
||||
build_sitemap_xml,
|
||||
build_robots_txt,
|
||||
)
|
||||
|
||||
|
||||
class TestSitemapRoutes:
|
||||
"""Verify the route configuration constants."""
|
||||
|
||||
def test_route_count(self):
|
||||
assert len(SEOMAP_ROUTES) == 5
|
||||
|
||||
def test_home_route(self):
|
||||
assert SEOMAP_ROUTES[0] == {
|
||||
"path": "/",
|
||||
"changefreq": "daily",
|
||||
"priority": 1.0,
|
||||
}
|
||||
|
||||
def test_all_paths_present(self):
|
||||
paths = [r["path"] for r in SEOMAP_ROUTES]
|
||||
expected = ["/", "/about", "/schedule", "/events", "/contact"]
|
||||
assert paths == expected
|
||||
|
||||
def test_priority_range(self):
|
||||
for route in SEOMAP_ROUTES:
|
||||
assert 0.0 <= route["priority"] <= 1.0
|
||||
|
||||
def test_valid_changefreq_values(self):
|
||||
valid = {"always", "hourly", "daily", "weekly", "monthly", "yearly", "never"}
|
||||
for route in SEOMAP_ROUTES:
|
||||
assert route["changefreq"] in valid
|
||||
|
||||
|
||||
class TestBuildSitemapXml:
|
||||
"""Tests for the sitemap XML builder."""
|
||||
|
||||
def test_returns_string(self):
|
||||
result = build_sitemap_xml("https://example.com")
|
||||
assert isinstance(result, str)
|
||||
|
||||
def test_contains_xml_declaration(self):
|
||||
result = build_sitemap_xml("https://example.com")
|
||||
assert '<?xml version="1.0" encoding="UTF-8"?>' in result
|
||||
|
||||
def test_contains_urlset_namespace(self):
|
||||
result = build_sitemap_xml("https://example.com")
|
||||
assert 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' in result
|
||||
|
||||
def test_contains_all_routes(self):
|
||||
result = build_sitemap_xml("https://example.com")
|
||||
for route in SEOMAP_ROUTES:
|
||||
assert f"<loc>https://example.com{route['path']}</loc>" in result
|
||||
|
||||
def test_strips_trailing_slash_from_base_url(self):
|
||||
result = build_sitemap_xml("https://example.com/")
|
||||
assert "https://example.com//about" not in result
|
||||
assert "<loc>https://example.com/about</loc>" in result
|
||||
|
||||
def test_different_base_url(self):
|
||||
result = build_sitemap_xml("https://mystation.org")
|
||||
assert "https://mystation.org/schedule" in result
|
||||
assert "https://example.com" not in result
|
||||
|
||||
def test_valid_xml(self):
|
||||
result = build_sitemap_xml("https://example.com")
|
||||
# Should parse without error
|
||||
root = ET.fromstring(result)
|
||||
# The root tag includes the namespace
|
||||
assert "urlset" in root.tag
|
||||
|
||||
def test_contains_changefreq_and_priority(self):
|
||||
result = build_sitemap_xml("https://example.com")
|
||||
assert "<changefreq>daily</changefreq>" in result
|
||||
assert "<priority>1.0</priority>" in result
|
||||
assert "<priority>0.5</priority>" in result
|
||||
|
||||
|
||||
class TestBuildRobotsTxt:
|
||||
"""Tests for the robots.txt builder."""
|
||||
|
||||
def test_returns_string(self):
|
||||
result = build_robots_txt("https://example.com")
|
||||
assert isinstance(result, str)
|
||||
|
||||
def test_user_agent_allows_all(self):
|
||||
result = build_robots_txt("https://example.com")
|
||||
assert "User-agent: *" in result
|
||||
assert "Allow: /" in result
|
||||
|
||||
def test_disallows_admin_and_login(self):
|
||||
result = build_robots_txt("https://example.com")
|
||||
assert "Disallow: /admin" in result
|
||||
assert "Disallow: /login" in result
|
||||
|
||||
def test_sitemap_reference(self):
|
||||
result = build_robots_txt("https://example.com")
|
||||
assert "Sitemap: https://example.com/sitemap.xml" in result
|
||||
|
||||
def test_strips_trailing_slash(self):
|
||||
result = build_robots_txt("https://example.com/")
|
||||
assert "Sitemap: https://example.com//sitemap.xml" not in result
|
||||
assert "Sitemap: https://example.com/sitemap.xml" in result
|
||||
|
||||
def test_different_base_url(self):
|
||||
result = build_robots_txt("https://mystation.org")
|
||||
assert "Sitemap: https://mystation.org/sitemap.xml" in result
|
||||
Reference in new issue
Block a user