CI Pipeline / backend-test (push) Successful in 10m0s
CI Pipeline / frontend-build (push) Successful in 32s
CI Pipeline / backend-lint (push) Successful in 10s
CI Pipeline / frontend-test (push) Successful in 28s
CI Pipeline / backend-test (pull_request) Successful in 11m42s
CI Pipeline / frontend-test (pull_request) Successful in 31s
CI Pipeline / frontend-build (pull_request) Successful in 32s
CI Pipeline / backend-lint (pull_request) Successful in 12s
CI Pipeline / quality-gate (push) Successful in 3s
CI Pipeline / quality-gate (pull_request) Successful in 3s
82 lines
2.9 KiB
Python
82 lines
2.9 KiB
Python
"""Tests for sitemap API endpoints — /api/sitemap/sitemap.xml, /api/sitemap/robots.txt, /api/sitemap/regenerate.
|
|
|
|
Note: The sitemap and robots.txt endpoints now read the site URL from
|
|
StationConfig.website in the database (falling back to KMTN_WEBSITE_URL
|
|
if the config is missing). These tests assume a seeded database with the
|
|
default station config (website = 'kmountainflower.org').
|
|
"""
|
|
|
|
import pytest
|
|
from httpx import AsyncClient
|
|
from starlette.testclient import TestClient
|
|
|
|
from app.api.sitemap import _build_base_url
|
|
from app.main import app
|
|
|
|
|
|
class TestBuildBaseUrl:
|
|
"""Tests for the _build_base_url helper."""
|
|
|
|
def test_bare_domain_gets_https(self):
|
|
assert _build_base_url("kmountainflower.org") == "https://kmountainflower.org"
|
|
|
|
def test_domain_with_trailing_slash(self):
|
|
assert _build_base_url("kmountainflower.org/") == "https://kmountainflower.org"
|
|
|
|
def test_domain_with_whitespace(self):
|
|
assert _build_base_url(" mystation.org ") == "https://mystation.org"
|
|
|
|
def test_already_has_https_scheme(self):
|
|
assert _build_base_url("https://kmountainflower.org") == "https://kmountainflower.org"
|
|
|
|
def test_already_has_http_scheme(self):
|
|
assert _build_base_url("http://kmountainflower.org") == "http://kmountainflower.org"
|
|
|
|
|
|
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)
|