Skip to content

Commit 22696b8

Browse files
committed
chore: utilze app factory for tests
1 parent 82819fe commit 22696b8

File tree

3 files changed

+53
-45
lines changed

3 files changed

+53
-45
lines changed

tests/test_defaults.py

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
"""Basic test cases for the proxy app."""
22

33
import pytest
4-
from fastapi import FastAPI
54
from fastapi.testclient import TestClient
5+
from utils import AppFactory
66

7-
from stac_auth_proxy import Settings, create_app
8-
9-
10-
@pytest.fixture(scope="module")
11-
def test_app(source_api_server: str) -> FastAPI:
12-
"""Fixture for the proxy app, pointing to the source API."""
13-
return create_app(
14-
Settings.model_validate(
15-
{
16-
"upstream_url": source_api_server,
17-
"oidc_discovery_url": "https://samples.auth0.com/.well-known/openid-configuration",
18-
"default_public": False,
19-
},
20-
)
21-
)
7+
app_factory = AppFactory(
8+
oidc_discovery_url="https://samples.auth0.com/.well-known/openid-configuration"
9+
)
2210

2311

2412
@pytest.mark.parametrize(
@@ -49,14 +37,17 @@ def test_default_public_true(source_api_server, path, method, expected_status):
4937
When default_public=true and private_endpoints aren't set, all endpoints should be
5038
public except for transaction endpoints.
5139
"""
52-
test_app = create_app(
53-
Settings.model_validate(
54-
{
55-
"upstream_url": source_api_server,
56-
"oidc_discovery_url": "https://samples.auth0.com/.well-known/openid-configuration",
57-
"default_public": True,
58-
},
59-
)
40+
test_app = app_factory(
41+
upstream_url=source_api_server,
42+
public_endpoints={},
43+
private_endpoints={
44+
"/collections": ["POST"],
45+
"/collections/{collection_id}": ["PUT", "PATCH", "DELETE"],
46+
"/collections/{collection_id}/items": ["POST"],
47+
"/collections/{collection_id}/items/{item_id}": ["PUT", "PATCH", "DELETE"],
48+
"/collections/{collection_id}/bulk_items": ["POST"],
49+
},
50+
default_public=True,
6051
)
6152
client = TestClient(test_app)
6253
response = client.request(method=method, url=path)
@@ -91,14 +82,11 @@ def test_default_public_false(source_api_server, path, method, expected_status):
9182
When default_public=false and private_endpoints aren't set, all endpoints should be
9283
public except for transaction endpoints.
9384
"""
94-
test_app = create_app(
95-
Settings.model_validate(
96-
{
97-
"upstream_url": source_api_server,
98-
"oidc_discovery_url": "https://samples.auth0.com/.well-known/openid-configuration",
99-
"default_public": False,
100-
},
101-
)
85+
test_app = app_factory(
86+
upstream_url=source_api_server,
87+
public_endpoints={"/api.html": ["GET"], "/api": ["GET"]},
88+
private_endpoints={},
89+
default_public=False,
10290
)
10391
client = TestClient(test_app)
10492
response = client.request(method=method, url=path)

tests/test_openapi.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
from fastapi import FastAPI
44
from fastapi.testclient import TestClient
5+
from utils import AppFactory
56

6-
from stac_auth_proxy import Settings, create_app
7+
app_factory = AppFactory(
8+
oidc_discovery_url="https://samples.auth0.com/.well-known/openid-configuration"
9+
)
710

811

912
def test_no_edit_openapi_spec(source_api_server):
1013
"""When no OpenAPI spec endpoint is set, the proxied OpenAPI spec is unaltered."""
11-
app = create_app(
12-
Settings(
13-
upstream_url=source_api_server,
14-
oidc_discovery_url="https://samples.auth0.com/.well-known/openid-configuration",
15-
openapi_spec_endpoint=None,
16-
)
14+
app = app_factory(
15+
upstream_url=source_api_server,
16+
openapi_spec_endpoint=None,
1717
)
1818
client = TestClient(app)
1919
response = client.get("/api")
@@ -27,12 +27,9 @@ def test_no_edit_openapi_spec(source_api_server):
2727

2828
def test_oidc_in_openapi_spec(source_api: FastAPI, source_api_server: str):
2929
"""When OpenAPI spec endpoint is set, the proxied OpenAPI spec is augmented with oidc details."""
30-
app = create_app(
31-
Settings(
32-
upstream_url=source_api_server,
33-
oidc_discovery_url="https://samples.auth0.com/.well-known/openid-configuration",
34-
openapi_spec_endpoint=source_api.openapi_url,
35-
)
30+
app = app_factory(
31+
upstream_url=source_api_server,
32+
openapi_spec_endpoint=source_api.openapi_url,
3633
)
3734
client = TestClient(app)
3835
response = client.get(source_api.openapi_url)

tests/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Utilities for testing."""
2+
3+
from dataclasses import dataclass, field
4+
from typing import Callable
5+
from stac_auth_proxy import Settings, create_app
6+
7+
8+
class AppFactory:
9+
"""Factory for creating test apps with default settings."""
10+
11+
def __init__(self, **defaults):
12+
self.defaults = defaults
13+
14+
def __call__(self, *, upstream_url, **overrides) -> Callable:
15+
return create_app(
16+
Settings.model_validate(
17+
{
18+
**self.defaults,
19+
**overrides,
20+
"upstream_url": upstream_url,
21+
},
22+
)
23+
)

0 commit comments

Comments
 (0)