Skip to content

Commit

Permalink
Add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alukach committed Nov 30, 2024
1 parent 9bc1d34 commit eceb834
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
50 changes: 50 additions & 0 deletions tests/conftest.py
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)
17 changes: 17 additions & 0 deletions tests/test_proxy.py
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"}

0 comments on commit eceb834

Please sign in to comment.