blob: ecb87134b889e2afe883ab0e16e9bf46a1053892 [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.
8
9These tests are read-only in order to ensure they're repeatable. They also
10only work with publicly visible data in order to avoid dealing with OAuth.
11"""
ade@google.com2ab0de72010-09-27 23:26:54 +010012import httplib2
ade@google.com4f2794e2010-10-11 02:11:48 -070013import pprint
ade@google.com46179d32010-08-21 00:00:03 +010014
15__author__ = 'ade@google.com (Ade Oshineye)'
16
17from apiclient.discovery import build
ade@google.com2ab0de72010-09-27 23:26:54 +010018import httplib2
ade@google.com46179d32010-08-21 00:00:03 +010019import logging
ade@google.com6a05e7d2010-10-05 00:29:51 -070020import pickle
ade@google.com2ab0de72010-09-27 23:26:54 +010021import os
Joe Gregorio8963ff92010-10-11 13:14:43 -040022import time
ade@google.com46179d32010-08-21 00:00:03 +010023import unittest
24
ade@google.com2ab0de72010-09-27 23:26:54 +010025# TODO(ade) Remove this mock once the bug in the discovery document is fixed
26DATA_DIR = os.path.join(logging.os.path.dirname(__file__), '../tests/data')
27class HttpMock(object):
28
29 def __init__(self, filename, headers):
30 f = file(os.path.join(DATA_DIR, filename), 'r')
31 self.data = f.read()
32 f.close()
33 self.headers = headers
34
35 def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None):
36 return httplib2.Response(self.headers), self.data
37
ade@google.com46179d32010-08-21 00:00:03 +010038class BuzzFunctionalTest(unittest.TestCase):
ade@google.com4f2794e2010-10-11 02:11:48 -070039 def test_can_get_specific_activity(self):
40 buzz = build('buzz', 'v1')
41 activity = buzz.activities().get(userId='105037104815911535953',
42 postId='B:z12sspviqyakfvye123wehng0muwz5jzq04').execute()
43
44 self.assertTrue(activity is not None)
45
46 def test_can_get_specific_activity_with_tag_id(self):
47 buzz = build('buzz', 'v1')
48 activity = buzz.activities().get(userId='105037104815911535953',
49 postId='tag:google.com,2010:buzz:z13ptnw5usmnv15ey22fzlswnuqoebasu').execute()
50
51 self.assertTrue(activity is not None)
52
ade@google.com46179d32010-08-21 00:00:03 +010053 def test_can_get_buzz_activities_with_many_params(self):
54 buzz = build('buzz', 'v1')
55 max_results = 2
ade@google.com4f2794e2010-10-11 02:11:48 -070056 activities_command = buzz.activities()
57 activities = activities_command.list(userId='googlebuzz', scope='@self',
Joe Gregorioc5c5a372010-09-22 11:42:32 -040058 max_comments=max_results*2 ,max_liked=max_results*3,
Joe Gregoriodb849af2010-09-22 16:53:59 -040059 max_results=max_results).execute()
60 activity_count = len(activities['items'])
ade@google.com46179d32010-08-21 00:00:03 +010061 self.assertEquals(max_results, activity_count)
Joe Gregorioc5c5a372010-09-22 11:42:32 -040062
ade@google.com4f2794e2010-10-11 02:11:48 -070063 activities = activities_command.list_next(activities).execute()
Joe Gregoriodb849af2010-09-22 16:53:59 -040064 activity_count = len(activities['items'])
65 self.assertEquals(max_results, activity_count)
66
ade@google.com2ab0de72010-09-27 23:26:54 +010067 def test_can_get_multiple_pages_of_buzz_activities(self):
ade@google.com973d1a62010-09-23 21:21:21 +010068 buzz = build('buzz', 'v1')
69 max_results = 2
ade@google.com4f2794e2010-10-11 02:11:48 -070070 activities_command = buzz.activities()
Joe Gregorioc9359072010-10-25 15:26:13 -040071
ade@google.com4f2794e2010-10-11 02:11:48 -070072 activities = activities_command.list(userId='adewale', scope='@self',
ade@google.com973d1a62010-09-23 21:21:21 +010073 max_results=max_results).execute()
74 for count in range(10):
ade@google.com4f2794e2010-10-11 02:11:48 -070075 activities = activities_command.list_next(activities).execute()
ade@google.com973d1a62010-09-23 21:21:21 +010076 activity_count = len(activities['items'])
77 self.assertEquals(max_results, activity_count, 'Failed after %s pages' % str(count))
78
ade@google.com2ab0de72010-09-27 23:26:54 +010079 def IGNORE__test_can_get_multiple_pages_of_buzz_likers(self):
80 # Ignore this test until the Buzz API fixes the bug with next links
81 # http://code.google.com/p/google-buzz-api/issues/detail?id=114
82 self.http = HttpMock('buzz.json', {'status': '200'})
83 buzz = build('buzz', 'v1', self.http)
84 max_results = 1
85 people_cmd = buzz.people()
ade@google.comf21095c2010-12-10 20:44:13 +000086 # The post https://www.googleapis.com/buzz/v1/activities/111062888259659218284/@self/B:z13nh535yk2syfob004cdjyb3mjeulcwv3c?alt=json#
87 #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 +010088 people = people_cmd.liked(groupId='@liked', userId='googlebuzz', scope='@self',
89 postId='B:z13nh535yk2syfob004cdjyb3mjeulcwv3c', max_results=max_results).execute()
90
91 for count in range(10):
92 people = people_cmd.liked_next(people).execute()
93 people_count = len(people['items'])
94 self.assertEquals(max_results, people_count, 'Failed after %s pages' % str(count))
95
ade@google.comd6d6f2f2010-09-28 07:37:25 +010096 def test_can_get_user_profile(self):
97 buzz = build('buzz', 'v1')
98 person = buzz.people().get(userId='googlebuzz').execute()
99
100 self.assertTrue(person is not None)
101 self.assertEquals('buzz#person', person['kind'])
102 self.assertEquals('Google Buzz Team', person['displayName'])
103 self.assertEquals('111062888259659218284', person['id'])
104 self.assertEquals('http://www.google.com/profiles/googlebuzz', person['profileUrl'])
105
ade@google.com51858532010-10-14 12:09:22 -0500106 def test_can_get_user_profile_using_numeric_identifier(self):
107 buzz = build('buzz', 'v1')
108 person = buzz.people().get(userId='108242092577082601423').execute()
109
110 self.assertTrue(person is not None)
111 self.assertEquals('buzz#person', person['kind'])
112 self.assertEquals('Test Account', person['displayName'])
113 self.assertEquals('108242092577082601423', person['id'])
114 self.assertEquals('http://www.google.com/profiles/108242092577082601423', person['profileUrl'])
115
ade@google.com567ee8d2010-10-06 06:13:18 -0700116 def test_can_get_followees_of_user(self):
117 buzz = build('buzz', 'v1')
118 expected_followees = 30
119 following = buzz.people().list(userId='googlebuzz', groupId='@following', max_results=expected_followees).execute()
120
121 self.assertEquals(expected_followees, following['totalResults'])
122 self.assertEquals(expected_followees, len(following['entry']))
123
124 def test_can_efficiently_get_follower_count_of_user(self):
125 buzz = build('buzz', 'v1')
126
127 # Restricting max_results to 1 means only a tiny amount of data comes back but the totalResults still has the total.
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500128 following = buzz.people().list(userId='googlebuzz', groupId='@followers',
129 max_results='1').execute()
ade@google.com567ee8d2010-10-06 06:13:18 -0700130
131 # @googlebuzz has a large but fluctuating number of followers
132 # It is sufficient if the result is bigger than 10, 000
133 follower_count = following['totalResults']
134 self.assertTrue(follower_count > 10000, follower_count)
135
136 def test_follower_count_is_zero_for_user_with_hidden_follower_count(self):
137 buzz = build('buzz', 'v1')
138 following = buzz.people().list(userId='adewale', groupId='@followers').execute()
Joe Gregorioc9359072010-10-25 15:26:13 -0400139
ade@google.com567ee8d2010-10-06 06:13:18 -0700140 self.assertEquals(0, following['totalResults'])
141
ade@google.com75fdddd2010-09-29 16:44:00 +0100142
143class BuzzAuthenticatedFunctionalTest(unittest.TestCase):
ade@google.com6a05e7d2010-10-05 00:29:51 -0700144 def __init__(self, method_name):
145 unittest.TestCase.__init__(self, method_name)
146 credentials_dir = os.path.join(logging.os.path.dirname(__file__), './data')
147 f = file(os.path.join(credentials_dir, 'buzz_credentials.dat'), 'r')
148 credentials = pickle.loads(f.read())
149 f.close()
ade@google.com75fdddd2010-09-29 16:44:00 +0100150
ade@google.com6a05e7d2010-10-05 00:29:51 -0700151 self.http = credentials.authorize(httplib2.Http())
152
ade@google.com4f2794e2010-10-11 02:11:48 -0700153 def test_can_create_activity(self):
154 buzz = build('buzz', 'v1', http=self.http)
155
156 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500157 'data': {
158 'title': 'Testing insert',
159 'object': {
160 'content': u'Just a short note to show that insert is working. ?',
161 'type': 'note'}
162 }
163 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700164 ).execute()
165 self.assertTrue(activity is not None)
166
ade@google.com9d821b02010-10-11 03:33:51 -0700167 def test_can_create_private_activity(self):
168 buzz = build('buzz', 'v1', http=self.http)
169
170 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500171 'data': {
172 'title': 'Testing insert',
173 'object': {
174 'content': 'This is a private post.'
175 },
176 'visibility': {
177 'entries': [
178 { 'id': 'tag:google.com,2010:buzz-group:108242092577082601423:13' }
179 ]
180 }
181 }
182 }
ade@google.com9d821b02010-10-11 03:33:51 -0700183 ).execute()
184 self.assertTrue(activity is not None)
185
ade@google.com41b26032010-10-20 17:37:18 -0700186 def test_can_create_and_delete_new_group(self):
187 buzz = build('buzz', 'v1', http=self.http)
188 group_name = 'New Group Created At' + str(time.time())
189 group = buzz.groups().insert(userId='@me', body = {
190 'data': {
191 'title': group_name
192 }
193 }).execute()
194 self.assertTrue(group is not None)
195
196 result = buzz.groups().delete(userId='@me', groupId=group['id']).execute()
197 self.assertEquals({}, result)
198
ade@google.com4f2794e2010-10-11 02:11:48 -0700199 def test_can_identify_number_of_groups_belonging_to_user(self):
ade@google.com6a05e7d2010-10-05 00:29:51 -0700200 buzz = build('buzz', 'v1', http=self.http)
201 groups = buzz.groups().list(userId='108242092577082601423').execute()
202
ade@google.com41b26032010-10-20 17:37:18 -0700203 # 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 -0700204 expected_default_number_of_groups = 4
ade@google.com41b26032010-10-20 17:37:18 -0700205 self.assertTrue(len(groups['items']) > expected_default_number_of_groups)
ade@google.com75fdddd2010-09-29 16:44:00 +0100206
ade@google.com4f2794e2010-10-11 02:11:48 -0700207 def IGNORE__test_can_like_activity(self):
208 buzz = build('buzz', 'v1', http=self.http)
209 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500210 'data': {
211 'title': 'Testing insert',
212 'object': {
213 'content': u'Just a short note to show that insert is working. ?',
214 'type': 'note'}
215 }
216 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700217 ).execute()
218 pprint.pprint(activity)
219 id = activity['id']
220 likers = buzz.people().liked(userId='105037104815911535953', postId=id, groupId='@liked', scope='@self').execute()
221 # Todo(ade) Insert the new liker once the Buzz back-end bug is fixed
222
223 def test_can_comment_on_activity(self):
224 buzz = build('buzz', 'v1', http=self.http)
225
226 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500227 'data': {
228 'title': 'A new activity',
229 'object': {
230 'content': u'The body of the new activity',
231 'type': 'note'}
232 }
233 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700234 ).execute()
235
236 id = activity['id']
237 comment = buzz.comments().insert(userId='@me', postId=id, body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500238 'data': {
239 'content': 'A comment on the new activity'
240 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700241 }).execute()
242
ade@google.comc36ac532010-10-25 15:22:50 -0400243 def test_can_list_groups_belonging_to_user(self):
ade@google.com4f2794e2010-10-11 02:11:48 -0700244 buzz = build('buzz', 'v1', http=self.http)
245 groups = buzz.groups().list(userId='108242092577082601423').execute()
ade@google.com4f2794e2010-10-11 02:11:48 -0700246
247 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:15').execute()
248 self.assertEquals('G:108242092577082601423:15', group['id'], group)
249
250 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:14').execute()
251 self.assertEquals('G:108242092577082601423:14', group['id'], group)
252
253 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:13').execute()
254 self.assertEquals('G:108242092577082601423:13', group['id'], group)
255
256 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:6').execute()
257 self.assertEquals('G:108242092577082601423:6', group['id'], group)
258
ade@google.com9d821b02010-10-11 03:33:51 -0700259 def test_can_delete_activity(self):
260 buzz = build('buzz', 'v1', http=self.http)
Joe Gregorio8963ff92010-10-11 13:14:43 -0400261
ade@google.com9d821b02010-10-11 03:33:51 -0700262 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500263 'data': {
264 'title': 'Activity to be deleted',
265 'object': {
266 'content': u'Created this activity so that it can be deleted.',
267 'type': 'note'}
268 }
269 }
ade@google.com9d821b02010-10-11 03:33:51 -0700270 ).execute()
271 id = activity['id']
272
273 buzz.activities().delete(scope='@self', userId='@me', postId=id).execute()
Joe Gregorio8963ff92010-10-11 13:14:43 -0400274 time.sleep(2)
ade@google.com9d821b02010-10-11 03:33:51 -0700275
276 activity_url = activity['links']['self'][0]['href']
277 resp, content = self.http.request(activity_url, 'GET')
278 self.assertEquals(404, resp.status)
ade@google.com75fdddd2010-09-29 16:44:00 +0100279
Joe Gregoriodb849af2010-09-22 16:53:59 -0400280if __name__ == '__main__':
281 unittest.main()