diff --git a/optimade_gateway/routers/gateways.py b/optimade_gateway/routers/gateways.py index 518c8a1e..fbc8da92 100644 --- a/optimade_gateway/routers/gateways.py +++ b/optimade_gateway/routers/gateways.py @@ -7,7 +7,6 @@ where, `id` may be left out. """ from fastapi import APIRouter, Depends, Request -from fastapi.responses import RedirectResponse from optimade.models import ToplevelLinks from optimade.server.query_params import EntryListingQueryParams from optimade.server.schemas import ERROR_RESPONSES @@ -121,15 +120,20 @@ async def post_gateways( async def get_gateway(request: Request, gateway_id: str) -> GatewaysResponseSingle: """`GET /gateways/{gateway ID}` - Represent an OPTIMADE server. - - !!! note - For now, redirect to the gateway's `/structures` entry listing endpoint. - + Return a single [`GatewayResource`][optimade_gateway.models.gateways.GatewayResource]. """ - from optimade_gateway.routers.utils import validate_resource + from optimade.server.routers.utils import meta_values + from optimade_gateway.routers.utils import get_valid_resource + + result = await get_valid_resource(GATEWAYS_COLLECTION, gateway_id) - await validate_resource(GATEWAYS_COLLECTION, gateway_id) - return RedirectResponse( - request.url.replace(path=f"{request.url.path.rstrip('/')}/structures") + return GatewaysResponseSingle( + links=ToplevelLinks(next=None), + data=result, + meta=meta_values( + url=request.url, + data_returned=1, + data_available=await GATEWAYS_COLLECTION.count(), + more_data_available=False, + ), ) diff --git a/tests/conftest.py b/tests/conftest.py index 6519243d..1df9a105 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -132,6 +132,19 @@ async def _get_gateway(id: str) -> dict: return _get_gateway +@pytest.fixture +async def random_gateway() -> dict: + """Get a random gateway currently in the MongoDB""" + from optimade_gateway.mongo.database import MONGO_DB + + gateway_ids = set() + async for gateway in MONGO_DB["gateways"].find( + filter={}, projection={"id": True, "_id": False} + ): + gateway_ids.add(gateway["id"]) + return gateway_ids.pop() + + @pytest.fixture async def reset_db_after(top_dir: Path) -> None: """Reset MongoDB with original test data after the test has run""" diff --git a/tests/routers/test_gateways.py b/tests/routers/test_gateways.py index 69fb2787..ce25f427 100644 --- a/tests/routers/test_gateways.py +++ b/tests/routers/test_gateways.py @@ -190,6 +190,7 @@ async def test_post_gateways_database_ids( assert db["id"] in data["database_ids"] +@pytest.mark.usefixtures("reset_db_after") async def test_post_gateways_create_with_db_ids( client: Callable[ [str, FastAPI, str, Literal["get", "post", "put", "delete", "patch"]], @@ -252,3 +253,30 @@ async def test_post_gateways_create_with_db_ids( db_datum = await MONGO_DB["gateways"].find_one(mongo_filter) for db in db_datum["databases"]: assert db["id"] in [data["databases"][0]["id"], data["database_ids"][0]] + + +async def test_get_single_gateway( + client: Callable[ + [str, FastAPI, str, Literal["get", "post", "put", "delete", "patch"]], + Awaitable[httpx.Response], + ], + random_gateway: str, + top_dir: Path, +): + """Test GET /gateways/{gateway_id}""" + import json + + from optimade_gateway.models.responses import GatewaysResponseSingle + + response = await client(f"/gateways/{random_gateway}") + + assert response.status_code == 200, f"Request failed: {response.json()}" + response = GatewaysResponseSingle(**response.json()) + assert response + + with open(top_dir / "tests/static/test_gateways.json") as handle: + test_data = json.load(handle) + + assert response.meta.data_returned == 1 + assert response.meta.data_available == len(test_data) + assert not response.meta.more_data_available