From c9d6d8db647a9cd8ea2659c48b71ca4be6aac47b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 30 Jul 2024 21:05:37 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tap_github/authenticator.py | 24 +++++++++++++---- tap_github/tests/test_authenticator.py | 36 ++++++++++++++------------ 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/tap_github/authenticator.py b/tap_github/authenticator.py index a1d7be84..7e7bbfda 100644 --- a/tap_github/authenticator.py +++ b/tap_github/authenticator.py @@ -22,7 +22,12 @@ class TokenManager: # - not consume all available calls when we rare using an org or user token. DEFAULT_RATE_LIMIT_BUFFER = 1000 - def __init__(self, token: str, rate_limit_buffer: Optional[int] = None, logger: Optional[Any] = None): + def __init__( + self, + token: str, + rate_limit_buffer: Optional[int] = None, + logger: Optional[Any] = None, + ): """Init TokenManager info.""" self.token = token self.logger = logger @@ -143,7 +148,9 @@ def prepare_tokens(self) -> List[TokenManager]: if "auth_token" in self._config: personal_tokens.add(self._config["auth_token"]) if "additional_auth_tokens" in self._config: - personal_tokens = personal_tokens.union(self._config["additional_auth_tokens"]) + personal_tokens = personal_tokens.union( + self._config["additional_auth_tokens"] + ) else: # Accept multiple tokens using environment variables GITHUB_TOKEN* env_tokens = { @@ -159,7 +166,9 @@ def prepare_tokens(self) -> List[TokenManager]: token_managers: List[TokenManager] = [] for token in personal_tokens: - token_manager = TokenManager(token, rate_limit_buffer=rate_limit_buffer, logger=self.logger) + token_manager = TokenManager( + token, rate_limit_buffer=rate_limit_buffer, logger=self.logger + ) if token_manager.is_valid_token(): token_managers.append(token_manager) @@ -182,7 +191,9 @@ def prepare_tokens(self) -> List[TokenManager]: app_token = generate_app_access_token( github_app_id, github_private_key, github_installation_id or None ) - token_manager = TokenManager(app_token, rate_limit_buffer=rate_limit_buffer, logger=self.logger) + token_manager = TokenManager( + app_token, rate_limit_buffer=rate_limit_buffer, logger=self.logger + ) if token_manager.is_valid_token(): token_managers.append(token_manager) @@ -212,7 +223,10 @@ def get_next_auth_token(self) -> None: current_token = self.active_token.token if self.active_token else "" shuffle(token_managers) for token_manager in token_managers: - if token_manager.has_calls_remaining() and current_token != token_manager.token: + if ( + token_manager.has_calls_remaining() + and current_token != token_manager.token + ): self.active_token = token_manager self.logger.info(f"Switching to fresh auth token") return diff --git a/tap_github/tests/test_authenticator.py b/tap_github/tests/test_authenticator.py index ee52ee34..c63853ca 100644 --- a/tap_github/tests/test_authenticator.py +++ b/tap_github/tests/test_authenticator.py @@ -1,11 +1,13 @@ from datetime import datetime, timedelta +from unittest.mock import MagicMock, patch + import pytest import requests -from unittest.mock import patch, MagicMock + from tap_github.authenticator import TokenManager -class TestTokenManager(): +class TestTokenManager: def test_default_rate_limits(self): token_manager = TokenManager("mytoken", rate_limit_buffer=700) @@ -24,7 +26,7 @@ def test_update_rate_limit(self): "X-RateLimit-Limit": "5000", "X-RateLimit-Remaining": "4999", "X-RateLimit-Reset": "1372700873", - "X-RateLimit-Used": "1" + "X-RateLimit-Used": "1", } token_manager = TokenManager("mytoken") @@ -36,7 +38,7 @@ def test_update_rate_limit(self): assert token_manager.rate_limit_used == 1 def test_is_valid_token_successful(self): - with patch('requests.get') as mock_get: + with patch("requests.get") as mock_get: mock_response = mock_get.return_value mock_response.raise_for_status.return_value = None @@ -45,17 +47,17 @@ def test_is_valid_token_successful(self): assert token_manager.is_valid_token() mock_get.assert_called_once_with( url="https://api.github.com/rate_limit", - headers={"Authorization": "token validtoken"} + headers={"Authorization": "token validtoken"}, ) def test_is_valid_token_failure(self): - with patch('requests.get') as mock_get: + with patch("requests.get") as mock_get: # Setup for a failed request mock_response = mock_get.return_value mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError() mock_response.status_code = 401 - mock_response.content = b'Unauthorized Access' - mock_response.reason = 'Unauthorized' + mock_response.content = b"Unauthorized Access" + mock_response.reason = "Unauthorized" token_manager = TokenManager("invalidtoken") token_manager.logger = MagicMock() @@ -73,7 +75,7 @@ def test_has_calls_remaining_succeeds_if_lots_remaining(self): "X-RateLimit-Limit": "5000", "X-RateLimit-Remaining": "4999", "X-RateLimit-Reset": "1372700873", - "X-RateLimit-Used": "1" + "X-RateLimit-Used": "1", } token_manager = TokenManager("mytoken") @@ -86,7 +88,7 @@ def test_has_calls_remaining_succeeds_if_reset_time_reached(self): "X-RateLimit-Limit": "5000", "X-RateLimit-Remaining": "1", "X-RateLimit-Reset": "1372700873", - "X-RateLimit-Used": "4999" + "X-RateLimit-Used": "4999", } token_manager = TokenManager("mytoken", rate_limit_buffer=1000) @@ -94,19 +96,19 @@ def test_has_calls_remaining_succeeds_if_reset_time_reached(self): assert token_manager.has_calls_remaining() - def test_has_calls_remaining_fails_if_few_calls_remaining_and_reset_time_not_reached(self): + def test_has_calls_remaining_fails_if_few_calls_remaining_and_reset_time_not_reached( + self, + ): mock_response_headers = { "X-RateLimit-Limit": "5000", "X-RateLimit-Remaining": "1", - "X-RateLimit-Reset": str(int((datetime.now() + timedelta(days=100)).timestamp())), - "X-RateLimit-Used": "4999" + "X-RateLimit-Reset": str( + int((datetime.now() + timedelta(days=100)).timestamp()) + ), + "X-RateLimit-Used": "4999", } token_manager = TokenManager("mytoken", rate_limit_buffer=1000) token_manager.update_rate_limit(mock_response_headers) assert not token_manager.has_calls_remaining() - - - -