|
| 1 | +"""Authentication API tests""" |
| 2 | +import pytest |
| 3 | +from httpx import AsyncClient |
| 4 | + |
| 5 | + |
| 6 | +@pytest.mark.asyncio |
| 7 | +async def test_register_user(client: AsyncClient): |
| 8 | + """Test user registration""" |
| 9 | + response = await client.post( |
| 10 | + "/api/v1/auth/register", |
| 11 | + json={ |
| 12 | + "email": "test@example.com", |
| 13 | + "password": "testpassword123", |
| 14 | + "display_name": "Test User", |
| 15 | + }, |
| 16 | + ) |
| 17 | + assert response.status_code == 200 |
| 18 | + data = response.json() |
| 19 | + assert data["code"] == 0 |
| 20 | + assert data["data"]["email"] == "test@example.com" |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.asyncio |
| 24 | +async def test_register_duplicate_email(client: AsyncClient): |
| 25 | + """Test registration with duplicate email fails""" |
| 26 | + # First registration |
| 27 | + await client.post( |
| 28 | + "/api/v1/auth/register", |
| 29 | + json={ |
| 30 | + "email": "duplicate@example.com", |
| 31 | + "password": "testpassword123", |
| 32 | + }, |
| 33 | + ) |
| 34 | + # Second registration with same email |
| 35 | + response = await client.post( |
| 36 | + "/api/v1/auth/register", |
| 37 | + json={ |
| 38 | + "email": "duplicate@example.com", |
| 39 | + "password": "testpassword123", |
| 40 | + }, |
| 41 | + ) |
| 42 | + assert response.status_code == 400 |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.asyncio |
| 46 | +async def test_login_success(client: AsyncClient): |
| 47 | + """Test successful login""" |
| 48 | + # Register first |
| 49 | + await client.post( |
| 50 | + "/api/v1/auth/register", |
| 51 | + json={ |
| 52 | + "email": "login@example.com", |
| 53 | + "password": "testpassword123", |
| 54 | + }, |
| 55 | + ) |
| 56 | + # Login |
| 57 | + response = await client.post( |
| 58 | + "/api/v1/auth/login", |
| 59 | + data={ |
| 60 | + "username": "login@example.com", |
| 61 | + "password": "testpassword123", |
| 62 | + }, |
| 63 | + ) |
| 64 | + assert response.status_code == 200 |
| 65 | + data = response.json() |
| 66 | + assert "access_token" in data |
| 67 | + assert data["token_type"] == "bearer" |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_login_wrong_password(client: AsyncClient): |
| 72 | + """Test login with wrong password fails""" |
| 73 | + # Register first |
| 74 | + await client.post( |
| 75 | + "/api/v1/auth/register", |
| 76 | + json={ |
| 77 | + "email": "wrongpw@example.com", |
| 78 | + "password": "correctpassword", |
| 79 | + }, |
| 80 | + ) |
| 81 | + # Login with wrong password |
| 82 | + response = await client.post( |
| 83 | + "/api/v1/auth/login", |
| 84 | + data={ |
| 85 | + "username": "wrongpw@example.com", |
| 86 | + "password": "wrongpassword", |
| 87 | + }, |
| 88 | + ) |
| 89 | + assert response.status_code == 401 |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.asyncio |
| 93 | +async def test_get_current_user(client: AsyncClient): |
| 94 | + """Test getting current user info""" |
| 95 | + # Register and login |
| 96 | + await client.post( |
| 97 | + "/api/v1/auth/register", |
| 98 | + json={ |
| 99 | + "email": "me@example.com", |
| 100 | + "password": "testpassword123", |
| 101 | + "display_name": "Me User", |
| 102 | + }, |
| 103 | + ) |
| 104 | + login_response = await client.post( |
| 105 | + "/api/v1/auth/login", |
| 106 | + data={ |
| 107 | + "username": "me@example.com", |
| 108 | + "password": "testpassword123", |
| 109 | + }, |
| 110 | + ) |
| 111 | + token = login_response.json()["access_token"] |
| 112 | + |
| 113 | + # Get current user |
| 114 | + response = await client.get( |
| 115 | + "/api/v1/auth/me", |
| 116 | + headers={"Authorization": f"Bearer {token}"}, |
| 117 | + ) |
| 118 | + assert response.status_code == 200 |
| 119 | + data = response.json() |
| 120 | + assert data["data"]["email"] == "me@example.com" |
| 121 | + assert data["data"]["display_name"] == "Me User" |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.asyncio |
| 125 | +async def test_unauthorized_access(client: AsyncClient): |
| 126 | + """Test accessing protected endpoint without token""" |
| 127 | + response = await client.get("/api/v1/auth/me") |
| 128 | + assert response.status_code == 401 |
0 commit comments