From ec6dbe0ae140e31b8864bf59bd29337b04f46900 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:20:08 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tap_github/tests/test_authenticator.py | 236 ++++++++++++++++--------- 1 file changed, 149 insertions(+), 87 deletions(-) diff --git a/tap_github/tests/test_authenticator.py b/tap_github/tests/test_authenticator.py index 998f7ae1..b5d574d5 100644 --- a/tap_github/tests/test_authenticator.py +++ b/tap_github/tests/test_authenticator.py @@ -3,7 +3,6 @@ import pytest import requests - from singer_sdk.streams import RESTStream from tap_github.authenticator import ( @@ -147,18 +146,20 @@ def test_initialization_with_malformed_env_key(self): ) def test_generate_token_with_invalid_credentials(self): - with patch.object(AppTokenManager, "is_valid_token", return_value=False), \ - patch("tap_github.authenticator.generate_app_access_token", - return_value=("some_token", MagicMock())): + with patch.object(AppTokenManager, "is_valid_token", return_value=False), patch( + "tap_github.authenticator.generate_app_access_token", + return_value=("some_token", MagicMock()), + ): token_manager = AppTokenManager("12345;;key\\ncontent;;67890") assert token_manager.token is None assert token_manager.token_expires_at is None def test_successful_token_generation(self): token_time = MagicMock() - with patch.object(AppTokenManager, "is_valid_token", return_value=True), \ - patch("tap_github.authenticator.generate_app_access_token", - return_value=("valid_token", token_time)): + with patch.object(AppTokenManager, "is_valid_token", return_value=True), patch( + "tap_github.authenticator.generate_app_access_token", + return_value=("valid_token", token_time), + ): token_manager = AppTokenManager("12345;;key\\ncontent;;67890") token_manager.claim_token() assert token_manager.token == "valid_token" @@ -170,17 +171,16 @@ def mock_stream(): stream = MagicMock(spec=RESTStream) stream.logger = MagicMock() stream.tap_name = "tap_github" - stream.config = { - "rate_limit_buffer": 5 - } + stream.config = {"rate_limit_buffer": 5} return stream class TestGitHubTokenAuthenticator: def test_prepare_tokens_returns_empty_if_none_found(self, mock_stream): - with patch("os.environ", {"GITHUB_TLJKJFDS": "gt1"}), \ - patch.object(PersonalTokenManager, "is_valid_token", return_value=True): + with patch("os.environ", {"GITHUB_TLJKJFDS": "gt1"}), patch.object( + PersonalTokenManager, "is_valid_token", return_value=True + ): auth = GitHubTokenAuthenticator(stream=mock_stream) token_managers = auth.prepare_tokens() @@ -188,9 +188,11 @@ def test_prepare_tokens_returns_empty_if_none_found(self, mock_stream): assert len(token_managers) == 0 def test_config_auth_token_only(self, mock_stream): - with patch.object(GitHubTokenAuthenticator, "get_env", - return_value={"OTHER_TOKEN": "blah", "NOT_THE_RIGHT_TOKEN": "meh"}), \ - patch.object(PersonalTokenManager, "is_valid_token", return_value=True): + with patch.object( + GitHubTokenAuthenticator, + "get_env", + return_value={"OTHER_TOKEN": "blah", "NOT_THE_RIGHT_TOKEN": "meh"}, + ), patch.object(PersonalTokenManager, "is_valid_token", return_value=True): stream = mock_stream stream.config.update({"auth_token": "gt5"}) @@ -201,14 +203,14 @@ def test_config_auth_token_only(self, mock_stream): assert token_managers[0].token == "gt5" def test_config_additional_auth_tokens_only(self, mock_stream): - with patch.object(GitHubTokenAuthenticator, "get_env", - return_value={"OTHER_TOKEN": "blah", "NOT_THE_RIGHT_TOKEN": "meh"}), \ - patch.object(PersonalTokenManager, "is_valid_token", return_value=True): + with patch.object( + GitHubTokenAuthenticator, + "get_env", + return_value={"OTHER_TOKEN": "blah", "NOT_THE_RIGHT_TOKEN": "meh"}, + ), patch.object(PersonalTokenManager, "is_valid_token", return_value=True): stream = mock_stream - stream.config.update({ - "additional_auth_tokens": ["gt7", "gt8", "gt9"] - }) + stream.config.update({"additional_auth_tokens": ["gt7", "gt8", "gt9"]}) auth = GitHubTokenAuthenticator(stream=stream) token_managers = auth.prepare_tokens() @@ -216,9 +218,15 @@ def test_config_additional_auth_tokens_only(self, mock_stream): assert sorted({tm.token for tm in token_managers}) == ["gt7", "gt8", "gt9"] def test_env_personal_tokens_only(self, mock_stream): - with patch.object(GitHubTokenAuthenticator, "get_env", - return_value={"GITHUB_TOKEN1": "gt1", "GITHUB_TOKENxyz": "gt2", "OTHER_TOKEN": "blah"}), \ - patch.object(PersonalTokenManager, "is_valid_token", return_value=True): + with patch.object( + GitHubTokenAuthenticator, + "get_env", + return_value={ + "GITHUB_TOKEN1": "gt1", + "GITHUB_TOKENxyz": "gt2", + "OTHER_TOKEN": "blah", + }, + ), patch.object(PersonalTokenManager, "is_valid_token", return_value=True): auth = GitHubTokenAuthenticator(stream=mock_stream) token_managers = auth.prepare_tokens() @@ -227,11 +235,14 @@ def test_env_personal_tokens_only(self, mock_stream): assert sorted({tm.token for tm in token_managers}) == ["gt1", "gt2"] def test_env_app_key_only(self, mock_stream): - with patch.object(GitHubTokenAuthenticator, "get_env", - return_value={"GITHUB_APP_PRIVATE_KEY": "123;;key", "OTHER_TOKEN": "blah"}), \ - patch.object(AppTokenManager, "is_valid_token", return_value=True), \ - patch("tap_github.authenticator.generate_app_access_token", - return_value=("installationtoken12345", MagicMock())): + with patch.object( + GitHubTokenAuthenticator, + "get_env", + return_value={"GITHUB_APP_PRIVATE_KEY": "123;;key", "OTHER_TOKEN": "blah"}, + ), patch.object(AppTokenManager, "is_valid_token", return_value=True), patch( + "tap_github.authenticator.generate_app_access_token", + return_value=("installationtoken12345", MagicMock()), + ): auth = GitHubTokenAuthenticator(stream=mock_stream) token_managers = auth.prepare_tokens() @@ -243,56 +254,91 @@ def test_all_token_types(self, mock_stream): # Expectations: # - the presence of additional_auth_tokens causes personal tokens in the environment to be ignored. # - the other types all coexist - with patch.object(GitHubTokenAuthenticator, "get_env", - return_value={"GITHUB_TOKEN1": "gt1", "GITHUB_TOKENxyz": "gt2", - "GITHUB_APP_PRIVATE_KEY": "123;;key;;install_id", "OTHER_TOKEN": "blah"}), \ - patch.object(TokenManager, "is_valid_token", return_value=True), \ - patch("tap_github.authenticator.generate_app_access_token", - return_value=("installationtoken12345", MagicMock())): + with patch.object( + GitHubTokenAuthenticator, + "get_env", + return_value={ + "GITHUB_TOKEN1": "gt1", + "GITHUB_TOKENxyz": "gt2", + "GITHUB_APP_PRIVATE_KEY": "123;;key;;install_id", + "OTHER_TOKEN": "blah", + }, + ), patch.object(TokenManager, "is_valid_token", return_value=True), patch( + "tap_github.authenticator.generate_app_access_token", + return_value=("installationtoken12345", MagicMock()), + ): stream = mock_stream - stream.config.update({ - "auth_token": "gt5", - "additional_auth_tokens": ["gt7", "gt8", "gt9"] - }) + stream.config.update( + {"auth_token": "gt5", "additional_auth_tokens": ["gt7", "gt8", "gt9"]} + ) auth = GitHubTokenAuthenticator(stream=stream) token_managers = auth.prepare_tokens() assert len(token_managers) == 5 - assert sorted({tm.token for tm in token_managers}) == ["gt5", "gt7", "gt8", "gt9", "installationtoken12345"] + assert sorted({tm.token for tm in token_managers}) == [ + "gt5", + "gt7", + "gt8", + "gt9", + "installationtoken12345", + ] def test_all_token_types_except_additional_auth_tokens(self, mock_stream): # Expectations: # - in the absence of additional_auth_tokens, all the other types can coexist - with patch.object(GitHubTokenAuthenticator, "get_env", - return_value={"GITHUB_TOKEN1": "gt1", "GITHUB_TOKENxyz": "gt2", - "GITHUB_APP_PRIVATE_KEY": "123;;key;;install_id", "OTHER_TOKEN": "blah"}), \ - patch.object(TokenManager, "is_valid_token", return_value=True), \ - patch("tap_github.authenticator.generate_app_access_token", - return_value=("installationtoken12345", MagicMock())): + with patch.object( + GitHubTokenAuthenticator, + "get_env", + return_value={ + "GITHUB_TOKEN1": "gt1", + "GITHUB_TOKENxyz": "gt2", + "GITHUB_APP_PRIVATE_KEY": "123;;key;;install_id", + "OTHER_TOKEN": "blah", + }, + ), patch.object(TokenManager, "is_valid_token", return_value=True), patch( + "tap_github.authenticator.generate_app_access_token", + return_value=("installationtoken12345", MagicMock()), + ): stream = mock_stream - stream.config.update({ - "auth_token": "gt5", - }) + stream.config.update( + { + "auth_token": "gt5", + } + ) auth = GitHubTokenAuthenticator(stream=stream) token_managers = auth.prepare_tokens() assert len(token_managers) == 4 - assert sorted({tm.token for tm in token_managers}) == ["gt1", "gt2", "gt5", "installationtoken12345"] + assert sorted({tm.token for tm in token_managers}) == [ + "gt1", + "gt2", + "gt5", + "installationtoken12345", + ] def test_auth_token_and_additional_auth_tokens_deduped(self, mock_stream): - with patch.object(GitHubTokenAuthenticator, "get_env", - return_value={"GITHUB_TOKEN1": "gt1", "GITHUB_TOKENxyz": "gt2", "OTHER_TOKEN": "blah"}), \ - patch.object(TokenManager, "is_valid_token", return_value=True), \ - patch("tap_github.authenticator.generate_app_access_token", - return_value=("installationtoken12345", MagicMock())): + with patch.object( + GitHubTokenAuthenticator, + "get_env", + return_value={ + "GITHUB_TOKEN1": "gt1", + "GITHUB_TOKENxyz": "gt2", + "OTHER_TOKEN": "blah", + }, + ), patch.object(TokenManager, "is_valid_token", return_value=True), patch( + "tap_github.authenticator.generate_app_access_token", + return_value=("installationtoken12345", MagicMock()), + ): stream = mock_stream - stream.config.update({ - "auth_token": "gt1", - "additional_auth_tokens": ["gt1", "gt1", "gt8", "gt8", "gt9"] - }) + stream.config.update( + { + "auth_token": "gt1", + "additional_auth_tokens": ["gt1", "gt1", "gt8", "gt8", "gt9"], + } + ) auth = GitHubTokenAuthenticator(stream=stream) token_managers = auth.prepare_tokens() @@ -300,16 +346,22 @@ def test_auth_token_and_additional_auth_tokens_deduped(self, mock_stream): assert sorted({tm.token for tm in token_managers}) == ["gt1", "gt8", "gt9"] def test_auth_token_and_env_tokens_deduped(self, mock_stream): - with patch.object(GitHubTokenAuthenticator, "get_env", - return_value={"GITHUB_TOKEN1": "gt1", "GITHUB_TOKENa": "gt2", "GITHUB_TOKENxyz": "gt2", "OTHER_TOKEN": "blah"}), \ - patch.object(TokenManager, "is_valid_token", return_value=True), \ - patch("tap_github.authenticator.generate_app_access_token", - return_value=("installationtoken12345", MagicMock())): + with patch.object( + GitHubTokenAuthenticator, + "get_env", + return_value={ + "GITHUB_TOKEN1": "gt1", + "GITHUB_TOKENa": "gt2", + "GITHUB_TOKENxyz": "gt2", + "OTHER_TOKEN": "blah", + }, + ), patch.object(TokenManager, "is_valid_token", return_value=True), patch( + "tap_github.authenticator.generate_app_access_token", + return_value=("installationtoken12345", MagicMock()), + ): stream = mock_stream - stream.config.update({ - "auth_token": "gt1" - }) + stream.config.update({"auth_token": "gt1"}) auth = GitHubTokenAuthenticator(stream=stream) token_managers = auth.prepare_tokens() @@ -321,23 +373,29 @@ def test_handle_error_if_app_key_invalid(self, mock_stream): # - don"t crash # - print the error as a warning # - continue with any other obtained tokens - with patch.object(GitHubTokenAuthenticator, "get_env", - return_value={"GITHUB_APP_PRIVATE_KEY": "123garbagekey"}), \ - patch("tap_github.authenticator.AppTokenManager") as mock_app_manager: + with patch.object( + GitHubTokenAuthenticator, + "get_env", + return_value={"GITHUB_APP_PRIVATE_KEY": "123garbagekey"}, + ), patch("tap_github.authenticator.AppTokenManager") as mock_app_manager: mock_app_manager.side_effect = ValueError("Invalid key format") auth = GitHubTokenAuthenticator(stream=mock_stream) auth.prepare_tokens() mock_stream.logger.warn.assert_called_with( - "An error was thrown while preparing an app token: Invalid key format") + "An error was thrown while preparing an app token: Invalid key format" + ) def test_exclude_generated_app_token_if_invalid(self, mock_stream): - with patch.object(GitHubTokenAuthenticator, "get_env", - return_value={"GITHUB_APP_PRIVATE_KEY": "123;;key"}), \ - patch.object(AppTokenManager, "is_valid_token", return_value=False), \ - patch("tap_github.authenticator.generate_app_access_token", - return_value=("installationtoken12345", MagicMock())): + with patch.object( + GitHubTokenAuthenticator, + "get_env", + return_value={"GITHUB_APP_PRIVATE_KEY": "123;;key"}, + ), patch.object(AppTokenManager, "is_valid_token", return_value=False), patch( + "tap_github.authenticator.generate_app_access_token", + return_value=("installationtoken12345", MagicMock()), + ): auth = GitHubTokenAuthenticator(stream=mock_stream) token_managers = auth.prepare_tokens() @@ -345,20 +403,24 @@ def test_exclude_generated_app_token_if_invalid(self, mock_stream): assert len(token_managers) == 0 def test_prepare_tokens_returns_empty_if_all_tokens_invalid(self, mock_stream): - with patch.object(GitHubTokenAuthenticator, "get_env", - return_value={"GITHUB_TOKEN1": "gt1", "GITHUB_APP_PRIVATE_KEY": "123;;key"}), \ - patch.object(PersonalTokenManager, "is_valid_token", return_value=False), \ - patch.object(AppTokenManager, "is_valid_token", return_value=False), \ - patch("tap_github.authenticator.generate_app_access_token", - return_value=("installationtoken12345", MagicMock())): + with patch.object( + GitHubTokenAuthenticator, + "get_env", + return_value={"GITHUB_TOKEN1": "gt1", "GITHUB_APP_PRIVATE_KEY": "123;;key"}, + ), patch.object( + PersonalTokenManager, "is_valid_token", return_value=False + ), patch.object( + AppTokenManager, "is_valid_token", return_value=False + ), patch( + "tap_github.authenticator.generate_app_access_token", + return_value=("installationtoken12345", MagicMock()), + ): stream = mock_stream - stream.config.update({ - "auth_token": "gt5", - "additional_auth_tokens": ["gt7", "gt8", "gt9"] - }) + stream.config.update( + {"auth_token": "gt5", "additional_auth_tokens": ["gt7", "gt8", "gt9"]} + ) auth = GitHubTokenAuthenticator(stream=stream) token_managers = auth.prepare_tokens() assert len(token_managers) == 0 -