Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [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 | |
| 18 | """Discovery document tests |
| 19 | |
| 20 | Unit tests for objects created from discovery documents. |
| 21 | """ |
| 22 | |
| 23 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 24 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 25 | import base64 |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 26 | import httplib2 |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame^] | 27 | import time |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 28 | import unittest |
| 29 | import urlparse |
| 30 | |
| 31 | try: |
| 32 | from urlparse import parse_qs |
| 33 | except ImportError: |
| 34 | from cgi import parse_qs |
| 35 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 36 | import dev_appserver |
| 37 | dev_appserver.fix_sys_path() |
| 38 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 39 | from apiclient.http import HttpMockSequence |
| 40 | from google.appengine.api import apiproxy_stub |
| 41 | from google.appengine.api import apiproxy_stub_map |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame^] | 42 | from google.appengine.api import app_identity |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 43 | from google.appengine.api import users |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame^] | 44 | from google.appengine.api.memcache import memcache_stub |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 45 | from google.appengine.ext import testbed |
| 46 | from google.appengine.ext import webapp |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame^] | 47 | from google.appengine.runtime import apiproxy_errors |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 48 | from oauth2client.anyjson import simplejson |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 49 | from oauth2client.appengine import AppAssertionCredentials |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 50 | from oauth2client.appengine import OAuth2Decorator |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 51 | from oauth2client.appengine import OAuth2Handler |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 52 | from oauth2client.client import AccessTokenRefreshError |
| 53 | from oauth2client.client import FlowExchangeError |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 54 | from webtest import TestApp |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 55 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 56 | class UserMock(object): |
| 57 | """Mock the app engine user service""" |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 58 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 59 | def user_id(self): |
| 60 | return 'foo_user' |
| 61 | |
| 62 | |
| 63 | class Http2Mock(object): |
| 64 | """Mock httplib2.Http""" |
| 65 | status = 200 |
| 66 | content = { |
| 67 | 'access_token': 'foo_access_token', |
| 68 | 'refresh_token': 'foo_refresh_token', |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 69 | 'expires_in': 3600, |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | def request(self, token_uri, method, body, headers, *args, **kwargs): |
| 73 | self.body = body |
| 74 | self.headers = headers |
| 75 | return (self, simplejson.dumps(self.content)) |
| 76 | |
| 77 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 78 | class TestAppAssertionCredentials(unittest.TestCase): |
| 79 | account_name = "service_account_name@appspot.com" |
| 80 | signature = "signature" |
| 81 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame^] | 82 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 83 | class AppIdentityStubImpl(apiproxy_stub.APIProxyStub): |
| 84 | |
| 85 | def __init__(self): |
| 86 | super(TestAppAssertionCredentials.AppIdentityStubImpl, self).__init__( |
| 87 | 'app_identity_service') |
| 88 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame^] | 89 | def _Dynamic_GetAccessToken(self, request, response): |
| 90 | response.set_access_token('a_token_123') |
| 91 | response.set_expiration_time(time.time() + 1800) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 92 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 93 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame^] | 94 | class ErroringAppIdentityStubImpl(apiproxy_stub.APIProxyStub): |
| 95 | |
| 96 | def __init__(self): |
| 97 | super(TestAppAssertionCredentials.ErroringAppIdentityStubImpl, self).__init__( |
| 98 | 'app_identity_service') |
| 99 | |
| 100 | def _Dynamic_GetAccessToken(self, request, response): |
| 101 | raise app_identity.BackendDeadlineExceeded() |
| 102 | |
| 103 | def test_raise_correct_type_of_exception(self): |
| 104 | app_identity_stub = self.ErroringAppIdentityStubImpl() |
| 105 | apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap() |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 106 | apiproxy_stub_map.apiproxy.RegisterStub("app_identity_service", |
| 107 | app_identity_stub) |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame^] | 108 | apiproxy_stub_map.apiproxy.RegisterStub( |
| 109 | 'memcache', memcache_stub.MemcacheServiceStub()) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 110 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame^] | 111 | scope = "http://www.googleapis.com/scope" |
| 112 | try: |
| 113 | credentials = AppAssertionCredentials(scope) |
| 114 | http = httplib2.Http() |
| 115 | credentials.refresh(http) |
| 116 | self.fail('Should have raised an AccessTokenRefreshError') |
| 117 | except AccessTokenRefreshError: |
| 118 | pass |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 119 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame^] | 120 | def test_get_access_token_on_refresh(self): |
| 121 | app_identity_stub = self.AppIdentityStubImpl() |
| 122 | apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap() |
| 123 | apiproxy_stub_map.apiproxy.RegisterStub("app_identity_service", |
| 124 | app_identity_stub) |
| 125 | apiproxy_stub_map.apiproxy.RegisterStub( |
| 126 | 'memcache', memcache_stub.MemcacheServiceStub()) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 127 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame^] | 128 | scope = "http://www.googleapis.com/scope" |
| 129 | credentials = AppAssertionCredentials(scope) |
| 130 | http = httplib2.Http() |
| 131 | credentials.refresh(http) |
| 132 | self.assertEqual('a_token_123', credentials.access_token) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 133 | |
| 134 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 135 | class DecoratorTests(unittest.TestCase): |
| 136 | |
| 137 | def setUp(self): |
| 138 | self.testbed = testbed.Testbed() |
| 139 | self.testbed.activate() |
| 140 | self.testbed.init_datastore_v3_stub() |
| 141 | self.testbed.init_memcache_stub() |
| 142 | self.testbed.init_user_stub() |
| 143 | |
| 144 | decorator = OAuth2Decorator(client_id='foo_client_id', |
| 145 | client_secret='foo_client_secret', |
Johan Euphrosine | acf517f | 2012-02-13 21:08:33 +0100 | [diff] [blame] | 146 | scope=['foo_scope', 'bar_scope'], |
| 147 | user_agent='foo') |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 148 | self.decorator = decorator |
| 149 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 150 | class TestRequiredHandler(webapp.RequestHandler): |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 151 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 152 | @decorator.oauth_required |
| 153 | def get(self): |
| 154 | pass |
| 155 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 156 | class TestAwareHandler(webapp.RequestHandler): |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 157 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 158 | @decorator.oauth_aware |
| 159 | def get(self): |
| 160 | self.response.out.write('Hello World!') |
| 161 | |
| 162 | |
| 163 | application = webapp.WSGIApplication([('/oauth2callback', OAuth2Handler), |
| 164 | ('/foo_path', TestRequiredHandler), |
| 165 | ('/bar_path', TestAwareHandler)], |
| 166 | debug=True) |
| 167 | self.app = TestApp(application) |
| 168 | users.get_current_user = UserMock |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 169 | self.httplib2_orig = httplib2.Http |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 170 | httplib2.Http = Http2Mock |
| 171 | |
| 172 | def tearDown(self): |
| 173 | self.testbed.deactivate() |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 174 | httplib2.Http = self.httplib2_orig |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 175 | |
| 176 | def test_required(self): |
| 177 | # An initial request to an oauth_required decorated path should be a |
| 178 | # redirect to start the OAuth dance. |
| 179 | response = self.app.get('/foo_path') |
| 180 | self.assertTrue(response.status.startswith('302')) |
| 181 | q = parse_qs(response.headers['Location'].split('?', 1)[1]) |
| 182 | self.assertEqual('http://localhost/oauth2callback', q['redirect_uri'][0]) |
| 183 | self.assertEqual('foo_client_id', q['client_id'][0]) |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 184 | self.assertEqual('foo_scope bar_scope', q['scope'][0]) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 185 | self.assertEqual('http://localhost/foo_path', q['state'][0]) |
| 186 | self.assertEqual('code', q['response_type'][0]) |
| 187 | self.assertEqual(False, self.decorator.has_credentials()) |
| 188 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 189 | # Now simulate the callback to /oauth2callback. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 190 | response = self.app.get('/oauth2callback', { |
| 191 | 'code': 'foo_access_code', |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 192 | 'state': 'foo_path', |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 193 | }) |
| 194 | self.assertEqual('http://localhost/foo_path', response.headers['Location']) |
| 195 | self.assertEqual(None, self.decorator.credentials) |
| 196 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 197 | # Now requesting the decorated path should work. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 198 | response = self.app.get('/foo_path') |
| 199 | self.assertEqual('200 OK', response.status) |
| 200 | self.assertEqual(True, self.decorator.has_credentials()) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 201 | self.assertEqual('foo_refresh_token', |
| 202 | self.decorator.credentials.refresh_token) |
| 203 | self.assertEqual('foo_access_token', |
| 204 | self.decorator.credentials.access_token) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 205 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 206 | # Invalidate the stored Credentials. |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 207 | self.decorator.credentials.invalid = True |
| 208 | self.decorator.credentials.store.put(self.decorator.credentials) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 209 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 210 | # Invalid Credentials should start the OAuth dance again. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 211 | response = self.app.get('/foo_path') |
| 212 | self.assertTrue(response.status.startswith('302')) |
| 213 | q = parse_qs(response.headers['Location'].split('?', 1)[1]) |
| 214 | self.assertEqual('http://localhost/oauth2callback', q['redirect_uri'][0]) |
| 215 | |
Joe Gregorio | ec75dc1 | 2012-02-06 13:40:42 -0500 | [diff] [blame] | 216 | def test_storage_delete(self): |
| 217 | # An initial request to an oauth_required decorated path should be a |
| 218 | # redirect to start the OAuth dance. |
| 219 | response = self.app.get('/foo_path') |
| 220 | self.assertTrue(response.status.startswith('302')) |
| 221 | |
| 222 | # Now simulate the callback to /oauth2callback. |
| 223 | response = self.app.get('/oauth2callback', { |
| 224 | 'code': 'foo_access_code', |
| 225 | 'state': 'foo_path', |
| 226 | }) |
| 227 | self.assertEqual('http://localhost/foo_path', response.headers['Location']) |
| 228 | self.assertEqual(None, self.decorator.credentials) |
| 229 | |
| 230 | # Now requesting the decorated path should work. |
| 231 | response = self.app.get('/foo_path') |
| 232 | |
| 233 | # Invalidate the stored Credentials. |
| 234 | self.decorator.credentials.store.delete() |
| 235 | |
| 236 | # Invalid Credentials should start the OAuth dance again. |
| 237 | response = self.app.get('/foo_path') |
| 238 | self.assertTrue(response.status.startswith('302')) |
| 239 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 240 | def test_aware(self): |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 241 | # An initial request to an oauth_aware decorated path should not redirect. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 242 | response = self.app.get('/bar_path') |
| 243 | self.assertEqual('Hello World!', response.body) |
| 244 | self.assertEqual('200 OK', response.status) |
| 245 | self.assertEqual(False, self.decorator.has_credentials()) |
| 246 | url = self.decorator.authorize_url() |
| 247 | q = parse_qs(url.split('?', 1)[1]) |
| 248 | self.assertEqual('http://localhost/oauth2callback', q['redirect_uri'][0]) |
| 249 | self.assertEqual('foo_client_id', q['client_id'][0]) |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 250 | self.assertEqual('foo_scope bar_scope', q['scope'][0]) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 251 | self.assertEqual('http://localhost/bar_path', q['state'][0]) |
| 252 | self.assertEqual('code', q['response_type'][0]) |
| 253 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 254 | # Now simulate the callback to /oauth2callback. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 255 | url = self.decorator.authorize_url() |
| 256 | response = self.app.get('/oauth2callback', { |
| 257 | 'code': 'foo_access_code', |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 258 | 'state': 'bar_path', |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 259 | }) |
| 260 | self.assertEqual('http://localhost/bar_path', response.headers['Location']) |
| 261 | self.assertEqual(False, self.decorator.has_credentials()) |
| 262 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 263 | # Now requesting the decorated path will have credentials. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 264 | response = self.app.get('/bar_path') |
| 265 | self.assertEqual('200 OK', response.status) |
| 266 | self.assertEqual('Hello World!', response.body) |
| 267 | self.assertEqual(True, self.decorator.has_credentials()) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 268 | self.assertEqual('foo_refresh_token', |
| 269 | self.decorator.credentials.refresh_token) |
| 270 | self.assertEqual('foo_access_token', |
| 271 | self.decorator.credentials.access_token) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 272 | |
Joe Gregorio | 1adde1a | 2012-01-06 12:30:35 -0500 | [diff] [blame] | 273 | |
| 274 | def test_kwargs_are_passed_to_underlying_flow(self): |
| 275 | decorator = OAuth2Decorator(client_id='foo_client_id', |
| 276 | client_secret='foo_client_secret', |
Johan Euphrosine | acf517f | 2012-02-13 21:08:33 +0100 | [diff] [blame] | 277 | user_agent='foo_user_agent', |
Joe Gregorio | 1adde1a | 2012-01-06 12:30:35 -0500 | [diff] [blame] | 278 | scope=['foo_scope', 'bar_scope'], |
| 279 | access_type='offline', |
| 280 | approval_prompt='force') |
| 281 | self.assertEqual('offline', decorator.flow.params['access_type']) |
| 282 | self.assertEqual('force', decorator.flow.params['approval_prompt']) |
Johan Euphrosine | acf517f | 2012-02-13 21:08:33 +0100 | [diff] [blame] | 283 | self.assertEqual('foo_user_agent', decorator.flow.user_agent) |
| 284 | self.assertEqual(None, decorator.flow.params.get('user_agent', None)) |
Joe Gregorio | 1adde1a | 2012-01-06 12:30:35 -0500 | [diff] [blame] | 285 | |
| 286 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 287 | if __name__ == '__main__': |
| 288 | unittest.main() |