-
Notifications
You must be signed in to change notification settings - Fork 522
test: Add test for api.ietf_utils #8965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2e2015f
test: Add test for api.ietf_utils
kesara d89138b
test: Use ietf.utils.test_utils instead of django.test
kesara 9895c8f
test: Add test case for request without a token
kesara 6e5868d
test: Test for misconfigured endpoint
kesara b9cbbe7
test: Improve tests
kesara 4e80ea1
test: Add test for a API call without X_API_KEY header
kesara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,86 @@ | ||
# Copyright The IETF Trust 2025, All Rights Reserved | ||
|
||
from django.test import RequestFactory | ||
from django.test.utils import override_settings | ||
|
||
from ietf.api.ietf_utils import is_valid_token, requires_api_token | ||
from ietf.utils.test_utils import TestCase | ||
|
||
|
||
class IetfUtilsTests(TestCase): | ||
@override_settings( | ||
APP_API_TOKENS={ | ||
"ietf.api.foobar": ["valid-token"], | ||
"ietf.api.misconfigured": "valid-token", # misconfigured | ||
} | ||
) | ||
def test_is_valid_token(self): | ||
self.assertFalse(is_valid_token("ietf.fake.endpoint", "valid-token")) | ||
self.assertFalse(is_valid_token("ietf.api.foobar", "invalid-token")) | ||
self.assertFalse(is_valid_token("ietf.api.foobar", None)) | ||
self.assertTrue(is_valid_token("ietf.api.foobar", "valid-token")) | ||
|
||
# misconfiguration | ||
self.assertFalse(is_valid_token("ietf.api.misconfigured", "v")) | ||
self.assertFalse(is_valid_token("ietf.api.misconfigured", None)) | ||
self.assertTrue(is_valid_token("ietf.api.misconfigured", "valid-token")) | ||
|
||
@override_settings( | ||
APP_API_TOKENS={ | ||
"ietf.api.foo": ["valid-token"], | ||
"ietf.api.bar": ["another-token"], | ||
"ietf.api.misconfigured": "valid-token", # misconfigured | ||
} | ||
) | ||
def test_requires_api_token(self): | ||
@requires_api_token("ietf.api.foo") | ||
def protected_function(request): | ||
return f"Access granted: {request.method}" | ||
|
||
# request with a valid token | ||
request = RequestFactory().get( | ||
"/some/url", headers={"X_API_KEY": "valid-token"} | ||
) | ||
result = protected_function(request) | ||
self.assertEqual(result, "Access granted: GET") | ||
|
||
# request with an invalid token | ||
request = RequestFactory().get( | ||
"/some/url", headers={"X_API_KEY": "invalid-token"} | ||
) | ||
result = protected_function(request) | ||
self.assertEqual(result.status_code, 403) | ||
|
||
# request without a token | ||
request = RequestFactory().get("/some/url", headers={"X_API_KEY": ""}) | ||
kesara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result = protected_function(request) | ||
self.assertEqual(result.status_code, 403) | ||
|
||
# request without a X_API_KEY token | ||
request = RequestFactory().get("/some/url") | ||
result = protected_function(request) | ||
self.assertEqual(result.status_code, 403) | ||
|
||
# request with a valid token for another API endpoint | ||
request = RequestFactory().get( | ||
"/some/url", headers={"X_API_KEY": "another-token"} | ||
) | ||
result = protected_function(request) | ||
self.assertEqual(result.status_code, 403) | ||
kesara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# requests for a misconfigured endpoint | ||
@requires_api_token("ietf.api.misconfigured") | ||
def another_protected_function(request): | ||
return f"Access granted: {request.method}" | ||
|
||
# request with valid token | ||
request = RequestFactory().get( | ||
"/some/url", headers={"X_API_KEY": "valid-token"} | ||
) | ||
result = another_protected_function(request) | ||
self.assertEqual(result, "Access granted: GET") | ||
|
||
# request with invalid token with the correct initial character | ||
request = RequestFactory().get("/some/url", headers={"X_API_KEY": "v"}) | ||
result = another_protected_function(request) | ||
self.assertEqual(result.status_code, 403) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.