Files
CollabTable/shared/packages/python-backend/tests/test_contract.py
T
Gabriel20xx 97c237be80
Lint / Lint (push) Successful in 1m8s
Tests / Go backend (push) Failing after 1m23s
Tests / Dashboard checks (push) Failing after 38s
Docker / Docker Build & Push (push) Failing after 6m29s
feat(sync): paginate Android sync with change tokens
2026-10-20 15:27:53 +00:00

101 lines
4.2 KiB
Python

import json
import unittest
from pathlib import Path
from shared_backend.auth import (
hash_password,
password_hash_needs_upgrade,
verify_and_upgrade_password,
verify_password,
)
from shared_backend.clientip import client_address, parse_trusted_proxies
from shared_backend.coreconfig import (
EXAMPLE_INTERNAL_SECRETS,
MIN_INTERNAL_SECRET_LENGTH,
internal_secret_matches,
)
from shared_backend.totp import verify_totp, verify_totp_step
# The Go, Node and Python auth helpers load the same fixtures; see "description" in the file.
CONTRACT_PATH = Path(__file__).resolve().parents[3] / "contract" / "auth-contract.json"
@unittest.skipUnless(
CONTRACT_PATH.exists(), "contract/auth-contract.json is not part of this checkout"
)
class AuthContractTests(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.contract = json.loads(CONTRACT_PATH.read_text(encoding="utf-8"))
def test_password_hashes_verify_as_the_contract_says_for_python(self) -> None:
password = self.contract["password"]
for entry in password["hashes"]:
with self.subTest(entry["name"]):
plaintext = entry.get("password", password["plaintext"])
wrong_password = entry.get("wrongPassword", password["wrongPassword"])
self.assertEqual(
verify_password(plaintext, entry["hash"]),
entry["verifies"]["python"],
)
self.assertFalse(verify_password(wrong_password, entry["hash"]))
if "python" in entry.get("needsUpgrade", {}):
want = entry["needsUpgrade"]["python"]
self.assertEqual(password_hash_needs_upgrade(entry["hash"]), want)
valid, upgraded = verify_and_upgrade_password(
plaintext, entry["hash"]
)
self.assertTrue(valid)
self.assertEqual(upgraded is not None, want)
if upgraded is not None:
self.assertTrue(upgraded.startswith(("bcrypt$", "bcrypt-sha256$")))
self.assertTrue(verify_password(plaintext, upgraded))
def test_the_password_length_contract_holds_for_python(self) -> None:
too_long = self.contract["password"]["tooLongToHash"]
try:
hash_password("a" * too_long["bytes"])
accepted = True
except ValueError:
accepted = False
self.assertEqual(accepted, too_long["accepted"]["python"])
def test_totp_verifies_as_the_contract_says(self) -> None:
totp = self.contract["totp"]
for entry in totp["cases"]:
with self.subTest(entry["name"]):
secret = entry.get("secret", totp["secret"])
self.assertEqual(verify_totp(secret, entry["code"], entry["unix"]), entry["valid"])
if entry["valid"]:
self.assertEqual(
verify_totp_step(secret, entry["code"], entry["unix"]),
entry.get("matchedStep", entry["unix"] // 30),
)
def test_client_address_follows_the_contract(self) -> None:
section = self.contract["clientAddress"]
for entry in section["cases"]:
with self.subTest(entry["name"]):
trusted = parse_trusted_proxies(entry["trusted"])
self.assertEqual(
client_address(entry["peer"], entry["forwarded"], trusted), entry["want"]
)
for value in section["invalidTrustedProxyLists"]:
with self.subTest(value), self.assertRaises(ValueError):
parse_trusted_proxies(value)
def test_internal_secret_gate_follows_the_contract(self) -> None:
rule = self.contract["internalSecret"]
self.assertEqual(MIN_INTERNAL_SECRET_LENGTH, rule["minLength"])
self.assertEqual(EXAMPLE_INTERNAL_SECRETS, frozenset(rule["placeholders"]))
for entry in rule["cases"]:
with self.subTest(entry["name"]):
self.assertEqual(
internal_secret_matches(entry["provided"], entry["secret"]),
entry["matches"],
)
if __name__ == "__main__":
unittest.main()