Files
gaokao-volunteer-system/admin/tests/test_password.py
Hermes Agent 3f46811e49
Some checks failed
CI / pytest (Python 3.10) (push) Has been cancelled
CI / pytest (Python 3.11) (push) Has been cancelled
CI / pytest (Python 3.12) (push) Has been cancelled
release: cut v2.1
2026-06-13 14:49:58 +08:00

39 lines
1.0 KiB
Python

"""password.py 单元测试 (T6.1)."""
from __future__ import annotations
import pytest
from admin.password import hash_password, verify_password
def test_hash_then_verify_roundtrip():
h = hash_password("hunter2")
assert h != "hunter2"
assert verify_password("hunter2", h) is True
def test_verify_wrong_password_fails():
h = hash_password("hunter2")
assert verify_password("hunter2-wrong", h) is False
def test_hash_is_salted_different_each_time():
h1 = hash_password("hunter2")
h2 = hash_password("hunter2")
assert h1 != h2 # 不同的 salt → 不同的 hash
# 但都能校验同一个密码
assert verify_password("hunter2", h1)
assert verify_password("hunter2", h2)
def test_hash_empty_password_raises():
with pytest.raises(ValueError):
hash_password("")
def test_verify_handles_malformed_stored():
assert verify_password("hunter2", "no-separator") is False
assert verify_password("hunter2", "") is False
assert verify_password("hunter2", "garbage$garbage") is False # bad hex