64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
|
|
"""Tests for the example plugin."""
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
from httpx import AsyncClient, ASGITransport
|
||
|
|
|
||
|
|
from app.main import create_app
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
async def client():
|
||
|
|
"""Create a test client."""
|
||
|
|
app = create_app()
|
||
|
|
transport = ASGITransport(app=app)
|
||
|
|
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
||
|
|
yield ac
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def auth_headers():
|
||
|
|
"""Return headers with valid authentication."""
|
||
|
|
return {
|
||
|
|
"Authorization": "Bearer test-token",
|
||
|
|
"X-Tenant-ID": "test-tenant",
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_list_items_requires_auth(client: AsyncClient):
|
||
|
|
"""Test that listing items requires authentication."""
|
||
|
|
response = await client.get("/api/v1/example")
|
||
|
|
assert response.status_code in (401, 403)
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_list_items_with_auth(client: AsyncClient, auth_headers: dict):
|
||
|
|
"""Test that listing items works with authentication."""
|
||
|
|
response = await client.get("/api/v1/example", headers=auth_headers)
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
assert "items" in data
|
||
|
|
assert "total" in data
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_create_item_requires_write_permission(client: AsyncClient, auth_headers: dict):
|
||
|
|
"""Test that creating items requires write permission."""
|
||
|
|
response = await client.post(
|
||
|
|
"/api/v1/example",
|
||
|
|
headers=auth_headers,
|
||
|
|
json={"name": "Test Item"},
|
||
|
|
)
|
||
|
|
# May return 201 or 403 depending on test permissions
|
||
|
|
assert response.status_code in (201, 403)
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.asyncio
|
||
|
|
async def test_get_item_returns_404_for_missing(client: AsyncClient, auth_headers: dict):
|
||
|
|
"""Test that getting a non-existent item returns 404."""
|
||
|
|
response = await client.get(
|
||
|
|
"/api/v1/example/non-existent-id",
|
||
|
|
headers=auth_headers,
|
||
|
|
)
|
||
|
|
assert response.status_code == 404
|