Test infrastructure: - conftest.py with SQLite in-memory DB, isolated per-test cleanup - pytest.ini with asyncio auto mode - Fixtures: admin/dba/developer/guest/disabled users, tokens, test instance Test coverage (44 tests): - test_auth: login/logout/me/token expiry (8 tests) - test_users: CRUD + role permissions (8 tests) - test_instances: CRUD + connection test mock (8 tests) - test_tickets: CRUD + review/execute workflow + pagination (11 tests) - test_query: select/drop/delete rejection + guest deny (4 tests) - test_dashboard: stats + auth check (2 tests) Backend fix: - query endpoint now catches ValueError from QueryService → 400 instead of 500 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
import pytest
|
|
from httpx import AsyncClient
|
|
|
|
from app.core.security import create_access_token
|
|
|
|
|
|
class TestLogin:
|
|
async def test_login_success(self, client: AsyncClient, admin_user):
|
|
resp = await client.post("/api/v1/auth/login", json={
|
|
"username": "admin",
|
|
"password": "123456",
|
|
})
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert "access_token" in data
|
|
assert data["token_type"] == "bearer"
|
|
assert data["user"]["username"] == "admin"
|
|
assert data["user"]["role"] == "admin"
|
|
|
|
async def test_login_wrong_password(self, client: AsyncClient, admin_user):
|
|
resp = await client.post("/api/v1/auth/login", json={
|
|
"username": "admin",
|
|
"password": "wrong",
|
|
})
|
|
assert resp.status_code == 401
|
|
|
|
async def test_login_nonexistent_user(self, client: AsyncClient):
|
|
resp = await client.post("/api/v1/auth/login", json={
|
|
"username": "nobody",
|
|
"password": "123456",
|
|
})
|
|
assert resp.status_code == 401
|
|
|
|
async def test_login_disabled_user(self, client: AsyncClient, disabled_user):
|
|
resp = await client.post("/api/v1/auth/login", json={
|
|
"username": "disabled",
|
|
"password": "123456",
|
|
})
|
|
assert resp.status_code == 403
|
|
|
|
|
|
class TestGetMe:
|
|
async def test_get_me_no_token(self, client: AsyncClient):
|
|
resp = await client.get("/api/v1/auth/me")
|
|
assert resp.status_code in (401, 403)
|
|
|
|
async def test_get_me_valid_token(self, client: AsyncClient, admin_user, admin_token):
|
|
resp = await client.get(
|
|
"/api/v1/auth/me",
|
|
headers={"Authorization": f"Bearer {admin_token}"},
|
|
)
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["username"] == "admin"
|
|
assert "id" in data
|
|
assert "role" in data
|
|
|
|
async def test_get_me_expired_token(self, client: AsyncClient, admin_user):
|
|
from datetime import timedelta
|
|
expired = create_access_token(
|
|
data={"sub": str(admin_user.id), "role": admin_user.role},
|
|
expires_delta=timedelta(seconds=-1),
|
|
)
|
|
resp = await client.get(
|
|
"/api/v1/auth/me",
|
|
headers={"Authorization": f"Bearer {expired}"},
|
|
)
|
|
assert resp.status_code == 401
|
|
|
|
|
|
class TestLogout:
|
|
async def test_logout(self, client: AsyncClient, admin_token):
|
|
resp = await client.post(
|
|
"/api/v1/auth/logout",
|
|
headers={"Authorization": f"Bearer {admin_token}"},
|
|
)
|
|
assert resp.status_code == 200
|