blob: f9263a31ce9449fab14ffc6f5b1edc742067acad [file] [log] [blame]
ade@google.com46179d32010-08-21 00:00:03 +01001#!/usr/bin/python2.4
2#
3# Copyright 2010 Google Inc. All Rights Reserved.
4
5"""Discovery document tests
6
7Functional tests that verify we can retrieve data from existing services.
ade@google.com46179d32010-08-21 00:00:03 +01008"""
9
10__author__ = 'ade@google.com (Ade Oshineye)'
11
ade@google.coma9907a22010-12-11 02:44:37 +000012import httplib2
13import pprint
14
ade@google.com46179d32010-08-21 00:00:03 +010015from apiclient.discovery import build
ade@google.com2ab0de72010-09-27 23:26:54 +010016import httplib2
ade@google.com46179d32010-08-21 00:00:03 +010017import logging
ade@google.com6a05e7d2010-10-05 00:29:51 -070018import pickle
ade@google.com2ab0de72010-09-27 23:26:54 +010019import os
Joe Gregorio8963ff92010-10-11 13:14:43 -040020import time
ade@google.com46179d32010-08-21 00:00:03 +010021import unittest
22
ade@google.com46179d32010-08-21 00:00:03 +010023class BuzzFunctionalTest(unittest.TestCase):
Joe Gregoriob6964672011-05-26 22:02:23 -040024 def setUp(self):
25 self.buzz = build('buzz', 'v1', developerKey='AIzaSyD7aEm5tyC9BAdoC-MfL0ol7VV1P4zQgig')
26
ade@google.com4f2794e2010-10-11 02:11:48 -070027 def test_can_get_specific_activity(self):
Joe Gregoriob6964672011-05-26 22:02:23 -040028 activity = self.buzz.activities().get(userId='105037104815911535953',
ade@google.com4f2794e2010-10-11 02:11:48 -070029 postId='B:z12sspviqyakfvye123wehng0muwz5jzq04').execute()
30
31 self.assertTrue(activity is not None)
32
33 def test_can_get_specific_activity_with_tag_id(self):
Joe Gregoriob6964672011-05-26 22:02:23 -040034 activity = self.buzz.activities().get(userId='105037104815911535953',
ade@google.com4f2794e2010-10-11 02:11:48 -070035 postId='tag:google.com,2010:buzz:z13ptnw5usmnv15ey22fzlswnuqoebasu').execute()
36
37 self.assertTrue(activity is not None)
38
ade@google.com46179d32010-08-21 00:00:03 +010039 def test_can_get_buzz_activities_with_many_params(self):
ade@google.com46179d32010-08-21 00:00:03 +010040 max_results = 2
Joe Gregoriob6964672011-05-26 22:02:23 -040041 activities_command = self.buzz.activities()
ade@google.com4f2794e2010-10-11 02:11:48 -070042 activities = activities_command.list(userId='googlebuzz', scope='@self',
Joe Gregorioc5c5a372010-09-22 11:42:32 -040043 max_comments=max_results*2 ,max_liked=max_results*3,
Joe Gregoriodb849af2010-09-22 16:53:59 -040044 max_results=max_results).execute()
45 activity_count = len(activities['items'])
ade@google.com46179d32010-08-21 00:00:03 +010046 self.assertEquals(max_results, activity_count)
Joe Gregorioc5c5a372010-09-22 11:42:32 -040047
ade@google.com4f2794e2010-10-11 02:11:48 -070048 activities = activities_command.list_next(activities).execute()
Joe Gregoriodb849af2010-09-22 16:53:59 -040049 activity_count = len(activities['items'])
50 self.assertEquals(max_results, activity_count)
51
ade@google.com2ab0de72010-09-27 23:26:54 +010052 def test_can_get_multiple_pages_of_buzz_activities(self):
ade@google.com973d1a62010-09-23 21:21:21 +010053 max_results = 2
Joe Gregoriob6964672011-05-26 22:02:23 -040054 activities_command = self.buzz.activities()
Joe Gregorioc9359072010-10-25 15:26:13 -040055
ade@google.com4f2794e2010-10-11 02:11:48 -070056 activities = activities_command.list(userId='adewale', scope='@self',
ade@google.com973d1a62010-09-23 21:21:21 +010057 max_results=max_results).execute()
58 for count in range(10):
ade@google.com4f2794e2010-10-11 02:11:48 -070059 activities = activities_command.list_next(activities).execute()
ade@google.com973d1a62010-09-23 21:21:21 +010060 activity_count = len(activities['items'])
61 self.assertEquals(max_results, activity_count, 'Failed after %s pages' % str(count))
62
Joe Gregoriob6964672011-05-26 22:02:23 -040063 def IGNORE_test_can_get_multiple_pages_of_buzz_likers(self):
ade@google.com2ab0de72010-09-27 23:26:54 +010064 # Ignore this test until the Buzz API fixes the bug with next links
65 # http://code.google.com/p/google-buzz-api/issues/detail?id=114
ade@google.com2ab0de72010-09-27 23:26:54 +010066 max_results = 1
Joe Gregoriob6964672011-05-26 22:02:23 -040067 people_cmd = self.buzz.people()
ade@google.comf21095c2010-12-10 20:44:13 +000068 # The post https://www.googleapis.com/buzz/v1/activities/111062888259659218284/@self/B:z13nh535yk2syfob004cdjyb3mjeulcwv3c?alt=json#
69 #Perform this call https://www.googleapis.com/buzz/v1/activities/111062888259659218284/@self/B:z13nh535yk2syfob004cdjyb3mjeulcwv3c/@liked?alt=json&max-results=1
ade@google.com2ab0de72010-09-27 23:26:54 +010070 people = people_cmd.liked(groupId='@liked', userId='googlebuzz', scope='@self',
71 postId='B:z13nh535yk2syfob004cdjyb3mjeulcwv3c', max_results=max_results).execute()
72
73 for count in range(10):
Joe Gregoriob6964672011-05-26 22:02:23 -040074 print count
ade@google.com2ab0de72010-09-27 23:26:54 +010075 people = people_cmd.liked_next(people).execute()
76 people_count = len(people['items'])
77 self.assertEquals(max_results, people_count, 'Failed after %s pages' % str(count))
78
ade@google.comd6d6f2f2010-09-28 07:37:25 +010079 def test_can_get_user_profile(self):
Joe Gregoriob6964672011-05-26 22:02:23 -040080 person = self.buzz.people().get(userId='googlebuzz').execute()
ade@google.comd6d6f2f2010-09-28 07:37:25 +010081
82 self.assertTrue(person is not None)
83 self.assertEquals('buzz#person', person['kind'])
84 self.assertEquals('Google Buzz Team', person['displayName'])
85 self.assertEquals('111062888259659218284', person['id'])
ade@google.com14071072011-03-13 00:46:21 +000086 self.assertEquals('https://profiles.google.com/googlebuzz', person['profileUrl'])
ade@google.comd6d6f2f2010-09-28 07:37:25 +010087
ade@google.com51858532010-10-14 12:09:22 -050088 def test_can_get_user_profile_using_numeric_identifier(self):
Joe Gregoriob6964672011-05-26 22:02:23 -040089 person = self.buzz.people().get(userId='108242092577082601423').execute()
ade@google.com51858532010-10-14 12:09:22 -050090
91 self.assertTrue(person is not None)
92 self.assertEquals('buzz#person', person['kind'])
93 self.assertEquals('Test Account', person['displayName'])
94 self.assertEquals('108242092577082601423', person['id'])
ade@google.com14071072011-03-13 00:46:21 +000095 self.assertEquals('https://profiles.google.com/108242092577082601423', person['profileUrl'])
ade@google.com51858532010-10-14 12:09:22 -050096
ade@google.com567ee8d2010-10-06 06:13:18 -070097 def test_can_get_followees_of_user(self):
ade@google.com567ee8d2010-10-06 06:13:18 -070098 expected_followees = 30
Joe Gregoriob6964672011-05-26 22:02:23 -040099 following = self.buzz.people().list(userId='googlebuzz', groupId='@following', max_results=expected_followees).execute()
ade@google.com567ee8d2010-10-06 06:13:18 -0700100
101 self.assertEquals(expected_followees, following['totalResults'])
102 self.assertEquals(expected_followees, len(following['entry']))
103
104 def test_can_efficiently_get_follower_count_of_user(self):
ade@google.com567ee8d2010-10-06 06:13:18 -0700105
106 # Restricting max_results to 1 means only a tiny amount of data comes back but the totalResults still has the total.
Joe Gregoriob6964672011-05-26 22:02:23 -0400107 followers = self.buzz.people().list(userId='googlebuzz', groupId='@followers',
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500108 max_results='1').execute()
ade@google.com567ee8d2010-10-06 06:13:18 -0700109
110 # @googlebuzz has a large but fluctuating number of followers
111 # It is sufficient if the result is bigger than 10, 000
ade@google.come45ea6c2011-01-11 18:15:57 +0000112 follower_count = followers['totalResults']
ade@google.com567ee8d2010-10-06 06:13:18 -0700113 self.assertTrue(follower_count > 10000, follower_count)
114
ade@google.comd9427cf2011-01-03 19:18:44 +0000115 def test_follower_count_is_missing_for_user_with_hidden_follower_count(self):
Joe Gregoriob6964672011-05-26 22:02:23 -0400116 followers = self.buzz.people().list(userId='adewale', groupId='@followers').execute()
Joe Gregorioc9359072010-10-25 15:26:13 -0400117
ade@google.come45ea6c2011-01-11 18:15:57 +0000118 self.assertFalse('totalResults' in followers)
ade@google.com567ee8d2010-10-06 06:13:18 -0700119
ade@google.com75fdddd2010-09-29 16:44:00 +0100120
121class BuzzAuthenticatedFunctionalTest(unittest.TestCase):
ade@google.com6a05e7d2010-10-05 00:29:51 -0700122 def __init__(self, method_name):
123 unittest.TestCase.__init__(self, method_name)
124 credentials_dir = os.path.join(logging.os.path.dirname(__file__), './data')
125 f = file(os.path.join(credentials_dir, 'buzz_credentials.dat'), 'r')
126 credentials = pickle.loads(f.read())
127 f.close()
ade@google.com75fdddd2010-09-29 16:44:00 +0100128
ade@google.com6a05e7d2010-10-05 00:29:51 -0700129 self.http = credentials.authorize(httplib2.Http())
Joe Gregoriob6964672011-05-26 22:02:23 -0400130 self.buzz = build('buzz', 'v1', http=self.http, developerKey='AIzaSyD7aEm5tyC9BAdoC-MfL0ol7VV1P4zQgig')
ade@google.com6a05e7d2010-10-05 00:29:51 -0700131
ade@google.com4f2794e2010-10-11 02:11:48 -0700132 def test_can_create_activity(self):
ade@google.com4f2794e2010-10-11 02:11:48 -0700133
Joe Gregoriob6964672011-05-26 22:02:23 -0400134 activity = self.buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500135 'data': {
136 'title': 'Testing insert',
137 'object': {
138 'content': u'Just a short note to show that insert is working. ?',
139 'type': 'note'}
140 }
141 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700142 ).execute()
143 self.assertTrue(activity is not None)
144
Joe Gregorioc11bec62011-06-06 09:43:00 -0400145 def test_fields_parameter_restricts_response_fields(self):
146 activity = self.buzz.activities().insert(userId='@me', body={
147 'data': {
148 'title': 'Testing patch',
149 'object': {
150 'content': u'Just a short note to show that insert is working. ?',
151 'type': 'note'}
152 }
153 }
154 ).execute()
155 self.assertTrue('kind' in activity)
156
157 # test fields to restrict what is returned
158 activity = self.buzz.activities().get(userId='@me', postId=activity['id'],
159 fields='object,id').execute()
160 self.assertTrue('kind' not in activity)
161 self.assertTrue('object' in activity)
162 self.assertTrue('id' in activity)
163
164 def test_patch(self):
165 activity = self.buzz.activities().insert(userId='@me', body={
166 'data': {
167 'title': 'Testing patch',
168 'object': {
169 'content': u'Just a short note to show that insert is working. ?',
170 'type': 'note'}
171 }
172 }).execute()
173 # Construct a raw patch to send, also restrict the response with fields
174 activity = self.buzz.activities().patch(userId='@me',
175 scope='@self',
176 postId=activity['id'],
177 body={
178 'object': {
179 'content': 'Updated content only!'}},
180 fields='object').execute()
181 self.assertEquals(activity['object']['content'], 'Updated content only!')
182 self.assertTrue('id' not in activity)
183
ade@google.com9d821b02010-10-11 03:33:51 -0700184 def test_can_create_private_activity(self):
Joe Gregoriob6964672011-05-26 22:02:23 -0400185 activity = self.buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500186 'data': {
187 'title': 'Testing insert',
188 'object': {
189 'content': 'This is a private post.'
190 },
191 'visibility': {
192 'entries': [
193 { 'id': 'tag:google.com,2010:buzz-group:108242092577082601423:13' }
194 ]
195 }
196 }
197 }
ade@google.com9d821b02010-10-11 03:33:51 -0700198 ).execute()
199 self.assertTrue(activity is not None)
200
ade@google.com41b26032010-10-20 17:37:18 -0700201 def test_can_create_and_delete_new_group(self):
ade@google.com41b26032010-10-20 17:37:18 -0700202 group_name = 'New Group Created At' + str(time.time())
Joe Gregoriob6964672011-05-26 22:02:23 -0400203 group = self.buzz.groups().insert(userId='@me', body = {
ade@google.com41b26032010-10-20 17:37:18 -0700204 'data': {
205 'title': group_name
206 }
207 }).execute()
208 self.assertTrue(group is not None)
209
Joe Gregoriob6964672011-05-26 22:02:23 -0400210 result = self.buzz.groups().delete(userId='@me', groupId=group['id']).execute()
ade@google.com41b26032010-10-20 17:37:18 -0700211 self.assertEquals({}, result)
212
ade@google.com4f2794e2010-10-11 02:11:48 -0700213 def test_can_identify_number_of_groups_belonging_to_user(self):
Joe Gregoriob6964672011-05-26 22:02:23 -0400214 groups = self.buzz.groups().list(userId='108242092577082601423').execute()
ade@google.com6a05e7d2010-10-05 00:29:51 -0700215
ade@google.com41b26032010-10-20 17:37:18 -0700216 # This should work as long as no-one deletes the 4 default groups for this test account
ade@google.com6a05e7d2010-10-05 00:29:51 -0700217 expected_default_number_of_groups = 4
ade@google.com41b26032010-10-20 17:37:18 -0700218 self.assertTrue(len(groups['items']) > expected_default_number_of_groups)
ade@google.com75fdddd2010-09-29 16:44:00 +0100219
ade@google.com4f2794e2010-10-11 02:11:48 -0700220 def IGNORE__test_can_like_activity(self):
Joe Gregoriob6964672011-05-26 22:02:23 -0400221 activity = self.buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500222 'data': {
223 'title': 'Testing insert',
224 'object': {
225 'content': u'Just a short note to show that insert is working. ?',
226 'type': 'note'}
227 }
228 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700229 ).execute()
230 pprint.pprint(activity)
231 id = activity['id']
Joe Gregoriob6964672011-05-26 22:02:23 -0400232 likers = self.buzz.people().liked(userId='105037104815911535953', postId=id, groupId='@liked', scope='@self').execute()
ade@google.com4f2794e2010-10-11 02:11:48 -0700233 # Todo(ade) Insert the new liker once the Buzz back-end bug is fixed
234
235 def test_can_comment_on_activity(self):
Joe Gregoriob6964672011-05-26 22:02:23 -0400236 activity = self.buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500237 'data': {
238 'title': 'A new activity',
239 'object': {
240 'content': u'The body of the new activity',
241 'type': 'note'}
242 }
243 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700244 ).execute()
245
246 id = activity['id']
Joe Gregoriob6964672011-05-26 22:02:23 -0400247 comment = self.buzz.comments().insert(userId='@me', postId=id, body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500248 'data': {
249 'content': 'A comment on the new activity'
250 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700251 }).execute()
252
ade@google.comc36ac532010-10-25 15:22:50 -0400253 def test_can_list_groups_belonging_to_user(self):
Joe Gregoriob6964672011-05-26 22:02:23 -0400254 groups = self.buzz.groups().list(userId='108242092577082601423').execute()
ade@google.com4f2794e2010-10-11 02:11:48 -0700255
Joe Gregoriob6964672011-05-26 22:02:23 -0400256 group = self.buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:15').execute()
ade@google.com4f2794e2010-10-11 02:11:48 -0700257 self.assertEquals('G:108242092577082601423:15', group['id'], group)
258
Joe Gregoriob6964672011-05-26 22:02:23 -0400259 group = self.buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:14').execute()
ade@google.com4f2794e2010-10-11 02:11:48 -0700260 self.assertEquals('G:108242092577082601423:14', group['id'], group)
261
Joe Gregoriob6964672011-05-26 22:02:23 -0400262 group = self.buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:13').execute()
ade@google.com4f2794e2010-10-11 02:11:48 -0700263 self.assertEquals('G:108242092577082601423:13', group['id'], group)
264
Joe Gregoriob6964672011-05-26 22:02:23 -0400265 group = self.buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:6').execute()
ade@google.com4f2794e2010-10-11 02:11:48 -0700266 self.assertEquals('G:108242092577082601423:6', group['id'], group)
267
ade@google.com9d821b02010-10-11 03:33:51 -0700268 def test_can_delete_activity(self):
Joe Gregoriob6964672011-05-26 22:02:23 -0400269 activity = self.buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500270 'data': {
271 'title': 'Activity to be deleted',
272 'object': {
273 'content': u'Created this activity so that it can be deleted.',
274 'type': 'note'}
275 }
276 }
ade@google.com9d821b02010-10-11 03:33:51 -0700277 ).execute()
278 id = activity['id']
279
Joe Gregoriob6964672011-05-26 22:02:23 -0400280 self.buzz.activities().delete(scope='@self', userId='@me', postId=id).execute()
Joe Gregorio8963ff92010-10-11 13:14:43 -0400281 time.sleep(2)
ade@google.com9d821b02010-10-11 03:33:51 -0700282
283 activity_url = activity['links']['self'][0]['href']
284 resp, content = self.http.request(activity_url, 'GET')
285 self.assertEquals(404, resp.status)
ade@google.com75fdddd2010-09-29 16:44:00 +0100286
Joe Gregoriodb849af2010-09-22 16:53:59 -0400287if __name__ == '__main__':
288 unittest.main()