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() |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame^] | 38 | import webapp2 |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 39 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 40 | from apiclient.http import HttpMockSequence |
| 41 | from google.appengine.api import apiproxy_stub |
| 42 | from google.appengine.api import apiproxy_stub_map |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 43 | from google.appengine.api import app_identity |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 44 | from google.appengine.api import users |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 45 | from google.appengine.api.memcache import memcache_stub |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 46 | from google.appengine.ext import testbed |
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 | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame^] | 150 | class TestRequiredHandler(webapp2.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 | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame^] | 156 | class TestAwareHandler(webapp2.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 |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame^] | 159 | def get(self, *args, **kwargs): |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 160 | self.response.out.write('Hello World!') |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame^] | 161 | assert(kwargs['year'] == '2012') |
| 162 | assert(kwargs['month'] == '01') |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 163 | |
| 164 | |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame^] | 165 | application = webapp2.WSGIApplication([ |
| 166 | ('/oauth2callback', OAuth2Handler), |
| 167 | ('/foo_path', TestRequiredHandler), |
| 168 | webapp2.Route(r'/bar_path/<year:\d{4}>/<month:\d{2}>', |
| 169 | handler=TestAwareHandler, name='bar')], |
| 170 | debug=True) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 171 | self.app = TestApp(application) |
| 172 | users.get_current_user = UserMock |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 173 | self.httplib2_orig = httplib2.Http |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 174 | httplib2.Http = Http2Mock |
| 175 | |
| 176 | def tearDown(self): |
| 177 | self.testbed.deactivate() |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 178 | httplib2.Http = self.httplib2_orig |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 179 | |
| 180 | def test_required(self): |
| 181 | # An initial request to an oauth_required decorated path should be a |
| 182 | # redirect to start the OAuth dance. |
| 183 | response = self.app.get('/foo_path') |
| 184 | self.assertTrue(response.status.startswith('302')) |
| 185 | q = parse_qs(response.headers['Location'].split('?', 1)[1]) |
| 186 | self.assertEqual('http://localhost/oauth2callback', q['redirect_uri'][0]) |
| 187 | self.assertEqual('foo_client_id', q['client_id'][0]) |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 188 | self.assertEqual('foo_scope bar_scope', q['scope'][0]) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 189 | self.assertEqual('http://localhost/foo_path', q['state'][0]) |
| 190 | self.assertEqual('code', q['response_type'][0]) |
| 191 | self.assertEqual(False, self.decorator.has_credentials()) |
| 192 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 193 | # Now simulate the callback to /oauth2callback. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 194 | response = self.app.get('/oauth2callback', { |
| 195 | 'code': 'foo_access_code', |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 196 | 'state': 'foo_path', |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 197 | }) |
| 198 | self.assertEqual('http://localhost/foo_path', response.headers['Location']) |
| 199 | self.assertEqual(None, self.decorator.credentials) |
| 200 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 201 | # Now requesting the decorated path should work. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 202 | response = self.app.get('/foo_path') |
| 203 | self.assertEqual('200 OK', response.status) |
| 204 | self.assertEqual(True, self.decorator.has_credentials()) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 205 | self.assertEqual('foo_refresh_token', |
| 206 | self.decorator.credentials.refresh_token) |
| 207 | self.assertEqual('foo_access_token', |
| 208 | self.decorator.credentials.access_token) |
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 | # Invalidate the stored Credentials. |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 211 | self.decorator.credentials.invalid = True |
| 212 | self.decorator.credentials.store.put(self.decorator.credentials) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 213 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 214 | # Invalid Credentials should start the OAuth dance again. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 215 | response = self.app.get('/foo_path') |
| 216 | self.assertTrue(response.status.startswith('302')) |
| 217 | q = parse_qs(response.headers['Location'].split('?', 1)[1]) |
| 218 | self.assertEqual('http://localhost/oauth2callback', q['redirect_uri'][0]) |
| 219 | |
Joe Gregorio | ec75dc1 | 2012-02-06 13:40:42 -0500 | [diff] [blame] | 220 | def test_storage_delete(self): |
| 221 | # An initial request to an oauth_required decorated path should be a |
| 222 | # redirect to start the OAuth dance. |
| 223 | response = self.app.get('/foo_path') |
| 224 | self.assertTrue(response.status.startswith('302')) |
| 225 | |
| 226 | # Now simulate the callback to /oauth2callback. |
| 227 | response = self.app.get('/oauth2callback', { |
| 228 | 'code': 'foo_access_code', |
| 229 | 'state': 'foo_path', |
| 230 | }) |
| 231 | self.assertEqual('http://localhost/foo_path', response.headers['Location']) |
| 232 | self.assertEqual(None, self.decorator.credentials) |
| 233 | |
| 234 | # Now requesting the decorated path should work. |
| 235 | response = self.app.get('/foo_path') |
| 236 | |
| 237 | # Invalidate the stored Credentials. |
| 238 | self.decorator.credentials.store.delete() |
| 239 | |
| 240 | # Invalid Credentials should start the OAuth dance again. |
| 241 | response = self.app.get('/foo_path') |
| 242 | self.assertTrue(response.status.startswith('302')) |
| 243 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 244 | def test_aware(self): |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 245 | # An initial request to an oauth_aware decorated path should not redirect. |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame^] | 246 | response = self.app.get('/bar_path/2012/01') |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 247 | self.assertEqual('Hello World!', response.body) |
| 248 | self.assertEqual('200 OK', response.status) |
| 249 | self.assertEqual(False, self.decorator.has_credentials()) |
| 250 | url = self.decorator.authorize_url() |
| 251 | q = parse_qs(url.split('?', 1)[1]) |
| 252 | self.assertEqual('http://localhost/oauth2callback', q['redirect_uri'][0]) |
| 253 | self.assertEqual('foo_client_id', q['client_id'][0]) |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 254 | self.assertEqual('foo_scope bar_scope', q['scope'][0]) |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame^] | 255 | self.assertEqual('http://localhost/bar_path/2012/01', q['state'][0]) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 256 | self.assertEqual('code', q['response_type'][0]) |
| 257 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 258 | # Now simulate the callback to /oauth2callback. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 259 | url = self.decorator.authorize_url() |
| 260 | response = self.app.get('/oauth2callback', { |
| 261 | 'code': 'foo_access_code', |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 262 | 'state': 'bar_path', |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 263 | }) |
| 264 | self.assertEqual('http://localhost/bar_path', response.headers['Location']) |
| 265 | self.assertEqual(False, self.decorator.has_credentials()) |
| 266 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 267 | # Now requesting the decorated path will have credentials. |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame^] | 268 | response = self.app.get('/bar_path/2012/01') |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 269 | self.assertEqual('200 OK', response.status) |
| 270 | self.assertEqual('Hello World!', response.body) |
| 271 | self.assertEqual(True, self.decorator.has_credentials()) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 272 | self.assertEqual('foo_refresh_token', |
| 273 | self.decorator.credentials.refresh_token) |
| 274 | self.assertEqual('foo_access_token', |
| 275 | self.decorator.credentials.access_token) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 276 | |
Joe Gregorio | 1adde1a | 2012-01-06 12:30:35 -0500 | [diff] [blame] | 277 | |
| 278 | def test_kwargs_are_passed_to_underlying_flow(self): |
| 279 | decorator = OAuth2Decorator(client_id='foo_client_id', |
| 280 | client_secret='foo_client_secret', |
Johan Euphrosine | acf517f | 2012-02-13 21:08:33 +0100 | [diff] [blame] | 281 | user_agent='foo_user_agent', |
Joe Gregorio | 1adde1a | 2012-01-06 12:30:35 -0500 | [diff] [blame] | 282 | scope=['foo_scope', 'bar_scope'], |
| 283 | access_type='offline', |
| 284 | approval_prompt='force') |
| 285 | self.assertEqual('offline', decorator.flow.params['access_type']) |
| 286 | self.assertEqual('force', decorator.flow.params['approval_prompt']) |
Johan Euphrosine | acf517f | 2012-02-13 21:08:33 +0100 | [diff] [blame] | 287 | self.assertEqual('foo_user_agent', decorator.flow.user_agent) |
| 288 | self.assertEqual(None, decorator.flow.params.get('user_agent', None)) |
Joe Gregorio | 1adde1a | 2012-01-06 12:30:35 -0500 | [diff] [blame] | 289 | |
| 290 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 291 | if __name__ == '__main__': |
| 292 | unittest.main() |