from app.services.auth import AuthService from app.services import auth as auth_module def test_validate_password_rejects_over_72_utf8_bytes_with_friendly_message() -> None: password = "a" * 73 try: AuthService.hash_password(password) raise AssertionError("expected ValueError for overlong password") except ValueError as exc: message = str(exc) assert "72 个 UTF-8 字节" in message assert "72 bytes" not in message.lower() def test_validate_password_counts_utf8_bytes_not_only_character_length() -> None: password = "密" * 25 try: AuthService.validate_password(password) raise AssertionError("expected ValueError for multibyte password overflow") except ValueError as exc: message = str(exc) assert "72 个 UTF-8 字节" in message def test_hash_password_falls_back_when_bcrypt_backend_misreports_72_bytes(monkeypatch) -> None: class _BrokenPwdContext: @staticmethod def hash(_password: str) -> str: raise ValueError("password cannot be longer than 72 bytes, truncate manually if necessary") monkeypatch.setattr(auth_module, "HAS_PASSLIB", True) monkeypatch.setattr(auth_module, "pwd_context", _BrokenPwdContext(), raising=False) hashed = AuthService.hash_password("Passw0rd!") assert hashed == AuthService._simple_hash("Passw0rd!")