From d86ade0db20c40c4e5d41a690e474371adaafb5c Mon Sep 17 00:00:00 2001 From: David Kirov Date: Fri, 10 Jan 2025 17:55:51 +0100 Subject: [PATCH] Add tests for `expires_in` values --- .../datadog_checks/base/utils/http.py | 2 +- .../tests/base/utils/http/test_authtoken.py | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/datadog_checks_base/datadog_checks/base/utils/http.py b/datadog_checks_base/datadog_checks/base/utils/http.py index c1674694f6486..40e1cd41fb17d 100644 --- a/datadog_checks_base/datadog_checks/base/utils/http.py +++ b/datadog_checks_base/datadog_checks/base/utils/http.py @@ -862,7 +862,7 @@ def read(self, **request): token_expiration = response.get('expires_in', 0) self._expiration += token_expiration except TypeError: - self.logger.warning('OAuth2 included an `expires_in` value of unexpected type %s.', type(token_expiration)) + LOGGER.warning('OAuth2 included an `expires_in` value of unexpected type %s.', type(token_expiration)) return self._token diff --git a/datadog_checks_base/tests/base/utils/http/test_authtoken.py b/datadog_checks_base/tests/base/utils/http/test_authtoken.py index f2d27fa033255..90130f48fa301 100644 --- a/datadog_checks_base/tests/base/utils/http/test_authtoken.py +++ b/datadog_checks_base/tests/base/utils/http/test_authtoken.py @@ -468,8 +468,16 @@ def fetch_token(self, *args, **kwargs): ): with pytest.raises(Exception, match='OAuth2 client credentials grant error: unauthorized_client'): http.get('https://www.google.com') - - def test_success(self): + + @pytest.mark.parametrize( + 'token_response', + [ + pytest.param({'access_token': 'foo', 'expires_in': 9000}, id='With expires_in'), + pytest.param({'access_token': 'foo'}, id='Without expires_in'), + pytest.param({'access_token': 'foo', 'expires_in': 'two minutes'}, id='With string expires_in'), + ] + ) + def test_success(self, token_response): instance = { 'auth_token': { 'reader': {'type': 'oauth', 'url': 'foo', 'client_id': 'bar', 'client_secret': 'baz'}, @@ -487,7 +495,7 @@ def __init__(self, *args, **kwargs): pass def fetch_token(self, *args, **kwargs): - return {'access_token': 'foo', 'expires_in': 9000} + return token_response with mock.patch('requests.get') as get, mock.patch('oauthlib.oauth2.BackendApplicationClient'), mock.patch( 'requests_oauthlib.OAuth2Session', side_effect=MockOAuth2Session