aee0f09db8
- Datafabrik: Dockerfile fix, agentorkestrering fungerar - Vision: Identify-modell, FAISS, OCR alla testade - API: Alla 7 integrationstester passerade - Upplösare: Entitetsupplösning verifierad
74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
"""Tester för user CRUD."""
|
|
import pytest
|
|
from httpx import AsyncClient
|
|
|
|
from app.models import User, Tenant
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_user(client: AsyncClient, admin_token: str, test_tenant: Tenant):
|
|
response = await client.post("/api/v1/users", headers={"Authorization": f"Bearer {admin_token}"}, json={
|
|
"email": "newuser@landvex.se",
|
|
"first_name": "New",
|
|
"last_name": "User",
|
|
"password": "securepass123",
|
|
"role": "analyst",
|
|
"tenant_id": str(test_tenant.id),
|
|
})
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert data["email"] == "newuser@landvex.se"
|
|
assert data["role"] == "analyst"
|
|
assert data["status"] == "active"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_users(client: AsyncClient, admin_token: str):
|
|
response = await client.get("/api/v1/users", headers={"Authorization": f"Bearer {admin_token}"})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "items" in data
|
|
assert "total" in data
|
|
assert len(data["items"]) >= 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_user(client: AsyncClient, admin_token: str, test_user: User):
|
|
response = await client.get(f"/api/v1/users/{test_user.id}", headers={"Authorization": f"Bearer {admin_token}"})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["email"] == test_user.email
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_user(client: AsyncClient, admin_token: str, test_user: User):
|
|
response = await client.patch(f"/api/v1/users/{test_user.id}", headers={"Authorization": f"Bearer {admin_token}"}, json={
|
|
"first_name": "Updated",
|
|
})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["first_name"] == "Updated"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_user(client: AsyncClient, admin_token: str, test_tenant: Tenant, db_session):
|
|
# Skapa en användare att ta bort
|
|
from app.models import User, UserRole, UserStatus
|
|
from app.security import get_password_hash
|
|
import uuid
|
|
user = User(
|
|
id=uuid.uuid4(),
|
|
email="delete-me@landvex.se",
|
|
first_name="Delete",
|
|
last_name="Me",
|
|
hashed_password=get_password_hash("password123"),
|
|
role=UserRole.VIEWER,
|
|
status=UserStatus.ACTIVE,
|
|
tenant_id=test_tenant.id,
|
|
)
|
|
db_session.add(user)
|
|
await db_session.commit()
|
|
|
|
response = await client.delete(f"/api/v1/users/{user.id}", headers={"Authorization": f"Bearer {admin_token}"})
|
|
assert response.status_code == 204
|