# -*- coding: utf-8 -*- """Einmaliger Backend-Deploy-Smoke-Test — gibt keine Secrets aus.""" from __future__ import annotations import json import os import sys import urllib.error import urllib.parse import urllib.request BASE = "https://api.aza-medwork.ch" ROOT = os.path.dirname(os.path.abspath(__file__)) TOKEN_PATH = os.path.join(ROOT, "backend_token.txt") def _read_token() -> str: with open(TOKEN_PATH, encoding="utf-8") as fh: return fh.read().strip() def _req(method: str, path: str, *, headers: dict | None = None, body: dict | None = None) -> tuple[int, dict | str]: url = BASE + path hdrs = dict(headers or {}) data = None if body is not None: data = json.dumps(body).encode("utf-8") hdrs.setdefault("Content-Type", "application/json") req = urllib.request.Request(url, data=data, headers=hdrs, method=method) try: with urllib.request.urlopen(req, timeout=20) as resp: raw = resp.read().decode("utf-8", errors="replace") try: return resp.status, json.loads(raw) except json.JSONDecodeError: return resp.status, raw except urllib.error.HTTPError as exc: raw = exc.read().decode("utf-8", errors="replace") try: return exc.code, json.loads(raw) except json.JSONDecodeError: return exc.code, raw def main() -> int: results: list[str] = [] ok = True code, _data = _req("GET", "/health") results.append(f"health: {code}") if code != 200: ok = False tok = _read_token() auth = {"X-API-Token": tok} pid = (os.environ.get("SMOKE_PRACTICE_ID") or "").strip() email = (os.environ.get("SMOKE_LICENSE_EMAIL") or "").strip() hdrs_base = {**auth, "X-User-Id": "smoke_test_user"} if pid: hdrs_base["X-Practice-Id"] = pid code, data = _req("GET", "/v1/doku-prompts/public", headers=hdrs_base) results.append(f"doku_public_list: {code}") if code == 200 and isinstance(data, dict): results.append(f" count={data.get('count', '?')}") elif code not in (200, 403): ok = False code, data = _req("GET", "/v1/library/public", headers=hdrs_base) results.append(f"library_public_list: {code}") if code == 200 and isinstance(data, dict): results.append(f" count={data.get('count', '?')}") elif code not in (200, 403): ok = False pub_id = "" if pid: hdrs = {**auth, "X-Practice-Id": pid, "X-User-Id": "smoke_test_user"} payload = { "doc_type": "verlauf", "title": "AZA Smoke Test Vorlage", "description": "Smoke-Test, kann gelöscht werden.", "content": "Dies ist eine technische Testvorlage ohne Patientendaten.", "author_display": "AzA Smoke Test", "city": "Winterthur", "specialty": "Dermatologie", "language": "de", "visibility": "public", } code, data = _req("POST", "/v1/doku-prompts/publish", headers=hdrs, body=payload) results.append(f"doku_publish: {code}") if code == 200 and isinstance(data, dict) and data.get("ok"): pub_id = str(data.get("id") or "") results.append(f" published_id={pub_id[:24]}..." if len(pub_id) > 24 else f" published_id={pub_id}") else: detail = data.get("detail") if isinstance(data, dict) else str(data)[:120] results.append(f" detail={detail}") if code not in (200, 403): ok = False if pub_id: code, data = _req("POST", f"/v1/doku-prompts/unpublish/{pub_id}", headers=hdrs, body={}) results.append(f"doku_unpublish: {code}") if code != 200: ok = False else: results.append("doku_publish: SKIPPED (SMOKE_PRACTICE_ID not set)") results.append("doku_unpublish: SKIPPED") lic_path = "/license/status" if email: lic_path += "?" + urllib.parse.urlencode({"email": email}) lic_hdrs = {**auth} if pid: lic_hdrs["X-Practice-Id"] = pid lic_hdrs["X-Device-Id"] = "smoke-device-readonly" code, data = _req("GET", lic_path, headers=lic_hdrs) results.append(f"license_status: {code}") if isinstance(data, dict): for k in ( "chat_device_limit", "chat_devices_used", "contributing_office_licenses", "chat_devices_per_license", "valid", "device_allowed", "reason", ): if k in data: results.append(f" {k}={data[k]}") print("\n".join(results)) return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())