SHA256
initial commit
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
"""纯函数单元测试: 密码生成、邮件模板构建"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from tjhlp.commands.notify import _build_email_text, _read_credentials_csv
|
||||
from tjhlp.commands.users import _generate_password, _read_members_csv
|
||||
from tjhlp.models import CredentialRecord, MemberRole
|
||||
|
||||
|
||||
class TestGeneratePassword:
|
||||
def test_default_length(self):
|
||||
pw = _generate_password()
|
||||
assert len(pw) == 12
|
||||
|
||||
def test_custom_length(self):
|
||||
pw = _generate_password(16)
|
||||
assert len(pw) == 16
|
||||
|
||||
def test_min_length_enforced(self):
|
||||
"""length < 8 should be corrected to 8"""
|
||||
pw = _generate_password(4)
|
||||
assert len(pw) == 8
|
||||
|
||||
def test_contains_lowercase(self):
|
||||
for _ in range(20):
|
||||
pw = _generate_password()
|
||||
assert any(c.islower() for c in pw), f"No lowercase in: {pw}"
|
||||
|
||||
def test_contains_uppercase(self):
|
||||
for _ in range(20):
|
||||
pw = _generate_password()
|
||||
assert any(c.isupper() for c in pw), f"No uppercase in: {pw}"
|
||||
|
||||
def test_contains_digit(self):
|
||||
for _ in range(20):
|
||||
pw = _generate_password()
|
||||
assert any(c.isdigit() for c in pw), f"No digit in: {pw}"
|
||||
|
||||
def test_contains_special(self):
|
||||
special = set("!@#$%&*")
|
||||
for _ in range(20):
|
||||
pw = _generate_password()
|
||||
assert any(c in special for c in pw), f"No special char in: {pw}"
|
||||
|
||||
def test_randomness(self):
|
||||
"""多次生成不应产生相同的密码"""
|
||||
passwords = {_generate_password() for _ in range(50)}
|
||||
assert len(passwords) > 45 # 极低概率碰撞
|
||||
|
||||
|
||||
class TestReadMembersCsv:
|
||||
def test_basic(self, tmp_path: Path):
|
||||
csv_file = tmp_path / "members.csv"
|
||||
csv_file.write_text(
|
||||
"id,name,role\n2659999,赵一伤,student\n10001,李四催,ta\n", encoding="utf-8"
|
||||
)
|
||||
|
||||
records = _read_members_csv(csv_file)
|
||||
assert len(records) == 2
|
||||
assert records[0].id == "2659999"
|
||||
assert records[0].role == "student"
|
||||
assert records[1].role == "ta"
|
||||
|
||||
def test_bom_encoding(self, tmp_path: Path):
|
||||
csv_file = tmp_path / "members.csv"
|
||||
csv_file.write_bytes(
|
||||
b"\xef\xbb\xbfid,name,role\n2659999,\xe5\xbc\xa0\xe4\xb8\x89,student\n"
|
||||
)
|
||||
|
||||
records = _read_members_csv(csv_file)
|
||||
assert len(records) == 1
|
||||
assert records[0].id == "2659999"
|
||||
|
||||
def test_empty_file(self, tmp_path: Path):
|
||||
csv_file = tmp_path / "members.csv"
|
||||
csv_file.write_text("id,name,role\n", encoding="utf-8")
|
||||
|
||||
records = _read_members_csv(csv_file)
|
||||
assert len(records) == 0
|
||||
|
||||
|
||||
class TestBuildEmailText:
|
||||
def test_contains_fields(self):
|
||||
cred = CredentialRecord(
|
||||
id="2659999",
|
||||
name="赵一伤",
|
||||
role=MemberRole.STUDENT,
|
||||
username="2659999",
|
||||
password="testpass",
|
||||
email="[email protected]",
|
||||
)
|
||||
text = _build_email_text(cred, "http://gitea.test")
|
||||
assert "赵一伤" in text
|
||||
assert "2659999" in text
|
||||
assert "testpass" in text
|
||||
assert "http://gitea.test" in text
|
||||
|
||||
def test_is_plain_text(self):
|
||||
cred = CredentialRecord(
|
||||
id="1",
|
||||
name="test",
|
||||
role=MemberRole.STUDENT,
|
||||
username="1",
|
||||
password="pw",
|
||||
email="[email protected]",
|
||||
)
|
||||
text = _build_email_text(cred, "http://x")
|
||||
assert "<html>" not in text
|
||||
|
||||
|
||||
class TestReadCredentialsCsv:
|
||||
def test_basic(self, tmp_path: Path):
|
||||
csv_file = tmp_path / "creds.csv"
|
||||
csv_file.write_text(
|
||||
"id,name,role,username,password,email\n"
|
||||
"2659999,赵一伤,student,2659999,abc123,[email protected]\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
records = _read_credentials_csv(csv_file)
|
||||
assert len(records) == 1
|
||||
assert records[0].password == "abc123"
|
||||
Reference in New Issue
Block a user