"""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"https://kmountainflower.org{path}" 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)