|
| 1 | +"""Tests for authentication endpoints.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from httpx import ASGITransport, AsyncClient |
| 5 | + |
| 6 | +from app.main import app |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.asyncio |
| 10 | +async def test_login_success() -> None: |
| 11 | + """Valid credentials should return access + refresh tokens.""" |
| 12 | + transport = ASGITransport(app=app) |
| 13 | + async with AsyncClient(transport=transport, base_url="http://test") as client: |
| 14 | + response = await client.post( |
| 15 | + "/api/auth/login", |
| 16 | + json={"id": "admin", "pw": "admin"}, |
| 17 | + ) |
| 18 | + assert response.status_code == 200 |
| 19 | + data = response.json() |
| 20 | + assert "access_token" in data |
| 21 | + assert "refresh_token" in data |
| 22 | + assert data["token_type"] == "bearer" |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.asyncio |
| 26 | +async def test_login_invalid_password() -> None: |
| 27 | + """Wrong password should return 401.""" |
| 28 | + transport = ASGITransport(app=app) |
| 29 | + async with AsyncClient(transport=transport, base_url="http://test") as client: |
| 30 | + response = await client.post( |
| 31 | + "/api/auth/login", |
| 32 | + json={"id": "admin", "pw": "wrongpassword"}, |
| 33 | + ) |
| 34 | + assert response.status_code == 401 |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.asyncio |
| 38 | +async def test_login_invalid_id() -> None: |
| 39 | + """Wrong ID should return 401.""" |
| 40 | + transport = ASGITransport(app=app) |
| 41 | + async with AsyncClient(transport=transport, base_url="http://test") as client: |
| 42 | + response = await client.post( |
| 43 | + "/api/auth/login", |
| 44 | + json={"id": "wronguser", "pw": "admin"}, |
| 45 | + ) |
| 46 | + assert response.status_code == 401 |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.asyncio |
| 50 | +async def test_refresh_token() -> None: |
| 51 | + """Valid refresh token should return new access token.""" |
| 52 | + transport = ASGITransport(app=app) |
| 53 | + async with AsyncClient(transport=transport, base_url="http://test") as client: |
| 54 | + login_resp = await client.post( |
| 55 | + "/api/auth/login", |
| 56 | + json={"id": "admin", "pw": "admin"}, |
| 57 | + ) |
| 58 | + refresh_token = login_resp.json()["refresh_token"] |
| 59 | + |
| 60 | + refresh_resp = await client.post( |
| 61 | + "/api/auth/refresh", |
| 62 | + json={"refresh_token": refresh_token}, |
| 63 | + ) |
| 64 | + assert refresh_resp.status_code == 200 |
| 65 | + data = refresh_resp.json() |
| 66 | + assert "access_token" in data |
| 67 | + assert "refresh_token" in data |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_refresh_with_access_token_fails() -> None: |
| 72 | + """Using access token as refresh token should fail.""" |
| 73 | + transport = ASGITransport(app=app) |
| 74 | + async with AsyncClient(transport=transport, base_url="http://test") as client: |
| 75 | + login_resp = await client.post( |
| 76 | + "/api/auth/login", |
| 77 | + json={"id": "admin", "pw": "admin"}, |
| 78 | + ) |
| 79 | + access_token = login_resp.json()["access_token"] |
| 80 | + |
| 81 | + refresh_resp = await client.post( |
| 82 | + "/api/auth/refresh", |
| 83 | + json={"refresh_token": access_token}, |
| 84 | + ) |
| 85 | + assert refresh_resp.status_code == 401 |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_health_endpoint_without_token() -> None: |
| 90 | + """Health check should work without authentication.""" |
| 91 | + transport = ASGITransport(app=app) |
| 92 | + async with AsyncClient(transport=transport, base_url="http://test") as client: |
| 93 | + response = await client.get("/api/health") |
| 94 | + assert response.status_code == 200 |
0 commit comments