22import time
33import json
44import pytest
5+ from oauthlib .oauth2 import OAuth2Token
56from requests_oauthlib import OAuth2Session
67from requests import HTTPError , JSONDecodeError , Response
78
@@ -110,7 +111,7 @@ def test_token_expiration(self, requests_mock, test_instance,
110111 .headers ['Authorization' ] == 'Bearer super-secret-token'
111112
112113 # The token obtained above expires in 1s, so wait out expiration:
113- time .sleep (1.1 )
114+ time .sleep (2 )
114115
115116 # Register new token response:
116117 second_token_response = dict (_TOKEN_RESPONSE )
@@ -138,7 +139,7 @@ def test_error_status_raises_error(self, requests_mock, test_instance,
138139 test_instance ._do_http_method ('GET' , 'foo' )
139140
140141 def test_token_refresh_failure_raises_error (
141- self , requests_mock , test_instance , token_server_post ):
142+ self , requests_mock , test_instance , token_server_post , mocker ):
142143 """
143144 Failure to fetch a token can raise a number of errors including:
144145 - requests.exceptions.HTTPError for invalid access_token
@@ -150,12 +151,25 @@ def test_token_refresh_failure_raises_error(
150151 a new valid token in response to token expiration. This test asserts
151152 that the client will not allow more than successive 3 retries.
152153 """
153- requests_mock .get (f'{ BASE_URL } /foo' , json = {'foo' : 'bar' })
154+ test_instance ._create_oauth_client ()
155+
156+ def set_token (* args , scope ):
157+ test_instance .oauth_client .token = OAuth2Token (
158+ json .loads (args [0 ]))
159+ test_instance .oauth_client ._client .populate_token_attributes (
160+ json .loads (args [0 ]))
154161
162+ requests_mock .get (f'{ BASE_URL } /foo' , json = {'foo' : 'bar' })
155163 token_response = dict (_TOKEN_RESPONSE )
156- token_response ['expires_in' ] = 0
157- token_server_post = requests_mock \
158- .post (TOKEN_URL , text = json .dumps (token_response ))
164+ token_response ["expires_in" ] = 0
165+ token_response ["expires_at" ] = 1000000000
166+ token_server_post = requests_mock .post (
167+ TOKEN_URL , text = json .dumps (token_response ))
168+
169+ test_instance .oauth_client ._client .parse_request_body_response = (
170+ mocker .MagicMock (name = "method" , side_effect = set_token )
171+ )
172+ test_instance ._generate_access_token ()
159173
160174 with pytest .raises (Oauth2ApiClientError ):
161175 test_instance ._do_http_method ('GET' , 'foo' )
0 commit comments