cloud-api/test/test_service.py

91 lines
2.5 KiB
Python
Raw Normal View History

2026-05-29 16:55:21 +01:00
"""
409 on [POST]/service/ not tested because SQLite throws a different error than Postgres
"""
import pytest
from httpx import AsyncClient
from .conftest import default_client
2026-05-29 16:55:21 +01:00
@pytest.mark.anyio
async def test_get_services_success(default_client: AsyncClient):
resp = await default_client.get("/service/?org_id=1")
2026-05-29 16:55:21 +01:00
data = resp.json()
assert resp.status_code == 200
assert "services" in data
assert data["services"][0]["id"] == 1
assert data["services"][0]["name"] == "Test Service"
@pytest.mark.anyio
@pytest.mark.parametrize(
"query, expected_status",
[
("org_id=2", 404),
("org_id=banana", 422),
("", 422),
],
)
@pytest.mark.anyio
async def test_get_services_failure(default_client: AsyncClient, query: str, expected_status: int):
resp = await default_client.get(f"/service/?{query}")
2026-05-29 16:55:21 +01:00
assert resp.status_code == expected_status
@pytest.mark.anyio
async def test_post_service_success(default_client: AsyncClient):
resp = await default_client.post("/service/", json={"name": "New Test Service"})
2026-05-29 16:55:21 +01:00
data = resp.json()
assert resp.status_code == 200
assert "service" in data
assert data["service"]["name"] == "New Test Service"
assert data["service"]["id"] == 2
assert type(data["service"]["api_key"]) == str
@pytest.mark.anyio
@pytest.mark.parametrize(
"body, expected_status",
[
({"name": 42}, 422),
({}, 422),
],
)
@pytest.mark.anyio
async def test_post_services_failure(default_client: AsyncClient, body: dict[str, str], expected_status: int):
resp = await default_client.post("/service/", json=body)
2026-05-29 16:55:21 +01:00
assert resp.status_code == expected_status
@pytest.mark.anyio
async def test_patch_service_success(default_client: AsyncClient):
resp = await default_client.patch("/service/key", json={"service_id": 1})
2026-05-29 16:55:21 +01:00
data = resp.json()
assert resp.status_code == 200
assert "service" in data
assert data["service"]["name"] == "Test Service"
assert data["service"]["id"] == 1
assert type(data["service"]["api_key"]) == str
@pytest.mark.anyio
@pytest.mark.parametrize(
"body, expected_status",
[
({"service_id": 42}, 404),
({"service_id": "Test Service"}, 422),
({"service_id": ""}, 422),
({}, 422),
],
)
@pytest.mark.anyio
async def test_patch_services_failure(default_client: AsyncClient, body: dict[str, str], expected_status: int):
resp = await default_client.patch("/service/key", json=body)
2026-05-29 16:55:21 +01:00
assert resp.status_code == expected_status