Skip to content

Commit

Permalink
test handling of trailing slashes in routes
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssadai committed Jul 31, 2024
1 parent 3b27cb4 commit 1e1cc7f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 34 deletions.
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,21 @@ async def _mock_httpx_get_with_connect_error(self, **kwargs):
raise httpx.ConnectError("Some connection error")

return _mock_httpx_get_with_connect_error


@pytest.fixture()
def mocked_single_matching_dataset_result():
"""Valid aggregate query result for a single matching dataset."""
return {
"dataset_uuid": "http://neurobagel.org/vocab/12345",
"dataset_name": "QPN",
"dataset_portal_uri": "https://rpq-qpn.ca/en/researchers-section/databases/",
"dataset_total_subjects": 200,
"num_matching_subjects": 5,
"records_protected": True,
"subject_data": "protected",
"image_modals": [
"http://purl.org/nidash/nidm#T1Weighted",
"http://purl.org/nidash/nidm#T2Weighted",
],
}
16 changes: 0 additions & 16 deletions tests/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,6 @@
from fastapi import status


def test_root(test_app, set_valid_test_federation_nodes):
"""Given a GET request to the root endpoint, Check for 200 status and expected content."""

response = test_app.get("/")

assert response.status_code == status.HTTP_200_OK
assert all(
substring in response.text
for substring in [
"Welcome to",
"Neurobagel",
'<a href="/docs">documentation</a>',
]
)


def test_partially_failed_terms_fetching_handled_gracefully(
test_app, monkeypatch, set_valid_test_federation_nodes, caplog
):
Expand Down
18 changes: 0 additions & 18 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,6 @@ def mock_token():
return "Bearer foo"


@pytest.fixture()
def mocked_single_matching_dataset_result():
"""Valid aggregate query result for a single matching dataset."""
return {
"dataset_uuid": "http://neurobagel.org/vocab/12345",
"dataset_name": "QPN",
"dataset_portal_uri": "https://rpq-qpn.ca/en/researchers-section/databases/",
"dataset_total_subjects": 200,
"num_matching_subjects": 5,
"records_protected": True,
"subject_data": "protected",
"image_modals": [
"http://purl.org/nidash/nidm#T1Weighted",
"http://purl.org/nidash/nidm#T2Weighted",
],
}


def test_partial_node_failure_responses_handled_gracefully(
monkeypatch,
test_app,
Expand Down
59 changes: 59 additions & 0 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import httpx
import pytest
from fastapi import status


def test_root(test_app, set_valid_test_federation_nodes):
"""Given a GET request to the root endpoint, Check for 200 status and expected content."""

response = test_app.get("/")

assert response.status_code == status.HTTP_200_OK
assert all(
substring in response.text
for substring in [
"Welcome to",
"Neurobagel",
'<a href="/docs">documentation</a>',
]
)


@pytest.mark.parametrize(
"valid_route",
["/query", "/query?min_age=20", "/nodes"],
)
def test_request_without_trailing_slash_not_redirected(
test_app,
monkeypatch,
set_valid_test_federation_nodes,
mocked_single_matching_dataset_result,
disable_auth,
valid_route,
):
"""Test that a request to the /query route is not redirected to have a trailing slash."""

async def mock_httpx_get(self, **kwargs):
return httpx.Response(
status_code=200, json=[mocked_single_matching_dataset_result]
)

monkeypatch.setattr(httpx.AsyncClient, "get", mock_httpx_get)

response = test_app.get(valid_route, follow_redirects=False)
assert response.status_code == status.HTTP_200_OK


@pytest.mark.parametrize(
"invalid_route",
["/query/", "/query/?min_age=20", "/nodes/", "/attributes/nb:SomeClass/"],
)
def test_request_including_trailing_slash_fails(
test_app, disable_auth, invalid_route
):
"""
Test that a request to routes including a trailing slash, where none is expected,
is *not* redirected to exclude the slash, and fails with a 404.
"""
response = test_app.get(invalid_route)
assert response.status_code == status.HTTP_404_NOT_FOUND

0 comments on commit 1e1cc7f

Please sign in to comment.