def test_decorator_from_cached_client_secrets(self): cache_mock = http_mock.CacheMock() load_and_cache('client_secrets.json', 'secret', cache_mock) decorator = appengine.OAuth2DecoratorFromClientSecrets( # filename, scope, message=None, cache=None 'secret', '', cache=cache_mock) self.assertFalse(decorator._in_error)
def test_decorator_from_unfilled_client_secrets_aware(self): MESSAGE = 'File is missing' try: appengine.OAuth2DecoratorFromClientSecrets( datafile('unfilled_client_secrets.json'), scope=['foo_scope', 'bar_scope'], message=MESSAGE) except clientsecrets.InvalidClientSecretsError: pass
def test_decorator_from_client_secrets_not_logged_in_aware(self): decorator = appengine.OAuth2DecoratorFromClientSecrets( datafile('client_secrets.json'), scope=['foo_scope', 'bar_scope'], message='NotLoggedInMessage') self.decorator = decorator self._finish_setup(decorator, user_mock=UserNotLoggedInMock) # An initial request to an oauth_aware decorated path should be a # redirect to login. response = self.app.get('/bar_path/2012/03') self.assertTrue(response.status.startswith('302')) self.assertTrue('Login' in str(response))
def test_decorator_from_client_secrets_bad_type(self): # NOTE: this code path is not currently reachable, as the only types # that oauth2client.clientsecrets can load is web and installed, so # this test forces execution of this code path. Despite not being # normally reachable, this should remain in case future types of # credentials are added. loadfile_patch = mock.patch( 'oauth2client.contrib.appengine.clientsecrets.loadfile') with loadfile_patch as loadfile_mock: loadfile_mock.return_value = ('badtype', None) with self.assertRaises(clientsecrets.InvalidClientSecretsError): appengine.OAuth2DecoratorFromClientSecrets( 'doesntmatter.json', scope=['foo_scope', 'bar_scope'])
def read_tokens(): tokens_file = open('tokens/tokens.json', 'r') json = from_json(tokens_file.read()) tokens_file.close() tokens.flickr_api_key = json[u'flickr_api_key'] tokens.google_api_key = json[u'google_api_key'] tokens.jwt_token = json[u'jwt_token'] oauth_tokens_path = os.path.join(os.path.dirname(__file__), 'oauth_tokens.json') tokens.google_oauth_decorator = appengine.OAuth2DecoratorFromClientSecrets( oauth_tokens_path, scope=['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'], message='Check that the file `{}` exists in your project'.format(oauth_tokens_path), callback_path='/agendamlg-api/oauth2callback' )
def test_decorator_from_client_secrets(self, new_http): new_http.return_value = http_mock.HttpMock(data=DEFAULT_RESP) # Execute test after setting up mock. decorator = appengine.OAuth2DecoratorFromClientSecrets( datafile('client_secrets.json'), scope=['foo_scope', 'bar_scope']) self._finish_setup(decorator, user_mock=UserMock) self.assertFalse(decorator._in_error) self.decorator = decorator self.test_required() http = self.decorator.http() self.assertEquals('foo_access_token', http.request.credentials.access_token) # revoke_uri is not required self.assertEqual(self.decorator._revoke_uri, 'https://oauth2.googleapis.com/revoke') self.assertEqual(self.decorator._revoke_uri, self.decorator.credentials.revoke_uri) # Check the mocks were called. new_http.assert_called_once_with()
def test_decorator_from_client_secrets_with_optional_settings(self): # Test that the decorator works with the absense of a revoke_uri in # the client secrets. loadfile_patch = mock.patch( 'oauth2client.contrib.appengine.clientsecrets.loadfile') with loadfile_patch as loadfile_mock: loadfile_mock.return_value = (clientsecrets.TYPE_WEB, { "client_id": "foo_client_id", "client_secret": "foo_client_secret", "redirect_uris": [], "auth_uri": "https://accounts.google.com/o/oauth2/v2/auth", "token_uri": "https://www.googleapis.com/oauth2/v4/token", # No revoke URI }) decorator = appengine.OAuth2DecoratorFromClientSecrets( 'doesntmatter.json', scope=['foo_scope', 'bar_scope']) self.assertEqual(decorator._revoke_uri, oauth2client.GOOGLE_REVOKE_URI) # This is never set, but it's consistent with other tests. self.assertFalse(decorator._in_error)
def test_decorator_from_client_secrets_with_optional_settings(self): # Test that the decorator works with the absense of a revoke_uri in # the client secrets. loadfile_patch = mock.patch( 'oauth2client.contrib.appengine.clientsecrets.loadfile') with loadfile_patch as loadfile_mock: loadfile_mock.return_value = ( clientsecrets.TYPE_WEB, { 'client_id': 'foo_client_id', 'client_secret': 'foo_client_secret', 'redirect_uris': [], 'auth_uri': oauth2client.GOOGLE_AUTH_URI, 'token_uri': oauth2client.GOOGLE_TOKEN_URI, # No revoke URI }) decorator = appengine.OAuth2DecoratorFromClientSecrets( 'doesntmatter.json', scope=['foo_scope', 'bar_scope']) self.assertEqual(decorator._revoke_uri, oauth2client.GOOGLE_REVOKE_URI) # This is never set, but it's consistent with other tests. self.assertFalse(decorator._in_error)
def test_decorator_from_client_secrets_kwargs(self): decorator = appengine.OAuth2DecoratorFromClientSecrets( datafile('client_secrets.json'), scope=['foo_scope', 'bar_scope'], prompt='consent') self.assertIn('prompt', decorator._kwargs)