Spaces:
No application file
No application file
File size: 2,746 Bytes
7ab2fcd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | """
Tests for Docker & Kubernetes Document Generation API API.
"""
import pytest
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_health():
r = client.get("/health")
assert r.status_code == 200
data = r.json()
assert data["status"] == "healthy"
assert "version" in data
def test_unauthorized():
r = client.get("/items")
assert r.status_code == 403
def test_login():
r = client.post("/auth/token", params={"username": "admin", "password": "admin123"})
assert r.status_code == 200
assert "access_token" in r.json()
def _auth_headers():
r = client.post("/auth/token", params={"username": "admin", "password": "admin123"})
token = r.json()["access_token"]
return {"Authorization": f"Bearer {token}"}
def test_create_item():
headers = _auth_headers()
payload = {"name": "Test Item", "description": "A test", "tags": ["test"]}
r = client.post("/items", json=payload, headers=headers)
assert r.status_code == 201
data = r.json()
assert data["name"] == "Test Item"
assert "id" in data
def test_list_items():
headers = _auth_headers()
r = client.get("/items", headers=headers)
assert r.status_code == 200
assert isinstance(r.json(), list)
def test_get_item():
headers = _auth_headers()
# Create
payload = {"name": "Get Test", "tags": []}
r = client.post("/items", json=payload, headers=headers)
item_id = r.json()["id"]
# Get
r = client.get(f"/items/{item_id}", headers=headers)
assert r.status_code == 200
assert r.json()["id"] == item_id
def test_update_item():
headers = _auth_headers()
payload = {"name": "Update Test", "tags": []}
r = client.post("/items", json=payload, headers=headers)
item_id = r.json()["id"]
r = client.patch(f"/items/{item_id}", json={"name": "Updated"}, headers=headers)
assert r.status_code == 200
assert r.json()["name"] == "Updated"
def test_delete_item():
headers = _auth_headers()
payload = {"name": "Delete Test", "tags": []}
r = client.post("/items", json=payload, headers=headers)
item_id = r.json()["id"]
r = client.delete(f"/items/{item_id}", headers=headers)
assert r.status_code == 204
r = client.get(f"/items/{item_id}", headers=headers)
assert r.status_code == 404
def test_stats():
headers = _auth_headers()
r = client.get("/stats", headers=headers)
assert r.status_code == 200
assert "total_items" in r.json()
def test_openapi_docs():
r = client.get("/openapi.json")
assert r.status_code == 200
assert "openapi" in r.json()
|