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 e06985a
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 2 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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ requires = ["hatchling>=1.12.0"]
[dependency-groups]
dev = [
"pre-commit>=3.5.0",
"pytest>=8.3.3",
]
5 changes: 5 additions & 0 deletions src/stac_auth_proxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
It includes FastAPI routes for handling authentication, authorization, and interaction
with some internal STAC API.
"""

from .app import create_app
from .config import Settings

__all__ = ["create_app", "Settings"]
2 changes: 1 addition & 1 deletion src/stac_auth_proxy/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Main module for the STAC Auth Proxy."""
"""Entry point for running the module without customized code."""

import uvicorn
from uvicorn.config import LOGGING_CONFIG
Expand Down
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)
19 changes: 19 additions & 0 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Basic test cases for the proxy app."""

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"}
89 changes: 88 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e06985a

Please sign in to comment.