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 |
| 27 | import unittest |
| 28 | import urlparse |
| 29 | |
| 30 | try: |
| 31 | from urlparse import parse_qs |
| 32 | except ImportError: |
| 33 | from cgi import parse_qs |
| 34 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 35 | import dev_appserver |
| 36 | dev_appserver.fix_sys_path() |
| 37 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 38 | from apiclient.anyjson import simplejson |
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 |
| 42 | from google.appengine.api import users |
| 43 | from google.appengine.ext import testbed |
| 44 | from google.appengine.ext import webapp |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 45 | from oauth2client.client import AccessTokenRefreshError |
| 46 | from oauth2client.client import FlowExchangeError |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 47 | from oauth2client.appengine import AppAssertionCredentials |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 48 | from oauth2client.appengine import OAuth2Decorator |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 49 | from oauth2client.appengine import OAuth2Handler |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 50 | from webtest import TestApp |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 51 | |
| 52 | |
| 53 | class UserMock(object): |
| 54 | """Mock the app engine user service""" |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 55 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 56 | def user_id(self): |
| 57 | return 'foo_user' |
| 58 | |
| 59 | |
| 60 | class Http2Mock(object): |
| 61 | """Mock httplib2.Http""" |
| 62 | status = 200 |
| 63 | content = { |
| 64 | 'access_token': 'foo_access_token', |
| 65 | 'refresh_token': 'foo_refresh_token', |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 66 | 'expires_in': 3600, |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | def request(self, token_uri, method, body, headers, *args, **kwargs): |
| 70 | self.body = body |
| 71 | self.headers = headers |
| 72 | return (self, simplejson.dumps(self.content)) |
| 73 | |
| 74 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 75 | class TestAppAssertionCredentials(unittest.TestCase): |
| 76 | account_name = "service_account_name@appspot.com" |
| 77 | signature = "signature" |
| 78 | |
| 79 | class AppIdentityStubImpl(apiproxy_stub.APIProxyStub): |
| 80 | |
| 81 | def __init__(self): |
| 82 | super(TestAppAssertionCredentials.AppIdentityStubImpl, self).__init__( |
| 83 | 'app_identity_service') |
| 84 | |
| 85 | def _Dynamic_GetServiceAccountName(self, request, response): |
| 86 | return response.set_service_account_name( |
| 87 | TestAppAssertionCredentials.account_name) |
| 88 | |
| 89 | def _Dynamic_SignForApp(self, request, response): |
| 90 | return response.set_signature_bytes( |
| 91 | TestAppAssertionCredentials.signature) |
| 92 | |
| 93 | def setUp(self): |
| 94 | app_identity_stub = self.AppIdentityStubImpl() |
| 95 | apiproxy_stub_map.apiproxy.RegisterStub("app_identity_service", |
| 96 | app_identity_stub) |
| 97 | |
| 98 | self.scope = "http://www.googleapis.com/scope" |
JacobMoshenko | cb6d891 | 2011-07-08 13:35:15 -0400 | [diff] [blame] | 99 | self.credentials = AppAssertionCredentials(self.scope) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 100 | |
| 101 | def test_assertion(self): |
| 102 | assertion = self.credentials._generate_assertion() |
| 103 | |
| 104 | parts = assertion.split(".") |
| 105 | self.assertTrue(len(parts) == 3) |
| 106 | |
| 107 | header, body, signature = [base64.b64decode(part) for part in parts] |
| 108 | |
| 109 | header_dict = simplejson.loads(header) |
| 110 | self.assertEqual(header_dict['typ'], 'JWT') |
| 111 | self.assertEqual(header_dict['alg'], 'RS256') |
| 112 | |
| 113 | body_dict = simplejson.loads(body) |
| 114 | self.assertEqual(body_dict['aud'], |
| 115 | 'https://accounts.google.com/o/oauth2/token') |
| 116 | self.assertEqual(body_dict['scope'], self.scope) |
| 117 | self.assertEqual(body_dict['iss'], self.account_name) |
| 118 | |
| 119 | issuedAt = body_dict['iat'] |
| 120 | self.assertTrue(issuedAt > 0) |
| 121 | self.assertEqual(body_dict['exp'], issuedAt + 3600) |
| 122 | |
| 123 | self.assertEqual(signature, self.signature) |
| 124 | |
| 125 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 126 | class DecoratorTests(unittest.TestCase): |
| 127 | |
| 128 | def setUp(self): |
| 129 | self.testbed = testbed.Testbed() |
| 130 | self.testbed.activate() |
| 131 | self.testbed.init_datastore_v3_stub() |
| 132 | self.testbed.init_memcache_stub() |
| 133 | self.testbed.init_user_stub() |
| 134 | |
| 135 | decorator = OAuth2Decorator(client_id='foo_client_id', |
| 136 | client_secret='foo_client_secret', |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 137 | scope=['foo_scope', 'bar_scope']) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 138 | self.decorator = decorator |
| 139 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 140 | class TestRequiredHandler(webapp.RequestHandler): |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 141 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 142 | @decorator.oauth_required |
| 143 | def get(self): |
| 144 | pass |
| 145 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 146 | class TestAwareHandler(webapp.RequestHandler): |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 147 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 148 | @decorator.oauth_aware |
| 149 | def get(self): |
| 150 | self.response.out.write('Hello World!') |
| 151 | |
| 152 | |
| 153 | application = webapp.WSGIApplication([('/oauth2callback', OAuth2Handler), |
| 154 | ('/foo_path', TestRequiredHandler), |
| 155 | ('/bar_path', TestAwareHandler)], |
| 156 | debug=True) |
| 157 | self.app = TestApp(application) |
| 158 | users.get_current_user = UserMock |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 159 | self.httplib2_orig = httplib2.Http |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 160 | httplib2.Http = Http2Mock |
| 161 | |
| 162 | def tearDown(self): |
| 163 | self.testbed.deactivate() |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 164 | httplib2.Http = self.httplib2_orig |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 165 | |
| 166 | def test_required(self): |
| 167 | # An initial request to an oauth_required decorated path should be a |
| 168 | # redirect to start the OAuth dance. |
| 169 | response = self.app.get('/foo_path') |
| 170 | self.assertTrue(response.status.startswith('302')) |
| 171 | q = parse_qs(response.headers['Location'].split('?', 1)[1]) |
| 172 | self.assertEqual('http://localhost/oauth2callback', q['redirect_uri'][0]) |
| 173 | self.assertEqual('foo_client_id', q['client_id'][0]) |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 174 | self.assertEqual('foo_scope bar_scope', q['scope'][0]) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 175 | self.assertEqual('http://localhost/foo_path', q['state'][0]) |
| 176 | self.assertEqual('code', q['response_type'][0]) |
| 177 | self.assertEqual(False, self.decorator.has_credentials()) |
| 178 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 179 | # Now simulate the callback to /oauth2callback. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 180 | response = self.app.get('/oauth2callback', { |
| 181 | 'code': 'foo_access_code', |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 182 | 'state': 'foo_path', |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 183 | }) |
| 184 | self.assertEqual('http://localhost/foo_path', response.headers['Location']) |
| 185 | self.assertEqual(None, self.decorator.credentials) |
| 186 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 187 | # Now requesting the decorated path should work. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 188 | response = self.app.get('/foo_path') |
| 189 | self.assertEqual('200 OK', response.status) |
| 190 | self.assertEqual(True, self.decorator.has_credentials()) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 191 | self.assertEqual('foo_refresh_token', |
| 192 | self.decorator.credentials.refresh_token) |
| 193 | self.assertEqual('foo_access_token', |
| 194 | self.decorator.credentials.access_token) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 195 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 196 | # Invalidate the stored Credentials. |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 197 | self.decorator.credentials.invalid = True |
| 198 | self.decorator.credentials.store.put(self.decorator.credentials) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 199 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 200 | # Invalid Credentials should start the OAuth dance again. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 201 | response = self.app.get('/foo_path') |
| 202 | self.assertTrue(response.status.startswith('302')) |
| 203 | q = parse_qs(response.headers['Location'].split('?', 1)[1]) |
| 204 | self.assertEqual('http://localhost/oauth2callback', q['redirect_uri'][0]) |
| 205 | |
| 206 | def test_aware(self): |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 207 | # An initial request to an oauth_aware decorated path should not redirect. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 208 | response = self.app.get('/bar_path') |
| 209 | self.assertEqual('Hello World!', response.body) |
| 210 | self.assertEqual('200 OK', response.status) |
| 211 | self.assertEqual(False, self.decorator.has_credentials()) |
| 212 | url = self.decorator.authorize_url() |
| 213 | q = parse_qs(url.split('?', 1)[1]) |
| 214 | self.assertEqual('http://localhost/oauth2callback', q['redirect_uri'][0]) |
| 215 | self.assertEqual('foo_client_id', q['client_id'][0]) |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 216 | self.assertEqual('foo_scope bar_scope', q['scope'][0]) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 217 | self.assertEqual('http://localhost/bar_path', q['state'][0]) |
| 218 | self.assertEqual('code', q['response_type'][0]) |
| 219 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 220 | # Now simulate the callback to /oauth2callback. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 221 | url = self.decorator.authorize_url() |
| 222 | response = self.app.get('/oauth2callback', { |
| 223 | 'code': 'foo_access_code', |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 224 | 'state': 'bar_path', |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 225 | }) |
| 226 | self.assertEqual('http://localhost/bar_path', response.headers['Location']) |
| 227 | self.assertEqual(False, self.decorator.has_credentials()) |
| 228 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 229 | # Now requesting the decorated path will have credentials. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 230 | response = self.app.get('/bar_path') |
| 231 | self.assertEqual('200 OK', response.status) |
| 232 | self.assertEqual('Hello World!', response.body) |
| 233 | self.assertEqual(True, self.decorator.has_credentials()) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 234 | self.assertEqual('foo_refresh_token', |
| 235 | self.decorator.credentials.refresh_token) |
| 236 | self.assertEqual('foo_access_token', |
| 237 | self.decorator.credentials.access_token) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 238 | |
| 239 | if __name__ == '__main__': |
| 240 | unittest.main() |