|
| 1 | +"""test API things""" |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +from fastapi.testclient import TestClient |
| 7 | +import pytest |
| 8 | +from loguru import logger |
| 9 | +from thin_controller import app |
| 10 | + |
| 11 | +logger.remove() |
| 12 | +logger.add(sys.stderr, level="DEBUG") |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(scope="function") |
| 16 | +def client() -> TestClient: |
| 17 | + """Create a test client for the FastAPI app.""" |
| 18 | + return TestClient(app) |
| 19 | + |
| 20 | + |
| 21 | +def test_index(client: TestClient) -> None: |
| 22 | + """Test the main endpoint.""" |
| 23 | + |
| 24 | + response = client.get("/") |
| 25 | + assert response.status_code == 200 |
| 26 | + assert "Thin Controller" in response.content.decode() |
| 27 | + |
| 28 | + |
| 29 | +def test_css(client: TestClient) -> None: |
| 30 | + """Test the main endpoint.""" |
| 31 | + |
| 32 | + for url in ["/css/styles.css", "/css/simple.min.css"]: |
| 33 | + response = client.get(url) |
| 34 | + assert response.status_code == 200 |
| 35 | + |
| 36 | + assert client.get("/css/doesnotexist.css").status_code == 404 |
| 37 | + |
| 38 | + |
| 39 | +def test_favicon(client: TestClient) -> None: |
| 40 | + """Test the favicon endpoint.""" |
| 41 | + |
| 42 | + response = client.get("/img/favicon.png") |
| 43 | + assert response.status_code == 200 |
| 44 | + |
| 45 | + assert client.get("/img/doesnotexist.png").status_code == 404 |
| 46 | + |
| 47 | + |
| 48 | +def test_api_config(client: TestClient) -> None: |
| 49 | + """Test the config endpoint.""" |
| 50 | + |
| 51 | + response = client.get("/api/config") |
| 52 | + assert response.status_code == 200 |
| 53 | + assert "regions" in response.json() |
| 54 | + |
| 55 | + |
| 56 | +def test_read_instance(client: TestClient) -> None: |
| 57 | + """Test the instance read endpoint.""" |
| 58 | + |
| 59 | + response = client.get("/api/instances") |
| 60 | + print(response.content) |
| 61 | + if os.getenv("AWS_ACCESS_KEY_ID") is None: |
| 62 | + assert response.status_code == 500 |
| 63 | + else: |
| 64 | + assert response.status_code == 200 |
| 65 | + assert isinstance(response.json(), dict) |
0 commit comments