Skip to content

Commit e06985a

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

File tree

7 files changed

+180
-2
lines changed

7 files changed

+180
-2
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

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ requires = ["hatchling>=1.12.0"]
3939
[dependency-groups]
4040
dev = [
4141
"pre-commit>=3.5.0",
42+
"pytest>=8.3.3",
4243
]

src/stac_auth_proxy/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@
55
It includes FastAPI routes for handling authentication, authorization, and interaction
66
with some internal STAC API.
77
"""
8+
9+
from .app import create_app
10+
from .config import Settings
11+
12+
__all__ = ["create_app", "Settings"]

src/stac_auth_proxy/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Main module for the STAC Auth Proxy."""
1+
"""Entry point for running the module without customized code."""
22

33
import uvicorn
44
from uvicorn.config import LOGGING_CONFIG

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

uv.lock

Lines changed: 88 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)