Skip to content

Commit

Permalink
refactor tests of trailing slash behaviour into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssadai committed Jul 31, 2024
1 parent 17c3908 commit 9e29589
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
19 changes: 0 additions & 19 deletions tests/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@
from app.api import utility as util


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

response = test_app.get("/")

assert response.status_code == 200
assert "Welcome to the Neurobagel REST API!" in response.text
assert '<a href="/docs">documentation</a>' in response.text


@pytest.mark.filterwarnings("ignore:.*NB_API_ALLOWED_ORIGINS")
@pytest.mark.parametrize(
"valid_data_element_URI",
Expand Down Expand Up @@ -242,12 +232,3 @@ def mock_load_json(path):
"namespace_prefix": expected_namespace_pfx,
"term_labels": mock_term_labels,
}


def test_request_with_trailing_slash_not_redirected(test_app):
"""
Test that a request to a route with a trailing slash '/', where none is expected,
is *not* redirected and returns a 404.
"""
response = test_app.get("/attributes/nb:SomeClass/")
assert response.status_code == 404
18 changes: 0 additions & 18 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,21 +511,3 @@ def test_query_without_token_succeeds_when_auth_disabled(
monkeypatch.setattr(crud, "get", mock_successful_get)
response = test_app.get(ROUTE)
assert response.status_code == 200


def test_request_without_trailing_slash_not_redirected(
test_app, monkeypatch, mock_successful_get, disable_auth
):
"""Test that a request to the /query route is not redirected to have a trailing slash."""
monkeypatch.setattr(crud, "get", mock_successful_get)
response = test_app.get(ROUTE, follow_redirects=False)
assert response.status_code == 200


def test_query_params_with_leading_slash_fails(test_app, disable_auth):
"""
Test that a request to the /query route with a slash before query parameters
is *not* redirected to exclude the slash, and fails with a 404.
"""
response = test_app.get(f"{ROUTE}/?min_age=20")
assert response.status_code == 404
45 changes: 45 additions & 0 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pytest

from app.api import crud


@pytest.mark.parametrize(
"root_path",
["/", ""],
)
def test_root(test_app, root_path):
"""Given a GET request to the root endpoint, Check for 200 status and expected content."""

response = test_app.get(root_path, follow_redirects=False)

assert response.status_code == 200
assert "Welcome to the Neurobagel REST API!" in response.text
assert '<a href="/docs">documentation</a>' in response.text


@pytest.mark.parametrize(
"valid_route",
["/query", "/query?min_age=20"],
)
def test_request_without_trailing_slash_not_redirected(
test_app, monkeypatch, mock_successful_get, disable_auth, valid_route
):
"""Test that a request to a route without a / is not redirected to have a trailing slash."""
monkeypatch.setattr(crud, "get", mock_successful_get)
response = test_app.get(valid_route, follow_redirects=False)
assert response.status_code == 200


@pytest.mark.parametrize(
"invalid_route",
["/query/", "/query/?min_age=20", "/attributes/nb:SomeClass/"],
)
def test_request_with_trailing_slash_not_redirected(
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 returns a 404.
"""
response = test_app.get(invalid_route)
assert response.status_code == 404

0 comments on commit 9e29589

Please sign in to comment.