from datetime import datetime import json import httpx import pytest from app.modules.website_content.backend.service import NotFoundError, WebsiteContentAPIError, WebsiteContentService def service_for(handler): return WebsiteContentService( "https://website.test/api/admin-content.php", "secret", httpx.Client(transport=httpx.MockTransport(handler)), ) def test_list_uses_authenticated_https_api(): def handler(request): assert request.headers["x-website-admin-token"] == "secret" assert request.url.params["resource"] == "customers" assert request.url.params["include_hidden"] == "1" return httpx.Response(200, json={"items": [{"id": 1, "customer_name": "Kunde"}]}) assert service_for(handler).list("customers") == [{"id": 1, "customer_name": "Kunde"}] def test_complete_operation_calls_transactional_webhook_action(): def handler(request): assert request.method == "POST" assert request.url.params["resource"] == "operations" assert request.url.params["id"] == "12" assert request.url.params["action"] == "complete" payload = json.loads(request.content) assert payload == {"ends_at": "2026-08-26T10:00:00", "is_public": True} return httpx.Response(201, json={"id": 77, "title": "Fiberfejl"}) result = service_for(handler).complete_operation(12, datetime(2026, 8, 26, 10), True) assert result["id"] == 77 def test_logo_is_sent_as_multipart(): def handler(request): assert request.url.params["action"] == "logo" assert request.headers["content-type"].startswith("multipart/form-data") assert b"PNG-data" in request.content return httpx.Response(200, json={"id": 3, "logo_url": "/api/content.php?logo=3"}) assert service_for(handler).upload_logo(3, b"PNG-data", "image/png")["id"] == 3 def test_404_is_mapped_to_not_found(): service = service_for(lambda _request: httpx.Response(404, json={"error": "not_found"})) with pytest.raises(NotFoundError): service.get("customers", 999) def test_missing_token_is_rejected_before_network_call(): service = WebsiteContentService("https://website.test/admin.php", "", httpx.Client()) with pytest.raises(WebsiteContentAPIError, match="ikke konfigureret"): service.list("customers")