Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 4, 2024
1 parent 7bbc6ed commit ec6dbe0
Showing 1 changed file with 149 additions and 87 deletions.
236 changes: 149 additions & 87 deletions tap_github/tests/test_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import pytest
import requests

from singer_sdk.streams import RESTStream

from tap_github.authenticator import (
Expand Down Expand Up @@ -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"
Expand All @@ -170,27 +171,28 @@ 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()

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"})
Expand All @@ -201,24 +203,30 @@ 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()

assert len(token_managers) == 3
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()
Expand All @@ -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()
Expand All @@ -243,73 +254,114 @@ 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()

assert len(token_managers) == 3
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()

Expand All @@ -321,44 +373,54 @@ 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()

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

0 comments on commit ec6dbe0

Please sign in to comment.