Files
ZitPlane/tests/ci/account_negative.py
T

59 lines
2.3 KiB
Python

"""CI-only negative access checks against a disposable Next.js account server.
No login, credential acquisition, mock positive authentication or real accounts.
"""
from __future__ import annotations
import os
import time
from urllib.error import HTTPError, URLError
from urllib.request import build_opener, HTTPRedirectHandler, Request
class NoRedirect(HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
return None
def main() -> None:
base = os.environ.get("ZITPLANE_CI_ACCOUNT", "http://127.0.0.1:3001")
if base != "http://127.0.0.1:3001":
raise RuntimeError("Account probe must use only CI localhost:3001")
opener = build_opener(NoRedirect())
# Keep bounded startup polling; every other error is an actual test failure.
deadline = time.monotonic() + 30
while True:
try:
opener.open(Request(base + "/api/account/me"), timeout=3)
raise AssertionError("Unauthenticated identity API returned 200")
except HTTPError as exc:
if exc.code in (401, 500, 503) and time.monotonic() < deadline and exc.code != 401:
time.sleep(1)
continue
if exc.code != 401:
raise AssertionError(f"Expected API HTTP 401, got {exc.code}") from exc
if "no-store" not in exc.headers.get("Cache-Control", "").lower():
raise AssertionError("Unauthorized identity response must be no-store")
break
except URLError:
if time.monotonic() >= deadline:
raise
time.sleep(1)
print("ZITPLANE_ACCOUNT_UNAUTHENTICATED_API_401_PASS")
try:
opener.open(Request(base + "/account"), timeout=8)
raise AssertionError("Unauthenticated account page returned 200")
except HTTPError as exc:
if exc.code not in (302, 303, 307, 308):
raise AssertionError(f"Expected protected redirect, got HTTP {exc.code}") from exc
location = exc.headers.get("Location", "")
if not (location.endswith("/signin") or location == "/signin"):
raise AssertionError("Protected route redirects anywhere other than /signin")
print("ZITPLANE_ACCOUNT_PROTECTED_REDIRECT_PASS")
print("ZITPLANE_INTERACTIVE_LOGIN_E2E_DEFERRED")
if __name__ == "__main__":
main()