blob: 2b7043cdc833a47d04ba8f79ae1731e5ef044689 [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()
86 #https://www.googleapis.com/buzz/v1/activities/111062888259659218284/@self/B:z13nh535yk2syfob004cdjyb3mjeulcwv3c?alt=json#
87 people = people_cmd.liked(groupId='@liked', userId='googlebuzz', scope='@self',
88 postId='B:z13nh535yk2syfob004cdjyb3mjeulcwv3c', max_results=max_results).execute()
89
90 for count in range(10):
91 people = people_cmd.liked_next(people).execute()
92 people_count = len(people['items'])
93 self.assertEquals(max_results, people_count, 'Failed after %s pages' % str(count))
94
ade@google.comd6d6f2f2010-09-28 07:37:25 +010095 def test_can_get_user_profile(self):
96 buzz = build('buzz', 'v1')
97 person = buzz.people().get(userId='googlebuzz').execute()
98
99 self.assertTrue(person is not None)
100 self.assertEquals('buzz#person', person['kind'])
101 self.assertEquals('Google Buzz Team', person['displayName'])
102 self.assertEquals('111062888259659218284', person['id'])
103 self.assertEquals('http://www.google.com/profiles/googlebuzz', person['profileUrl'])
104
ade@google.com51858532010-10-14 12:09:22 -0500105 def test_can_get_user_profile_using_numeric_identifier(self):
106 buzz = build('buzz', 'v1')
107 person = buzz.people().get(userId='108242092577082601423').execute()
108
109 self.assertTrue(person is not None)
110 self.assertEquals('buzz#person', person['kind'])
111 self.assertEquals('Test Account', person['displayName'])
112 self.assertEquals('108242092577082601423', person['id'])
113 self.assertEquals('http://www.google.com/profiles/108242092577082601423', person['profileUrl'])
114
ade@google.com567ee8d2010-10-06 06:13:18 -0700115 def test_can_get_followees_of_user(self):
116 buzz = build('buzz', 'v1')
117 expected_followees = 30
118 following = buzz.people().list(userId='googlebuzz', groupId='@following', max_results=expected_followees).execute()
119
120 self.assertEquals(expected_followees, following['totalResults'])
121 self.assertEquals(expected_followees, len(following['entry']))
122
123 def test_can_efficiently_get_follower_count_of_user(self):
124 buzz = build('buzz', 'v1')
125
126 # Restricting max_results to 1 means only a tiny amount of data comes back but the totalResults still has the total.
127 following = buzz.people().list(userId='googlebuzz', groupId='@followers', max_results=1).execute()
128
129 # @googlebuzz has a large but fluctuating number of followers
130 # It is sufficient if the result is bigger than 10, 000
131 follower_count = following['totalResults']
132 self.assertTrue(follower_count > 10000, follower_count)
133
134 def test_follower_count_is_zero_for_user_with_hidden_follower_count(self):
135 buzz = build('buzz', 'v1')
136 following = buzz.people().list(userId='adewale', groupId='@followers').execute()
Joe Gregorioc9359072010-10-25 15:26:13 -0400137
ade@google.com567ee8d2010-10-06 06:13:18 -0700138 self.assertEquals(0, following['totalResults'])
139
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={
155 'title': 'Testing insert',
156 'object': {
157 'content': u'Just a short note to show that insert is working. ?',
158 'type': 'note'}
159 }
160 ).execute()
161 self.assertTrue(activity is not None)
162
ade@google.com9d821b02010-10-11 03:33:51 -0700163 def test_can_create_private_activity(self):
164 buzz = build('buzz', 'v1', http=self.http)
165
166 activity = buzz.activities().insert(userId='@me', body={
167 'title': 'Testing insert',
168 'object': {
169 'content': 'This is a private post.'
170 },
171 'visibility': {
172 'entries': [
173 { 'id': 'tag:google.com,2010:buzz-group:108242092577082601423:13' }
174 ]
175 }
176 }
177 ).execute()
178 self.assertTrue(activity is not None)
179
ade@google.com41b26032010-10-20 17:37:18 -0700180 def test_can_create_and_delete_new_group(self):
181 buzz = build('buzz', 'v1', http=self.http)
182 group_name = 'New Group Created At' + str(time.time())
183 group = buzz.groups().insert(userId='@me', body = {
184 'data': {
185 'title': group_name
186 }
187 }).execute()
188 self.assertTrue(group is not None)
189
190 result = buzz.groups().delete(userId='@me', groupId=group['id']).execute()
191 self.assertEquals({}, result)
192
ade@google.com4f2794e2010-10-11 02:11:48 -0700193 def test_can_identify_number_of_groups_belonging_to_user(self):
ade@google.com6a05e7d2010-10-05 00:29:51 -0700194 buzz = build('buzz', 'v1', http=self.http)
195 groups = buzz.groups().list(userId='108242092577082601423').execute()
196
ade@google.com41b26032010-10-20 17:37:18 -0700197 # 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 -0700198 expected_default_number_of_groups = 4
ade@google.com41b26032010-10-20 17:37:18 -0700199 self.assertTrue(len(groups['items']) > expected_default_number_of_groups)
ade@google.com75fdddd2010-09-29 16:44:00 +0100200
ade@google.com4f2794e2010-10-11 02:11:48 -0700201 def IGNORE__test_can_like_activity(self):
202 buzz = build('buzz', 'v1', http=self.http)
203 activity = buzz.activities().insert(userId='@me', body={
204 'title': 'Testing insert',
205 'object': {
206 'content': u'Just a short note to show that insert is working. ?',
207 'type': 'note'}
208 }
209 ).execute()
210 pprint.pprint(activity)
211 id = activity['id']
212 likers = buzz.people().liked(userId='105037104815911535953', postId=id, groupId='@liked', scope='@self').execute()
213 # Todo(ade) Insert the new liker once the Buzz back-end bug is fixed
214
215 def test_can_comment_on_activity(self):
216 buzz = build('buzz', 'v1', http=self.http)
217
218 activity = buzz.activities().insert(userId='@me', body={
219 'title': 'A new activity',
220 'object': {
221 'content': u'The body of the new activity',
222 'type': 'note'}
223 }
224 ).execute()
225
226 id = activity['id']
227 comment = buzz.comments().insert(userId='@me', postId=id, body={
228 "content": "A comment on the new activity"
229 }).execute()
230
231 def IGNORE__test_can_list_groups_belonging_to_user(self):
232 # TODO(ade) Uncomment this test once the related Buzz back-end bug is fixed
233 buzz = build('buzz', 'v1', http=self.http)
234 groups = buzz.groups().list(userId='108242092577082601423').execute()
235 pprint.pprint(groups)
236
237 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:15').execute()
238 self.assertEquals('G:108242092577082601423:15', group['id'], group)
239
240 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:14').execute()
241 self.assertEquals('G:108242092577082601423:14', group['id'], group)
242
243 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:13').execute()
244 self.assertEquals('G:108242092577082601423:13', group['id'], group)
245
246 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:6').execute()
247 self.assertEquals('G:108242092577082601423:6', group['id'], group)
248
249 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:9999999').execute()
250 self.assertEquals(None, group, group)
251
ade@google.com9d821b02010-10-11 03:33:51 -0700252 def test_can_delete_activity(self):
253 buzz = build('buzz', 'v1', http=self.http)
Joe Gregorio8963ff92010-10-11 13:14:43 -0400254
ade@google.com9d821b02010-10-11 03:33:51 -0700255 activity = buzz.activities().insert(userId='@me', body={
256 'title': 'Activity to be deleted',
257 'object': {
258 'content': u'Created this activity so that it can be deleted.',
259 'type': 'note'}
260 }
261 ).execute()
262 id = activity['id']
263
264 buzz.activities().delete(scope='@self', userId='@me', postId=id).execute()
Joe Gregorio8963ff92010-10-11 13:14:43 -0400265 time.sleep(2)
ade@google.com9d821b02010-10-11 03:33:51 -0700266
267 activity_url = activity['links']['self'][0]['href']
268 resp, content = self.http.request(activity_url, 'GET')
269 self.assertEquals(404, resp.status)
ade@google.com75fdddd2010-09-29 16:44:00 +0100270
Joe Gregoriodb849af2010-09-22 16:53:59 -0400271if __name__ == '__main__':
272 unittest.main()