|
| 1 | +"""Basic test cases for the proxy app.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from fastapi import FastAPI |
| 5 | +from fastapi.testclient import TestClient |
| 6 | + |
| 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 | + ) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize( |
| 25 | + "path,method,expected_status", |
| 26 | + [ |
| 27 | + ("/", "GET", 200), |
| 28 | + ("/conformance", "GET", 200), |
| 29 | + ("/queryables", "GET", 200), |
| 30 | + ("/search", "GET", 200), |
| 31 | + ("/search", "POST", 200), |
| 32 | + ("/collections", "GET", 200), |
| 33 | + ("/collections", "POST", 403), |
| 34 | + ("/collections/example-collection", "GET", 200), |
| 35 | + ("/collections/example-collection", "PUT", 403), |
| 36 | + ("/collections/example-collection", "DELETE", 403), |
| 37 | + ("/collections/example-collection/items", "GET", 200), |
| 38 | + ("/collections/example-collection/items", "POST", 403), |
| 39 | + ("/collections/example-collection/items/example-item", "GET", 200), |
| 40 | + ("/collections/example-collection/items/example-item", "PUT", 403), |
| 41 | + ("/collections/example-collection/items/example-item", "DELETE", 403), |
| 42 | + ("/collections/example-collection/bulk_items", "POST", 403), |
| 43 | + ("/api.html", "GET", 200), |
| 44 | + ("/api", "GET", 200), |
| 45 | + ], |
| 46 | +) |
| 47 | +def test_default_public_true(source_api_server, path, method, expected_status): |
| 48 | + """ |
| 49 | + When default_public=true and private_endpoints aren't set, all endpoints should be |
| 50 | + public except for transaction endpoints. |
| 51 | + """ |
| 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 | + ) |
| 60 | + ) |
| 61 | + client = TestClient(test_app) |
| 62 | + response = client.request(method=method, url=path) |
| 63 | + assert response.status_code == expected_status |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize( |
| 67 | + "path,method,expected_status", |
| 68 | + [ |
| 69 | + ("/", "GET", 403), |
| 70 | + ("/conformance", "GET", 403), |
| 71 | + ("/queryables", "GET", 403), |
| 72 | + ("/search", "GET", 403), |
| 73 | + ("/search", "POST", 403), |
| 74 | + ("/collections", "GET", 403), |
| 75 | + ("/collections", "POST", 403), |
| 76 | + ("/collections/example-collection", "GET", 403), |
| 77 | + ("/collections/example-collection", "PUT", 403), |
| 78 | + ("/collections/example-collection", "DELETE", 403), |
| 79 | + ("/collections/example-collection/items", "GET", 403), |
| 80 | + ("/collections/example-collection/items", "POST", 403), |
| 81 | + ("/collections/example-collection/items/example-item", "GET", 403), |
| 82 | + ("/collections/example-collection/items/example-item", "PUT", 403), |
| 83 | + ("/collections/example-collection/items/example-item", "DELETE", 403), |
| 84 | + ("/collections/example-collection/bulk_items", "POST", 403), |
| 85 | + ("/api.html", "GET", 200), |
| 86 | + ("/api", "GET", 200), |
| 87 | + ], |
| 88 | +) |
| 89 | +def test_default_public_false(source_api_server, path, method, expected_status): |
| 90 | + """ |
| 91 | + When default_public=false and private_endpoints aren't set, all endpoints should be |
| 92 | + public except for transaction endpoints. |
| 93 | + """ |
| 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 | + ) |
| 102 | + ) |
| 103 | + client = TestClient(test_app) |
| 104 | + response = client.request(method=method, url=path) |
| 105 | + assert response.status_code == expected_status |
0 commit comments