diff --git a/docs/openapi.json b/docs/openapi.json index 4611654c..9c4d3c87 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -7,7 +7,7 @@ "name": "Apache 2.0", "url": "https://www.apache.org/licenses/LICENSE-2.0.html" }, - "version": "0.0.1" + "version": "0.1.0" }, "paths": { "/": { @@ -268,7 +268,10 @@ ] }, "503": { - "description": "Configuration can not be loaded" + "description": "Service Unavailable", + "detail": { + "response": "Configuration is no loaded" + } } } } diff --git a/src/app/endpoints/config.py b/src/app/endpoints/config.py index 07e79da9..8cedc74d 100644 --- a/src/app/endpoints/config.py +++ b/src/app/endpoints/config.py @@ -1,4 +1,4 @@ -"""Handler for REST API call to configuration.""" +"""Handler for REST API call to retrieve service configuration.""" import logging from typing import Any @@ -7,6 +7,7 @@ from models.config import Configuration from configuration import configuration +from utils.endpoints import check_configuration_loaded logger = logging.getLogger(__name__) router = APIRouter(tags=["config"]) @@ -46,11 +47,18 @@ {"name": "server3", "provider_id": "provider3", "url": "http://url.com:3"}, ], }, - 503: {"description": "Configuration can not be loaded"}, + 503: { + "detail": { + "response": "Configuration is no loaded", + } + }, } @router.get("/config", responses=get_config_responses) def config_endpoint_handler(_request: Request) -> Configuration: """Handle requests to the /config endpoint.""" + # ensure that configuration is loaded + check_configuration_loaded(configuration) + return configuration.configuration diff --git a/tests/unit/app/endpoints/test_config.py b/tests/unit/app/endpoints/test_config.py index 38296ce1..4ca1ad5d 100644 --- a/tests/unit/app/endpoints/test_config.py +++ b/tests/unit/app/endpoints/test_config.py @@ -1,5 +1,6 @@ import pytest +from fastapi import HTTPException, status from app.endpoints.config import config_endpoint_handler from configuration import AppConfig @@ -7,13 +8,16 @@ def test_config_endpoint_handler_configuration_not_loaded(mocker): """Test the config endpoint handler.""" mocker.patch( - "app.endpoints.query.configuration", + "app.endpoints.config.configuration", return_value=mocker.Mock(), ) + mocker.patch("app.endpoints.config.configuration", None) request = None - with pytest.raises(Exception, match="logic error: configuration is not loaded"): + with pytest.raises(HTTPException) as e: config_endpoint_handler(request) + assert e.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR + assert e.detail["response"] == "Configuration is not loaded" def test_config_endpoint_handler_configuration_loaded(mocker):