Skip to content

Commit

Permalink
Add integration test for Swagger UI
Browse files Browse the repository at this point in the history
  • Loading branch information
louise-davies authored and root committed Apr 11, 2023
1 parent fb3d05a commit 1daa60d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
9 changes: 9 additions & 0 deletions datagateway_api/src/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class DataGatewayAPI(BaseModel):

_validate_extension = validator("extension", allow_reuse=True)(validate_extension)

def __getitem__(self, item):
return getattr(self, item)

@validator("db_url", always=True)
def require_db_config_value(cls, value, values): # noqa: B902, N805
"""
Expand Down Expand Up @@ -147,6 +150,9 @@ class SearchAPI(BaseModel):

_validate_extension = validator("extension", allow_reuse=True)(validate_extension)

def __getitem__(self, item):
return getattr(self, item)


class TestUserCredentials(BaseModel):
username: StrictStr
Expand Down Expand Up @@ -189,6 +195,9 @@ class APIConfig(BaseModel):

_validate_extension = validator("url_prefix", allow_reuse=True)(validate_extension)

def __getitem__(self, item):
return getattr(self, item)

@classmethod
def load(cls, path=Path(__file__).parent.parent.parent / "config.yaml"):
"""
Expand Down
1 change: 1 addition & 0 deletions test/integration/datagateway_api/db/endpoints/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ def test_config_data():
"port": "5000",
"test_user_credentials": {"username": "root", "password": "pw"},
"test_mechanism": "simple",
"url_prefix": "",
}
69 changes: 69 additions & 0 deletions test/integration/datagateway_api/test_swagger_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import json
from unittest.mock import mock_open, patch

from flask import Flask
import pytest
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.wrappers import Response

from datagateway_api.src.api_start_utils import (
create_api_endpoints,
create_app_infrastructure,
create_openapi_endpoints,
)
from datagateway_api.src.common.config import APIConfig


@pytest.fixture(params=["", "/url-prefix"], ids=["No URL prefix", "Given a URL prefix"])
def test_config_swagger(test_config_data, request):
test_config_data["url_prefix"] = request.param
test_config_data["datagateway_api"]["extension"] = (
"" if request.param == "" else "/datagateway-api"
)
test_config_data["search_api"]["extension"] = (
"/search-api" if request.param == "" else ""
)

with patch("builtins.open", mock_open(read_data=json.dumps(test_config_data))):
return APIConfig.load("test/path")


class TestSwaggerUI:
@pytest.mark.parametrize(
"api_type",
[
pytest.param("datagateway_api", id="DataGateway API"),
pytest.param("search_api", id="Search API"),
],
)
def test_swagger_ui(self, test_config_swagger, api_type, request):
# derived from the param IDs set above, used to assert the page title
api_name = request.node.callspec.id.split("-")[1]
with patch(
"datagateway_api.src.common.config.Config.config", test_config_swagger,
):
test_app = Flask(__name__)
api, spec = create_app_infrastructure(test_app)
create_api_endpoints(test_app, api, spec)
create_openapi_endpoints(test_app, spec)
test_app.wsgi_app = DispatcherMiddleware(
Response("Not Found", status=404),
{test_config_swagger.url_prefix: test_app.wsgi_app},
)
test_client = test_app.test_client()

test_response = test_client.get(
f"{test_config_swagger.url_prefix}{test_config_swagger[api_type].extension}", # noqa: B950
)

test_response_string = test_response.get_data(as_text=True)

assert f"{api_name} OpenAPI Spec" in test_response_string
assert (
f"{test_config_swagger.url_prefix}{test_config_swagger[api_type].extension}/swagger-ui" # noqa: B950
in test_response_string
)
assert (
f"{test_config_swagger.url_prefix}/{api_type.replace('_', '-')}/openapi.json" # noqa: B950
in test_response_string
)

0 comments on commit 1daa60d

Please sign in to comment.