Added a test verifying that the SimpleBuzzWrapper can post as an authorised user. Added a property file in oacurl format for the test account. Changed wrapper to use user_ids rather than full email addresses

This commit is contained in:
ade@google.com
2010-12-11 02:44:37 +00:00
parent 2b3ce69b96
commit a9907a2d67
6 changed files with 75 additions and 66 deletions

View File

@@ -40,7 +40,7 @@ def _abstract():
def _oauth_uri(name, discovery, params):
"""Look up the OAuth UR from the discovery
"""Look up the OAuth URI from the discovery
document and add query parameters based on
params.

View File

@@ -1,18 +1,8 @@
# Copyright (C) 2010 Google Inc.
#!/usr/bin/python2.4
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Copyright 2010 Google Inc. All Rights Reserved.
__author__ = 'ade@google.com'
__author__ = 'ade@google.com (Ade Oshineye)'
import apiclient.discovery
import logging

View File

@@ -1,16 +1,9 @@
# Copyright (C) 2010 Google Inc.
#!/usr/bin/python2.4
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Copyright 2010 Google Inc. All Rights Reserved.
__author__ = 'ade@google.com (Ade Oshineye)'
import buzz_gae_client
import logging
@@ -22,7 +15,7 @@ class SimpleBuzzWrapper(object):
self.builder = buzz_gae_client.BuzzGaeClient(consumer_key, consumer_secret, api_key=api_key)
if oauth_token and oauth_token_secret:
logging.info('Using api_client with authorisation')
logging.debug('Using api_client with authorisation')
oauth_params_dict = {}
oauth_params_dict['consumer_key'] = consumer_key
oauth_params_dict['consumer_secret'] = consumer_secret
@@ -30,7 +23,7 @@ class SimpleBuzzWrapper(object):
oauth_params_dict['oauth_token_secret'] = oauth_token_secret
self.api_client = self.builder.build_api_client(oauth_params=oauth_params_dict)
else:
logging.info('Using api_client that doesn\'t have authorisation')
logging.debug('Using api_client that doesn\'t have authorisation')
self.api_client = self.builder.build_api_client()
def search(self, query, user_token=None, max_results=10):
@@ -42,14 +35,10 @@ class SimpleBuzzWrapper(object):
return json['items']
return []
def post(self, sender, message_body):
def post(self, message_body, user_id='@me'):
if message_body is None or message_body.strip() is '':
return None
#TODO(ade) What happens with users who have hidden their email address?
# Maybe we should switch to @me so it won't matter?
user_id = sender.split('@')[0]
activities = self.api_client.activities()
logging.info('Retrieved activities for: %s' % user_id)
activity = activities.insert(userId=user_id, body={

View File

@@ -5,6 +5,11 @@
__author__ = 'ade@google.com (Ade Oshineye)'
from contrib.buzz.simple_buzz_wrapper import SimpleBuzzWrapper
import httplib2
import logging
import os
import pickle
import unittest
class SimpleBuzzWrapperTest(unittest.TestCase):
@@ -13,48 +18,69 @@ class SimpleBuzzWrapperTest(unittest.TestCase):
def test_wrapper_rejects_empty_post(self):
wrapper = SimpleBuzzWrapper()
self.assertEquals(None, wrapper.post('sender@example.org', ''))
self.assertEquals(None, wrapper.post('', '108242092577082601423'))
def test_wrapper_rejects_post_containing_only_whitespace(self):
wrapper = SimpleBuzzWrapper()
self.assertEquals(None, wrapper.post('sender@example.org', ' '))
self.assertEquals(None, wrapper.post(' ', '108242092577082601423'))
def test_wrapper_rejects_none_post(self):
wrapper = SimpleBuzzWrapper()
self.assertEquals(None, wrapper.post('sender@example.org', None))
self.assertEquals(None, wrapper.post(None, '108242092577082601423'))
def test_wrapper_rejects_empty_search(self):
wrapper = SimpleBuzzWrapper()
self.assertEquals(None, wrapper.search(''))
wrapper = SimpleBuzzWrapper()
self.assertEquals(None, wrapper.search(''))
def test_wrapper_rejects_search_containing_only_whitespace(self):
wrapper = SimpleBuzzWrapper()
self.assertEquals(None, wrapper.search(' '))
wrapper = SimpleBuzzWrapper()
self.assertEquals(None, wrapper.search(' '))
def test_wrapper_rejects_search_with_none(self):
wrapper = SimpleBuzzWrapper()
self.assertEquals(None, wrapper.search(None))
wrapper = SimpleBuzzWrapper()
self.assertEquals(None, wrapper.search(None))
class SimpleBuzzWrapperRemoteTest(unittest.TestCase):
# These tests make remote calls
def test_searching_returns_results(self):
wrapper = SimpleBuzzWrapper()
results = wrapper.search('oshineye')
self.assertTrue(results is not None)
def test_searching_honours_max_results(self):
wrapper = SimpleBuzzWrapper()
max = 5
results = wrapper.search('oshineye', max_results=max)
self.assertEquals(max, len(results))
def test_can_fetch_profile(self):
wrapper = SimpleBuzzWrapper()
profile = wrapper.get_profile('googlebuzz')
self.assertTrue(profile is not None)
profile = wrapper.get_profile(user_id = 'adewale')
self.assertTrue(profile is not None)
# These tests make remote calls
def __init__(self, method_name):
unittest.TestCase.__init__(self, method_name)
oauth_params_dict = {}
for line in open('./contrib_tests/test_account.oacurl.properties'):
line = line.strip()
if line.startswith('#'):
continue
key,value = line.split('=')
oauth_params_dict[key.strip()] = value.strip()
self.wrapper = SimpleBuzzWrapper(consumer_key=oauth_params_dict['consumerKey'],
consumer_secret=oauth_params_dict['consumerSecret'], oauth_token=oauth_params_dict['accessToken'],
oauth_token_secret=oauth_params_dict['accessTokenSecret'])
def test_searching_returns_results(self):
results = self.wrapper.search('oshineye')
self.assertTrue(results is not None)
def test_searching_honours_max_results(self):
max = 5
results = self.wrapper.search('oshineye', max_results=max)
self.assertEquals(max, len(results))
def test_can_fetch_profile(self):
profile = self.wrapper.get_profile('googlebuzz')
self.assertTrue(profile is not None)
profile = self.wrapper.get_profile(user_id='adewale')
self.assertTrue(profile is not None)
def test_can_post_without_user_id(self):
url = self.wrapper.post('test message')
self.assertTrue(url is not None)
self.assertTrue(url.startswith('http://www.google.com/buzz/'))
def test_can_post_with_user_id(self):
url = self.wrapper.post('test message', '108242092577082601423')
self.assertTrue(url is not None)
self.assertTrue(url.startswith('http://www.google.com/buzz/'))
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,6 @@
#Mon Oct 04 23:45:49 PDT 2010
#A set of credentials for posting as http://www.google.com/profiles/108242092577082601423
consumerSecret=anonymous
accessToken=1/80QKKG4CbMwOZjmW1udam-fVaiUOY1zO-8u3dhiLK6g
consumerKey=anonymous
accessTokenSecret=R6CnehJTZf9aKuSMtgkmX7KZ

View File

@@ -5,15 +5,13 @@
"""Discovery document tests
Functional tests that verify we can retrieve data from existing services.
These tests are read-only in order to ensure they're repeatable. They also
only work with publicly visible data in order to avoid dealing with OAuth.
"""
import httplib2
import pprint
__author__ = 'ade@google.com (Ade Oshineye)'
import httplib2
import pprint
from apiclient.discovery import build
import httplib2
import logging