- Add Go, npm, Python, and Java validation to CI - Run Go tests against PostgreSQL - Record performance fixes in the report
78 lines
3.2 KiB
Python
78 lines
3.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.totp import verify_totp
|
|
|
|
# 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"]):
|
|
self.assertEqual(
|
|
verify_password(password["plaintext"], entry["hash"]),
|
|
entry["verifies"]["python"],
|
|
)
|
|
self.assertFalse(verify_password(password["wrongPassword"], 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(
|
|
password["plaintext"], entry["hash"]
|
|
)
|
|
self.assertTrue(valid)
|
|
self.assertEqual(upgraded is not None, want)
|
|
if upgraded is not None:
|
|
self.assertTrue(upgraded.startswith("bcrypt$"))
|
|
self.assertTrue(verify_password(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"])
|
|
|
|
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)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|