"""密码复杂度策略纯逻辑测试(不连接数据库)。""" from __future__ import annotations import pytest from dms.security.passwords import validate_password_policy def test_strong_passwords_with_three_or_more_classes_pass(): validate_password_policy("Initial@2026") # 四类 validate_password_policy("Password123") # 三类(大写/小写/数字) validate_password_policy("password123!") # 三类(小写/数字/特殊) validate_password_policy("ABcd!@#$") # 三类(大写/小写/特殊) def test_length_bounds_enforced(): with pytest.raises(ValueError, match="密码长度"): validate_password_policy("Ab1!x") # 过短(5字符) with pytest.raises(ValueError, match="密码长度"): validate_password_policy("Aa1!" * 20) # 过长(80字符) def test_only_short_password_rejected_by_length(): with pytest.raises(ValueError, match="密码长度"): validate_password_policy("Ab1!") # 4字符,长度先于复杂度失败 def test_two_classes_rejected(): with pytest.raises(ValueError, match="至少三类"): validate_password_policy("password123") # 仅小写+数字 with pytest.raises(ValueError, match="至少三类"): validate_password_policy("PASSWORD123") # 仅大写+数字 with pytest.raises(ValueError, match="至少三类"): validate_password_policy("abcdefgh") # 仅小写 with pytest.raises(ValueError, match="至少三类"): validate_password_policy("AbcdEfgh") # 仅大小写两类 def test_exact_boundary_lengths_accepted(): validate_password_policy("Aa1!bcde") # 恰8字符,三类 validate_password_policy("Aa1!" + "x" * 60) # 恰64字符,三类