-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,19 @@ jobs: | |
- uses: actions/checkout@v4 | ||
- uses: actions/setup-python@v3 | ||
- uses: pre-commit/[email protected] | ||
|
||
test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-python@v3 | ||
|
||
- name: Install uv | ||
uses: astral-sh/setup-uv@v4 | ||
with: | ||
enable-cache: true | ||
|
||
- name: Run tests | ||
run: | | ||
uv run pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Pytest fixtures.""" | ||
|
||
import threading | ||
|
||
import pytest | ||
import uvicorn | ||
from fastapi import FastAPI | ||
|
||
from stac_auth_proxy import Settings, create_app | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def source_api(): | ||
"""Create upstream API for testing purposes.""" | ||
app = FastAPI() | ||
|
||
@app.get("/") | ||
def read_root(): | ||
return {"message": "Hello from source API"} | ||
|
||
@app.get("/items/{item_id}") | ||
def read_item(item_id: int): | ||
return {"item_id": item_id} | ||
|
||
return app | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def source_api_server(source_api): | ||
"""Run the source API in a background thread.""" | ||
host, port = "127.0.0.1", 8000 | ||
server = uvicorn.Server( | ||
uvicorn.Config( | ||
source_api, | ||
host=host, | ||
port=port, | ||
) | ||
) | ||
thread = threading.Thread(target=server.run) | ||
thread.start() | ||
yield f"http://{host}:{port}" | ||
server.should_exit = True | ||
thread.join() | ||
|
||
|
||
@pytest.fixture | ||
def proxy_app(source_api_server: str) -> FastAPI: | ||
"""Fixture for the proxy app, pointing to the source API.""" | ||
test_app_settings = Settings(upstream_url=source_api_server, default_public=False) | ||
return create_app(test_app_settings) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from fastapi.testclient import TestClient | ||
|
||
|
||
def test_auth_applied(proxy_app): | ||
"""Verify authentication is applied.""" | ||
client = TestClient(proxy_app) | ||
response = client.get("/") | ||
assert response.status_code == 403, "Expect unauthorized without auth header" | ||
|
||
|
||
def test_correct_auth_header(proxy_app): | ||
"""Verify content is returned when correct auth header is provided.""" | ||
client = TestClient(proxy_app) | ||
headers = {"Authorization": "Bearer correct_token"} | ||
response = client.get("/", headers=headers) | ||
assert response.status_code == 200 | ||
assert response.json() == {"message": "Hello from source API"} |