|
25 | 25 | import os
|
26 | 26 | import socket
|
27 | 27 | import sys
|
| 28 | +import tempfile |
28 | 29 |
|
| 30 | +import httplib2 |
29 | 31 | import mock
|
30 | 32 | import six
|
31 | 33 | from six.moves import http_client
|
@@ -296,7 +298,6 @@ class GoogleCredentialsTests(unittest2.TestCase):
|
296 | 298 |
|
297 | 299 | def setUp(self):
|
298 | 300 | self.os_name = os.name
|
299 |
| - from oauth2client import client |
300 | 301 | client.SETTINGS.env_name = None
|
301 | 302 |
|
302 | 303 | def tearDown(self):
|
@@ -607,7 +608,6 @@ def test_get_adc_from_environment_variable_service_account(self, *stubs):
|
607 | 608 | in_gae.assert_called_once_with()
|
608 | 609 |
|
609 | 610 | def test_env_name(self):
|
610 |
| - from oauth2client import client |
611 | 611 | self.assertEqual(None, client.SETTINGS.env_name)
|
612 | 612 | self.test_get_adc_from_environment_variable_service_account()
|
613 | 613 | self.assertEqual(DEFAULT_ENV_NAME, client.SETTINGS.env_name)
|
@@ -990,6 +990,35 @@ def test_no_unicode_in_request_params(self):
|
990 | 990 | instance = OAuth2Credentials.from_json(self.credentials.to_json())
|
991 | 991 | self.assertEqual('foobar', instance.token_response)
|
992 | 992 |
|
| 993 | + def test__expires_in_no_expiry(self): |
| 994 | + credentials = OAuth2Credentials(None, None, None, None, |
| 995 | + None, None, None) |
| 996 | + self.assertIsNone(credentials.token_expiry) |
| 997 | + self.assertIsNone(credentials._expires_in()) |
| 998 | + |
| 999 | + @mock.patch('oauth2client.client._UTCNOW') |
| 1000 | + def test__expires_in_expired(self, utcnow): |
| 1001 | + credentials = OAuth2Credentials(None, None, None, None, |
| 1002 | + None, None, None) |
| 1003 | + credentials.token_expiry = datetime.datetime.utcnow() |
| 1004 | + now = credentials.token_expiry + datetime.timedelta(seconds=1) |
| 1005 | + self.assertLess(credentials.token_expiry, now) |
| 1006 | + utcnow.return_value = now |
| 1007 | + self.assertEqual(credentials._expires_in(), 0) |
| 1008 | + utcnow.assert_called_once_with() |
| 1009 | + |
| 1010 | + @mock.patch('oauth2client.client._UTCNOW') |
| 1011 | + def test__expires_in_not_expired(self, utcnow): |
| 1012 | + credentials = OAuth2Credentials(None, None, None, None, |
| 1013 | + None, None, None) |
| 1014 | + credentials.token_expiry = datetime.datetime.utcnow() |
| 1015 | + seconds = 1234 |
| 1016 | + now = credentials.token_expiry - datetime.timedelta(seconds=seconds) |
| 1017 | + self.assertLess(now, credentials.token_expiry) |
| 1018 | + utcnow.return_value = now |
| 1019 | + self.assertEqual(credentials._expires_in(), seconds) |
| 1020 | + utcnow.assert_called_once_with() |
| 1021 | + |
993 | 1022 | @mock.patch('oauth2client.client._UTCNOW')
|
994 | 1023 | def test_get_access_token(self, utcnow):
|
995 | 1024 | # Configure the patch.
|
@@ -1074,6 +1103,121 @@ def test_get_access_token(self, utcnow):
|
1074 | 1103 | expected_utcnow_calls = [mock.call()] * (2 + 3 + 5)
|
1075 | 1104 | self.assertEqual(expected_utcnow_calls, utcnow.mock_calls)
|
1076 | 1105 |
|
| 1106 | + @mock.patch.object(OAuth2Credentials, 'refresh') |
| 1107 | + @mock.patch.object(OAuth2Credentials, '_expires_in', |
| 1108 | + return_value=1835) |
| 1109 | + def test_get_access_token_without_http(self, expires_in, refresh_mock): |
| 1110 | + credentials = OAuth2Credentials(None, None, None, None, |
| 1111 | + None, None, None) |
| 1112 | + # Make sure access_token_expired returns True |
| 1113 | + credentials.invalid = True |
| 1114 | + # Specify a token so we can use it in the response. |
| 1115 | + credentials.access_token = 'ya29-s3kr3t' |
| 1116 | + |
| 1117 | + with mock.patch('httplib2.Http', |
| 1118 | + return_value=object) as http_kls: |
| 1119 | + token_info = credentials.get_access_token() |
| 1120 | + expires_in.assert_called_once_with() |
| 1121 | + refresh_mock.assert_called_once_with(http_kls.return_value) |
| 1122 | + |
| 1123 | + self.assertIsInstance(token_info, client.AccessTokenInfo) |
| 1124 | + self.assertEqual(token_info.access_token, |
| 1125 | + credentials.access_token) |
| 1126 | + self.assertEqual(token_info.expires_in, |
| 1127 | + expires_in.return_value) |
| 1128 | + |
| 1129 | + @mock.patch.object(OAuth2Credentials, 'refresh') |
| 1130 | + @mock.patch.object(OAuth2Credentials, '_expires_in', |
| 1131 | + return_value=1835) |
| 1132 | + def test_get_access_token_with_http(self, expires_in, refresh_mock): |
| 1133 | + credentials = OAuth2Credentials(None, None, None, None, |
| 1134 | + None, None, None) |
| 1135 | + # Make sure access_token_expired returns True |
| 1136 | + credentials.invalid = True |
| 1137 | + # Specify a token so we can use it in the response. |
| 1138 | + credentials.access_token = 'ya29-s3kr3t' |
| 1139 | + |
| 1140 | + http_obj = object() |
| 1141 | + token_info = credentials.get_access_token(http_obj) |
| 1142 | + self.assertIsInstance(token_info, client.AccessTokenInfo) |
| 1143 | + self.assertEqual(token_info.access_token, |
| 1144 | + credentials.access_token) |
| 1145 | + self.assertEqual(token_info.expires_in, |
| 1146 | + expires_in.return_value) |
| 1147 | + |
| 1148 | + expires_in.assert_called_once_with() |
| 1149 | + refresh_mock.assert_called_once_with(http_obj) |
| 1150 | + |
| 1151 | + @mock.patch('oauth2client.client.logger') |
| 1152 | + def _do_refresh_request_test_helper(self, response, content, |
| 1153 | + error_msg, logger, store=None): |
| 1154 | + credentials = OAuth2Credentials(None, None, None, None, |
| 1155 | + None, None, None) |
| 1156 | + credentials.store = store |
| 1157 | + http_request = mock.Mock() |
| 1158 | + http_request.return_value = response, content |
| 1159 | + |
| 1160 | + # HttpAccessTokenRefreshError(error_msg, status=resp.status) |
| 1161 | + with self.assertRaises(HttpAccessTokenRefreshError) as exc_manager: |
| 1162 | + credentials._do_refresh_request(http_request) |
| 1163 | + |
| 1164 | + self.assertEqual(exc_manager.exception.args, (error_msg,)) |
| 1165 | + self.assertEqual(exc_manager.exception.status, response.status) |
| 1166 | + |
| 1167 | + call1 = mock.call('Refreshing access_token') |
| 1168 | + failure_template = 'Failed to retrieve access token: %s' |
| 1169 | + call2 = mock.call(failure_template, content) |
| 1170 | + self.assertEqual(logger.info.mock_calls, [call1, call2]) |
| 1171 | + if store is not None: |
| 1172 | + store.locked_put.assert_called_once_with(credentials) |
| 1173 | + |
| 1174 | + def test__do_refresh_request_non_json_failure(self): |
| 1175 | + response = httplib2.Response({ |
| 1176 | + 'status': http_client.BAD_REQUEST, |
| 1177 | + }) |
| 1178 | + content = u'Bad request' |
| 1179 | + error_msg = 'Invalid response %s.' % (response.status,) |
| 1180 | + self._do_refresh_request_test_helper(response, content, error_msg) |
| 1181 | + |
| 1182 | + def test__do_refresh_request_basic_failure(self): |
| 1183 | + response = httplib2.Response({ |
| 1184 | + 'status': http_client.INTERNAL_SERVER_ERROR, |
| 1185 | + }) |
| 1186 | + content = u'{}' |
| 1187 | + error_msg = 'Invalid response %s.' % (response.status,) |
| 1188 | + self._do_refresh_request_test_helper(response, content, error_msg) |
| 1189 | + |
| 1190 | + def test__do_refresh_request_failure_w_json_error(self): |
| 1191 | + response = httplib2.Response({ |
| 1192 | + 'status': http_client.BAD_GATEWAY, |
| 1193 | + }) |
| 1194 | + error_msg = 'Hi I am an error not a bearer' |
| 1195 | + content = json.dumps({'error': error_msg}) |
| 1196 | + self._do_refresh_request_test_helper(response, content, error_msg) |
| 1197 | + |
| 1198 | + def test__do_refresh_request_failure_w_json_error_and_store(self): |
| 1199 | + response = httplib2.Response({ |
| 1200 | + 'status': http_client.BAD_GATEWAY, |
| 1201 | + }) |
| 1202 | + error_msg = 'Where are we going wearer?' |
| 1203 | + content = json.dumps({'error': error_msg}) |
| 1204 | + store = mock.MagicMock() |
| 1205 | + self._do_refresh_request_test_helper(response, content, error_msg, |
| 1206 | + store=store) |
| 1207 | + |
| 1208 | + def test__do_refresh_request_failure_w_json_error_and_desc(self): |
| 1209 | + response = httplib2.Response({ |
| 1210 | + 'status': http_client.SERVICE_UNAVAILABLE, |
| 1211 | + }) |
| 1212 | + base_error = 'Ruckus' |
| 1213 | + error_desc = 'Can you describe the ruckus' |
| 1214 | + content = json.dumps({ |
| 1215 | + 'error': base_error, |
| 1216 | + 'error_description': error_desc, |
| 1217 | + }) |
| 1218 | + error_msg = '%s: %s' % (base_error, error_desc) |
| 1219 | + self._do_refresh_request_test_helper(response, content, error_msg) |
| 1220 | + |
1077 | 1221 | def test_has_scopes(self):
|
1078 | 1222 | self.assertTrue(self.credentials.has_scopes('foo'))
|
1079 | 1223 | self.assertTrue(self.credentials.has_scopes(['foo']))
|
@@ -1625,13 +1769,11 @@ def _save_helper(self, filename):
|
1625 | 1769 | self.assertEqual(stat_mode, 0o600)
|
1626 | 1770 |
|
1627 | 1771 | def test_new(self):
|
1628 |
| - import tempfile |
1629 | 1772 | filename = tempfile.mktemp()
|
1630 | 1773 | self.assertFalse(os.path.exists(filename))
|
1631 | 1774 | self._save_helper(filename)
|
1632 | 1775 |
|
1633 | 1776 | def test_existing(self):
|
1634 |
| - import tempfile |
1635 | 1777 | filename = tempfile.mktemp()
|
1636 | 1778 | with open(filename, 'w') as f:
|
1637 | 1779 | f.write('a bunch of nonsense longer than []')
|
|
0 commit comments