Skip to content

Commit

Permalink
Add tests for expires_in values
Browse files Browse the repository at this point in the history
  • Loading branch information
dkirov-dd committed Jan 10, 2025
1 parent a01e3a2 commit d86ade0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion datadog_checks_base/datadog_checks/base/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 11 additions & 3 deletions datadog_checks_base/tests/base/utils/http/test_authtoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand All @@ -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
Expand Down

0 comments on commit d86ade0

Please sign in to comment.