Files
zcglxt/tests/services/test_auth_service.py
Claude e71181f0a3 fix: 修复多个关键问题
- 修复前端路由守卫:未登录时不显示提示,直接跳转登录页
- 修复API拦截器:401错误不显示提示,直接跳转
- 增强验证码显示:图片尺寸从120x40增加到200x80
- 增大验证码字体:从28号增加到48号
- 优化验证码字符:排除易混淆的0和1
- 减少干扰线:从5条减少到3条,添加背景色优化
- 增强登录API日志:添加详细的调试日志
- 增强验证码生成和验证日志
- 优化异常处理和错误追踪

影响文件:
- src/router/index.ts
- src/api/request.ts
- app/services/auth_service.py
- app/api/v1/auth.py
- app/schemas/user.py

测试状态:
- 前端构建通过
- 后端语法检查通过
- 验证码显示效果优化完成

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-25 00:26:21 +08:00

763 lines
20 KiB
Python

"""
认证服务测试
测试内容:
- 登录服务测试(15+用例)
- Token管理测试(10+用例)
- 密码管理测试(10+用例)
- 验证码测试(5+用例)
"""
import pytest
from datetime import datetime, timedelta
from sqlalchemy.ext.asyncio import AsyncSession
from app.services.auth_service import auth_service
from app.models.user import User
from app.core.exceptions import (
InvalidCredentialsException,
UserLockedException,
UserDisabledException
)
# ==================== 登录服务测试 ====================
class TestAuthServiceLogin:
"""测试认证服务登录功能"""
@pytest.mark.asyncio
async def test_login_success(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试登录成功"""
result = await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
assert result.access_token is not None
assert result.refresh_token is not None
assert result.token_type == "Bearer"
assert result.user.id == test_user.id
assert result.user.username == test_user.username
@pytest.mark.asyncio
async def test_login_wrong_password(
self,
db_session: AsyncSession,
test_user: User
):
"""测试密码错误"""
with pytest.raises(InvalidCredentialsException):
await auth_service.login(
db_session,
test_user.username,
"wrongpassword",
"1234",
"test-uuid"
)
@pytest.mark.asyncio
async def test_login_user_not_found(
self,
db_session: AsyncSession
):
"""测试用户不存在"""
with pytest.raises(InvalidCredentialsException):
await auth_service.login(
db_session,
"nonexistent",
"password",
"1234",
"test-uuid"
)
@pytest.mark.asyncio
async def test_login_account_disabled(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试账户被禁用"""
test_user.status = "disabled"
await db_session.commit()
with pytest.raises(UserDisabledException):
await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
@pytest.mark.asyncio
async def test_login_account_locked(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试账户被锁定"""
test_user.status = "locked"
test_user.locked_until = datetime.utcnow() + timedelta(minutes=30)
await db_session.commit()
with pytest.raises(UserLockedException):
await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
@pytest.mark.asyncio
async def test_login_auto_unlock_after_lock_period(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试锁定期后自动解锁"""
test_user.status = "locked"
test_user.locked_until = datetime.utcnow() - timedelta(minutes=1)
await db_session.commit()
# 应该能登录成功并自动解锁
result = await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
assert result.access_token is not None
# 验证用户已解锁
await db_session.refresh(test_user)
assert test_user.status == "active"
assert test_user.locked_until is None
@pytest.mark.asyncio
async def test_login_increases_fail_count(
self,
db_session: AsyncSession,
test_user: User
):
"""测试登录失败增加失败次数"""
initial_count = test_user.login_fail_count
with pytest.raises(InvalidCredentialsException):
await auth_service.login(
db_session,
test_user.username,
"wrongpassword",
"1234",
"test-uuid"
)
await db_session.refresh(test_user)
assert test_user.login_fail_count == initial_count + 1
@pytest.mark.asyncio
async def test_login_locks_after_max_failures(
self,
db_session: AsyncSession,
test_user: User
):
"""测试达到最大失败次数后锁定"""
test_user.login_fail_count = 4 # 差一次就锁定
await db_session.commit()
with pytest.raises(UserLockedException):
await auth_service.login(
db_session,
test_user.username,
"wrongpassword",
"1234",
"test-uuid"
)
await db_session.refresh(test_user)
assert test_user.status == "locked"
assert test_user.locked_until is not None
@pytest.mark.asyncio
async def test_login_resets_fail_count_on_success(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试登录成功重置失败次数"""
test_user.login_fail_count = 3
await db_session.commit()
await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
await db_session.refresh(test_user)
assert test_user.login_fail_count == 0
@pytest.mark.asyncio
async def test_login_updates_last_login_time(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试登录更新最后登录时间"""
await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
await db_session.refresh(test_user)
assert test_user.last_login_at is not None
assert test_user.last_login_at.date() == datetime.utcnow().date()
@pytest.mark.asyncio
async def test_login_case_sensitive_username(
self,
db_session: AsyncSession,
test_user: User
):
"""测试用户名大小写敏感"""
with pytest.raises(InvalidCredentialsException):
await auth_service.login(
db_session,
test_user.username.upper(), # 大写
"password",
"1234",
"test-uuid"
)
@pytest.mark.asyncio
async def test_login_with_admin_user(
self,
db_session: AsyncSession,
test_admin: User,
test_password: str
):
"""测试管理员登录"""
result = await auth_service.login(
db_session,
test_admin.username,
test_password,
"1234",
"test-uuid"
)
assert result.user.is_admin is True
@pytest.mark.asyncio
async def test_login_generates_different_tokens(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试每次登录生成不同的Token"""
result1 = await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
result2 = await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
# Access token应该不同
assert result1.access_token != result2.access_token
@pytest.mark.asyncio
async def test_login_includes_user_roles(
self,
db_session: AsyncSession,
test_user: User,
test_role,
test_password: str
):
"""测试登录返回用户角色"""
# 分配角色
from app.models.user import UserRole
user_role = UserRole(
user_id=test_user.id,
role_id=test_role.id
)
db_session.add(user_role)
await db_session.commit()
result = await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
# 应该包含角色信息
# ==================== Token管理测试 ====================
class TestTokenManagement:
"""测试Token管理"""
@pytest.mark.asyncio
async def test_refresh_token_success(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试刷新Token成功"""
# 先登录
login_result = await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
# 刷新Token
result = await auth_service.refresh_token(
db_session,
login_result.refresh_token
)
assert "access_token" in result
assert "expires_in" in result
assert result["access_token"] != login_result.access_token
@pytest.mark.asyncio
async def test_refresh_token_invalid(
self,
db_session: AsyncSession
):
"""测试无效的刷新Token"""
with pytest.raises(InvalidCredentialsException):
await auth_service.refresh_token(
db_session,
"invalid_refresh_token"
)
@pytest.mark.asyncio
async def test_refresh_token_for_disabled_user(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试为禁用用户刷新Token"""
# 先登录
login_result = await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
# 禁用用户
test_user.status = "disabled"
await db_session.commit()
# 尝试刷新Token
with pytest.raises(InvalidCredentialsException):
await auth_service.refresh_token(
db_session,
login_result.refresh_token
)
@pytest.mark.asyncio
async def test_access_token_expiration(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试访问Token过期时间"""
from app.core.config import settings
login_result = await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
assert login_result.expires_in == settings.ACCESS_TOKEN_EXPIRE_MINUTES * 60
@pytest.mark.asyncio
async def test_token_contains_user_info(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试Token包含用户信息"""
login_result = await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
# 解析Token
from app.core.security import security_manager
payload = security_manager.verify_token(
login_result.access_token,
token_type="access"
)
assert int(payload.get("sub")) == test_user.id
assert payload.get("username") == test_user.username
@pytest.mark.asyncio
async def test_refresh_token_longer_lifespan(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试刷新Token比访问Token有效期更长"""
login_result = await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
# 验证两种Token都存在
assert login_result.access_token is not None
assert login_result.refresh_token is not None
assert login_result.access_token != login_result.refresh_token
@pytest.mark.asyncio
async def test_multiple_refresh_tokens(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试多次刷新Token"""
# 先登录
login_result = await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
# 多次刷新
refresh_token = login_result.refresh_token
for _ in range(3):
result = await auth_service.refresh_token(
db_session,
refresh_token
)
assert "access_token" in result
@pytest.mark.asyncio
async def test_token_type_is_bearer(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试Token类型为Bearer"""
login_result = await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
assert login_result.token_type == "Bearer"
@pytest.mark.asyncio
async def test_admin_user_has_all_permissions(
self,
db_session: AsyncSession,
test_admin: User,
test_password: str
):
"""测试管理员用户拥有所有权限"""
login_result = await auth_service.login(
db_session,
test_admin.username,
test_password,
"1234",
"test-uuid"
)
# 管理员应该有所有权限标记
# ==================== 密码管理测试 ====================
class TestPasswordManagement:
"""测试密码管理"""
@pytest.mark.asyncio
async def test_change_password_success(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试修改密码成功"""
result = await auth_service.change_password(
db_session,
test_user,
test_password,
"NewPassword123"
)
assert result is True
@pytest.mark.asyncio
async def test_change_password_wrong_old_password(
self,
db_session: AsyncSession,
test_user: User
):
"""测试修改密码时旧密码错误"""
with pytest.raises(InvalidCredentialsException):
await auth_service.change_password(
db_session,
test_user,
"wrongoldpassword",
"NewPassword123"
)
@pytest.mark.asyncio
async def test_change_password_updates_hash(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试修改密码更新哈希值"""
old_hash = test_user.password_hash
await auth_service.change_password(
db_session,
test_user,
test_password,
"NewPassword123"
)
await db_session.refresh(test_user)
assert test_user.password_hash != old_hash
@pytest.mark.asyncio
async def test_change_password_resets_lock_status(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试修改密码重置锁定状态"""
# 设置为锁定状态
test_user.status = "locked"
test_user.locked_until = datetime.utcnow() + timedelta(minutes=30)
test_user.login_fail_count = 5
await db_session.commit()
# 修改密码
await auth_service.change_password(
db_session,
test_user,
test_password,
"NewPassword123"
)
await db_session.refresh(test_user)
assert test_user.login_fail_count == 0
assert test_user.locked_until is None
@pytest.mark.asyncio
async def test_reset_password_by_admin(
self,
db_session: AsyncSession,
test_user: User
):
"""测试管理员重置密码"""
result = await auth_service.reset_password(
db_session,
test_user.id,
"AdminReset123"
)
assert result is True
@pytest.mark.asyncio
async def test_reset_password_non_existent_user(
self,
db_session: AsyncSession
):
"""测试重置不存在的用户密码"""
result = await auth_service.reset_password(
db_session,
999999,
"NewPassword123"
)
assert result is False
@pytest.mark.asyncio
async def test_password_hash_strength(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试密码哈希强度(bcrypt)"""
from app.core.security import get_password_hash
hash1 = get_password_hash("password123")
hash2 = get_password_hash("password123")
# 相同密码应该产生不同哈希(因为盐值不同)
assert hash1 != hash2
# 但都应该能验证成功
from app.core.security import security_manager
assert security_manager.verify_password("password123", hash1)
assert security_manager.verify_password("password123", hash2)
@pytest.mark.asyncio
async def test_new_password_login(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试用新密码登录"""
new_password = "NewPassword123"
# 修改密码
await auth_service.change_password(
db_session,
test_user,
test_password,
new_password
)
# 用新密码登录
result = await auth_service.login(
db_session,
test_user.username,
new_password,
"1234",
"test-uuid"
)
assert result.access_token is not None
@pytest.mark.asyncio
async def test_old_password_not_work_after_change(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试修改密码后旧密码不能登录"""
new_password = "NewPassword123"
# 修改密码
await auth_service.change_password(
db_session,
test_user,
test_password,
new_password
)
# 用旧密码登录应该失败
with pytest.raises(InvalidCredentialsException):
await auth_service.login(
db_session,
test_user.username,
test_password,
"1234",
"test-uuid"
)
# ==================== 验证码测试 ====================
class TestCaptchaVerification:
"""测试验证码验证"""
@pytest.mark.asyncio
async def test_captcha_verification_bypassed_in_test(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试验证码在测试环境中被绕过"""
# 当前的实现中,验证码验证总是返回True
result = await auth_service.login(
db_session,
test_user.username,
test_password,
"any_captcha",
"any_uuid"
)
assert result.access_token is not None
@pytest.mark.asyncio
async def test_captcha_required_parameter(
self,
db_session: AsyncSession,
test_user: User,
test_password: str
):
"""测试验证码参数存在"""
# 应该传递验证码参数,即使测试环境不验证
result = await auth_service.login(
db_session,
test_user.username,
test_password,
"",
""
)
# 验证码为空,测试环境应该允许
assert result.access_token is not None