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

@@ -1,9 +1,26 @@
import asyncio
import os
import unittest
from datetime import timedelta
from shared_backend.auth import ResetManager, hash_password, verify_password
import bcrypt
from shared_backend.auth import (
ResetManager,
hash_password,
hash_password_async,
password_hash_needs_upgrade,
verify_and_upgrade_password,
verify_and_upgrade_password_async,
verify_password,
verify_password_async,
)
from shared_backend.config import DatabaseConfig
from shared_backend.postgres import (
StartupRetryOptions,
is_transient_connection_error,
retry_startup,
)
from shared_backend.tokens import hash_token, new_session_token
from shared_backend.totp import totp_provisioning_uri, verify_totp
@@ -14,6 +31,95 @@ class SharedBackendTests(unittest.TestCase):
self.assertTrue(verify_password("correct horse battery staple", password_hash))
self.assertFalse(verify_password("wrong password", password_hash))
def test_async_password_helpers_match_the_sync_ones(self):
async def run():
password_hash = await hash_password_async("correct horse battery staple")
self.assertTrue(verify_password("correct horse battery staple", password_hash))
self.assertTrue(
await verify_password_async("correct horse battery staple", password_hash)
)
self.assertFalse(await verify_password_async("wrong password", password_hash))
asyncio.run(run())
def test_new_hashes_carry_the_scheme_prefix_and_the_current_cost(self):
password_hash = hash_password("correct horse battery staple")
self.assertTrue(password_hash.startswith("bcrypt$$2b$12$"))
self.assertFalse(password_hash_needs_upgrade(password_hash))
def test_unprefixed_legacy_bcrypt_hashes_still_verify_and_are_upgraded(self):
legacy = bcrypt.hashpw(
b"correct horse battery staple", bcrypt.gensalt(rounds=12)
).decode()
self.assertTrue(verify_password("correct horse battery staple", legacy))
self.assertFalse(verify_password("wrong password", legacy))
self.assertTrue(password_hash_needs_upgrade(legacy))
valid, upgraded = verify_and_upgrade_password("correct horse battery staple", legacy)
self.assertTrue(valid)
self.assertTrue(upgraded.startswith("bcrypt$$2b$12$"))
self.assertTrue(verify_password("correct horse battery staple", upgraded))
self.assertFalse(password_hash_needs_upgrade(upgraded))
def test_a_cost_below_the_current_policy_is_upgraded(self):
low_cost = "bcrypt$" + bcrypt.hashpw(
b"correct horse battery staple", bcrypt.gensalt(rounds=4)
).decode()
self.assertTrue(password_hash_needs_upgrade(low_cost))
valid, upgraded = verify_and_upgrade_password("correct horse battery staple", low_cost)
self.assertTrue(valid)
self.assertIsNotNone(upgraded)
def test_a_wrong_password_and_a_current_hash_are_not_rewritten(self):
current = hash_password("correct horse battery staple")
password = "correct horse battery staple"
self.assertEqual(verify_and_upgrade_password(password, current), (True, None))
self.assertEqual(verify_and_upgrade_password("wrong password", current), (False, None))
def test_unknown_formats_fail_closed(self):
unknown = [
"",
"bcrypt$",
"bcrypt$not-a-bcrypt-string",
"pbkdf2-sha256$600000$00$00",
"$1$abc",
]
for stored in unknown:
self.assertFalse(verify_password("correct horse battery staple", stored), stored)
def test_async_upgrade_matches_the_sync_one(self):
password = "correct horse battery staple"
legacy = bcrypt.hashpw(password.encode(), bcrypt.gensalt(rounds=12)).decode()
async def run():
valid, upgraded = await verify_and_upgrade_password_async(password, legacy)
self.assertTrue(valid)
self.assertTrue(await verify_password_async(password, upgraded))
asyncio.run(run())
def test_startup_retry_only_retries_transient_connection_errors(self):
import asyncpg
async def retry_with(error):
attempts = []
async def operation():
attempts.append(1)
raise error
policy = StartupRetryOptions(interval_seconds=0.001, max_attempts=3)
with self.assertRaises(type(error)):
await retry_startup(operation, policy)
return len(attempts)
self.assertTrue(is_transient_connection_error(ConnectionRefusedError()))
self.assertTrue(is_transient_connection_error(asyncpg.exceptions.ConnectionDoesNotExistError("closed")))
self.assertFalse(is_transient_connection_error(asyncpg.exceptions.InvalidPasswordError("bad")))
self.assertFalse(is_transient_connection_error(ValueError("bad")))
self.assertEqual(asyncio.run(retry_with(ValueError("bad"))), 1)
self.assertEqual(asyncio.run(retry_with(ConnectionRefusedError())), 3)
def test_session_token_hash(self):
token, token_hash = new_session_token()
self.assertNotEqual(token, token_hash)