"""
认证测试用例
"""

import pytest
from config.settings import settings


@pytest.mark.auth
@pytest.mark.smoke
class TestAuth:
    """认证测试类"""
    
    @pytest.mark.asyncio
    async def test_login_success(self, http_client):
        """测试成功登录"""
        response = await http_client.post("/api/auth/login", json={
            "username": settings.TEST_USERNAME,
            "password": settings.TEST_PASSWORD
        })
        
        assert response.status_code == 200
        data = response.json()
        assert "token" in data
        assert isinstance(data["token"], str)
        assert "userId" in data
        assert "username" in data
    
    @pytest.mark.asyncio
    async def test_login_invalid_credentials(self, http_client):
        """测试无效凭证登录"""
        response = await http_client.post("/api/auth/login", json={
            "username": "invalid_user",
            "password": "invalid_password"
        })
        
        assert response.status_code == 401
    
    @pytest.mark.asyncio
    async def test_login_missing_fields(self, http_client):
        """测试缺少必填字段"""
        response = await http_client.post("/api/auth/login", json={
            "username": "test"
        })
        
        assert response.status_code == 400
    
    @pytest.mark.asyncio
    async def test_register_success(self, http_client):
        """测试注册成功"""
        import time
        timestamp = int(time.time() * 1000)
        response = await http_client.post("/api/auth/register", json={
            "username": f"testuser_{timestamp}",
            "password": "password123",
            "email": f"test_{timestamp}@example.com"
        })
        
        assert response.status_code == 201
        data = response.json()
        assert "id" in data
        assert data["username"] == f"testuser_{timestamp}"
    
    @pytest.mark.asyncio
    async def test_register_duplicate_username(self, http_client):
        """测试注册重复用户名"""
        response = await http_client.post("/api/auth/register", json={
            "username": "admin",
            "password": "password123",
            "email": "admin@example.com"
        })
        
        assert response.status_code == 400
    
    @pytest.mark.asyncio
    async def test_logout_success(self, http_client):
        """测试登出成功"""
        response = await http_client.post("/api/auth/logout")
        
        assert response.status_code == 200
