fix(blocklist): validate _v6_auth session JSON, add auth-failure test

This commit is contained in:
pyr0ball 2026-05-15 21:03:03 -07:00
parent a683297d8b
commit 0e887837d1
2 changed files with 16 additions and 1 deletions

View file

@ -64,7 +64,12 @@ class PiholeClient:
with httpx.Client(timeout=10) as c:
r = c.post(f"{self.url}/api/auth", json={"password": self.api_key})
r.raise_for_status()
return r.json()["session"]["sid"]
data = r.json()
sid = data.get("session", {}).get("sid")
if not sid:
msg = data.get("session", {}).get("message", "no sid returned")
raise ValueError(f"Pi-hole v6 auth failed: {msg}")
return sid
def _v6_post_domain(self, sid: str, domain: str, comment: str) -> None:
body = [{"domain": domain, "comment": comment, "enabled": True}]

View file

@ -78,3 +78,13 @@ class TestV6Client:
client.block("samsungads.com")
_, _, comment = mock_post.call_args.args
assert comment == "Turnstone block"
def test_auth_raises_on_invalid_session(self):
client = self._client()
invalid_resp = {"session": {"valid": False, "message": "wrong password"}}
with patch("httpx.Client") as mock_cls:
mock_c = mock_cls.return_value.__enter__.return_value
mock_c.post.return_value.raise_for_status.return_value = None
mock_c.post.return_value.json.return_value = invalid_resp
with pytest.raises(ValueError, match="Pi-hole v6 auth failed"):
client._v6_auth()