test(python-backend): restore test typing fixes and formatting

A later vendored sync reverted the test typing fixes from 23bd5b2 and the
line-length formatting from 50461c0, which fails the ty check in consumers
that run `ty check --error-on-warning`. Restore them:

- assert the narrowed upgraded/found values before use
- suppress the intentional _FakeDatabase argument mismatches with
  `# ty: ignore[invalid-argument-type]`
- reformat the long lines to the package's line-length of 100

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
GabrielandClaude Haiku 5.5 committed 2026-11-10 14:38:14 +01:00
1 parent 75c08beec6
commit 74942e55f9
4 files changed
+25 -14

No files matched your search

+14 -8
View File
@@ -48,15 +48,14 @@ class SharedBackendTests(unittest.TestCase):
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()
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)
assert upgraded is not None
self.assertTrue(upgraded.startswith("bcrypt$$2b$12$"))
self.assertTrue(verify_password("correct horse battery staple", upgraded))
self.assertFalse(password_hash_needs_upgrade(upgraded))
@@ -70,14 +69,16 @@ class SharedBackendTests(unittest.TestCase):
legacy = f"{salt}:{expected}"
valid, upgraded = verify_and_upgrade_password(password, legacy)
self.assertTrue(valid)
assert upgraded is not None
self.assertTrue(upgraded.startswith("bcrypt-sha256$$2b$12$"))
self.assertTrue(verify_password(password, upgraded))
self.assertFalse(verify_password(password + "x", 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()
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)
@@ -107,6 +108,7 @@ class SharedBackendTests(unittest.TestCase):
async def run():
valid, upgraded = await verify_and_upgrade_password_async(password, legacy)
self.assertTrue(valid)
assert upgraded is not None
self.assertTrue(await verify_password_async(password, upgraded))
asyncio.run(run())
@@ -127,8 +129,12 @@ class SharedBackendTests(unittest.TestCase):
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.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)