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 | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 32 | try: |
| 33 | from urlparse import parse_qs |
| 34 | except ImportError: |
| 35 | from cgi import parse_qs |
| 36 | |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 37 | from apiclient.http import HttpMock |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 38 | from apiclient.http import HttpMockSequence |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 39 | from oauth2client.anyjson import simplejson |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 40 | from oauth2client.client import AccessTokenCredentials |
| 41 | from oauth2client.client import AccessTokenCredentialsError |
| 42 | from oauth2client.client import AccessTokenRefreshError |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 43 | from oauth2client.client import AssertionCredentials |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 44 | from oauth2client.client import Credentials |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 45 | from oauth2client.client import FlowExchangeError |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 46 | from oauth2client.client import MemoryCache |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 47 | from oauth2client.client import NonAsciiHeaderError |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 48 | from oauth2client.client import OAuth2Credentials |
| 49 | from oauth2client.client import OAuth2WebServerFlow |
Joe Gregorio | f2326c0 | 2012-02-09 12:18:44 -0500 | [diff] [blame] | 50 | from oauth2client.client import OOB_CALLBACK_URN |
Joe Gregorio | 0bd8c41 | 2013-01-03 17:17:46 -0500 | [diff] [blame^] | 51 | from oauth2client.client import REFRESH_STATUS_CODES |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 52 | from oauth2client.client import VerifyJwtTokenError |
| 53 | from oauth2client.client import _extract_id_token |
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 |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 58 | |
| 59 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 60 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 61 | |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 62 | def datafile(filename): |
| 63 | return os.path.join(DATA_DIR, filename) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 64 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 65 | |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 66 | def load_and_cache(existing_file, fakename, cache_mock): |
| 67 | client_type, client_info = _loadfile(datafile(existing_file)) |
| 68 | cache_mock.cache[fakename] = {client_type: client_info} |
| 69 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 70 | |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 71 | class CacheMock(object): |
| 72 | def __init__(self): |
| 73 | self.cache = {} |
| 74 | |
| 75 | def get(self, key, namespace=''): |
| 76 | # ignoring namespace for easier testing |
| 77 | return self.cache.get(key, None) |
| 78 | |
| 79 | def set(self, key, value, namespace=''): |
| 80 | # ignoring namespace for easier testing |
| 81 | self.cache[key] = value |
| 82 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 83 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 84 | class CredentialsTests(unittest.TestCase): |
| 85 | |
| 86 | def test_to_from_json(self): |
| 87 | credentials = Credentials() |
| 88 | json = credentials.to_json() |
| 89 | restored = Credentials.new_from_json(json) |
| 90 | |
| 91 | |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 92 | class BasicCredentialsTests(unittest.TestCase): |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 93 | |
| 94 | def setUp(self): |
| 95 | access_token = "foo" |
| 96 | client_id = "some_client_id" |
| 97 | client_secret = "cOuDdkfjxxnv+" |
| 98 | refresh_token = "1/0/a.df219fjls0" |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 99 | token_expiry = datetime.datetime.utcnow() |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 100 | token_uri = "https://www.google.com/accounts/o8/oauth2/token" |
| 101 | user_agent = "refresh_checker/1.0" |
| 102 | self.credentials = OAuth2Credentials( |
| 103 | access_token, client_id, client_secret, |
| 104 | refresh_token, token_expiry, token_uri, |
| 105 | user_agent) |
| 106 | |
| 107 | def test_token_refresh_success(self): |
Joe Gregorio | 0bd8c41 | 2013-01-03 17:17:46 -0500 | [diff] [blame^] | 108 | for status_code in REFRESH_STATUS_CODES: |
Joe Gregorio | 7c7c6b1 | 2012-07-16 16:31:01 -0400 | [diff] [blame] | 109 | http = HttpMockSequence([ |
| 110 | ({'status': status_code}, ''), |
| 111 | ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), |
| 112 | ({'status': '200'}, 'echo_request_headers'), |
| 113 | ]) |
| 114 | http = self.credentials.authorize(http) |
| 115 | resp, content = http.request("http://example.com") |
| 116 | self.assertEqual('Bearer 1/3w', content['Authorization']) |
| 117 | self.assertFalse(self.credentials.access_token_expired) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 118 | |
| 119 | def test_token_refresh_failure(self): |
Joe Gregorio | 0bd8c41 | 2013-01-03 17:17:46 -0500 | [diff] [blame^] | 120 | for status_code in REFRESH_STATUS_CODES: |
Joe Gregorio | 7c7c6b1 | 2012-07-16 16:31:01 -0400 | [diff] [blame] | 121 | http = HttpMockSequence([ |
| 122 | ({'status': status_code}, ''), |
| 123 | ({'status': '400'}, '{"error":"access_denied"}'), |
| 124 | ]) |
| 125 | http = self.credentials.authorize(http) |
| 126 | try: |
| 127 | http.request("http://example.com") |
| 128 | self.fail("should raise AccessTokenRefreshError exception") |
| 129 | except AccessTokenRefreshError: |
| 130 | pass |
| 131 | self.assertTrue(self.credentials.access_token_expired) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 132 | |
| 133 | def test_non_401_error_response(self): |
| 134 | http = HttpMockSequence([ |
| 135 | ({'status': '400'}, ''), |
| 136 | ]) |
| 137 | http = self.credentials.authorize(http) |
| 138 | resp, content = http.request("http://example.com") |
| 139 | self.assertEqual(400, resp.status) |
| 140 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 141 | def test_to_from_json(self): |
| 142 | json = self.credentials.to_json() |
| 143 | instance = OAuth2Credentials.from_json(json) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 144 | self.assertEqual(OAuth2Credentials, type(instance)) |
Joe Gregorio | 1daa71b | 2011-09-15 18:12:14 -0400 | [diff] [blame] | 145 | instance.token_expiry = None |
| 146 | self.credentials.token_expiry = None |
| 147 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 148 | self.assertEqual(instance.__dict__, self.credentials.__dict__) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 149 | |
Joe Gregorio | 83f2ee6 | 2012-12-06 15:25:54 -0500 | [diff] [blame] | 150 | def test_no_unicode_in_request_params(self): |
| 151 | access_token = u'foo' |
| 152 | client_id = u'some_client_id' |
| 153 | client_secret = u'cOuDdkfjxxnv+' |
| 154 | refresh_token = u'1/0/a.df219fjls0' |
| 155 | token_expiry = unicode(datetime.datetime.utcnow()) |
| 156 | token_uri = u'https://www.google.com/accounts/o8/oauth2/token' |
| 157 | user_agent = u'refresh_checker/1.0' |
| 158 | credentials = OAuth2Credentials(access_token, client_id, client_secret, |
| 159 | refresh_token, token_expiry, token_uri, |
| 160 | user_agent) |
| 161 | |
| 162 | http = HttpMock(headers={'status': '200'}) |
| 163 | http = credentials.authorize(http) |
| 164 | http.request(u'http://example.com', method=u'GET', headers={ |
| 165 | u'foo': u'bar' |
| 166 | }) |
| 167 | for k, v in http.headers.iteritems(): |
| 168 | self.assertEqual(str, type(k)) |
| 169 | self.assertEqual(str, type(v)) |
| 170 | |
| 171 | # Test again with unicode strings that can't simple be converted to ASCII. |
| 172 | try: |
| 173 | http.request( |
| 174 | u'http://example.com', method=u'GET', headers={u'foo': u'\N{COMET}'}) |
| 175 | self.fail('Expected exception to be raised.') |
| 176 | except NonAsciiHeaderError: |
| 177 | pass |
| 178 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 179 | |
| 180 | class AccessTokenCredentialsTests(unittest.TestCase): |
| 181 | |
| 182 | def setUp(self): |
| 183 | access_token = "foo" |
| 184 | user_agent = "refresh_checker/1.0" |
| 185 | self.credentials = AccessTokenCredentials(access_token, user_agent) |
| 186 | |
| 187 | def test_token_refresh_success(self): |
Joe Gregorio | 0bd8c41 | 2013-01-03 17:17:46 -0500 | [diff] [blame^] | 188 | for status_code in REFRESH_STATUS_CODES: |
Joe Gregorio | 7c7c6b1 | 2012-07-16 16:31:01 -0400 | [diff] [blame] | 189 | http = HttpMockSequence([ |
| 190 | ({'status': status_code}, ''), |
| 191 | ]) |
| 192 | http = self.credentials.authorize(http) |
| 193 | try: |
| 194 | resp, content = http.request("http://example.com") |
| 195 | self.fail("should throw exception if token expires") |
| 196 | except AccessTokenCredentialsError: |
| 197 | pass |
| 198 | except Exception: |
| 199 | self.fail("should only throw AccessTokenCredentialsError") |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 200 | |
| 201 | def test_non_401_error_response(self): |
| 202 | http = HttpMockSequence([ |
| 203 | ({'status': '400'}, ''), |
| 204 | ]) |
| 205 | http = self.credentials.authorize(http) |
Joe Gregorio | 83cd439 | 2011-06-20 10:11:35 -0400 | [diff] [blame] | 206 | resp, content = http.request('http://example.com') |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 207 | self.assertEqual(400, resp.status) |
| 208 | |
Joe Gregorio | 83cd439 | 2011-06-20 10:11:35 -0400 | [diff] [blame] | 209 | def test_auth_header_sent(self): |
| 210 | http = HttpMockSequence([ |
| 211 | ({'status': '200'}, 'echo_request_headers'), |
| 212 | ]) |
| 213 | http = self.credentials.authorize(http) |
| 214 | resp, content = http.request('http://example.com') |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 215 | self.assertEqual('Bearer foo', content['Authorization']) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 216 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 217 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 218 | class TestAssertionCredentials(unittest.TestCase): |
| 219 | assertion_text = "This is the assertion" |
| 220 | assertion_type = "http://www.google.com/assertionType" |
| 221 | |
| 222 | class AssertionCredentialsTestImpl(AssertionCredentials): |
| 223 | |
| 224 | def _generate_assertion(self): |
| 225 | return TestAssertionCredentials.assertion_text |
| 226 | |
| 227 | def setUp(self): |
| 228 | user_agent = "fun/2.0" |
| 229 | self.credentials = self.AssertionCredentialsTestImpl(self.assertion_type, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 230 | user_agent=user_agent) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 231 | |
| 232 | def test_assertion_body(self): |
| 233 | body = urlparse.parse_qs(self.credentials._generate_refresh_request_body()) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 234 | self.assertEqual(self.assertion_text, body['assertion'][0]) |
| 235 | self.assertEqual(self.assertion_type, body['assertion_type'][0]) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 236 | |
| 237 | def test_assertion_refresh(self): |
| 238 | http = HttpMockSequence([ |
| 239 | ({'status': '200'}, '{"access_token":"1/3w"}'), |
| 240 | ({'status': '200'}, 'echo_request_headers'), |
| 241 | ]) |
| 242 | http = self.credentials.authorize(http) |
| 243 | resp, content = http.request("http://example.com") |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 244 | self.assertEqual('Bearer 1/3w', content['Authorization']) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 245 | |
| 246 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 247 | class ExtractIdTokenText(unittest.TestCase): |
| 248 | """Tests _extract_id_token().""" |
| 249 | |
| 250 | def test_extract_success(self): |
| 251 | body = {'foo': 'bar'} |
| 252 | payload = base64.urlsafe_b64encode(simplejson.dumps(body)).strip('=') |
| 253 | jwt = 'stuff.' + payload + '.signature' |
| 254 | |
| 255 | extracted = _extract_id_token(jwt) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 256 | self.assertEqual(extracted, body) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 257 | |
| 258 | def test_extract_failure(self): |
| 259 | body = {'foo': 'bar'} |
| 260 | payload = base64.urlsafe_b64encode(simplejson.dumps(body)).strip('=') |
| 261 | jwt = 'stuff.' + payload |
| 262 | |
| 263 | self.assertRaises(VerifyJwtTokenError, _extract_id_token, jwt) |
| 264 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 265 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 266 | class OAuth2WebServerFlowTest(unittest.TestCase): |
| 267 | |
| 268 | def setUp(self): |
| 269 | self.flow = OAuth2WebServerFlow( |
| 270 | client_id='client_id+1', |
| 271 | client_secret='secret+1', |
| 272 | scope='foo', |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 273 | redirect_uri=OOB_CALLBACK_URN, |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 274 | user_agent='unittest-sample/1.0', |
| 275 | ) |
| 276 | |
| 277 | def test_construct_authorize_url(self): |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 278 | authorize_url = self.flow.step1_get_authorize_url() |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 279 | |
| 280 | parsed = urlparse.urlparse(authorize_url) |
| 281 | q = parse_qs(parsed[4]) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 282 | self.assertEqual('client_id+1', q['client_id'][0]) |
| 283 | self.assertEqual('code', q['response_type'][0]) |
| 284 | self.assertEqual('foo', q['scope'][0]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 285 | self.assertEqual(OOB_CALLBACK_URN, q['redirect_uri'][0]) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 286 | self.assertEqual('offline', q['access_type'][0]) |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 287 | |
Joe Gregorio | 32f7319 | 2012-10-23 16:13:44 -0400 | [diff] [blame] | 288 | def test_override_flow_via_kwargs(self): |
| 289 | """Passing kwargs to override defaults.""" |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 290 | flow = OAuth2WebServerFlow( |
| 291 | client_id='client_id+1', |
| 292 | client_secret='secret+1', |
| 293 | scope='foo', |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 294 | redirect_uri=OOB_CALLBACK_URN, |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 295 | user_agent='unittest-sample/1.0', |
Joe Gregorio | 32f7319 | 2012-10-23 16:13:44 -0400 | [diff] [blame] | 296 | access_type='online', |
| 297 | response_type='token' |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 298 | ) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 299 | authorize_url = flow.step1_get_authorize_url() |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 300 | |
| 301 | parsed = urlparse.urlparse(authorize_url) |
| 302 | q = parse_qs(parsed[4]) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 303 | self.assertEqual('client_id+1', q['client_id'][0]) |
Joe Gregorio | 32f7319 | 2012-10-23 16:13:44 -0400 | [diff] [blame] | 304 | self.assertEqual('token', q['response_type'][0]) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 305 | self.assertEqual('foo', q['scope'][0]) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 306 | self.assertEqual(OOB_CALLBACK_URN, q['redirect_uri'][0]) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 307 | self.assertEqual('online', q['access_type'][0]) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 308 | |
| 309 | def test_exchange_failure(self): |
| 310 | http = HttpMockSequence([ |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 311 | ({'status': '400'}, '{"error":"invalid_request"}'), |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 312 | ]) |
| 313 | |
| 314 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 315 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 316 | self.fail("should raise exception if exchange doesn't get 200") |
| 317 | except FlowExchangeError: |
| 318 | pass |
| 319 | |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 320 | def test_urlencoded_exchange_failure(self): |
| 321 | http = HttpMockSequence([ |
| 322 | ({'status': '400'}, "error=invalid_request"), |
| 323 | ]) |
| 324 | |
| 325 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 326 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 327 | self.fail("should raise exception if exchange doesn't get 200") |
| 328 | except FlowExchangeError, e: |
| 329 | self.assertEquals('invalid_request', str(e)) |
| 330 | |
| 331 | def test_exchange_failure_with_json_error(self): |
| 332 | # Some providers have "error" attribute as a JSON object |
| 333 | # in place of regular string. |
| 334 | # This test makes sure no strange object-to-string coversion |
| 335 | # exceptions are being raised instead of FlowExchangeError. |
| 336 | http = HttpMockSequence([ |
| 337 | ({'status': '400'}, |
| 338 | """ {"error": { |
| 339 | "type": "OAuthException", |
| 340 | "message": "Error validating verification code."} }"""), |
| 341 | ]) |
| 342 | |
| 343 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 344 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 345 | self.fail("should raise exception if exchange doesn't get 200") |
| 346 | except FlowExchangeError, e: |
| 347 | pass |
| 348 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 349 | def test_exchange_success(self): |
| 350 | http = HttpMockSequence([ |
| 351 | ({'status': '200'}, |
| 352 | """{ "access_token":"SlAV32hkKG", |
| 353 | "expires_in":3600, |
| 354 | "refresh_token":"8xLOxBtZp8" }"""), |
| 355 | ]) |
| 356 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 357 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 358 | self.assertEqual('SlAV32hkKG', credentials.access_token) |
| 359 | self.assertNotEqual(None, credentials.token_expiry) |
| 360 | self.assertEqual('8xLOxBtZp8', credentials.refresh_token) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 361 | |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 362 | def test_urlencoded_exchange_success(self): |
| 363 | http = HttpMockSequence([ |
| 364 | ({'status': '200'}, "access_token=SlAV32hkKG&expires_in=3600"), |
| 365 | ]) |
| 366 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 367 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 368 | self.assertEqual('SlAV32hkKG', credentials.access_token) |
| 369 | self.assertNotEqual(None, credentials.token_expiry) |
| 370 | |
| 371 | def test_urlencoded_expires_param(self): |
| 372 | http = HttpMockSequence([ |
| 373 | # Note the "expires=3600" where you'd normally |
| 374 | # have if named "expires_in" |
| 375 | ({'status': '200'}, "access_token=SlAV32hkKG&expires=3600"), |
| 376 | ]) |
| 377 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 378 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 379 | self.assertNotEqual(None, credentials.token_expiry) |
| 380 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 381 | def test_exchange_no_expires_in(self): |
| 382 | http = HttpMockSequence([ |
| 383 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 384 | "refresh_token":"8xLOxBtZp8" }"""), |
| 385 | ]) |
| 386 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 387 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 388 | self.assertEqual(None, credentials.token_expiry) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 389 | |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 390 | def test_urlencoded_exchange_no_expires_in(self): |
| 391 | http = HttpMockSequence([ |
| 392 | # This might be redundant but just to make sure |
| 393 | # urlencoded access_token gets parsed correctly |
| 394 | ({'status': '200'}, "access_token=SlAV32hkKG"), |
| 395 | ]) |
| 396 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 397 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | ddb969a | 2012-07-11 11:04:12 -0400 | [diff] [blame] | 398 | self.assertEqual(None, credentials.token_expiry) |
| 399 | |
Joe Gregorio | 4b4002f | 2012-06-14 15:41:01 -0400 | [diff] [blame] | 400 | def test_exchange_fails_if_no_code(self): |
| 401 | http = HttpMockSequence([ |
| 402 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 403 | "refresh_token":"8xLOxBtZp8" }"""), |
| 404 | ]) |
| 405 | |
| 406 | code = {'error': 'thou shall not pass'} |
| 407 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 408 | credentials = self.flow.step2_exchange(code, http=http) |
Joe Gregorio | 4b4002f | 2012-06-14 15:41:01 -0400 | [diff] [blame] | 409 | self.fail('should raise exception if no code in dictionary.') |
| 410 | except FlowExchangeError, e: |
| 411 | self.assertTrue('shall not pass' in str(e)) |
| 412 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 413 | def test_exchange_id_token_fail(self): |
| 414 | http = HttpMockSequence([ |
| 415 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 416 | "refresh_token":"8xLOxBtZp8", |
| 417 | "id_token": "stuff.payload"}"""), |
| 418 | ]) |
| 419 | |
| 420 | self.assertRaises(VerifyJwtTokenError, self.flow.step2_exchange, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 421 | 'some random code', http=http) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 422 | |
| 423 | def test_exchange_id_token_fail(self): |
| 424 | body = {'foo': 'bar'} |
| 425 | payload = base64.urlsafe_b64encode(simplejson.dumps(body)).strip('=') |
Joe Gregorio | bd512b5 | 2011-12-06 15:39:26 -0500 | [diff] [blame] | 426 | jwt = (base64.urlsafe_b64encode('stuff')+ '.' + payload + '.' + |
| 427 | base64.urlsafe_b64encode('signature')) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 428 | |
| 429 | http = HttpMockSequence([ |
| 430 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 431 | "refresh_token":"8xLOxBtZp8", |
| 432 | "id_token": "%s"}""" % jwt), |
| 433 | ]) |
| 434 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 435 | credentials = self.flow.step2_exchange('some random code', http=http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 436 | self.assertEqual(credentials.id_token, body) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 437 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 438 | |
| 439 | class FlowFromCachedClientsecrets(unittest.TestCase): |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 440 | |
| 441 | def test_flow_from_clientsecrets_cached(self): |
| 442 | cache_mock = CacheMock() |
| 443 | load_and_cache('client_secrets.json', 'some_secrets', cache_mock) |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 444 | |
| 445 | flow = flow_from_clientsecrets( |
| 446 | 'some_secrets', '', redirect_uri='oob', cache=cache_mock) |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 447 | self.assertEquals('foo_client_secret', flow.client_secret) |
| 448 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 449 | |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 450 | class CredentialsFromCodeTests(unittest.TestCase): |
| 451 | def setUp(self): |
| 452 | self.client_id = 'client_id_abc' |
| 453 | self.client_secret = 'secret_use_code' |
| 454 | self.scope = 'foo' |
| 455 | self.code = '12345abcde' |
| 456 | self.redirect_uri = 'postmessage' |
| 457 | |
| 458 | def test_exchange_code_for_token(self): |
| 459 | http = HttpMockSequence([ |
| 460 | ({'status': '200'}, |
| 461 | """{ "access_token":"asdfghjkl", |
| 462 | "expires_in":3600 }"""), |
| 463 | ]) |
| 464 | credentials = credentials_from_code(self.client_id, self.client_secret, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 465 | self.scope, self.code, redirect_uri=self.redirect_uri, |
| 466 | http=http) |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 467 | self.assertEquals(credentials.access_token, 'asdfghjkl') |
| 468 | self.assertNotEqual(None, credentials.token_expiry) |
| 469 | |
| 470 | def test_exchange_code_for_token_fail(self): |
| 471 | http = HttpMockSequence([ |
| 472 | ({'status': '400'}, '{"error":"invalid_request"}'), |
| 473 | ]) |
| 474 | |
| 475 | try: |
| 476 | credentials = credentials_from_code(self.client_id, self.client_secret, |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame] | 477 | self.scope, self.code, redirect_uri=self.redirect_uri, |
| 478 | http=http) |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 479 | self.fail("should raise exception if exchange doesn't get 200") |
| 480 | except FlowExchangeError: |
| 481 | pass |
| 482 | |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 483 | def test_exchange_code_and_file_for_token(self): |
| 484 | http = HttpMockSequence([ |
| 485 | ({'status': '200'}, |
| 486 | """{ "access_token":"asdfghjkl", |
| 487 | "expires_in":3600 }"""), |
| 488 | ]) |
| 489 | credentials = credentials_from_clientsecrets_and_code( |
| 490 | datafile('client_secrets.json'), self.scope, |
| 491 | self.code, http=http) |
| 492 | self.assertEquals(credentials.access_token, 'asdfghjkl') |
| 493 | self.assertNotEqual(None, credentials.token_expiry) |
| 494 | |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame] | 495 | def test_exchange_code_and_cached_file_for_token(self): |
| 496 | http = HttpMockSequence([ |
| 497 | ({'status': '200'}, '{ "access_token":"asdfghjkl"}'), |
| 498 | ]) |
| 499 | cache_mock = CacheMock() |
| 500 | load_and_cache('client_secrets.json', 'some_secrets', cache_mock) |
| 501 | |
| 502 | credentials = credentials_from_clientsecrets_and_code( |
| 503 | 'some_secrets', self.scope, |
| 504 | self.code, http=http, cache=cache_mock) |
| 505 | self.assertEquals(credentials.access_token, 'asdfghjkl') |
| 506 | |
Joe Gregorio | 32d852d | 2012-06-14 09:08:18 -0400 | [diff] [blame] | 507 | def test_exchange_code_and_file_for_token_fail(self): |
| 508 | http = HttpMockSequence([ |
| 509 | ({'status': '400'}, '{"error":"invalid_request"}'), |
| 510 | ]) |
| 511 | |
| 512 | try: |
| 513 | credentials = credentials_from_clientsecrets_and_code( |
| 514 | datafile('client_secrets.json'), self.scope, |
| 515 | self.code, http=http) |
| 516 | self.fail("should raise exception if exchange doesn't get 200") |
| 517 | except FlowExchangeError: |
| 518 | pass |
| 519 | |
| 520 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 521 | class MemoryCacheTests(unittest.TestCase): |
| 522 | |
| 523 | def test_get_set_delete(self): |
| 524 | m = MemoryCache() |
| 525 | self.assertEqual(None, m.get('foo')) |
| 526 | self.assertEqual(None, m.delete('foo')) |
| 527 | m.set('foo', 'bar') |
| 528 | self.assertEqual('bar', m.get('foo')) |
| 529 | m.delete('foo') |
| 530 | self.assertEqual(None, m.get('foo')) |
| 531 | |
| 532 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 533 | if __name__ == '__main__': |
| 534 | unittest.main() |