Skip to content

Commit eceb834

Browse files
committed
Add basic tests
1 parent 9bc1d34 commit eceb834

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

.github/workflows/cicd.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,19 @@ jobs:
99
- uses: actions/checkout@v4
1010
- uses: actions/setup-python@v3
1111
- uses: pre-commit/[email protected]
12+
13+
test:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: actions/setup-python@v3
19+
20+
- name: Install uv
21+
uses: astral-sh/setup-uv@v4
22+
with:
23+
enable-cache: true
24+
25+
- name: Run tests
26+
run: |
27+
uv run pytest

tests/conftest.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Pytest fixtures."""
2+
3+
import threading
4+
5+
import pytest
6+
import uvicorn
7+
from fastapi import FastAPI
8+
9+
from stac_auth_proxy import Settings, create_app
10+
11+
12+
@pytest.fixture(scope="session")
13+
def source_api():
14+
"""Create upstream API for testing purposes."""
15+
app = FastAPI()
16+
17+
@app.get("/")
18+
def read_root():
19+
return {"message": "Hello from source API"}
20+
21+
@app.get("/items/{item_id}")
22+
def read_item(item_id: int):
23+
return {"item_id": item_id}
24+
25+
return app
26+
27+
28+
@pytest.fixture(scope="session")
29+
def source_api_server(source_api):
30+
"""Run the source API in a background thread."""
31+
host, port = "127.0.0.1", 8000
32+
server = uvicorn.Server(
33+
uvicorn.Config(
34+
source_api,
35+
host=host,
36+
port=port,
37+
)
38+
)
39+
thread = threading.Thread(target=server.run)
40+
thread.start()
41+
yield f"http://{host}:{port}"
42+
server.should_exit = True
43+
thread.join()
44+
45+
46+
@pytest.fixture
47+
def proxy_app(source_api_server: str) -> FastAPI:
48+
"""Fixture for the proxy app, pointing to the source API."""
49+
test_app_settings = Settings(upstream_url=source_api_server, default_public=False)
50+
return create_app(test_app_settings)

tests/test_proxy.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from fastapi.testclient import TestClient
2+
3+
4+
def test_auth_applied(proxy_app):
5+
"""Verify authentication is applied."""
6+
client = TestClient(proxy_app)
7+
response = client.get("/")
8+
assert response.status_code == 403, "Expect unauthorized without auth header"
9+
10+
11+
def test_correct_auth_header(proxy_app):
12+
"""Verify content is returned when correct auth header is provided."""
13+
client = TestClient(proxy_app)
14+
headers = {"Authorization": "Bearer correct_token"}
15+
response = client.get("/", headers=headers)
16+
assert response.status_code == 200
17+
assert response.json() == {"message": "Hello from source API"}

0 commit comments

Comments
 (0)