|
32 | 32 | import tempfile |
33 | 33 | import unittest |
34 | 34 |
|
| 35 | +from http_mock import HttpMockSequence |
35 | 36 | from oauth2client import GOOGLE_TOKEN_URI |
36 | 37 | from oauth2client import file |
37 | 38 | from oauth2client import locked_file |
@@ -64,11 +65,12 @@ def setUp(self): |
64 | 65 | except OSError: |
65 | 66 | pass |
66 | 67 |
|
67 | | - def create_test_credentials(self, client_id='some_client_id'): |
| 68 | + def create_test_credentials(self, client_id='some_client_id', |
| 69 | + expiration=None): |
68 | 70 | access_token = 'foo' |
69 | 71 | client_secret = 'cOuDdkfjxxnv+' |
70 | 72 | refresh_token = '1/0/a.df219fjls0' |
71 | | - token_expiry = datetime.datetime.utcnow() |
| 73 | + token_expiry = datetime.datetime.utcnow() if not expiration else expiration |
72 | 74 | token_uri = 'https://www.google.com/accounts/o8/oauth2/token' |
73 | 75 | user_agent = 'refresh_checker/1.0' |
74 | 76 |
|
@@ -119,8 +121,29 @@ def test_pickle_and_json_interop(self): |
119 | 121 | self.assertEquals(data['_class'], 'OAuth2Credentials') |
120 | 122 | self.assertEquals(data['_module'], OAuth2Credentials.__module__) |
121 | 123 |
|
122 | | - def test_token_refresh(self): |
123 | | - credentials = self.create_test_credentials() |
| 124 | + def test_token_refresh_store_expired(self): |
| 125 | + expiration = datetime.datetime.utcnow() - datetime.timedelta(minutes=15) |
| 126 | + credentials = self.create_test_credentials(expiration=expiration) |
| 127 | + |
| 128 | + s = file.Storage(FILENAME) |
| 129 | + s.put(credentials) |
| 130 | + credentials = s.get() |
| 131 | + new_cred = copy.copy(credentials) |
| 132 | + new_cred.access_token = 'bar' |
| 133 | + s.put(new_cred) |
| 134 | + |
| 135 | + access_token = '1/3w' |
| 136 | + token_response = {'access_token': access_token, 'expires_in': 3600} |
| 137 | + http = HttpMockSequence([ |
| 138 | + ({'status': '200'}, json.dumps(token_response)), |
| 139 | + ]) |
| 140 | + |
| 141 | + credentials._refresh(http.request) |
| 142 | + self.assertEquals(credentials.access_token, access_token) |
| 143 | + |
| 144 | + def test_token_refresh_good_store(self): |
| 145 | + expiration = datetime.datetime.utcnow() + datetime.timedelta(minutes=15) |
| 146 | + credentials = self.create_test_credentials(expiration=expiration) |
124 | 147 |
|
125 | 148 | s = file.Storage(FILENAME) |
126 | 149 | s.put(credentials) |
|
0 commit comments