From 14c757bbd2156ddbf5efb4c6884acb7d6d154479 Mon Sep 17 00:00:00 2001 From: sascha Date: Fri, 14 Aug 2026 10:30:31 +0200 Subject: [PATCH] Sichere Caddy- und DNS-Stilllegung: tests/test_app.py --- tests/test_app.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/test_app.py b/tests/test_app.py index b5db980..a3ea996 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -240,6 +240,43 @@ def test_iso_builder_restore_defaults_to_dry_run_and_has_no_free_target(monkeypa assert "/app-config/ansible" in decoded +def test_caddy_site_remove_defaults_to_dry_run(monkeypatch): + payload = {"hostname": "wiki.sascha-lutz.de", "dry_run": True, "found": True, "line_start": 10, "line_end": 13} + calls = [] + monkeypatch.setattr(app, "_ssh", lambda host, command, timeout=600: (calls.append((host, command, timeout)) or (0, __import__("json").dumps(payload), ""))) + with TestClient(app.app) as client: + response = client.delete("/caddy/site/wiki.sascha-lutz.de", headers={"Authorization": "Bearer test-token"}) + assert response.status_code == 200 + assert response.json()["dry_run"] is True + assert calls[0][0] == app.CADDY_HOST + assert calls[0][2] == 90 + + +def test_caddy_site_remove_rejects_unmanaged_hostname_before_ssh(monkeypatch): + monkeypatch.setattr(app, "_ssh", lambda *_args, **_kwargs: (_ for _ in ()).throw(AssertionError("must not SSH"))) + with TestClient(app.app) as client: + response = client.delete("/caddy/site/example.com", headers={"Authorization": "Bearer test-token"}) + assert response.status_code == 400 + + +def test_dns_rrset_dry_run_returns_only_selected_records(monkeypatch): + async def fake_lookup(zone): + assert zone == "sascha-lutz.de" + return {"id": 96805, "name": zone}, [ + {"name": "wiki", "type": "A", "records": [{"value": "46.225.230.72"}]}, + {"name": "wiki", "type": "AAAA", "records": [{"value": "::1"}]}, + {"name": "git", "type": "A", "records": [{"value": "46.225.230.72"}]}, + ], {"Authorization": "Bearer hidden"} + monkeypatch.setattr(app, "_hetzner_zone_and_rrsets", fake_lookup) + with TestClient(app.app) as client: + response = client.delete("/dns/rrset/sascha-lutz.de/wiki", headers={"Authorization": "Bearer test-token"}) + assert response.status_code == 200 + body = response.json() + assert body["dry_run"] is True + assert [item["type"] for item in body["rrsets"]] == ["A", "AAAA"] + assert body["deleted"] == [] + + def test_invalid_log_target_is_rejected_before_ssh(): with TestClient(app.app) as client: response = client.get(