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 | e84c944 | 2012-03-12 08:45:57 -0400 | [diff] [blame] | 26 | import datetime |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 27 | import httplib2 |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 28 | import os |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 29 | import time |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 30 | import unittest |
| 31 | import urlparse |
| 32 | |
| 33 | try: |
| 34 | from urlparse import parse_qs |
| 35 | except ImportError: |
| 36 | from cgi import parse_qs |
| 37 | |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 38 | import dev_appserver |
| 39 | dev_appserver.fix_sys_path() |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame] | 40 | import webapp2 |
Joe Gregorio | 8b4c173 | 2011-12-06 11:28:29 -0500 | [diff] [blame] | 41 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 42 | from apiclient.http import HttpMockSequence |
| 43 | from google.appengine.api import apiproxy_stub |
| 44 | from google.appengine.api import apiproxy_stub_map |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 45 | from google.appengine.api import app_identity |
Joe Gregorio | e84c944 | 2012-03-12 08:45:57 -0400 | [diff] [blame] | 46 | from google.appengine.api import memcache |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 47 | from google.appengine.api import users |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 48 | from google.appengine.api.memcache import memcache_stub |
Joe Gregorio | e84c944 | 2012-03-12 08:45:57 -0400 | [diff] [blame] | 49 | from google.appengine.ext import db |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 50 | from google.appengine.ext import testbed |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 51 | from google.appengine.runtime import apiproxy_errors |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 52 | from oauth2client.anyjson import simplejson |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 53 | from oauth2client.appengine import AppAssertionCredentials |
Joe Gregorio | e84c944 | 2012-03-12 08:45:57 -0400 | [diff] [blame] | 54 | from oauth2client.appengine import CredentialsModel |
Joe Gregorio | 4fbde1c | 2012-07-11 14:47:39 -0400 | [diff] [blame] | 55 | from oauth2client.appengine import FlowProperty |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 56 | from oauth2client.appengine import OAuth2Decorator |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 57 | from oauth2client.appengine import OAuth2Handler |
Joe Gregorio | e84c944 | 2012-03-12 08:45:57 -0400 | [diff] [blame] | 58 | from oauth2client.appengine import StorageByKeyName |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 59 | from oauth2client.appengine import oauth2decorator_from_clientsecrets |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 60 | from oauth2client.client import AccessTokenRefreshError |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 61 | from oauth2client.client import Credentials |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 62 | from oauth2client.client import FlowExchangeError |
Joe Gregorio | e84c944 | 2012-03-12 08:45:57 -0400 | [diff] [blame] | 63 | from oauth2client.client import OAuth2Credentials |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 64 | from oauth2client.client import flow_from_clientsecrets |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 65 | from webtest import TestApp |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 66 | |
Joe Gregorio | 4fbde1c | 2012-07-11 14:47:39 -0400 | [diff] [blame] | 67 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 68 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 69 | |
| 70 | |
| 71 | def datafile(filename): |
| 72 | return os.path.join(DATA_DIR, filename) |
| 73 | |
| 74 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 75 | class UserMock(object): |
| 76 | """Mock the app engine user service""" |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 77 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 78 | def __call__(self): |
| 79 | return self |
| 80 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 81 | def user_id(self): |
| 82 | return 'foo_user' |
| 83 | |
| 84 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 85 | class UserNotLoggedInMock(object): |
| 86 | """Mock the app engine user service""" |
| 87 | |
| 88 | def __call__(self): |
| 89 | return None |
| 90 | |
| 91 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 92 | class Http2Mock(object): |
| 93 | """Mock httplib2.Http""" |
| 94 | status = 200 |
| 95 | content = { |
| 96 | 'access_token': 'foo_access_token', |
| 97 | 'refresh_token': 'foo_refresh_token', |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 98 | 'expires_in': 3600, |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | def request(self, token_uri, method, body, headers, *args, **kwargs): |
| 102 | self.body = body |
| 103 | self.headers = headers |
| 104 | return (self, simplejson.dumps(self.content)) |
| 105 | |
| 106 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 107 | class TestAppAssertionCredentials(unittest.TestCase): |
| 108 | account_name = "service_account_name@appspot.com" |
| 109 | signature = "signature" |
| 110 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 111 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 112 | class AppIdentityStubImpl(apiproxy_stub.APIProxyStub): |
| 113 | |
| 114 | def __init__(self): |
| 115 | super(TestAppAssertionCredentials.AppIdentityStubImpl, self).__init__( |
| 116 | 'app_identity_service') |
| 117 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 118 | def _Dynamic_GetAccessToken(self, request, response): |
| 119 | response.set_access_token('a_token_123') |
| 120 | response.set_expiration_time(time.time() + 1800) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 121 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 122 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 123 | class ErroringAppIdentityStubImpl(apiproxy_stub.APIProxyStub): |
| 124 | |
| 125 | def __init__(self): |
| 126 | super(TestAppAssertionCredentials.ErroringAppIdentityStubImpl, self).__init__( |
| 127 | 'app_identity_service') |
| 128 | |
| 129 | def _Dynamic_GetAccessToken(self, request, response): |
| 130 | raise app_identity.BackendDeadlineExceeded() |
| 131 | |
| 132 | def test_raise_correct_type_of_exception(self): |
| 133 | app_identity_stub = self.ErroringAppIdentityStubImpl() |
| 134 | apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap() |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 135 | apiproxy_stub_map.apiproxy.RegisterStub("app_identity_service", |
| 136 | app_identity_stub) |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 137 | apiproxy_stub_map.apiproxy.RegisterStub( |
| 138 | 'memcache', memcache_stub.MemcacheServiceStub()) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 139 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 140 | scope = "http://www.googleapis.com/scope" |
| 141 | try: |
| 142 | credentials = AppAssertionCredentials(scope) |
| 143 | http = httplib2.Http() |
| 144 | credentials.refresh(http) |
| 145 | self.fail('Should have raised an AccessTokenRefreshError') |
| 146 | except AccessTokenRefreshError: |
| 147 | pass |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 148 | |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 149 | def test_get_access_token_on_refresh(self): |
| 150 | app_identity_stub = self.AppIdentityStubImpl() |
| 151 | apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap() |
| 152 | apiproxy_stub_map.apiproxy.RegisterStub("app_identity_service", |
| 153 | app_identity_stub) |
| 154 | apiproxy_stub_map.apiproxy.RegisterStub( |
| 155 | 'memcache', memcache_stub.MemcacheServiceStub()) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 156 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 157 | scope = ["http://www.googleapis.com/scope"] |
Joe Gregorio | d84d6b8 | 2012-02-28 14:53:00 -0500 | [diff] [blame] | 158 | credentials = AppAssertionCredentials(scope) |
| 159 | http = httplib2.Http() |
| 160 | credentials.refresh(http) |
| 161 | self.assertEqual('a_token_123', credentials.access_token) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 162 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 163 | json = credentials.to_json() |
| 164 | credentials = Credentials.new_from_json(json) |
| 165 | self.assertEqual(scope[0], credentials.scope) |
| 166 | |
| 167 | |
| 168 | class TestFlowModel(db.Model): |
| 169 | flow = FlowProperty() |
| 170 | |
| 171 | |
| 172 | class FlowPropertyTest(unittest.TestCase): |
| 173 | |
| 174 | def setUp(self): |
| 175 | self.testbed = testbed.Testbed() |
| 176 | self.testbed.activate() |
| 177 | self.testbed.init_datastore_v3_stub() |
| 178 | |
| 179 | def tearDown(self): |
| 180 | self.testbed.deactivate() |
| 181 | |
| 182 | def test_flow_get_put(self): |
| 183 | instance = TestFlowModel( |
| 184 | flow=flow_from_clientsecrets(datafile('client_secrets.json'), 'foo'), |
| 185 | key_name='foo' |
| 186 | ) |
| 187 | instance.put() |
| 188 | retrieved = TestFlowModel.get_by_key_name('foo') |
| 189 | |
| 190 | self.assertEqual('foo_client_id', retrieved.flow.client_id) |
| 191 | |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 192 | |
Joe Gregorio | e84c944 | 2012-03-12 08:45:57 -0400 | [diff] [blame] | 193 | def _http_request(*args, **kwargs): |
| 194 | resp = httplib2.Response({'status': '200'}) |
| 195 | content = simplejson.dumps({'access_token': 'bar'}) |
| 196 | |
| 197 | return resp, content |
| 198 | |
| 199 | |
| 200 | class StorageByKeyNameTest(unittest.TestCase): |
| 201 | |
| 202 | def setUp(self): |
| 203 | self.testbed = testbed.Testbed() |
| 204 | self.testbed.activate() |
| 205 | self.testbed.init_datastore_v3_stub() |
| 206 | self.testbed.init_memcache_stub() |
| 207 | self.testbed.init_user_stub() |
| 208 | |
| 209 | access_token = "foo" |
| 210 | client_id = "some_client_id" |
| 211 | client_secret = "cOuDdkfjxxnv+" |
| 212 | refresh_token = "1/0/a.df219fjls0" |
| 213 | token_expiry = datetime.datetime.utcnow() |
| 214 | token_uri = "https://www.google.com/accounts/o8/oauth2/token" |
| 215 | user_agent = "refresh_checker/1.0" |
| 216 | self.credentials = OAuth2Credentials( |
| 217 | access_token, client_id, client_secret, |
| 218 | refresh_token, token_expiry, token_uri, |
| 219 | user_agent) |
| 220 | |
| 221 | def tearDown(self): |
| 222 | self.testbed.deactivate() |
| 223 | |
| 224 | def test_get_and_put_simple(self): |
| 225 | storage = StorageByKeyName( |
| 226 | CredentialsModel, 'foo', 'credentials') |
| 227 | |
| 228 | self.assertEqual(None, storage.get()) |
| 229 | self.credentials.set_store(storage) |
| 230 | |
| 231 | self.credentials._refresh(_http_request) |
| 232 | credmodel = CredentialsModel.get_by_key_name('foo') |
| 233 | self.assertEqual('bar', credmodel.credentials.access_token) |
| 234 | |
| 235 | def test_get_and_put_cached(self): |
| 236 | storage = StorageByKeyName( |
| 237 | CredentialsModel, 'foo', 'credentials', cache=memcache) |
| 238 | |
| 239 | self.assertEqual(None, storage.get()) |
| 240 | self.credentials.set_store(storage) |
| 241 | |
| 242 | self.credentials._refresh(_http_request) |
| 243 | credmodel = CredentialsModel.get_by_key_name('foo') |
| 244 | self.assertEqual('bar', credmodel.credentials.access_token) |
| 245 | |
| 246 | # Now remove the item from the cache. |
| 247 | memcache.delete('foo') |
| 248 | |
| 249 | # Check that getting refreshes the cache. |
| 250 | credentials = storage.get() |
| 251 | self.assertEqual('bar', credentials.access_token) |
| 252 | self.assertNotEqual(None, memcache.get('foo')) |
| 253 | |
| 254 | # Deleting should clear the cache. |
| 255 | storage.delete() |
| 256 | credentials = storage.get() |
| 257 | self.assertEqual(None, credentials) |
| 258 | self.assertEqual(None, memcache.get('foo')) |
| 259 | |
| 260 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 261 | class DecoratorTests(unittest.TestCase): |
| 262 | |
| 263 | def setUp(self): |
| 264 | self.testbed = testbed.Testbed() |
| 265 | self.testbed.activate() |
| 266 | self.testbed.init_datastore_v3_stub() |
| 267 | self.testbed.init_memcache_stub() |
| 268 | self.testbed.init_user_stub() |
| 269 | |
| 270 | decorator = OAuth2Decorator(client_id='foo_client_id', |
| 271 | client_secret='foo_client_secret', |
Johan Euphrosine | acf517f | 2012-02-13 21:08:33 +0100 | [diff] [blame] | 272 | scope=['foo_scope', 'bar_scope'], |
| 273 | user_agent='foo') |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 274 | |
| 275 | self._finish_setup(decorator, user_mock=UserMock) |
| 276 | |
| 277 | def _finish_setup(self, decorator, user_mock): |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 278 | self.decorator = decorator |
| 279 | |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame] | 280 | class TestRequiredHandler(webapp2.RequestHandler): |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 281 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 282 | @decorator.oauth_required |
| 283 | def get(self): |
| 284 | pass |
| 285 | |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame] | 286 | class TestAwareHandler(webapp2.RequestHandler): |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 287 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 288 | @decorator.oauth_aware |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame] | 289 | def get(self, *args, **kwargs): |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 290 | self.response.out.write('Hello World!') |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame] | 291 | assert(kwargs['year'] == '2012') |
| 292 | assert(kwargs['month'] == '01') |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 293 | |
| 294 | |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame] | 295 | application = webapp2.WSGIApplication([ |
| 296 | ('/oauth2callback', OAuth2Handler), |
| 297 | ('/foo_path', TestRequiredHandler), |
| 298 | webapp2.Route(r'/bar_path/<year:\d{4}>/<month:\d{2}>', |
| 299 | handler=TestAwareHandler, name='bar')], |
| 300 | debug=True) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 301 | self.app = TestApp(application) |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 302 | users.get_current_user = user_mock() |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 303 | self.httplib2_orig = httplib2.Http |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 304 | httplib2.Http = Http2Mock |
| 305 | |
| 306 | def tearDown(self): |
| 307 | self.testbed.deactivate() |
Joe Gregorio | 922b78c | 2011-05-26 21:36:34 -0400 | [diff] [blame] | 308 | httplib2.Http = self.httplib2_orig |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 309 | |
| 310 | def test_required(self): |
| 311 | # An initial request to an oauth_required decorated path should be a |
| 312 | # redirect to start the OAuth dance. |
| 313 | response = self.app.get('/foo_path') |
| 314 | self.assertTrue(response.status.startswith('302')) |
| 315 | q = parse_qs(response.headers['Location'].split('?', 1)[1]) |
| 316 | self.assertEqual('http://localhost/oauth2callback', q['redirect_uri'][0]) |
| 317 | self.assertEqual('foo_client_id', q['client_id'][0]) |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 318 | self.assertEqual('foo_scope bar_scope', q['scope'][0]) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 319 | self.assertEqual('http://localhost/foo_path', q['state'][0]) |
| 320 | self.assertEqual('code', q['response_type'][0]) |
| 321 | self.assertEqual(False, self.decorator.has_credentials()) |
| 322 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 323 | # Now simulate the callback to /oauth2callback. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 324 | response = self.app.get('/oauth2callback', { |
| 325 | 'code': 'foo_access_code', |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 326 | 'state': 'foo_path', |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 327 | }) |
| 328 | self.assertEqual('http://localhost/foo_path', response.headers['Location']) |
| 329 | self.assertEqual(None, self.decorator.credentials) |
| 330 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 331 | # Now requesting the decorated path should work. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 332 | response = self.app.get('/foo_path') |
| 333 | self.assertEqual('200 OK', response.status) |
| 334 | self.assertEqual(True, self.decorator.has_credentials()) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 335 | self.assertEqual('foo_refresh_token', |
| 336 | self.decorator.credentials.refresh_token) |
| 337 | self.assertEqual('foo_access_token', |
| 338 | self.decorator.credentials.access_token) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 339 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 340 | # Invalidate the stored Credentials. |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 341 | self.decorator.credentials.invalid = True |
| 342 | self.decorator.credentials.store.put(self.decorator.credentials) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 343 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 344 | # Invalid Credentials should start the OAuth dance again. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 345 | response = self.app.get('/foo_path') |
| 346 | self.assertTrue(response.status.startswith('302')) |
| 347 | q = parse_qs(response.headers['Location'].split('?', 1)[1]) |
| 348 | self.assertEqual('http://localhost/oauth2callback', q['redirect_uri'][0]) |
| 349 | |
Joe Gregorio | ec75dc1 | 2012-02-06 13:40:42 -0500 | [diff] [blame] | 350 | def test_storage_delete(self): |
| 351 | # An initial request to an oauth_required decorated path should be a |
| 352 | # redirect to start the OAuth dance. |
| 353 | response = self.app.get('/foo_path') |
| 354 | self.assertTrue(response.status.startswith('302')) |
| 355 | |
| 356 | # Now simulate the callback to /oauth2callback. |
| 357 | response = self.app.get('/oauth2callback', { |
| 358 | 'code': 'foo_access_code', |
| 359 | 'state': 'foo_path', |
| 360 | }) |
| 361 | self.assertEqual('http://localhost/foo_path', response.headers['Location']) |
| 362 | self.assertEqual(None, self.decorator.credentials) |
| 363 | |
| 364 | # Now requesting the decorated path should work. |
| 365 | response = self.app.get('/foo_path') |
| 366 | |
| 367 | # Invalidate the stored Credentials. |
| 368 | self.decorator.credentials.store.delete() |
| 369 | |
| 370 | # Invalid Credentials should start the OAuth dance again. |
| 371 | response = self.app.get('/foo_path') |
| 372 | self.assertTrue(response.status.startswith('302')) |
| 373 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 374 | def test_aware(self): |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 375 | # An initial request to an oauth_aware decorated path should not redirect. |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame] | 376 | response = self.app.get('/bar_path/2012/01') |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 377 | self.assertEqual('Hello World!', response.body) |
| 378 | self.assertEqual('200 OK', response.status) |
| 379 | self.assertEqual(False, self.decorator.has_credentials()) |
| 380 | url = self.decorator.authorize_url() |
| 381 | q = parse_qs(url.split('?', 1)[1]) |
| 382 | self.assertEqual('http://localhost/oauth2callback', q['redirect_uri'][0]) |
| 383 | self.assertEqual('foo_client_id', q['client_id'][0]) |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 384 | self.assertEqual('foo_scope bar_scope', q['scope'][0]) |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame] | 385 | self.assertEqual('http://localhost/bar_path/2012/01', q['state'][0]) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 386 | self.assertEqual('code', q['response_type'][0]) |
| 387 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 388 | # Now simulate the callback to /oauth2callback. |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 389 | url = self.decorator.authorize_url() |
| 390 | response = self.app.get('/oauth2callback', { |
| 391 | 'code': 'foo_access_code', |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 392 | 'state': 'bar_path', |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 393 | }) |
| 394 | self.assertEqual('http://localhost/bar_path', response.headers['Location']) |
| 395 | self.assertEqual(False, self.decorator.has_credentials()) |
| 396 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 397 | # Now requesting the decorated path will have credentials. |
Joe Gregorio | 1777497 | 2012-03-01 11:11:59 -0500 | [diff] [blame] | 398 | response = self.app.get('/bar_path/2012/01') |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 399 | self.assertEqual('200 OK', response.status) |
| 400 | self.assertEqual('Hello World!', response.body) |
| 401 | self.assertEqual(True, self.decorator.has_credentials()) |
JacobMoshenko | 8e90510 | 2011-06-20 09:53:10 -0400 | [diff] [blame] | 402 | self.assertEqual('foo_refresh_token', |
| 403 | self.decorator.credentials.refresh_token) |
| 404 | self.assertEqual('foo_access_token', |
| 405 | self.decorator.credentials.access_token) |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 406 | |
Joe Gregorio | 1adde1a | 2012-01-06 12:30:35 -0500 | [diff] [blame] | 407 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 408 | def test_error_in_step2(self): |
| 409 | # An initial request to an oauth_aware decorated path should not redirect. |
| 410 | response = self.app.get('/bar_path/2012/01') |
| 411 | url = self.decorator.authorize_url() |
| 412 | response = self.app.get('/oauth2callback', { |
| 413 | 'error': 'BadStuffHappened' |
| 414 | }) |
| 415 | self.assertEqual('200 OK', response.status) |
| 416 | self.assertTrue('BadStuffHappened' in response.body) |
| 417 | |
Joe Gregorio | 1adde1a | 2012-01-06 12:30:35 -0500 | [diff] [blame] | 418 | def test_kwargs_are_passed_to_underlying_flow(self): |
| 419 | decorator = OAuth2Decorator(client_id='foo_client_id', |
| 420 | client_secret='foo_client_secret', |
Johan Euphrosine | acf517f | 2012-02-13 21:08:33 +0100 | [diff] [blame] | 421 | user_agent='foo_user_agent', |
Joe Gregorio | 1adde1a | 2012-01-06 12:30:35 -0500 | [diff] [blame] | 422 | scope=['foo_scope', 'bar_scope'], |
| 423 | access_type='offline', |
| 424 | approval_prompt='force') |
| 425 | self.assertEqual('offline', decorator.flow.params['access_type']) |
| 426 | self.assertEqual('force', decorator.flow.params['approval_prompt']) |
Johan Euphrosine | acf517f | 2012-02-13 21:08:33 +0100 | [diff] [blame] | 427 | self.assertEqual('foo_user_agent', decorator.flow.user_agent) |
| 428 | self.assertEqual(None, decorator.flow.params.get('user_agent', None)) |
Joe Gregorio | 1adde1a | 2012-01-06 12:30:35 -0500 | [diff] [blame] | 429 | |
Joe Gregorio | 08cdcb8 | 2012-03-14 00:09:33 -0400 | [diff] [blame] | 430 | def test_decorator_from_client_secrets(self): |
| 431 | decorator = oauth2decorator_from_clientsecrets( |
| 432 | datafile('client_secrets.json'), |
| 433 | scope=['foo_scope', 'bar_scope']) |
| 434 | self._finish_setup(decorator, user_mock=UserMock) |
| 435 | |
| 436 | self.assertFalse(decorator._in_error) |
| 437 | self.decorator = decorator |
| 438 | self.test_required() |
| 439 | http = self.decorator.http() |
| 440 | self.assertEquals('foo_access_token', http.request.credentials.access_token) |
| 441 | |
| 442 | def test_decorator_from_client_secrets_not_logged_in_required(self): |
| 443 | decorator = oauth2decorator_from_clientsecrets( |
| 444 | datafile('client_secrets.json'), |
| 445 | scope=['foo_scope', 'bar_scope'], message='NotLoggedInMessage') |
| 446 | self.decorator = decorator |
| 447 | self._finish_setup(decorator, user_mock=UserNotLoggedInMock) |
| 448 | |
| 449 | self.assertFalse(decorator._in_error) |
| 450 | |
| 451 | # An initial request to an oauth_required decorated path should be a |
| 452 | # redirect to login. |
| 453 | response = self.app.get('/foo_path') |
| 454 | self.assertTrue(response.status.startswith('302')) |
| 455 | self.assertTrue('Login' in str(response)) |
| 456 | |
| 457 | def test_decorator_from_client_secrets_not_logged_in_aware(self): |
| 458 | decorator = oauth2decorator_from_clientsecrets( |
| 459 | datafile('client_secrets.json'), |
| 460 | scope=['foo_scope', 'bar_scope'], message='NotLoggedInMessage') |
| 461 | self.decorator = decorator |
| 462 | self._finish_setup(decorator, user_mock=UserNotLoggedInMock) |
| 463 | |
| 464 | # An initial request to an oauth_aware decorated path should be a |
| 465 | # redirect to login. |
| 466 | response = self.app.get('/bar_path/2012/03') |
| 467 | self.assertTrue(response.status.startswith('302')) |
| 468 | self.assertTrue('Login' in str(response)) |
| 469 | |
| 470 | def test_decorator_from_unfilled_client_secrets_required(self): |
| 471 | MESSAGE = 'File is missing' |
| 472 | decorator = oauth2decorator_from_clientsecrets( |
| 473 | datafile('unfilled_client_secrets.json'), |
| 474 | scope=['foo_scope', 'bar_scope'], message=MESSAGE) |
| 475 | self._finish_setup(decorator, user_mock=UserNotLoggedInMock) |
| 476 | self.assertTrue(decorator._in_error) |
| 477 | self.assertEqual(MESSAGE, decorator._message) |
| 478 | |
| 479 | # An initial request to an oauth_required decorated path should be an |
| 480 | # error message. |
| 481 | response = self.app.get('/foo_path') |
| 482 | self.assertTrue(response.status.startswith('200')) |
| 483 | self.assertTrue(MESSAGE in str(response)) |
| 484 | |
| 485 | def test_decorator_from_unfilled_client_secrets_aware(self): |
| 486 | MESSAGE = 'File is missing' |
| 487 | decorator = oauth2decorator_from_clientsecrets( |
| 488 | datafile('unfilled_client_secrets.json'), |
| 489 | scope=['foo_scope', 'bar_scope'], message=MESSAGE) |
| 490 | self._finish_setup(decorator, user_mock=UserNotLoggedInMock) |
| 491 | self.assertTrue(decorator._in_error) |
| 492 | self.assertEqual(MESSAGE, decorator._message) |
| 493 | |
| 494 | # An initial request to an oauth_aware decorated path should be an |
| 495 | # error message. |
| 496 | response = self.app.get('/bar_path/2012/03') |
| 497 | self.assertTrue(response.status.startswith('200')) |
| 498 | self.assertTrue(MESSAGE in str(response)) |
| 499 | |
Joe Gregorio | 1adde1a | 2012-01-06 12:30:35 -0500 | [diff] [blame] | 500 | |
Joe Gregorio | 432f17e | 2011-05-22 23:18:00 -0400 | [diff] [blame] | 501 | if __name__ == '__main__': |
| 502 | unittest.main() |