Skip to content

Commit

Permalink
#150: Add tests to check all endpoints exist and have correct HTTP m…
Browse files Browse the repository at this point in the history
…ethods on them
  • Loading branch information
MRichards99 committed Nov 26, 2020
1 parent ba59926 commit 79bdbbe
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 30 deletions.
74 changes: 74 additions & 0 deletions test/icat/endpoints/test_endpoint_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import pytest

from datagateway_api.src.main import api
from datagateway_api.src.resources.entities.entity_map import endpoints


class TestEndpointRules:
"""
Test class to ensure all endpoints on the API exist & have the correct HTTP methods
"""

@pytest.mark.parametrize(
"endpoint_ending, expected_methods",
[
pytest.param("/findone", ["GET"], id="findone"),
pytest.param("/count", ["GET"], id="count"),
pytest.param("/<int:id_>", ["DELETE", "GET", "PATCH"], id="id"),
pytest.param("", ["GET", "PATCH", "POST"], id="typical endpoints"),
],
)
def test_entity_endpoints(self, endpoint_ending, expected_methods):
for endpoint_entity in endpoints.keys():
endpoint_found = False

for rule in api.app.url_map.iter_rules():
if f"/{endpoint_entity.lower()}{endpoint_ending}" == rule.rule:
endpoint_found = True

for method_name in expected_methods:
# Can't do a simple equality check as .methods contains other
# methods not added by the API which aren't utilised
assert method_name in rule.methods

assert endpoint_found

@pytest.mark.parametrize(
"endpoint_name, expected_methods",
[
pytest.param("/sessions", ["DELETE", "GET", "POST", "PUT"], id="sessions"),
pytest.param(
"/instruments/<int:id_>/facilitycycles",
["GET"],
id="ISIS instrument's facility cycles",
),
pytest.param(
"/instruments/<int:id_>/facilitycycles/count",
["GET"],
id="count ISIS instrument's facility cycles",
),
pytest.param(
"/instruments/<int:instrument_id>/facilitycycles/<int:cycle_id>"
"/investigations",
["GET"],
id="ISIS investigations",
),
pytest.param(
"/instruments/<int:instrument_id>/facilitycycles/<int:cycle_id>"
"/investigations/count",
["GET"],
id="count ISIS investigations",
),
],
)
def test_non_entity_endpoints(self, endpoint_name, expected_methods):
endpoint_found = False

for rule in api.app.url_map.iter_rules():
if endpoint_name == rule.rule:
endpoint_found = True

for method_name in expected_methods:
assert method_name in rule.methods

assert endpoint_found
30 changes: 0 additions & 30 deletions test/icat/test_standard_endpoints.py

This file was deleted.

0 comments on commit 79bdbbe

Please sign in to comment.