67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
"""Tests for batch API and fetch_all pagination."""
|
|
|
|
import app.main # noqa: F401
|
|
|
|
from app.pagination_helpers import slice_page
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.main import app
|
|
|
|
|
|
def test_slice_page_fetch_all():
|
|
items = list(range(5))
|
|
page, total, lim, off = slice_page(items, offset=0, limit=2, fetch_all=True)
|
|
assert page == items
|
|
assert total == 5
|
|
assert lim == 5
|
|
assert off == 0
|
|
|
|
|
|
def test_slice_page_normal():
|
|
items = list(range(10))
|
|
page, total, lim, off = slice_page(items, offset=3, limit=4, fetch_all=False)
|
|
assert page == [3, 4, 5, 6]
|
|
assert total == 10
|
|
assert lim == 4
|
|
assert off == 3
|
|
|
|
|
|
def test_batch_api_work_report():
|
|
client = TestClient(app)
|
|
# health without key
|
|
r = client.post(
|
|
"/api/batch",
|
|
json={
|
|
"requests": [
|
|
{
|
|
"id": "h",
|
|
"method": "GET",
|
|
"path": "/api/health",
|
|
}
|
|
]
|
|
},
|
|
)
|
|
# batch requires api key if configured - may be 401 without key
|
|
assert r.status_code in (200, 401)
|
|
|
|
|
|
def test_batch_invalid_path():
|
|
client = TestClient(app)
|
|
os_key = __import__("os").environ.get("USER_READER_API_KEY", "").strip()
|
|
headers = {"X-Api-Key": os_key} if os_key else {}
|
|
r = client.post(
|
|
"/api/batch",
|
|
headers=headers,
|
|
json={
|
|
"requests": [
|
|
{"id": "bad", "method": "GET", "path": "http://evil/api/health"}
|
|
]
|
|
},
|
|
)
|
|
if r.status_code == 401:
|
|
return
|
|
assert r.status_code == 200
|
|
data = r.json()
|
|
assert data["results"][0]["ok"] is False
|
|
assert data["results"][0]["status"] == 400
|