ci: expand shared package validation and test coverage
- 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:
1 parent
937f51b4fb
commit
0ff94f9b10
107 files changed
+10124
-410
No files matched your search
@@ -13,6 +13,7 @@ from shared_backend.authclient import (
|
||||
InvalidSessionError,
|
||||
ManagedUser,
|
||||
PublicUser,
|
||||
forwarded_client_ip,
|
||||
resolve_user,
|
||||
token_from_request,
|
||||
)
|
||||
@@ -49,6 +50,37 @@ def _fake_http_error(status: int, payload: dict) -> urllib.error.HTTPError:
|
||||
)
|
||||
|
||||
|
||||
class AuthClientForwardedClientIpTests(unittest.IsolatedAsyncioTestCase):
|
||||
"""forwarded_client_ip makes credential calls carry the end user's address (S-009)."""
|
||||
|
||||
async def _login_request(self, address):
|
||||
client = AuthClient("https://authguard.example")
|
||||
payload = {"user": {"id": 1, "username": "a", "email": "", "role": "user"}}
|
||||
with patch(
|
||||
"shared_backend.authclient.urllib.request.urlopen",
|
||||
return_value=_FakeResponse(200, payload),
|
||||
) as urlopen:
|
||||
if address is None:
|
||||
await client.login("a", "secret-password")
|
||||
else:
|
||||
with forwarded_client_ip(address):
|
||||
await client.login("a", "secret-password")
|
||||
return urlopen.call_args.args[0]
|
||||
|
||||
async def test_the_address_is_sent_as_x_forwarded_for(self) -> None:
|
||||
request = await self._login_request("203.0.113.9")
|
||||
self.assertEqual(request.get_header("X-forwarded-for"), "203.0.113.9")
|
||||
|
||||
async def test_no_header_is_sent_outside_the_block(self) -> None:
|
||||
request = await self._login_request(None)
|
||||
self.assertIsNone(request.get_header("X-forwarded-for"))
|
||||
|
||||
async def test_a_value_that_is_not_an_ip_address_is_dropped(self) -> None:
|
||||
for value in ("203.0.113.9\r\nX-Evil: 1", "not-an-ip", ""):
|
||||
request = await self._login_request(value)
|
||||
self.assertIsNone(request.get_header("X-forwarded-for"), repr(value))
|
||||
|
||||
|
||||
class AuthClientBaseUrlTests(unittest.TestCase):
|
||||
def test_bare_public_host_assumes_https(self) -> None:
|
||||
self.assertEqual(AuthClient("auth.example.com").base_url, "https://auth.example.com")
|
||||
@@ -155,6 +187,57 @@ class AuthClientValidateTests(unittest.IsolatedAsyncioTestCase):
|
||||
await self.client.validate("a-token")
|
||||
|
||||
|
||||
class AuthClientValidationCacheTests(unittest.IsolatedAsyncioTestCase):
|
||||
def setUp(self) -> None:
|
||||
self.client = AuthClient("https://authguard.example")
|
||||
payload = {"valid": True, "user_id": 42, "username": "alice", "role": "admin"}
|
||||
self.valid = _FakeResponse(200, payload)
|
||||
|
||||
async def test_repeated_validate_reuses_the_answer(self) -> None:
|
||||
with patch(
|
||||
"shared_backend.authclient.urllib.request.urlopen", return_value=self.valid
|
||||
) as urlopen:
|
||||
first = await self.client.validate("a-token")
|
||||
second = await self.client.validate("a-token")
|
||||
|
||||
self.assertEqual(first, second)
|
||||
urlopen.assert_called_once()
|
||||
|
||||
async def test_invalid_session_is_cached_too(self) -> None:
|
||||
with patch(
|
||||
"shared_backend.authclient.urllib.request.urlopen",
|
||||
return_value=_FakeResponse(401, {"valid": False}),
|
||||
) as urlopen:
|
||||
with self.assertRaises(InvalidSessionError):
|
||||
await self.client.validate("bad-token")
|
||||
with self.assertRaises(InvalidSessionError):
|
||||
await self.client.validate("bad-token")
|
||||
|
||||
urlopen.assert_called_once()
|
||||
|
||||
async def test_logout_drops_the_cached_answer(self) -> None:
|
||||
with patch(
|
||||
"shared_backend.authclient.urllib.request.urlopen",
|
||||
side_effect=[self.valid, _FakeResponse(200, {}), _FakeResponse(401, {"valid": False})],
|
||||
) as urlopen:
|
||||
await self.client.validate("a-token")
|
||||
await self.client.logout("a-token")
|
||||
with self.assertRaises(InvalidSessionError):
|
||||
await self.client.validate("a-token")
|
||||
|
||||
self.assertEqual(urlopen.call_count, 3)
|
||||
|
||||
async def test_set_base_url_clears_the_cache(self) -> None:
|
||||
with patch(
|
||||
"shared_backend.authclient.urllib.request.urlopen", return_value=self.valid
|
||||
) as urlopen:
|
||||
await self.client.validate("a-token")
|
||||
self.client.set_base_url("https://authguard2.example")
|
||||
await self.client.validate("a-token")
|
||||
|
||||
self.assertEqual(urlopen.call_count, 2)
|
||||
|
||||
|
||||
class TokenFromRequestTests(unittest.TestCase):
|
||||
def test_prefers_authorization_header_over_cookie(self) -> None:
|
||||
headers = {"Authorization": "Bearer header-token"}
|
||||
|
||||
Reference in new issue
Block a user