-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#150: Add tests to check all endpoints exist and have correct HTTP m…
…ethods on them
- Loading branch information
1 parent
ba59926
commit 79bdbbe
Showing
2 changed files
with
74 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.