|
| 1 | +import importlib |
| 2 | + |
| 3 | +import pytest |
| 4 | +from starlette.testclient import TestClient |
| 5 | + |
| 6 | +from stac_fastapi.pgstac.db import close_db_connection, connect_to_db |
| 7 | + |
| 8 | +BASE_URL = "http://api.acme.com" |
| 9 | +ROOT_PATH = "/stac/v1" |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(scope="function") |
| 13 | +async def app_with_root_path(database, monkeypatch): |
| 14 | + """ |
| 15 | + Provides the global stac_fastapi.pgstac.app.app instance, configured with a |
| 16 | + specific ROOT_PATH environment variable and connected to the test database. |
| 17 | + """ |
| 18 | + |
| 19 | + monkeypatch.setenv("ROOT_PATH", ROOT_PATH) |
| 20 | + monkeypatch.setenv("PGUSER", database.user) |
| 21 | + monkeypatch.setenv("PGPASSWORD", database.password) |
| 22 | + monkeypatch.setenv("PGHOST", database.host) |
| 23 | + monkeypatch.setenv("PGPORT", str(database.port)) |
| 24 | + monkeypatch.setenv("PGDATABASE", database.dbname) |
| 25 | + monkeypatch.setenv("ENABLE_TRANSACTIONS_EXTENSIONS", "TRUE") |
| 26 | + |
| 27 | + # Reload the app module to pick up the new environment variables |
| 28 | + import stac_fastapi.pgstac.app |
| 29 | + |
| 30 | + importlib.reload(stac_fastapi.pgstac.app) |
| 31 | + |
| 32 | + from stac_fastapi.pgstac.app import app, with_transactions |
| 33 | + |
| 34 | + # Ensure the app's root_path is configured as expected |
| 35 | + assert ( |
| 36 | + app.root_path == ROOT_PATH |
| 37 | + ), f"app_with_root_path fixture: app.root_path is '{app.root_path}', expected '{ROOT_PATH}'" |
| 38 | + |
| 39 | + await connect_to_db(app, add_write_connection_pool=with_transactions) |
| 40 | + yield app |
| 41 | + await close_db_connection(app) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture(scope="function") |
| 45 | +def client_with_root_path(app_with_root_path): |
| 46 | + with TestClient( |
| 47 | + app_with_root_path, |
| 48 | + base_url=BASE_URL, |
| 49 | + root_path=ROOT_PATH, |
| 50 | + ) as c: |
| 51 | + yield c |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture(scope="function") |
| 55 | +def loaded_client(client_with_root_path, load_test_data): |
| 56 | + col = load_test_data("test_collection.json") |
| 57 | + resp = client_with_root_path.post( |
| 58 | + "/collections", |
| 59 | + json=col, |
| 60 | + ) |
| 61 | + assert resp.status_code == 201 |
| 62 | + item = load_test_data("test_item.json") |
| 63 | + resp = client_with_root_path.post( |
| 64 | + f"/collections/{col['id']}/items", |
| 65 | + json=item, |
| 66 | + ) |
| 67 | + assert resp.status_code == 201 |
| 68 | + item = load_test_data("test_item2.json") |
| 69 | + resp = client_with_root_path.post( |
| 70 | + f"/collections/{col['id']}/items", |
| 71 | + json=item, |
| 72 | + ) |
| 73 | + assert resp.status_code == 201 |
| 74 | + yield client_with_root_path |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize( |
| 78 | + "path", |
| 79 | + [ |
| 80 | + "/search?limit=1", |
| 81 | + "/collections?limit=1", |
| 82 | + "/collections/test-collection/items?limit=1", |
| 83 | + ], |
| 84 | +) |
| 85 | +def test_search_links_are_valid(loaded_client, path): |
| 86 | + resp = loaded_client.get(path) |
| 87 | + assert resp.status_code == 200 |
| 88 | + response_json = resp.json() |
| 89 | + |
| 90 | + # Ensure all links start with the expected URL prefix and check that |
| 91 | + # there is no root_path duplicated in the URL. |
| 92 | + failed_links = [] |
| 93 | + expected_prefix = f"{BASE_URL}{ROOT_PATH}" |
| 94 | + |
| 95 | + for link in response_json.get("links", []): |
| 96 | + href = link["href"] |
| 97 | + rel = link.get("rel", "unknown") |
| 98 | + |
| 99 | + # Check if link starts with the expected prefix |
| 100 | + if not href.startswith(expected_prefix): |
| 101 | + failed_links.append( |
| 102 | + { |
| 103 | + "rel": rel, |
| 104 | + "href": href, |
| 105 | + "error": f"does not start with expected prefix '{expected_prefix}'", |
| 106 | + } |
| 107 | + ) |
| 108 | + continue |
| 109 | + |
| 110 | + # Check for duplicated root path |
| 111 | + remainder = href[len(expected_prefix) :] |
| 112 | + if remainder.startswith(ROOT_PATH): |
| 113 | + failed_links.append( |
| 114 | + { |
| 115 | + "rel": rel, |
| 116 | + "href": href, |
| 117 | + "error": f"contains duplicated root path '{ROOT_PATH}'", |
| 118 | + } |
| 119 | + ) |
| 120 | + |
| 121 | + # If there are failed links, create a detailed error report |
| 122 | + if failed_links: |
| 123 | + error_report = "Link validation failed:\n" |
| 124 | + for failed_link in failed_links: |
| 125 | + error_report += f" - rel: '{failed_link['rel']}', href: '{failed_link['href']}' - {failed_link['error']}\n" |
| 126 | + |
| 127 | + raise AssertionError(error_report) |
0 commit comments