-
Notifications
You must be signed in to change notification settings - Fork 18
Ignore empty GitHub API keys #484
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # SPDX-License-Identifier: MIT | ||
| import pytest | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| from collectoss.tasks.github.util.github_api_key_handler import GithubApiKeyHandler | ||
|
|
||
|
|
||
| github_whitespace_api_keys_list = ["", " "] | ||
| github_none_api_key = None | ||
| github_valid_api_key = "ghp_1234567890abcdef1234567890abcdef12345678" | ||
| github_valid_db_api_key = "ghp_abcdef1234567890abcdef1234567890abcdef12" | ||
|
|
||
| def build_handler(config_key, db_keys): | ||
| logger = Mock() | ||
|
|
||
| with patch("collectoss.tasks.github.util.github_api_key_handler.RedisList"), \ | ||
| patch.object(GithubApiKeyHandler, "get_config_key", return_value=config_key), \ | ||
| patch.object(GithubApiKeyHandler, "get_api_keys_from_database", return_value=db_keys), \ | ||
| patch.object(GithubApiKeyHandler, "is_bad_api_key", return_value=False) as mock_is_bad_api_key: | ||
| handler = GithubApiKeyHandler(logger) | ||
|
|
||
| return handler, mock_is_bad_api_key, logger | ||
|
|
||
| @pytest.mark.unit | ||
| class TestConfigKeys: | ||
|
|
||
| @pytest.mark.parametrize("github_whitespace_api_key", github_whitespace_api_keys_list) | ||
| def test_whitespace_config_key_with_no_db_keys(self, github_whitespace_api_key): | ||
| db_keys = [] | ||
| handler, mock_is_bad_api_key, logger = build_handler(github_whitespace_api_key, db_keys) | ||
|
|
||
| assert handler.keys == [] | ||
| assert mock_is_bad_api_key.call_count == 0 | ||
| # with no keys left, get_api_keys returns before it reaches redis | ||
| handler.redis_key_list.clear.assert_not_called() | ||
|
Check warning on line 35 in tests/test_classes/test_github_api_keys.py
|
||
| handler.redis_key_list.extend.assert_not_called() | ||
|
Check warning on line 36 in tests/test_classes/test_github_api_keys.py
|
||
| logger.warning.assert_called_once() | ||
|
|
||
| def test_none_config_key_with_no_db_keys(self): | ||
| db_keys = [] | ||
| handler, mock_is_bad_api_key, logger = build_handler(github_none_api_key, db_keys) | ||
|
|
||
| assert handler.keys == [] | ||
| assert mock_is_bad_api_key.call_count == 0 | ||
| handler.redis_key_list.clear.assert_not_called() | ||
|
Check warning on line 45 in tests/test_classes/test_github_api_keys.py
|
||
| handler.redis_key_list.extend.assert_not_called() | ||
|
Check warning on line 46 in tests/test_classes/test_github_api_keys.py
|
||
| logger.warning.assert_not_called() | ||
|
|
||
| def test_valid_config_key_with_no_db_keys(self): | ||
| db_keys = [] | ||
| handler, mock_is_bad_api_key, logger = build_handler(github_valid_api_key, db_keys) | ||
|
|
||
| assert handler.keys == [github_valid_api_key] | ||
| assert mock_is_bad_api_key.call_count == 1 | ||
| handler.redis_key_list.extend.assert_called_once_with([github_valid_api_key]) | ||
|
Check warning on line 55 in tests/test_classes/test_github_api_keys.py
|
||
| logger.warning.assert_not_called() | ||
|
MoralCode marked this conversation as resolved.
|
||
|
|
||
| @pytest.mark.parametrize("github_whitespace_api_key", github_whitespace_api_keys_list) | ||
| def test_whitespace_config_key_with_db_keys(self, github_whitespace_api_key): | ||
| expected_keys = [github_valid_db_api_key] | ||
| # get_api_keys appends to the list it gets back, so hand it a copy | ||
| handler, mock_is_bad_api_key, logger = build_handler(github_whitespace_api_key, list(expected_keys)) | ||
|
|
||
| assert handler.keys == expected_keys | ||
| assert mock_is_bad_api_key.call_count == 1 | ||
| handler.redis_key_list.extend.assert_called_once_with(expected_keys) | ||
|
Check warning on line 66 in tests/test_classes/test_github_api_keys.py
|
||
| logger.warning.assert_called_once() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm i wonder if we could shorten this to
if self.config_key:(and keep the else block)
AFAIK (needs testing) i think python treats empty strings as falsy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried this approach before the actual nested implementation. The three cases are pinned in the test file (
""," ",None)The problem is that
if self.config_key:would catchNoneand"", but" "is not falsy, so it gets added . With a flatelse,Nonelogs the warning, whichtest_none_config_key_with_no_db_keysasserts it shouldn't:Nonejust means the variable was never set.Also tried
if self.config_key.strip(). Catches de whitespace but raises anAttributeErroronNone.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think it would it be better to throw a
self.config_key = self.config_key.strip()earlier in the process? then it would collapse the whitespace case into the "" case that is already handled withif self.config_key?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, probably the besst approach.
I'll add
right after the key is read with
self.config_key = self.get_config_key(). Then, this part can end up as:That
elifforNoneneeds to be there so we don't log a warning for a missing token.Note:
self.config_keyis also read to filter the config key out of the database keys, so that comparison now will see the normalized value. I think that's an improvement