ci: expand shared package validation and test coverage
Lint / Lint (push) Successful in 32s
Docker / Docker Build & Push (push) Failing after 2m22s
Tests / Go backend (push) Failing after 1m2s
Tests / Dashboard checks (push) Failing after 35s

- Add lint, formatting, vet, and publish workflow checks
- Run Go database tests with PostgreSQL and add Java tests
- Make consumer sync opt-in; update docs and Go dependency
This commit is contained in:
Gabriel20xx committed 2026-10-19 21:46:52 +00:00
1 parent 937f51b4fb
commit 0ff94f9b10
107 files changed
+10124 -410

No files matched your search

@@ -10,6 +10,7 @@ from shared_backend.authclient import (
AuthClient,
PublicUser,
SessionResult,
_forwarded_client_ip,
)
@@ -44,6 +45,8 @@ def _build_app(
client: AuthClient | None = None,
*,
internal_secret: str | None = None,
local_profile_without_authguard: bool = False,
trusted_proxies: str = "",
) -> FastAPI:
def get_client(request: Request) -> AuthClient | None:
return client
@@ -53,15 +56,23 @@ def _build_app(
app = FastAPI()
app.include_router(
ap.build_auth_router(get_client, get_store, internal_secret=internal_secret),
ap.build_auth_router(
get_client,
get_store,
internal_secret=internal_secret,
local_profile_without_authguard=local_profile_without_authguard,
trusted_proxies=trusted_proxies,
),
prefix="/api/auth",
)
return app
class AuthDisabledModeTests(unittest.TestCase):
"""client=None (AUTHGUARD_URL unset) - every route reports the fixed
placeholder superadmin identity and nothing is gated. This is also a
"""client=None (AUTHGUARD_URL unset) with the local-mode opt-in - every
route reports the fixed placeholder superadmin identity and nothing is
gated. Without the opt-in the same deployment fails closed; see
UnconfiguredAuthGuardTests. This is also a
regression test for a real bug this module had: request/response models
declared *inside* build_auth_router, combined with `from __future__
import annotations`, made FastAPI silently misparse every JSON body as a
@@ -69,7 +80,7 @@ class AuthDisabledModeTests(unittest.TestCase):
bug)."""
def setUp(self) -> None:
self.client = TestClient(_build_app(_FakeStore()))
self.client = TestClient(_build_app(_FakeStore(), local_profile_without_authguard=True))
def test_status_reports_local_superadmin(self) -> None:
response = self.client.get("/api/auth/status")
@@ -118,6 +129,25 @@ class AuthDisabledModeTests(unittest.TestCase):
self.assertEqual(self.client.put("/api/auth/me", json={}).status_code, 409)
class UnconfiguredAuthGuardTests(unittest.TestCase):
"""client=None without local_profile_without_authguard: a missing
AUTHGUARD_URL must not turn guarded routes into the local superadmin."""
def setUp(self) -> None:
self.client = TestClient(
_build_app(_FakeStore(login_required=True)), raise_server_exceptions=False
)
def test_guarded_routes_fail_closed(self) -> None:
self.assertEqual(self.client.get("/api/auth/status").status_code, 500)
self.assertEqual(self.client.get("/api/auth/me").status_code, 500)
self.assertEqual(self.client.get("/api/auth/users").status_code, 500)
self.assertEqual(self.client.post("/api/auth/logout").status_code, 500)
def test_login_flag_read_is_still_public(self) -> None:
self.assertEqual(self.client.get("/api/auth/access").json(), {"loginRequired": True})
class AccessSettingsTests(unittest.TestCase):
def test_get_is_public_and_put_requires_an_admin_session(self) -> None:
store = _FakeStore(login_required=True)
@@ -142,7 +172,7 @@ class AccessSettingsTests(unittest.TestCase):
store = _FakeStore(login_required=True)
client = TestClient(_build_app(store))
response = client.put("/api/auth/access", json={"loginRequired": False})
self.assertEqual(response.status_code, 401)
self.assertEqual(response.status_code, 500)
self.assertEqual(client.get("/api/auth/access").json(), {"loginRequired": True})
def test_internal_update_disables_login_and_uses_admin_identity(self) -> None:
@@ -275,12 +305,142 @@ class EnabledModeSetupAndLoginTests(unittest.TestCase):
self.assertIsNone(body["user"])
class _AddressRecordingClient(_RoleClient):
"""Records the address AuthClient would send as X-Forwarded-For for each credential call, and
counts the calls that reached AuthGuard."""
def __init__(self) -> None:
super().__init__()
self.forwarded: list[str | None] = []
self.login_calls = 0
self.totp_calls = 0
async def login(self, username_or_email: str, password: str, totp_token: str = ""):
self.login_calls += 1
self.forwarded.append(_forwarded_client_ip.get())
return await super().login(username_or_email, password, totp_token)
async def register(self, username: str, email: str, password: str) -> SessionResult:
self.forwarded.append(_forwarded_client_ip.get())
return await super().register(username, email, password)
async def totp_verify(self, token: str, code: str) -> None:
self.totp_calls += 1
self.forwarded.append(_forwarded_client_ip.get())
if code != "123456":
raise APIError(401, "bad code")
async def totp_disable(self, token: str, code: str) -> None:
self.totp_calls += 1
self.forwarded.append(_forwarded_client_ip.get())
raise APIError(401, "bad code")
class AttemptLimitTests(unittest.TestCase):
"""Login and the TOTP routes refuse further attempts with 429 (S-009)."""
def _login(self, client: TestClient, identifier: str = "Victim") -> int:
response = client.post(
"/api/auth/login", json={"identifier": identifier, "password": "wrong"}
)
return response.status_code
def test_login_is_limited_per_account_before_authguard_is_called(self) -> None:
fake = _AddressRecordingClient()
client = TestClient(_build_app(_FakeStore(), fake))
for attempt in range(ap.LOGIN_ACCOUNT_LIMIT):
self.assertEqual(self._login(client), 401, f"attempt {attempt + 1}")
self.assertEqual(self._login(client), 429)
self.assertEqual(self._login(client, "VICTIM"), 429) # the account key ignores case
self.assertEqual(fake.login_calls, ap.LOGIN_ACCOUNT_LIMIT)
self.assertEqual(self._login(client, "someone-else"), 401)
def test_login_is_limited_per_client_address(self) -> None:
fake = _AddressRecordingClient()
client = TestClient(_build_app(_FakeStore(), fake), client=("203.0.113.7", 40000))
statuses = [self._login(client, f"user{i}") for i in range(ap.LOGIN_CLIENT_LIMIT + 1)]
self.assertEqual(statuses[:-1], [401] * ap.LOGIN_CLIENT_LIMIT)
self.assertEqual(statuses[-1], 429)
def test_totp_verify_and_disable_share_a_per_user_limit(self) -> None:
fake = _AddressRecordingClient()
client = TestClient(_build_app(_FakeStore(), fake))
client.cookies.set(DEFAULT_COOKIE_NAME, "user-token")
for _ in range(ap.TOTP_ATTEMPT_LIMIT - 1):
response = client.post("/api/auth/totp/verify", json={"totpToken": "000000"})
self.assertEqual(response.status_code, 401)
self.assertEqual(
client.post("/api/auth/totp/disable", json={"totpToken": "000000"}).status_code, 401
)
self.assertEqual(
client.post("/api/auth/totp/verify", json={"totpToken": "123456"}).status_code, 429
)
self.assertEqual(
client.post("/api/auth/totp/disable", json={"totpToken": "000000"}).status_code, 429
)
self.assertEqual(fake.totp_calls, ap.TOTP_ATTEMPT_LIMIT)
class ClientAddressForwardingTests(unittest.TestCase):
"""The client address is the TCP peer unless the peer is a trusted proxy (decision 5), and it
reaches AuthGuard as X-Forwarded-For through forwarded_client_ip."""
def _login_address(self, *, peer: str, trusted: str, forwarded: str | None) -> str | None:
fake = _AddressRecordingClient()
client = TestClient(
_build_app(_FakeStore(), fake, trusted_proxies=trusted), client=(peer, 40000)
)
headers = {"X-Forwarded-For": forwarded} if forwarded is not None else {}
response = client.post(
"/api/auth/login",
json={"identifier": "a@b.com", "password": "correct"},
headers=headers,
)
self.assertEqual(response.status_code, 200)
return fake.forwarded[-1]
def test_the_tcp_peer_is_forwarded_without_trusted_proxies(self) -> None:
got = self._login_address(peer="203.0.113.7", trusted="", forwarded="6.6.6.6")
self.assertEqual(got, "203.0.113.7")
def test_a_spoofed_header_from_an_untrusted_peer_is_ignored(self) -> None:
got = self._login_address(peer="203.0.113.7", trusted="10.0.0.0/8", forwarded="6.6.6.6")
self.assertEqual(got, "203.0.113.7")
def test_a_trusted_peer_has_its_header_walked_right_to_left(self) -> None:
got = self._login_address(
peer="10.0.0.2", trusted="10.0.0.0/8", forwarded="6.6.6.6, 198.51.100.9, 10.0.0.1"
)
self.assertEqual(got, "198.51.100.9")
def test_setup_and_totp_forward_the_address_too(self) -> None:
fake = _AddressRecordingClient()
fake.setup_required = True
client = TestClient(_build_app(_FakeStore(), fake), client=("198.51.100.20", 40000))
client.post(
"/api/auth/setup",
json={"username": "admin", "email": "admin@example.com", "password": "password123"},
)
client.cookies.set(DEFAULT_COOKIE_NAME, "user-token")
client.post("/api/auth/totp/verify", json={"totpToken": "123456"})
client.post("/api/auth/totp/disable", json={"totpToken": "123456"})
self.assertEqual(fake.forwarded, ["198.51.100.20"] * 3)
def test_the_forwarded_address_does_not_leak_outside_the_call(self) -> None:
self._login_address(peer="203.0.113.7", trusted="", forwarded=None)
self.assertIsNone(_forwarded_client_ip.get())
def test_an_invalid_trusted_proxy_list_fails_at_startup(self) -> None:
with self.assertRaises(ValueError):
_build_app(_FakeStore(), trusted_proxies="10.0.0.0/8, not-an-ip")
class RequestValidationTests(unittest.TestCase):
"""Every JSON-body route must actually parse its body (see the docstring
on AuthDisabledModeTests for the regression this guards against)."""
def setUp(self) -> None:
self.client = TestClient(_build_app(_FakeStore()))
self.client = TestClient(_build_app(_FakeStore(), local_profile_without_authguard=True))
def test_setup_rejects_missing_fields_with_422_not_500(self) -> None:
response = self.client.post("/api/auth/setup", json={})