Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | |
Joe Gregorio | 0bc7091 | 2011-05-24 15:30:49 -0400 | [diff] [blame] | 18 | """Oauth2client tests |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 19 | |
Joe Gregorio | 0bc7091 | 2011-05-24 15:30:49 -0400 | [diff] [blame] | 20 | Unit tests for oauth2client. |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 21 | """ |
| 22 | |
| 23 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 24 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 25 | import base64 |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 26 | import datetime |
Joe Gregorio | e1de416 | 2011-02-23 11:30:29 -0500 | [diff] [blame] | 27 | import httplib2 |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 28 | import os |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 29 | import unittest |
| 30 | import urlparse |
Joe Gregorio | e1de416 | 2011-02-23 11:30:29 -0500 | [diff] [blame] | 31 | |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 32 | from apiclient.http import HttpMock |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 33 | from apiclient.http import HttpMockSequence |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 34 | from oauth2client import GOOGLE_REVOKE_URI |
| 35 | from oauth2client import GOOGLE_TOKEN_URI |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 36 | from oauth2client.anyjson import simplejson |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 37 | from oauth2client.client import AccessTokenCredentials |
| 38 | from oauth2client.client import AccessTokenCredentialsError |
| 39 | from oauth2client.client import AccessTokenRefreshError |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 40 | from oauth2client.client import AssertionCredentials |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 41 | from oauth2client.client import Credentials |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 42 | from oauth2client.client import FlowExchangeError |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 43 | from oauth2client.client import MemoryCache |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 44 | from oauth2client.client import NonAsciiHeaderError |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 45 | from oauth2client.client import OAuth2Credentials |
| 46 | from oauth2client.client import OAuth2WebServerFlow |
Joe Gregorio | f2326c0 | 2012-02-09 12:18:44 -0500 | [diff] [blame] | 47 | from oauth2client.client import OOB_CALLBACK_URN |
Joe Gregorio | 0bd8c41 | 2013-01-03 17:17:46 -0500 | [diff] [blame] | 48 | from oauth2client.client import REFRESH_STATUS_CODES |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 49 | from oauth2client.client import Storage |
| 50 | from oauth2client.client import TokenRevokeError |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 51 | from oauth2client.client import VerifyJwtTokenError |
| 52 | from oauth2client.client import _extract_id_token |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 53 | from oauth2client.client import _update_query_params |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 54 | from oauth2client.client import credentials_from_clientsecrets_and_code |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 55 | from oauth2client.client import credentials_from_code |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 56 | from oauth2client.client import flow_from_clientsecrets |
Joe Gregorio | 0bd8c41 | 2013-01-03 17:17:46 -0500 | [diff] [blame] | 57 | from oauth2client.clientsecrets import _loadfile |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 58 | from test_discovery import assertUrisEqual |
| 59 | |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 60 | |
| 61 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 62 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 63 | |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 64 | def datafile(filename): |
| 65 | return os.path.join(DATA_DIR, filename) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 66 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 67 | |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 68 | def load_and_cache(existing_file, fakename, cache_mock): |
| 69 | client_type, client_info = _loadfile(datafile(existing_file)) |
| 70 | cache_mock.cache[fakename] = {client_type: client_info} |
| 71 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 72 | |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 73 | class CacheMock(object): |
| 74 | def __init__(self): |
| 75 | self.cache = {} |
| 76 | |
| 77 | def get(self, key, namespace=''): |
| 78 | # ignoring namespace for easier testing |
| 79 | return self.cache.get(key, None) |
| 80 | |
| 81 | def set(self, key, value, namespace=''): |
| 82 | # ignoring namespace for easier testing |
| 83 | self.cache[key] = value |
| 84 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 85 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 86 | class CredentialsTests(unittest.TestCase): |
| 87 | |
| 88 | def test_to_from_json(self): |
| 89 | credentials = Credentials() |
| 90 | json = credentials.to_json() |
| 91 | restored = Credentials.new_from_json(json) |
| 92 | |
| 93 | |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 94 | class DummyDeleteStorage(Storage): |
| 95 | delete_called = False |
| 96 | |
| 97 | def locked_delete(self): |
| 98 | self.delete_called = True |
| 99 | |
| 100 | |
| 101 | def _token_revoke_test_helper(testcase, status, revoke_raise, |
| 102 | valid_bool_value, token_attr): |
| 103 | current_store = getattr(testcase.credentials, 'store', None) |
| 104 | |
| 105 | dummy_store = DummyDeleteStorage() |
| 106 | testcase.credentials.set_store(dummy_store) |
| 107 | |
| 108 | actual_do_revoke = testcase.credentials._do_revoke |
| 109 | testcase.token_from_revoke = None |
| 110 | def do_revoke_stub(http_request, token): |
| 111 | testcase.token_from_revoke = token |
| 112 | return actual_do_revoke(http_request, token) |
| 113 | testcase.credentials._do_revoke = do_revoke_stub |
| 114 | |
| 115 | http = HttpMock(headers={'status': status}) |
| 116 | if revoke_raise: |
| 117 | testcase.assertRaises(TokenRevokeError, testcase.credentials.revoke, http) |
| 118 | else: |
| 119 | testcase.credentials.revoke(http) |
| 120 | |
| 121 | testcase.assertEqual(getattr(testcase.credentials, token_attr), |
| 122 | testcase.token_from_revoke) |
| 123 | testcase.assertEqual(valid_bool_value, testcase.credentials.invalid) |
| 124 | testcase.assertEqual(valid_bool_value, dummy_store.delete_called) |
| 125 | |
| 126 | testcase.credentials.set_store(current_store) |
| 127 | |
| 128 | |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 129 | class BasicCredentialsTests(unittest.TestCase): |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 130 | |
| 131 | def setUp(self): |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 132 | access_token = 'foo' |
| 133 | client_id = 'some_client_id' |
| 134 | client_secret = 'cOuDdkfjxxnv+' |
| 135 | refresh_token = '1/0/a.df219fjls0' |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 136 | token_expiry = datetime.datetime.utcnow() |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 137 | user_agent = 'refresh_checker/1.0' |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 138 | self.credentials = OAuth2Credentials( |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 139 | access_token, client_id, client_secret, |
| 140 | refresh_token, token_expiry, GOOGLE_TOKEN_URI, |
| 141 | user_agent, revoke_uri=GOOGLE_REVOKE_URI) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 142 | |
| 143 | def test_token_refresh_success(self): |
Joe Gregorio | 0bd8c41 | 2013-01-03 17:17:46 -0500 | [diff] [blame] | 144 | for status_code in REFRESH_STATUS_CODES: |
Joe Gregorio | 7c7c6b1 | 2012-07-16 16:31:01 -0400 | [diff] [blame] | 145 | http = HttpMockSequence([ |
| 146 | ({'status': status_code}, ''), |
| 147 | ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), |
| 148 | ({'status': '200'}, 'echo_request_headers'), |
| 149 | ]) |
| 150 | http = self.credentials.authorize(http) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 151 | resp, content = http.request('http://example.com') |
Joe Gregorio | 7c7c6b1 | 2012-07-16 16:31:01 -0400 | [diff] [blame] | 152 | self.assertEqual('Bearer 1/3w', content['Authorization']) |
| 153 | self.assertFalse(self.credentials.access_token_expired) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 154 | |
| 155 | def test_token_refresh_failure(self): |
Joe Gregorio | 0bd8c41 | 2013-01-03 17:17:46 -0500 | [diff] [blame] | 156 | for status_code in REFRESH_STATUS_CODES: |
Joe Gregorio | 7c7c6b1 | 2012-07-16 16:31:01 -0400 | [diff] [blame] | 157 | http = HttpMockSequence([ |
| 158 | ({'status': status_code}, ''), |
| 159 | ({'status': '400'}, '{"error":"access_denied"}'), |
| 160 | ]) |
| 161 | http = self.credentials.authorize(http) |
| 162 | try: |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 163 | http.request('http://example.com') |
| 164 | self.fail('should raise AccessTokenRefreshError exception') |
Joe Gregorio | 7c7c6b1 | 2012-07-16 16:31:01 -0400 | [diff] [blame] | 165 | except AccessTokenRefreshError: |
| 166 | pass |
| 167 | self.assertTrue(self.credentials.access_token_expired) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 168 | |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 169 | def test_token_revoke_success(self): |
| 170 | _token_revoke_test_helper( |
| 171 | self, '200', revoke_raise=False, |
| 172 | valid_bool_value=True, token_attr='refresh_token') |
| 173 | |
| 174 | def test_token_revoke_failure(self): |
| 175 | _token_revoke_test_helper( |
| 176 | self, '400', revoke_raise=True, |
| 177 | valid_bool_value=False, token_attr='refresh_token') |
| 178 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 179 | def test_non_401_error_response(self): |
| 180 | http = HttpMockSequence([ |
| 181 | ({'status': '400'}, ''), |
| 182 | ]) |
| 183 | http = self.credentials.authorize(http) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 184 | resp, content = http.request('http://example.com') |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 185 | self.assertEqual(400, resp.status) |
| 186 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 187 | def test_to_from_json(self): |
| 188 | json = self.credentials.to_json() |
| 189 | instance = OAuth2Credentials.from_json(json) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 190 | self.assertEqual(OAuth2Credentials, type(instance)) |
Joe Gregorio | 1daa71b | 2011-09-15 18:12:14 -0400 | [diff] [blame] | 191 | instance.token_expiry = None |
| 192 | self.credentials.token_expiry = None |
| 193 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 194 | self.assertEqual(instance.__dict__, self.credentials.__dict__) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 195 | |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 196 | def test_no_unicode_in_request_params(self): |
| 197 | access_token = u'foo' |
| 198 | client_id = u'some_client_id' |
| 199 | client_secret = u'cOuDdkfjxxnv+' |
| 200 | refresh_token = u'1/0/a.df219fjls0' |
| 201 | token_expiry = unicode(datetime.datetime.utcnow()) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 202 | token_uri = unicode(GOOGLE_TOKEN_URI) |
| 203 | revoke_uri = unicode(GOOGLE_REVOKE_URI) |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 204 | user_agent = u'refresh_checker/1.0' |
| 205 | credentials = OAuth2Credentials(access_token, client_id, client_secret, |
| 206 | refresh_token, token_expiry, token_uri, |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 207 | user_agent, revoke_uri=revoke_uri) |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 208 | |
| 209 | http = HttpMock(headers={'status': '200'}) |
| 210 | http = credentials.authorize(http) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 211 | http.request(u'http://example.com', method=u'GET', headers={u'foo': u'bar'}) |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 212 | for k, v in http.headers.iteritems(): |
| 213 | self.assertEqual(str, type(k)) |
| 214 | self.assertEqual(str, type(v)) |
| 215 | |
| 216 | # Test again with unicode strings that can't simple be converted to ASCII. |
| 217 | try: |
| 218 | http.request( |
| 219 | u'http://example.com', method=u'GET', headers={u'foo': u'\N{COMET}'}) |
| 220 | self.fail('Expected exception to be raised.') |
| 221 | except NonAsciiHeaderError: |
| 222 | pass |
| 223 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 224 | |
| 225 | class AccessTokenCredentialsTests(unittest.TestCase): |
| 226 | |
| 227 | def setUp(self): |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 228 | access_token = 'foo' |
| 229 | user_agent = 'refresh_checker/1.0' |
| 230 | self.credentials = AccessTokenCredentials(access_token, user_agent, |
| 231 | revoke_uri=GOOGLE_REVOKE_URI) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 232 | |
| 233 | def test_token_refresh_success(self): |
Joe Gregorio | 0bd8c41 | 2013-01-03 17:17:46 -0500 | [diff] [blame] | 234 | for status_code in REFRESH_STATUS_CODES: |
Joe Gregorio | 7c7c6b1 | 2012-07-16 16:31:01 -0400 | [diff] [blame] | 235 | http = HttpMockSequence([ |
| 236 | ({'status': status_code}, ''), |
| 237 | ]) |
| 238 | http = self.credentials.authorize(http) |
| 239 | try: |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 240 | resp, content = http.request('http://example.com') |
| 241 | self.fail('should throw exception if token expires') |
Joe Gregorio | 7c7c6b1 | 2012-07-16 16:31:01 -0400 | [diff] [blame] | 242 | except AccessTokenCredentialsError: |
| 243 | pass |
| 244 | except Exception: |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 245 | self.fail('should only throw AccessTokenCredentialsError') |
| 246 | |
| 247 | def test_token_revoke_success(self): |
| 248 | _token_revoke_test_helper( |
| 249 | self, '200', revoke_raise=False, |
| 250 | valid_bool_value=True, token_attr='access_token') |
| 251 | |
| 252 | def test_token_revoke_failure(self): |
| 253 | _token_revoke_test_helper( |
| 254 | self, '400', revoke_raise=True, |
| 255 | valid_bool_value=False, token_attr='access_token') |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 256 | |
| 257 | def test_non_401_error_response(self): |
| 258 | http = HttpMockSequence([ |
| 259 | ({'status': '400'}, ''), |
| 260 | ]) |
| 261 | http = self.credentials.authorize(http) |
Joe Gregorio | 83cd439 | 2011-06-20 10:11:35 -0400 | [diff] [blame] | 262 | resp, content = http.request('http://example.com') |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 263 | self.assertEqual(400, resp.status) |
| 264 | |
Joe Gregorio | 83cd439 | 2011-06-20 10:11:35 -0400 | [diff] [blame] | 265 | def test_auth_header_sent(self): |
| 266 | http = HttpMockSequence([ |
| 267 | ({'status': '200'}, 'echo_request_headers'), |
| 268 | ]) |
| 269 | http = self.credentials.authorize(http) |
| 270 | resp, content = http.request('http://example.com') |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 271 | self.assertEqual('Bearer foo', content['Authorization']) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 272 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 273 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 274 | class TestAssertionCredentials(unittest.TestCase): |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 275 | assertion_text = 'This is the assertion' |
| 276 | assertion_type = 'http://www.google.com/assertionType' |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 277 | |
| 278 | class AssertionCredentialsTestImpl(AssertionCredentials): |
| 279 | |
| 280 | def _generate_assertion(self): |
| 281 | return TestAssertionCredentials.assertion_text |
| 282 | |
| 283 | def setUp(self): |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 284 | user_agent = 'fun/2.0' |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 285 | self.credentials = self.AssertionCredentialsTestImpl(self.assertion_type, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 286 | user_agent=user_agent) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 287 | |
| 288 | def test_assertion_body(self): |
| 289 | body = urlparse.parse_qs(self.credentials._generate_refresh_request_body()) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 290 | self.assertEqual(self.assertion_text, body['assertion'][0]) |
| 291 | self.assertEqual(self.assertion_type, body['assertion_type'][0]) |
Joe Gregorio | cdc350f | 2013-02-07 10:52:26 -0500 | [diff] [blame^] | 292 | self.assertEqual('urn:ietf:params:oauth:grant-type:jwt-bearer', |
| 293 | body['grant_type'][0]) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 294 | |
| 295 | def test_assertion_refresh(self): |
| 296 | http = HttpMockSequence([ |
| 297 | ({'status': '200'}, '{"access_token":"1/3w"}'), |
| 298 | ({'status': '200'}, 'echo_request_headers'), |
| 299 | ]) |
| 300 | http = self.credentials.authorize(http) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 301 | resp, content = http.request('http://example.com') |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 302 | self.assertEqual('Bearer 1/3w', content['Authorization']) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 303 | |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 304 | def test_token_revoke_success(self): |
| 305 | _token_revoke_test_helper( |
| 306 | self, '200', revoke_raise=False, |
| 307 | valid_bool_value=True, token_attr='access_token') |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 308 | |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 309 | def test_token_revoke_failure(self): |
| 310 | _token_revoke_test_helper( |
| 311 | self, '400', revoke_raise=True, |
| 312 | valid_bool_value=False, token_attr='access_token') |
| 313 | |
| 314 | |
| 315 | class UpdateQueryParamsTest(unittest.TestCase): |
| 316 | def test_update_query_params_no_params(self): |
| 317 | uri = 'http://www.google.com' |
| 318 | updated = _update_query_params(uri, {'a': 'b'}) |
| 319 | self.assertEqual(updated, uri + '?a=b') |
| 320 | |
| 321 | def test_update_query_params_existing_params(self): |
| 322 | uri = 'http://www.google.com?x=y' |
| 323 | updated = _update_query_params(uri, {'a': 'b', 'c': 'd&'}) |
| 324 | hardcoded_update = uri + '&a=b&c=d%26' |
| 325 | assertUrisEqual(self, updated, hardcoded_update) |
| 326 | |
| 327 | |
| 328 | class ExtractIdTokenTest(unittest.TestCase): |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 329 | """Tests _extract_id_token().""" |
| 330 | |
| 331 | def test_extract_success(self): |
| 332 | body = {'foo': 'bar'} |
| 333 | payload = base64.urlsafe_b64encode(simplejson.dumps(body)).strip('=') |
| 334 | jwt = 'stuff.' + payload + '.signature' |
| 335 | |
| 336 | extracted = _extract_id_token(jwt) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 337 | self.assertEqual(extracted, body) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 338 | |
| 339 | def test_extract_failure(self): |
| 340 | body = {'foo': 'bar'} |
| 341 | payload = base64.urlsafe_b64encode(simplejson.dumps(body)).strip('=') |
| 342 | jwt = 'stuff.' + payload |
| 343 | |
| 344 | self.assertRaises(VerifyJwtTokenError, _extract_id_token, jwt) |
| 345 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 346 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 347 | class OAuth2WebServerFlowTest(unittest.TestCase): |
| 348 | |
| 349 | def setUp(self): |
| 350 | self.flow = OAuth2WebServerFlow( |
| 351 | client_id='client_id+1', |
| 352 | client_secret='secret+1', |
| 353 | scope='foo', |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 354 | redirect_uri=OOB_CALLBACK_URN, |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 355 | user_agent='unittest-sample/1.0', |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 356 | revoke_uri='dummy_revoke_uri', |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 357 | ) |
| 358 | |
| 359 | def test_construct_authorize_url(self): |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 360 | authorize_url = self.flow.step1_get_authorize_url() |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 361 | |
| 362 | parsed = urlparse.urlparse(authorize_url) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 363 | q = urlparse.parse_qs(parsed[4]) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 364 | self.assertEqual('client_id+1', q['client_id'][0]) |
| 365 | self.assertEqual('code', q['response_type'][0]) |
| 366 | self.assertEqual('foo', q['scope'][0]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 367 | self.assertEqual(OOB_CALLBACK_URN, q['redirect_uri'][0]) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 368 | self.assertEqual('offline', q['access_type'][0]) |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 369 | |
Joe Gregorio | 32f7319 | 2012-10-23 16:13:44 -0400 | [diff] [blame] | 370 | def test_override_flow_via_kwargs(self): |
| 371 | """Passing kwargs to override defaults.""" |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 372 | flow = OAuth2WebServerFlow( |
| 373 | client_id='client_id+1', |
| 374 | client_secret='secret+1', |
| 375 | scope='foo', |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 376 | redirect_uri=OOB_CALLBACK_URN, |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 377 | user_agent='unittest-sample/1.0', |
Joe Gregorio | 32f7319 | 2012-10-23 16:13:44 -0400 | [diff] [blame] | 378 | access_type='online', |
| 379 | response_type='token' |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 380 | ) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 381 | authorize_url = flow.step1_get_authorize_url() |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 382 | |
| 383 | parsed = urlparse.urlparse(authorize_url) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 384 | q = urlparse.parse_qs(parsed[4]) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 385 | self.assertEqual('client_id+1', q['client_id'][0]) |
Joe Gregorio | 32f7319 | 2012-10-23 16:13:44 -0400 | [diff] [blame] | 386 | self.assertEqual('token', q['response_type'][0]) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 387 | self.assertEqual('foo', q['scope'][0]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 388 | self.assertEqual(OOB_CALLBACK_URN, q['redirect_uri'][0]) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 389 | self.assertEqual('online', q['access_type'][0]) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 390 | |
| 391 | def test_exchange_failure(self): |
| 392 | http = HttpMockSequence([ |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 393 | ({'status': '400'}, '{"error":"invalid_request"}'), |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 394 | ]) |
| 395 | |
| 396 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 397 | credentials = self.flow.step2_exchange('some random code', http=http) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 398 | self.fail('should raise exception if exchange doesn\'t get 200') |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 399 | except FlowExchangeError: |
| 400 | pass |
| 401 | |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 402 | def test_urlencoded_exchange_failure(self): |
| 403 | http = HttpMockSequence([ |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 404 | ({'status': '400'}, 'error=invalid_request'), |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 405 | ]) |
| 406 | |
| 407 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 408 | credentials = self.flow.step2_exchange('some random code', http=http) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 409 | self.fail('should raise exception if exchange doesn\'t get 200') |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 410 | except FlowExchangeError, e: |
| 411 | self.assertEquals('invalid_request', str(e)) |
| 412 | |
| 413 | def test_exchange_failure_with_json_error(self): |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 414 | # Some providers have 'error' attribute as a JSON object |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 415 | # in place of regular string. |
| 416 | # This test makes sure no strange object-to-string coversion |
| 417 | # exceptions are being raised instead of FlowExchangeError. |
| 418 | http = HttpMockSequence([ |
| 419 | ({'status': '400'}, |
| 420 | """ {"error": { |
| 421 | "type": "OAuthException", |
| 422 | "message": "Error validating verification code."} }"""), |
| 423 | ]) |
| 424 | |
| 425 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 426 | credentials = self.flow.step2_exchange('some random code', http=http) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 427 | self.fail('should raise exception if exchange doesn\'t get 200') |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 428 | except FlowExchangeError, e: |
| 429 | pass |
| 430 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 431 | def test_exchange_success(self): |
| 432 | http = HttpMockSequence([ |
| 433 | ({'status': '200'}, |
| 434 | """{ "access_token":"SlAV32hkKG", |
| 435 | "expires_in":3600, |
| 436 | "refresh_token":"8xLOxBtZp8" }"""), |
| 437 | ]) |
| 438 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 439 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 440 | self.assertEqual('SlAV32hkKG', credentials.access_token) |
| 441 | self.assertNotEqual(None, credentials.token_expiry) |
| 442 | self.assertEqual('8xLOxBtZp8', credentials.refresh_token) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 443 | self.assertEqual('dummy_revoke_uri', credentials.revoke_uri) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 444 | |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 445 | def test_urlencoded_exchange_success(self): |
| 446 | http = HttpMockSequence([ |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 447 | ({'status': '200'}, 'access_token=SlAV32hkKG&expires_in=3600'), |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 448 | ]) |
| 449 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 450 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 451 | self.assertEqual('SlAV32hkKG', credentials.access_token) |
| 452 | self.assertNotEqual(None, credentials.token_expiry) |
| 453 | |
| 454 | def test_urlencoded_expires_param(self): |
| 455 | http = HttpMockSequence([ |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 456 | # Note the 'expires=3600' where you'd normally |
| 457 | # have if named 'expires_in' |
| 458 | ({'status': '200'}, 'access_token=SlAV32hkKG&expires=3600'), |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 459 | ]) |
| 460 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 461 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 462 | self.assertNotEqual(None, credentials.token_expiry) |
| 463 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 464 | def test_exchange_no_expires_in(self): |
| 465 | http = HttpMockSequence([ |
| 466 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 467 | "refresh_token":"8xLOxBtZp8" }"""), |
| 468 | ]) |
| 469 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 470 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 471 | self.assertEqual(None, credentials.token_expiry) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 472 | |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 473 | def test_urlencoded_exchange_no_expires_in(self): |
| 474 | http = HttpMockSequence([ |
| 475 | # This might be redundant but just to make sure |
| 476 | # urlencoded access_token gets parsed correctly |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 477 | ({'status': '200'}, 'access_token=SlAV32hkKG'), |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 478 | ]) |
| 479 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 480 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 481 | self.assertEqual(None, credentials.token_expiry) |
| 482 | |
Joe Gregorio | 4b4002f | 2012-06-14 15:41:01 -0400 | [diff] [blame] | 483 | def test_exchange_fails_if_no_code(self): |
| 484 | http = HttpMockSequence([ |
| 485 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 486 | "refresh_token":"8xLOxBtZp8" }"""), |
| 487 | ]) |
| 488 | |
| 489 | code = {'error': 'thou shall not pass'} |
| 490 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 491 | credentials = self.flow.step2_exchange(code, http=http) |
Joe Gregorio | 4b4002f | 2012-06-14 15:41:01 -0400 | [diff] [blame] | 492 | self.fail('should raise exception if no code in dictionary.') |
| 493 | except FlowExchangeError, e: |
| 494 | self.assertTrue('shall not pass' in str(e)) |
| 495 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 496 | def test_exchange_id_token_fail(self): |
| 497 | http = HttpMockSequence([ |
| 498 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 499 | "refresh_token":"8xLOxBtZp8", |
| 500 | "id_token": "stuff.payload"}"""), |
| 501 | ]) |
| 502 | |
| 503 | self.assertRaises(VerifyJwtTokenError, self.flow.step2_exchange, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 504 | 'some random code', http=http) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 505 | |
| 506 | def test_exchange_id_token_fail(self): |
| 507 | body = {'foo': 'bar'} |
| 508 | payload = base64.urlsafe_b64encode(simplejson.dumps(body)).strip('=') |
Joe Gregorio | bd512b5 | 2011-12-06 15:39:26 -0500 | [diff] [blame] | 509 | jwt = (base64.urlsafe_b64encode('stuff')+ '.' + payload + '.' + |
| 510 | base64.urlsafe_b64encode('signature')) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 511 | |
| 512 | http = HttpMockSequence([ |
| 513 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 514 | "refresh_token":"8xLOxBtZp8", |
| 515 | "id_token": "%s"}""" % jwt), |
| 516 | ]) |
| 517 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 518 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 519 | self.assertEqual(credentials.id_token, body) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 520 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 521 | |
| 522 | class FlowFromCachedClientsecrets(unittest.TestCase): |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 523 | |
| 524 | def test_flow_from_clientsecrets_cached(self): |
| 525 | cache_mock = CacheMock() |
| 526 | load_and_cache('client_secrets.json', 'some_secrets', cache_mock) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 527 | |
| 528 | flow = flow_from_clientsecrets( |
| 529 | 'some_secrets', '', redirect_uri='oob', cache=cache_mock) |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 530 | self.assertEquals('foo_client_secret', flow.client_secret) |
| 531 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 532 | |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 533 | class CredentialsFromCodeTests(unittest.TestCase): |
| 534 | def setUp(self): |
| 535 | self.client_id = 'client_id_abc' |
| 536 | self.client_secret = 'secret_use_code' |
| 537 | self.scope = 'foo' |
| 538 | self.code = '12345abcde' |
| 539 | self.redirect_uri = 'postmessage' |
| 540 | |
| 541 | def test_exchange_code_for_token(self): |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 542 | token = 'asdfghjkl' |
| 543 | payload =simplejson.dumps({'access_token': token, 'expires_in': 3600}) |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 544 | http = HttpMockSequence([ |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 545 | ({'status': '200'}, payload), |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 546 | ]) |
| 547 | credentials = credentials_from_code(self.client_id, self.client_secret, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 548 | self.scope, self.code, redirect_uri=self.redirect_uri, |
| 549 | http=http) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 550 | self.assertEquals(credentials.access_token, token) |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 551 | self.assertNotEqual(None, credentials.token_expiry) |
| 552 | |
| 553 | def test_exchange_code_for_token_fail(self): |
| 554 | http = HttpMockSequence([ |
| 555 | ({'status': '400'}, '{"error":"invalid_request"}'), |
| 556 | ]) |
| 557 | |
| 558 | try: |
| 559 | credentials = credentials_from_code(self.client_id, self.client_secret, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 560 | self.scope, self.code, redirect_uri=self.redirect_uri, |
| 561 | http=http) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 562 | self.fail('should raise exception if exchange doesn\'t get 200') |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 563 | except FlowExchangeError: |
| 564 | pass |
| 565 | |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 566 | def test_exchange_code_and_file_for_token(self): |
| 567 | http = HttpMockSequence([ |
| 568 | ({'status': '200'}, |
| 569 | """{ "access_token":"asdfghjkl", |
| 570 | "expires_in":3600 }"""), |
| 571 | ]) |
| 572 | credentials = credentials_from_clientsecrets_and_code( |
| 573 | datafile('client_secrets.json'), self.scope, |
| 574 | self.code, http=http) |
| 575 | self.assertEquals(credentials.access_token, 'asdfghjkl') |
| 576 | self.assertNotEqual(None, credentials.token_expiry) |
| 577 | |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 578 | def test_exchange_code_and_cached_file_for_token(self): |
| 579 | http = HttpMockSequence([ |
| 580 | ({'status': '200'}, '{ "access_token":"asdfghjkl"}'), |
| 581 | ]) |
| 582 | cache_mock = CacheMock() |
| 583 | load_and_cache('client_secrets.json', 'some_secrets', cache_mock) |
| 584 | |
| 585 | credentials = credentials_from_clientsecrets_and_code( |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 586 | 'some_secrets', self.scope, |
| 587 | self.code, http=http, cache=cache_mock) |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 588 | self.assertEquals(credentials.access_token, 'asdfghjkl') |
| 589 | |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 590 | def test_exchange_code_and_file_for_token_fail(self): |
| 591 | http = HttpMockSequence([ |
| 592 | ({'status': '400'}, '{"error":"invalid_request"}'), |
| 593 | ]) |
| 594 | |
| 595 | try: |
| 596 | credentials = credentials_from_clientsecrets_and_code( |
| 597 | datafile('client_secrets.json'), self.scope, |
| 598 | self.code, http=http) |
dhermes@google.com | a9eb0bb | 2013-02-06 09:19:01 -0800 | [diff] [blame] | 599 | self.fail('should raise exception if exchange doesn\'t get 200') |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 600 | except FlowExchangeError: |
| 601 | pass |
| 602 | |
| 603 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 604 | class MemoryCacheTests(unittest.TestCase): |
| 605 | |
| 606 | def test_get_set_delete(self): |
| 607 | m = MemoryCache() |
| 608 | self.assertEqual(None, m.get('foo')) |
| 609 | self.assertEqual(None, m.delete('foo')) |
| 610 | m.set('foo', 'bar') |
| 611 | self.assertEqual('bar', m.get('foo')) |
| 612 | m.delete('foo') |
| 613 | self.assertEqual(None, m.get('foo')) |
| 614 | |
| 615 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 616 | if __name__ == '__main__': |
| 617 | unittest.main() |