Merge remote-tracking branch 'origin/main' into fix/rest-cert-tofu-17

The REST login now checks the certificate before it posts the password
(#17) and then tells a web page from a refused login (#13); the two
login-answer checks moved into _require_login so connect() stays within
the function-length limit. #13's REST tests stand in for the switch's
certificate, since connect() fetches it before the login.
This commit is contained in:
Christian Manivong committed 2026-11-02 00:37:11 +01:00
commit 586632efe0
13 files changed
+517 -75

No files matched your search

+14 -4
View File
@@ -141,10 +141,7 @@ class ProcurveApiClient:
json={"userName": self.username, "password": self.password},
timeout=self.timeout,
)
if resp.status_code != 201:
raise ConnectAuthError(
f"REST API login failed (HTTP {resp.status_code}) for {self.hostname}"
)
self._require_login(resp)
# AOS-Switch returns the session cookie in the JSON body rather than
# via Set-Cookie — it must be sent back as a Cookie header on every
# subsequent request, otherwise writes (POST/PUT/DELETE) are rejected
@@ -156,6 +153,19 @@ class ProcurveApiClient:
if check is not None:
check.observe()
def _require_login(self, resp: requests.Response) -> None:
"""Raise unless *resp* is a successful login."""
if "html" in resp.headers.get("Content-Type", "").lower():
# The web UI's "session timed out" page, for any user (#13): not a login result.
raise ConnectionException(
f"REST API on {self.hostname} answered with a web page instead of JSON "
f"(HTTP {resp.status_code})"
)
if resp.status_code != 201:
raise ConnectAuthError(
f"REST API login failed (HTTP {resp.status_code}) for {self.hostname}"
)
def _open_session(
self, certificate_args: Mapping[str, Any] | None, check_timeout: float
) -> CertificateCheck | None: