-
Notifications
You must be signed in to change notification settings - Fork 13
Rate limit by group #466
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
Rate limit by group #466
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
36dc273
Update docs to explain rate_limit_group
bitterpanda63 d7367fb
add rate_limit_group attribute to context
bitterpanda63 6cb6434
Create starter template of set_rate_limit_group
bitterpanda63 4ef163b
re-export set_rate_limit_group
bitterpanda63 e44feef
Improve readability of should_block_request
bitterpanda63 d3b89cf
lint
bitterpanda63 037c996
SHOULD_RATELIMIT: pass along group & check group for rlm
bitterpanda63 cbb0f52
set_rate_limit_group add error messages
bitterpanda63 b2596e2
Add test cases for set_rate_limit_group
bitterpanda63 2325aff
add test cases for the should_ratelimit function
bitterpanda63 e2871c6
test with the set_rate_limit_group
bitterpanda63 f46b302
Fix context/init_test test cases with rate_limit_group
bitterpanda63 67d00c5
fix process_should_ratelimit_test.py test cases
bitterpanda63 3d575d8
lint
bitterpanda63 a6082f2
remove wrongful import in test cases
bitterpanda63 35134d8
Allow numbers as group id
bitterpanda63 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
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
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
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
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
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
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,23 @@ | ||
| from aikido_zen.context import get_current_context | ||
| from aikido_zen.helpers.logging import logger | ||
|
|
||
|
|
||
| def set_rate_limit_group(group_id: str): | ||
| if not group_id or not isinstance(group_id, str): | ||
| logger.warning("Group ID cannot be empty.") | ||
| return | ||
|
|
||
| context = get_current_context() | ||
| if not context: | ||
| logger.warning( | ||
| "set_rate_limit_group(...) was called without a context. Make sure to call set_rate_limit_group(...) within an HTTP request." | ||
| ) | ||
| return | ||
|
|
||
| if context.executed_middleware: | ||
| logger.warning( | ||
| "set_rate_limit_group(...) must be called before the Zen middleware is executed." | ||
| ) | ||
|
|
||
| context.rate_limit_group = group_id | ||
| context.set_as_current_context() |
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,97 @@ | ||
| import pytest | ||
| from aikido_zen.context import get_current_context, Context | ||
| from aikido_zen.thread.thread_cache import get_cache | ||
| from .set_rate_limit_group import set_rate_limit_group | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def run_around_tests(): | ||
| get_cache().reset() | ||
| yield | ||
| # Reset context and cache after every test | ||
| from aikido_zen.context import current_context | ||
|
|
||
| current_context.set(None) | ||
| get_cache().reset() | ||
|
|
||
|
|
||
| def set_context_and_lifecycle(): | ||
| wsgi_request = { | ||
| "REQUEST_METHOD": "GET", | ||
| "HTTP_HEADER_1": "header 1 value", | ||
| "HTTP_HEADER_2": "Header 2 value", | ||
| "RANDOM_VALUE": "Random value", | ||
| "HTTP_COOKIE": "sessionId=abc123xyz456;", | ||
| "wsgi.url_scheme": "http", | ||
| "HTTP_HOST": "localhost:8080", | ||
| "PATH_INFO": "/hello", | ||
| "QUERY_STRING": "user=JohnDoe&age=30&age=35", | ||
| "CONTENT_TYPE": "application/json", | ||
| "REMOTE_ADDR": "198.51.100.23", | ||
| } | ||
| context = Context( | ||
| req=wsgi_request, | ||
| body=None, | ||
| source="flask", | ||
| ) | ||
| context.set_as_current_context() | ||
| return context | ||
|
|
||
|
|
||
| def test_set_rate_limit_group_valid_group_id(caplog): | ||
| context1 = set_context_and_lifecycle() | ||
| assert context1.rate_limit_group is None | ||
| set_rate_limit_group("group1") | ||
| assert context1.rate_limit_group == "group1" | ||
| assert "Group ID cannot be empty." not in caplog.text | ||
| assert "was called without a context" not in caplog.text | ||
| assert "must be called before the Zen middleware is executed" not in caplog.text | ||
|
|
||
|
|
||
| def test_set_rate_limit_group_empty_group_id(caplog): | ||
| context1 = set_context_and_lifecycle() | ||
| assert context1.rate_limit_group is None | ||
| set_rate_limit_group("") | ||
| assert context1.rate_limit_group is None | ||
| assert "Group ID cannot be empty." in caplog.text | ||
|
|
||
|
|
||
| def test_set_rate_limit_group_none_group_id(caplog): | ||
| context1 = set_context_and_lifecycle() | ||
| assert context1.rate_limit_group is None | ||
| set_rate_limit_group(None) | ||
| assert context1.rate_limit_group is None | ||
| assert "Group ID cannot be empty." in caplog.text | ||
|
|
||
|
|
||
| def test_set_rate_limit_group_no_context(caplog): | ||
| from aikido_zen.context import current_context | ||
|
|
||
| current_context.set(None) | ||
| set_rate_limit_group("group1") | ||
| assert "was called without a context" in caplog.text | ||
|
|
||
|
|
||
| def test_set_rate_limit_group_middleware_already_executed(caplog): | ||
| context1 = set_context_and_lifecycle() | ||
| context1.executed_middleware = True | ||
| set_rate_limit_group("group1") | ||
| assert "must be called before the Zen middleware is executed" in caplog.text | ||
| assert context1.rate_limit_group is "group1" | ||
|
|
||
|
|
||
| def test_set_rate_limit_group_non_string_group_id(caplog): | ||
| context1 = set_context_and_lifecycle() | ||
| assert context1.rate_limit_group is None | ||
| set_rate_limit_group(123) | ||
bitterpanda63 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert context1.rate_limit_group is None | ||
| assert "Group ID cannot be empty." in caplog.text | ||
|
|
||
|
|
||
| def test_set_rate_limit_group_overwrite_existing_group(): | ||
| context1 = set_context_and_lifecycle() | ||
| assert context1.rate_limit_group is None | ||
| set_rate_limit_group("group1") | ||
| assert context1.rate_limit_group == "group1" | ||
| set_rate_limit_group("group2") | ||
| assert context1.rate_limit_group == "group2" | ||
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
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
Oops, something went wrong.
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.