-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract
is_api_request()
and add unit tests
Extract `is_api_request()` into a shared helper function (instead of a private method) and add unit tests. Future commits will add code in multiple places that call the new shared helper function. But even with only one caller for the function there are still benefits to extracting it and unit-testing it directly: when writing the tests I noticed that it was returning `None` instead of `False` and added a `bool()` call to fix this.
- Loading branch information
Showing
3 changed files
with
27 additions
and
5 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
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,3 @@ | ||
def is_api_request(request) -> bool: | ||
"""Return True if `request` is an API request.""" | ||
return bool(request.matched_route and request.matched_route.name.startswith("api.")) |
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,22 @@ | ||
import pytest | ||
|
||
from h.security.policy.helpers import is_api_request | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"route_name,expected_result", | ||
[ | ||
("anything", False), | ||
("api.anything", True), | ||
], | ||
) | ||
def test_is_api_request(pyramid_request, route_name, expected_result): | ||
pyramid_request.matched_route.name = route_name | ||
|
||
assert is_api_request(pyramid_request) == expected_result | ||
|
||
|
||
def test_is_api_request_when_matched_route_is_None(pyramid_request): | ||
pyramid_request.matched_route = None | ||
|
||
assert is_api_request(pyramid_request) is False |