2424
2525import copy
2626import datetime
27- import httplib2
2827import json
2928import os
3029import pickle
3130import stat
3231import tempfile
3332import unittest
3433
35- from oauth2client import GOOGLE_TOKEN_URI
34+ from . http_mock import HttpMockSequence
3635from oauth2client import file
3736from oauth2client import locked_file
3837from oauth2client import multistore_file
3938from oauth2client import util
4039from oauth2client .client import AccessTokenCredentials
41- from oauth2client .client import AssertionCredentials
4240from oauth2client .client import OAuth2Credentials
4341try :
4442 # Python2
@@ -64,11 +62,12 @@ def setUp(self):
6462 except OSError :
6563 pass
6664
67- def create_test_credentials (self , client_id = 'some_client_id' ):
65+ def create_test_credentials (self , client_id = 'some_client_id' ,
66+ expiration = None ):
6867 access_token = 'foo'
6968 client_secret = 'cOuDdkfjxxnv+'
7069 refresh_token = '1/0/a.df219fjls0'
71- token_expiry = datetime .datetime .utcnow ()
70+ token_expiry = expiration or datetime .datetime .utcnow ()
7271 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
7372 user_agent = 'refresh_checker/1.0'
7473
@@ -119,8 +118,55 @@ def test_pickle_and_json_interop(self):
119118 self .assertEquals (data ['_class' ], 'OAuth2Credentials' )
120119 self .assertEquals (data ['_module' ], OAuth2Credentials .__module__ )
121120
122- def test_token_refresh (self ):
123- credentials = self .create_test_credentials ()
121+ def test_token_refresh_store_expired (self ):
122+ expiration = datetime .datetime .utcnow () - datetime .timedelta (minutes = 15 )
123+ credentials = self .create_test_credentials (expiration = expiration )
124+
125+ s = file .Storage (FILENAME )
126+ s .put (credentials )
127+ credentials = s .get ()
128+ new_cred = copy .copy (credentials )
129+ new_cred .access_token = 'bar'
130+ s .put (new_cred )
131+
132+ access_token = '1/3w'
133+ token_response = {'access_token' : access_token , 'expires_in' : 3600 }
134+ http = HttpMockSequence ([
135+ ({'status' : '200' }, json .dumps (token_response ).encode ('utf-8' )),
136+ ])
137+
138+ credentials ._refresh (http .request )
139+ self .assertEquals (credentials .access_token , access_token )
140+
141+ def test_token_refresh_store_expires_soon (self ):
142+ # Tests the case where an access token that is valid when it is read from
143+ # the store expires before the original request succeeds.
144+ expiration = datetime .datetime .utcnow () + datetime .timedelta (minutes = 15 )
145+ credentials = self .create_test_credentials (expiration = expiration )
146+
147+ s = file .Storage (FILENAME )
148+ s .put (credentials )
149+ credentials = s .get ()
150+ new_cred = copy .copy (credentials )
151+ new_cred .access_token = 'bar'
152+ s .put (new_cred )
153+
154+ access_token = '1/3w'
155+ token_response = {'access_token' : access_token , 'expires_in' : 3600 }
156+ http = HttpMockSequence ([
157+ ({'status' : '401' }, b'Initial token expired' ),
158+ ({'status' : '401' }, b'Store token expired' ),
159+ ({'status' : '200' }, json .dumps (token_response ).encode ('utf-8' )),
160+ ({'status' : '200' }, b'Valid response to original request' )
161+ ])
162+
163+ credentials .authorize (http )
164+ http .request ('https://example.com' )
165+ self .assertEquals (credentials .access_token , access_token )
166+
167+ def test_token_refresh_good_store (self ):
168+ expiration = datetime .datetime .utcnow () + datetime .timedelta (minutes = 15 )
169+ credentials = self .create_test_credentials (expiration = expiration )
124170
125171 s = file .Storage (FILENAME )
126172 s .put (credentials )
@@ -287,7 +333,6 @@ def test_multistore_file_backwards_compatibility(self):
287333
288334 self .assertEqual (credentials .access_token , stored_credentials .access_token )
289335
290-
291336 def test_multistore_file_get_all_keys (self ):
292337 # start with no keys
293338 keys = multistore_file .get_all_credential_keys (FILENAME )
0 commit comments