Skip to content

Commit

Permalink
Updated server validation for versioned/unversioned URLs and spec 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Jul 2, 2020
1 parent 24b4f9c commit f0fb738
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
14 changes: 6 additions & 8 deletions tests/server/test_server_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import unittest
from traceback import print_exc

from optimade import __api_version__
from optimade.validator import ImplementationValidator

from .utils import SetClient
Expand Down Expand Up @@ -60,14 +59,13 @@ class AsTypeTestsWithValidator(SetClient, unittest.TestCase):

def test_as_type_with_validator(self):

base_url = f"http://example.org/v{__api_version__.split('.')[0]}"
test_urls = {
f"{base_url}/structures": "structures",
f"{base_url}/structures/mpf_1": "structure",
f"{base_url}/references": "references",
f"{base_url}/references/dijkstra1968": "reference",
f"{base_url}/info": "info",
f"{base_url}/links": "links",
f"{self.client.base_url}/structures": "structures",
f"{self.client.base_url}/structures/mpf_1": "structure",
f"{self.client.base_url}/references": "references",
f"{self.client.base_url}/references/dijkstra1968": "reference",
f"{self.client.base_url}/info": "info",
f"{self.client.base_url}/links": "links",
}
with unittest.mock.patch(
"requests.get", unittest.mock.Mock(side_effect=self.client.get)
Expand Down
20 changes: 12 additions & 8 deletions tests/server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@

from fastapi.testclient import TestClient

from optimade import __api_version__

VERSION_PREFIX = f"/v{__api_version__.split('.')[0]}"


def get_regular_client() -> TestClient:
"""Return TestClient for regular OPTIMADE server"""
from optimade.server.main import app
from optimade.server.routers import info, links, references, structures

# We need to remove the version prefixes in order to have the tests run correctly.
app.include_router(info.router)
app.include_router(links.router)
app.include_router(references.router)
app.include_router(structures.router)
app.include_router(info.router, prefix=VERSION_PREFIX)
app.include_router(links.router, prefix=VERSION_PREFIX)
app.include_router(references.router, prefix=VERSION_PREFIX)
app.include_router(structures.router, prefix=VERSION_PREFIX)
# need to explicitly set base_url, as the default "http://testserver"
# does not validate as pydantic AnyUrl model
return TestClient(app, base_url="http://example.org/v0")
return TestClient(app, base_url=f"http://example.org{VERSION_PREFIX}")


def get_index_client() -> TestClient:
Expand All @@ -28,11 +32,11 @@ def get_index_client() -> TestClient:
from optimade.server.routers import index_info, links

# # We need to remove the version prefixes in order to have the tests run correctly.
app.include_router(index_info.router)
app.include_router(links.router)
app.include_router(index_info.router, prefix=VERSION_PREFIX)
app.include_router(links.router, prefix=VERSION_PREFIX)
# need to explicitly set base_url, as the default "http://testserver"
# does not validate as pydantic UrlStr model
return TestClient(app, base_url="http://example.org/v0")
return TestClient(app, base_url=f"http://example.org{VERSION_PREFIX}")


class SetClient(abc.ABC):
Expand Down

0 comments on commit f0fb738

Please sign in to comment.