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 | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 28 | import unittest |
| 29 | import urlparse |
Joe Gregorio | e1de416 | 2011-02-23 11:30:29 -0500 | [diff] [blame] | 30 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 31 | try: |
| 32 | from urlparse import parse_qs |
| 33 | except ImportError: |
| 34 | from cgi import parse_qs |
| 35 | |
| 36 | from apiclient.http import HttpMockSequence |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 37 | from oauth2client.anyjson import simplejson |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 38 | from oauth2client.client import AccessTokenCredentials |
| 39 | from oauth2client.client import AccessTokenCredentialsError |
| 40 | from oauth2client.client import AccessTokenRefreshError |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 41 | from oauth2client.client import AssertionCredentials |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame^] | 42 | from oauth2client.client import Credentials |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 43 | from oauth2client.client import FlowExchangeError |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame^] | 44 | from oauth2client.client import MemoryCache |
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 | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 48 | from oauth2client.client import VerifyJwtTokenError |
| 49 | from oauth2client.client import _extract_id_token |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 50 | |
| 51 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame^] | 52 | class CredentialsTests(unittest.TestCase): |
| 53 | |
| 54 | def test_to_from_json(self): |
| 55 | credentials = Credentials() |
| 56 | json = credentials.to_json() |
| 57 | restored = Credentials.new_from_json(json) |
| 58 | |
| 59 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 60 | class OAuth2CredentialsTests(unittest.TestCase): |
| 61 | |
| 62 | def setUp(self): |
| 63 | access_token = "foo" |
| 64 | client_id = "some_client_id" |
| 65 | client_secret = "cOuDdkfjxxnv+" |
| 66 | refresh_token = "1/0/a.df219fjls0" |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 67 | token_expiry = datetime.datetime.utcnow() |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 68 | token_uri = "https://www.google.com/accounts/o8/oauth2/token" |
| 69 | user_agent = "refresh_checker/1.0" |
| 70 | self.credentials = OAuth2Credentials( |
| 71 | access_token, client_id, client_secret, |
| 72 | refresh_token, token_expiry, token_uri, |
| 73 | user_agent) |
| 74 | |
| 75 | def test_token_refresh_success(self): |
| 76 | http = HttpMockSequence([ |
| 77 | ({'status': '401'}, ''), |
| 78 | ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), |
| 79 | ({'status': '200'}, 'echo_request_headers'), |
| 80 | ]) |
| 81 | http = self.credentials.authorize(http) |
| 82 | resp, content = http.request("http://example.com") |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 83 | self.assertEqual('Bearer 1/3w', content['Authorization']) |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame^] | 84 | self.assertFalse(self.credentials.access_token_expired) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 85 | |
| 86 | def test_token_refresh_failure(self): |
| 87 | http = HttpMockSequence([ |
| 88 | ({'status': '401'}, ''), |
| 89 | ({'status': '400'}, '{"error":"access_denied"}'), |
| 90 | ]) |
| 91 | http = self.credentials.authorize(http) |
| 92 | try: |
| 93 | http.request("http://example.com") |
| 94 | self.fail("should raise AccessTokenRefreshError exception") |
| 95 | except AccessTokenRefreshError: |
| 96 | pass |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame^] | 97 | self.assertTrue(self.credentials.access_token_expired) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 98 | |
| 99 | def test_non_401_error_response(self): |
| 100 | http = HttpMockSequence([ |
| 101 | ({'status': '400'}, ''), |
| 102 | ]) |
| 103 | http = self.credentials.authorize(http) |
| 104 | resp, content = http.request("http://example.com") |
| 105 | self.assertEqual(400, resp.status) |
| 106 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 107 | def test_to_from_json(self): |
| 108 | json = self.credentials.to_json() |
| 109 | instance = OAuth2Credentials.from_json(json) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 110 | self.assertEqual(OAuth2Credentials, type(instance)) |
Joe Gregorio | 1daa71b | 2011-09-15 18:12:14 -0400 | [diff] [blame] | 111 | instance.token_expiry = None |
| 112 | self.credentials.token_expiry = None |
| 113 | |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 114 | self.assertEqual(instance.__dict__, self.credentials.__dict__) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 115 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 116 | |
| 117 | class AccessTokenCredentialsTests(unittest.TestCase): |
| 118 | |
| 119 | def setUp(self): |
| 120 | access_token = "foo" |
| 121 | user_agent = "refresh_checker/1.0" |
| 122 | self.credentials = AccessTokenCredentials(access_token, user_agent) |
| 123 | |
| 124 | def test_token_refresh_success(self): |
| 125 | http = HttpMockSequence([ |
| 126 | ({'status': '401'}, ''), |
| 127 | ]) |
| 128 | http = self.credentials.authorize(http) |
| 129 | try: |
| 130 | resp, content = http.request("http://example.com") |
| 131 | self.fail("should throw exception if token expires") |
| 132 | except AccessTokenCredentialsError: |
| 133 | pass |
| 134 | except Exception: |
| 135 | self.fail("should only throw AccessTokenCredentialsError") |
| 136 | |
| 137 | def test_non_401_error_response(self): |
| 138 | http = HttpMockSequence([ |
| 139 | ({'status': '400'}, ''), |
| 140 | ]) |
| 141 | http = self.credentials.authorize(http) |
Joe Gregorio | 83cd439 | 2011-06-20 10:11:35 -0400 | [diff] [blame] | 142 | resp, content = http.request('http://example.com') |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 143 | self.assertEqual(400, resp.status) |
| 144 | |
Joe Gregorio | 83cd439 | 2011-06-20 10:11:35 -0400 | [diff] [blame] | 145 | def test_auth_header_sent(self): |
| 146 | http = HttpMockSequence([ |
| 147 | ({'status': '200'}, 'echo_request_headers'), |
| 148 | ]) |
| 149 | http = self.credentials.authorize(http) |
| 150 | resp, content = http.request('http://example.com') |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 151 | self.assertEqual('Bearer foo', content['Authorization']) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 152 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 153 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 154 | class TestAssertionCredentials(unittest.TestCase): |
| 155 | assertion_text = "This is the assertion" |
| 156 | assertion_type = "http://www.google.com/assertionType" |
| 157 | |
| 158 | class AssertionCredentialsTestImpl(AssertionCredentials): |
| 159 | |
| 160 | def _generate_assertion(self): |
| 161 | return TestAssertionCredentials.assertion_text |
| 162 | |
| 163 | def setUp(self): |
| 164 | user_agent = "fun/2.0" |
| 165 | self.credentials = self.AssertionCredentialsTestImpl(self.assertion_type, |
| 166 | user_agent) |
| 167 | |
| 168 | def test_assertion_body(self): |
| 169 | body = urlparse.parse_qs(self.credentials._generate_refresh_request_body()) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 170 | self.assertEqual(self.assertion_text, body['assertion'][0]) |
| 171 | self.assertEqual(self.assertion_type, body['assertion_type'][0]) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 172 | |
| 173 | def test_assertion_refresh(self): |
| 174 | http = HttpMockSequence([ |
| 175 | ({'status': '200'}, '{"access_token":"1/3w"}'), |
| 176 | ({'status': '200'}, 'echo_request_headers'), |
| 177 | ]) |
| 178 | http = self.credentials.authorize(http) |
| 179 | resp, content = http.request("http://example.com") |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 180 | self.assertEqual('Bearer 1/3w', content['Authorization']) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 181 | |
| 182 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 183 | class ExtractIdTokenText(unittest.TestCase): |
| 184 | """Tests _extract_id_token().""" |
| 185 | |
| 186 | def test_extract_success(self): |
| 187 | body = {'foo': 'bar'} |
| 188 | payload = base64.urlsafe_b64encode(simplejson.dumps(body)).strip('=') |
| 189 | jwt = 'stuff.' + payload + '.signature' |
| 190 | |
| 191 | extracted = _extract_id_token(jwt) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 192 | self.assertEqual(extracted, body) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 193 | |
| 194 | def test_extract_failure(self): |
| 195 | body = {'foo': 'bar'} |
| 196 | payload = base64.urlsafe_b64encode(simplejson.dumps(body)).strip('=') |
| 197 | jwt = 'stuff.' + payload |
| 198 | |
| 199 | self.assertRaises(VerifyJwtTokenError, _extract_id_token, jwt) |
| 200 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 201 | class OAuth2WebServerFlowTest(unittest.TestCase): |
| 202 | |
| 203 | def setUp(self): |
| 204 | self.flow = OAuth2WebServerFlow( |
| 205 | client_id='client_id+1', |
| 206 | client_secret='secret+1', |
| 207 | scope='foo', |
| 208 | user_agent='unittest-sample/1.0', |
| 209 | ) |
| 210 | |
| 211 | def test_construct_authorize_url(self): |
Joe Gregorio | f2326c0 | 2012-02-09 12:18:44 -0500 | [diff] [blame] | 212 | authorize_url = self.flow.step1_get_authorize_url('OOB_CALLBACK_URN') |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 213 | |
| 214 | parsed = urlparse.urlparse(authorize_url) |
| 215 | q = parse_qs(parsed[4]) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 216 | self.assertEqual('client_id+1', q['client_id'][0]) |
| 217 | self.assertEqual('code', q['response_type'][0]) |
| 218 | self.assertEqual('foo', q['scope'][0]) |
| 219 | self.assertEqual('OOB_CALLBACK_URN', q['redirect_uri'][0]) |
| 220 | self.assertEqual('offline', q['access_type'][0]) |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 221 | |
| 222 | def test_override_flow_access_type(self): |
| 223 | """Passing access_type overrides the default.""" |
| 224 | flow = OAuth2WebServerFlow( |
| 225 | client_id='client_id+1', |
| 226 | client_secret='secret+1', |
| 227 | scope='foo', |
| 228 | user_agent='unittest-sample/1.0', |
| 229 | access_type='online' |
| 230 | ) |
Joe Gregorio | f2326c0 | 2012-02-09 12:18:44 -0500 | [diff] [blame] | 231 | authorize_url = flow.step1_get_authorize_url('OOB_CALLBACK_URN') |
Joe Gregorio | 69a0aca | 2011-11-03 10:47:32 -0400 | [diff] [blame] | 232 | |
| 233 | parsed = urlparse.urlparse(authorize_url) |
| 234 | q = parse_qs(parsed[4]) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 235 | self.assertEqual('client_id+1', q['client_id'][0]) |
| 236 | self.assertEqual('code', q['response_type'][0]) |
| 237 | self.assertEqual('foo', q['scope'][0]) |
| 238 | self.assertEqual('OOB_CALLBACK_URN', q['redirect_uri'][0]) |
| 239 | self.assertEqual('online', q['access_type'][0]) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 240 | |
| 241 | def test_exchange_failure(self): |
| 242 | http = HttpMockSequence([ |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 243 | ({'status': '400'}, '{"error":"invalid_request"}'), |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 244 | ]) |
| 245 | |
| 246 | try: |
| 247 | credentials = self.flow.step2_exchange('some random code', http) |
| 248 | self.fail("should raise exception if exchange doesn't get 200") |
| 249 | except FlowExchangeError: |
| 250 | pass |
| 251 | |
| 252 | def test_exchange_success(self): |
| 253 | http = HttpMockSequence([ |
| 254 | ({'status': '200'}, |
| 255 | """{ "access_token":"SlAV32hkKG", |
| 256 | "expires_in":3600, |
| 257 | "refresh_token":"8xLOxBtZp8" }"""), |
| 258 | ]) |
| 259 | |
| 260 | credentials = self.flow.step2_exchange('some random code', http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 261 | self.assertEqual('SlAV32hkKG', credentials.access_token) |
| 262 | self.assertNotEqual(None, credentials.token_expiry) |
| 263 | self.assertEqual('8xLOxBtZp8', credentials.refresh_token) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 264 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 265 | def test_exchange_no_expires_in(self): |
| 266 | http = HttpMockSequence([ |
| 267 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 268 | "refresh_token":"8xLOxBtZp8" }"""), |
| 269 | ]) |
| 270 | |
| 271 | credentials = self.flow.step2_exchange('some random code', http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 272 | self.assertEqual(None, credentials.token_expiry) |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 273 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 274 | def test_exchange_id_token_fail(self): |
| 275 | http = HttpMockSequence([ |
| 276 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 277 | "refresh_token":"8xLOxBtZp8", |
| 278 | "id_token": "stuff.payload"}"""), |
| 279 | ]) |
| 280 | |
| 281 | self.assertRaises(VerifyJwtTokenError, self.flow.step2_exchange, |
| 282 | 'some random code', http) |
| 283 | |
| 284 | def test_exchange_id_token_fail(self): |
| 285 | body = {'foo': 'bar'} |
| 286 | payload = base64.urlsafe_b64encode(simplejson.dumps(body)).strip('=') |
Joe Gregorio | bd512b5 | 2011-12-06 15:39:26 -0500 | [diff] [blame] | 287 | jwt = (base64.urlsafe_b64encode('stuff')+ '.' + payload + '.' + |
| 288 | base64.urlsafe_b64encode('signature')) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 289 | |
| 290 | http = HttpMockSequence([ |
| 291 | ({'status': '200'}, """{ "access_token":"SlAV32hkKG", |
| 292 | "refresh_token":"8xLOxBtZp8", |
| 293 | "id_token": "%s"}""" % jwt), |
| 294 | ]) |
| 295 | |
| 296 | credentials = self.flow.step2_exchange('some random code', http) |
Joe Gregorio | 654f4a2 | 2012-02-09 14:15:44 -0500 | [diff] [blame] | 297 | self.assertEqual(credentials.id_token, body) |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 298 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 299 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame^] | 300 | class MemoryCacheTests(unittest.TestCase): |
| 301 | |
| 302 | def test_get_set_delete(self): |
| 303 | m = MemoryCache() |
| 304 | self.assertEqual(None, m.get('foo')) |
| 305 | self.assertEqual(None, m.delete('foo')) |
| 306 | m.set('foo', 'bar') |
| 307 | self.assertEqual('bar', m.get('foo')) |
| 308 | m.delete('foo') |
| 309 | self.assertEqual(None, m.get('foo')) |
| 310 | |
| 311 | |
Joe Gregorio | ccc7954 | 2011-02-19 00:05:26 -0500 | [diff] [blame] | 312 | if __name__ == '__main__': |
| 313 | unittest.main() |