Files
kmtnflower/backend/tests/test_sitemap_export.py
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

114 lines
3.9 KiB
Python

"""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