blob: da4672aa9adce4eb0fea666cb02c92698afb4bd1 [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.com2ab0de72010-09-27 23:26:54 +010023# TODO(ade) Remove this mock once the bug in the discovery document is fixed
24DATA_DIR = os.path.join(logging.os.path.dirname(__file__), '../tests/data')
25class HttpMock(object):
26
27 def __init__(self, filename, headers):
28 f = file(os.path.join(DATA_DIR, filename), 'r')
29 self.data = f.read()
30 f.close()
31 self.headers = headers
32
33 def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None):
34 return httplib2.Response(self.headers), self.data
35
ade@google.com46179d32010-08-21 00:00:03 +010036class BuzzFunctionalTest(unittest.TestCase):
ade@google.com4f2794e2010-10-11 02:11:48 -070037 def test_can_get_specific_activity(self):
38 buzz = build('buzz', 'v1')
39 activity = buzz.activities().get(userId='105037104815911535953',
40 postId='B:z12sspviqyakfvye123wehng0muwz5jzq04').execute()
41
42 self.assertTrue(activity is not None)
43
44 def test_can_get_specific_activity_with_tag_id(self):
45 buzz = build('buzz', 'v1')
46 activity = buzz.activities().get(userId='105037104815911535953',
47 postId='tag:google.com,2010:buzz:z13ptnw5usmnv15ey22fzlswnuqoebasu').execute()
48
49 self.assertTrue(activity is not None)
50
ade@google.com46179d32010-08-21 00:00:03 +010051 def test_can_get_buzz_activities_with_many_params(self):
52 buzz = build('buzz', 'v1')
53 max_results = 2
ade@google.com4f2794e2010-10-11 02:11:48 -070054 activities_command = buzz.activities()
55 activities = activities_command.list(userId='googlebuzz', scope='@self',
Joe Gregorioc5c5a372010-09-22 11:42:32 -040056 max_comments=max_results*2 ,max_liked=max_results*3,
Joe Gregoriodb849af2010-09-22 16:53:59 -040057 max_results=max_results).execute()
58 activity_count = len(activities['items'])
ade@google.com46179d32010-08-21 00:00:03 +010059 self.assertEquals(max_results, activity_count)
Joe Gregorioc5c5a372010-09-22 11:42:32 -040060
ade@google.com4f2794e2010-10-11 02:11:48 -070061 activities = activities_command.list_next(activities).execute()
Joe Gregoriodb849af2010-09-22 16:53:59 -040062 activity_count = len(activities['items'])
63 self.assertEquals(max_results, activity_count)
64
ade@google.com2ab0de72010-09-27 23:26:54 +010065 def test_can_get_multiple_pages_of_buzz_activities(self):
ade@google.com973d1a62010-09-23 21:21:21 +010066 buzz = build('buzz', 'v1')
67 max_results = 2
ade@google.com4f2794e2010-10-11 02:11:48 -070068 activities_command = buzz.activities()
Joe Gregorioc9359072010-10-25 15:26:13 -040069
ade@google.com4f2794e2010-10-11 02:11:48 -070070 activities = activities_command.list(userId='adewale', scope='@self',
ade@google.com973d1a62010-09-23 21:21:21 +010071 max_results=max_results).execute()
72 for count in range(10):
ade@google.com4f2794e2010-10-11 02:11:48 -070073 activities = activities_command.list_next(activities).execute()
ade@google.com973d1a62010-09-23 21:21:21 +010074 activity_count = len(activities['items'])
75 self.assertEquals(max_results, activity_count, 'Failed after %s pages' % str(count))
76
ade@google.com2ab0de72010-09-27 23:26:54 +010077 def IGNORE__test_can_get_multiple_pages_of_buzz_likers(self):
78 # Ignore this test until the Buzz API fixes the bug with next links
79 # http://code.google.com/p/google-buzz-api/issues/detail?id=114
80 self.http = HttpMock('buzz.json', {'status': '200'})
81 buzz = build('buzz', 'v1', self.http)
82 max_results = 1
83 people_cmd = buzz.people()
ade@google.comf21095c2010-12-10 20:44:13 +000084 # The post https://www.googleapis.com/buzz/v1/activities/111062888259659218284/@self/B:z13nh535yk2syfob004cdjyb3mjeulcwv3c?alt=json#
85 #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 +010086 people = people_cmd.liked(groupId='@liked', userId='googlebuzz', scope='@self',
87 postId='B:z13nh535yk2syfob004cdjyb3mjeulcwv3c', max_results=max_results).execute()
88
89 for count in range(10):
90 people = people_cmd.liked_next(people).execute()
91 people_count = len(people['items'])
92 self.assertEquals(max_results, people_count, 'Failed after %s pages' % str(count))
93
ade@google.comd6d6f2f2010-09-28 07:37:25 +010094 def test_can_get_user_profile(self):
95 buzz = build('buzz', 'v1')
96 person = buzz.people().get(userId='googlebuzz').execute()
97
98 self.assertTrue(person is not None)
99 self.assertEquals('buzz#person', person['kind'])
100 self.assertEquals('Google Buzz Team', person['displayName'])
101 self.assertEquals('111062888259659218284', person['id'])
102 self.assertEquals('http://www.google.com/profiles/googlebuzz', person['profileUrl'])
103
ade@google.com51858532010-10-14 12:09:22 -0500104 def test_can_get_user_profile_using_numeric_identifier(self):
105 buzz = build('buzz', 'v1')
106 person = buzz.people().get(userId='108242092577082601423').execute()
107
108 self.assertTrue(person is not None)
109 self.assertEquals('buzz#person', person['kind'])
110 self.assertEquals('Test Account', person['displayName'])
111 self.assertEquals('108242092577082601423', person['id'])
112 self.assertEquals('http://www.google.com/profiles/108242092577082601423', person['profileUrl'])
113
ade@google.com567ee8d2010-10-06 06:13:18 -0700114 def test_can_get_followees_of_user(self):
115 buzz = build('buzz', 'v1')
116 expected_followees = 30
117 following = buzz.people().list(userId='googlebuzz', groupId='@following', max_results=expected_followees).execute()
118
119 self.assertEquals(expected_followees, following['totalResults'])
120 self.assertEquals(expected_followees, len(following['entry']))
121
122 def test_can_efficiently_get_follower_count_of_user(self):
123 buzz = build('buzz', 'v1')
124
125 # Restricting max_results to 1 means only a tiny amount of data comes back but the totalResults still has the total.
ade@google.come45ea6c2011-01-11 18:15:57 +0000126 followers = buzz.people().list(userId='googlebuzz', groupId='@followers',
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500127 max_results='1').execute()
ade@google.com567ee8d2010-10-06 06:13:18 -0700128
129 # @googlebuzz has a large but fluctuating number of followers
130 # It is sufficient if the result is bigger than 10, 000
ade@google.come45ea6c2011-01-11 18:15:57 +0000131 follower_count = followers['totalResults']
ade@google.com567ee8d2010-10-06 06:13:18 -0700132 self.assertTrue(follower_count > 10000, follower_count)
133
ade@google.comd9427cf2011-01-03 19:18:44 +0000134 def test_follower_count_is_missing_for_user_with_hidden_follower_count(self):
ade@google.com567ee8d2010-10-06 06:13:18 -0700135 buzz = build('buzz', 'v1')
ade@google.come45ea6c2011-01-11 18:15:57 +0000136 followers = buzz.people().list(userId='adewale', groupId='@followers').execute()
Joe Gregorioc9359072010-10-25 15:26:13 -0400137
ade@google.come45ea6c2011-01-11 18:15:57 +0000138 self.assertFalse('totalResults' in followers)
ade@google.com567ee8d2010-10-06 06:13:18 -0700139
ade@google.com75fdddd2010-09-29 16:44:00 +0100140
141class BuzzAuthenticatedFunctionalTest(unittest.TestCase):
ade@google.com6a05e7d2010-10-05 00:29:51 -0700142 def __init__(self, method_name):
143 unittest.TestCase.__init__(self, method_name)
144 credentials_dir = os.path.join(logging.os.path.dirname(__file__), './data')
145 f = file(os.path.join(credentials_dir, 'buzz_credentials.dat'), 'r')
146 credentials = pickle.loads(f.read())
147 f.close()
ade@google.com75fdddd2010-09-29 16:44:00 +0100148
ade@google.com6a05e7d2010-10-05 00:29:51 -0700149 self.http = credentials.authorize(httplib2.Http())
150
ade@google.com4f2794e2010-10-11 02:11:48 -0700151 def test_can_create_activity(self):
152 buzz = build('buzz', 'v1', http=self.http)
153
154 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500155 'data': {
156 'title': 'Testing insert',
157 'object': {
158 'content': u'Just a short note to show that insert is working. ?',
159 'type': 'note'}
160 }
161 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700162 ).execute()
163 self.assertTrue(activity is not None)
164
ade@google.com9d821b02010-10-11 03:33:51 -0700165 def test_can_create_private_activity(self):
166 buzz = build('buzz', 'v1', http=self.http)
167
168 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500169 'data': {
170 'title': 'Testing insert',
171 'object': {
172 'content': 'This is a private post.'
173 },
174 'visibility': {
175 'entries': [
176 { 'id': 'tag:google.com,2010:buzz-group:108242092577082601423:13' }
177 ]
178 }
179 }
180 }
ade@google.com9d821b02010-10-11 03:33:51 -0700181 ).execute()
182 self.assertTrue(activity is not None)
183
ade@google.com41b26032010-10-20 17:37:18 -0700184 def test_can_create_and_delete_new_group(self):
185 buzz = build('buzz', 'v1', http=self.http)
186 group_name = 'New Group Created At' + str(time.time())
187 group = buzz.groups().insert(userId='@me', body = {
188 'data': {
189 'title': group_name
190 }
191 }).execute()
192 self.assertTrue(group is not None)
193
194 result = buzz.groups().delete(userId='@me', groupId=group['id']).execute()
195 self.assertEquals({}, result)
196
ade@google.com4f2794e2010-10-11 02:11:48 -0700197 def test_can_identify_number_of_groups_belonging_to_user(self):
ade@google.com6a05e7d2010-10-05 00:29:51 -0700198 buzz = build('buzz', 'v1', http=self.http)
199 groups = buzz.groups().list(userId='108242092577082601423').execute()
200
ade@google.com41b26032010-10-20 17:37:18 -0700201 # 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 -0700202 expected_default_number_of_groups = 4
ade@google.com41b26032010-10-20 17:37:18 -0700203 self.assertTrue(len(groups['items']) > expected_default_number_of_groups)
ade@google.com75fdddd2010-09-29 16:44:00 +0100204
ade@google.com4f2794e2010-10-11 02:11:48 -0700205 def IGNORE__test_can_like_activity(self):
206 buzz = build('buzz', 'v1', http=self.http)
207 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500208 'data': {
209 'title': 'Testing insert',
210 'object': {
211 'content': u'Just a short note to show that insert is working. ?',
212 'type': 'note'}
213 }
214 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700215 ).execute()
216 pprint.pprint(activity)
217 id = activity['id']
218 likers = buzz.people().liked(userId='105037104815911535953', postId=id, groupId='@liked', scope='@self').execute()
219 # Todo(ade) Insert the new liker once the Buzz back-end bug is fixed
220
221 def test_can_comment_on_activity(self):
222 buzz = build('buzz', 'v1', http=self.http)
223
224 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500225 'data': {
226 'title': 'A new activity',
227 'object': {
228 'content': u'The body of the new activity',
229 'type': 'note'}
230 }
231 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700232 ).execute()
233
234 id = activity['id']
235 comment = buzz.comments().insert(userId='@me', postId=id, body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500236 'data': {
237 'content': 'A comment on the new activity'
238 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700239 }).execute()
240
ade@google.comc36ac532010-10-25 15:22:50 -0400241 def test_can_list_groups_belonging_to_user(self):
ade@google.com4f2794e2010-10-11 02:11:48 -0700242 buzz = build('buzz', 'v1', http=self.http)
243 groups = buzz.groups().list(userId='108242092577082601423').execute()
ade@google.com4f2794e2010-10-11 02:11:48 -0700244
245 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:15').execute()
246 self.assertEquals('G:108242092577082601423:15', group['id'], group)
247
248 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:14').execute()
249 self.assertEquals('G:108242092577082601423:14', group['id'], group)
250
251 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:13').execute()
252 self.assertEquals('G:108242092577082601423:13', group['id'], group)
253
254 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:6').execute()
255 self.assertEquals('G:108242092577082601423:6', group['id'], group)
256
ade@google.com9d821b02010-10-11 03:33:51 -0700257 def test_can_delete_activity(self):
258 buzz = build('buzz', 'v1', http=self.http)
Joe Gregorio8963ff92010-10-11 13:14:43 -0400259
ade@google.com9d821b02010-10-11 03:33:51 -0700260 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500261 'data': {
262 'title': 'Activity to be deleted',
263 'object': {
264 'content': u'Created this activity so that it can be deleted.',
265 'type': 'note'}
266 }
267 }
ade@google.com9d821b02010-10-11 03:33:51 -0700268 ).execute()
269 id = activity['id']
270
271 buzz.activities().delete(scope='@self', userId='@me', postId=id).execute()
Joe Gregorio8963ff92010-10-11 13:14:43 -0400272 time.sleep(2)
ade@google.com9d821b02010-10-11 03:33:51 -0700273
274 activity_url = activity['links']['self'][0]['href']
275 resp, content = self.http.request(activity_url, 'GET')
276 self.assertEquals(404, resp.status)
ade@google.com75fdddd2010-09-29 16:44:00 +0100277
Joe Gregoriodb849af2010-09-22 16:53:59 -0400278if __name__ == '__main__':
279 unittest.main()