blob: 8a20d3f848db1822237c7765448c3bf4b20a481f [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.
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500127 following = buzz.people().list(userId='googlebuzz', groupId='@followers',
128 max_results='1').execute()
ade@google.com567ee8d2010-10-06 06:13:18 -0700129
130 # @googlebuzz has a large but fluctuating number of followers
131 # It is sufficient if the result is bigger than 10, 000
132 follower_count = following['totalResults']
133 self.assertTrue(follower_count > 10000, follower_count)
134
135 def test_follower_count_is_zero_for_user_with_hidden_follower_count(self):
136 buzz = build('buzz', 'v1')
137 following = buzz.people().list(userId='adewale', groupId='@followers').execute()
Joe Gregorioc9359072010-10-25 15:26:13 -0400138
ade@google.com567ee8d2010-10-06 06:13:18 -0700139 self.assertEquals(0, following['totalResults'])
140
ade@google.com75fdddd2010-09-29 16:44:00 +0100141
142class BuzzAuthenticatedFunctionalTest(unittest.TestCase):
ade@google.com6a05e7d2010-10-05 00:29:51 -0700143 def __init__(self, method_name):
144 unittest.TestCase.__init__(self, method_name)
145 credentials_dir = os.path.join(logging.os.path.dirname(__file__), './data')
146 f = file(os.path.join(credentials_dir, 'buzz_credentials.dat'), 'r')
147 credentials = pickle.loads(f.read())
148 f.close()
ade@google.com75fdddd2010-09-29 16:44:00 +0100149
ade@google.com6a05e7d2010-10-05 00:29:51 -0700150 self.http = credentials.authorize(httplib2.Http())
151
ade@google.com4f2794e2010-10-11 02:11:48 -0700152 def test_can_create_activity(self):
153 buzz = build('buzz', 'v1', http=self.http)
154
155 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500156 'data': {
157 'title': 'Testing insert',
158 'object': {
159 'content': u'Just a short note to show that insert is working. ?',
160 'type': 'note'}
161 }
162 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700163 ).execute()
164 self.assertTrue(activity is not None)
165
ade@google.com9d821b02010-10-11 03:33:51 -0700166 def test_can_create_private_activity(self):
167 buzz = build('buzz', 'v1', http=self.http)
168
169 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500170 'data': {
171 'title': 'Testing insert',
172 'object': {
173 'content': 'This is a private post.'
174 },
175 'visibility': {
176 'entries': [
177 { 'id': 'tag:google.com,2010:buzz-group:108242092577082601423:13' }
178 ]
179 }
180 }
181 }
ade@google.com9d821b02010-10-11 03:33:51 -0700182 ).execute()
183 self.assertTrue(activity is not None)
184
ade@google.com41b26032010-10-20 17:37:18 -0700185 def test_can_create_and_delete_new_group(self):
186 buzz = build('buzz', 'v1', http=self.http)
187 group_name = 'New Group Created At' + str(time.time())
188 group = buzz.groups().insert(userId='@me', body = {
189 'data': {
190 'title': group_name
191 }
192 }).execute()
193 self.assertTrue(group is not None)
194
195 result = buzz.groups().delete(userId='@me', groupId=group['id']).execute()
196 self.assertEquals({}, result)
197
ade@google.com4f2794e2010-10-11 02:11:48 -0700198 def test_can_identify_number_of_groups_belonging_to_user(self):
ade@google.com6a05e7d2010-10-05 00:29:51 -0700199 buzz = build('buzz', 'v1', http=self.http)
200 groups = buzz.groups().list(userId='108242092577082601423').execute()
201
ade@google.com41b26032010-10-20 17:37:18 -0700202 # 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 -0700203 expected_default_number_of_groups = 4
ade@google.com41b26032010-10-20 17:37:18 -0700204 self.assertTrue(len(groups['items']) > expected_default_number_of_groups)
ade@google.com75fdddd2010-09-29 16:44:00 +0100205
ade@google.com4f2794e2010-10-11 02:11:48 -0700206 def IGNORE__test_can_like_activity(self):
207 buzz = build('buzz', 'v1', http=self.http)
208 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500209 'data': {
210 'title': 'Testing insert',
211 'object': {
212 'content': u'Just a short note to show that insert is working. ?',
213 'type': 'note'}
214 }
215 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700216 ).execute()
217 pprint.pprint(activity)
218 id = activity['id']
219 likers = buzz.people().liked(userId='105037104815911535953', postId=id, groupId='@liked', scope='@self').execute()
220 # Todo(ade) Insert the new liker once the Buzz back-end bug is fixed
221
222 def test_can_comment_on_activity(self):
223 buzz = build('buzz', 'v1', http=self.http)
224
225 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500226 'data': {
227 'title': 'A new activity',
228 'object': {
229 'content': u'The body of the new activity',
230 'type': 'note'}
231 }
232 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700233 ).execute()
234
235 id = activity['id']
236 comment = buzz.comments().insert(userId='@me', postId=id, body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500237 'data': {
238 'content': 'A comment on the new activity'
239 }
ade@google.com4f2794e2010-10-11 02:11:48 -0700240 }).execute()
241
ade@google.comc36ac532010-10-25 15:22:50 -0400242 def test_can_list_groups_belonging_to_user(self):
ade@google.com4f2794e2010-10-11 02:11:48 -0700243 buzz = build('buzz', 'v1', http=self.http)
244 groups = buzz.groups().list(userId='108242092577082601423').execute()
ade@google.com4f2794e2010-10-11 02:11:48 -0700245
246 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:15').execute()
247 self.assertEquals('G:108242092577082601423:15', group['id'], group)
248
249 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:14').execute()
250 self.assertEquals('G:108242092577082601423:14', group['id'], group)
251
252 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:13').execute()
253 self.assertEquals('G:108242092577082601423:13', group['id'], group)
254
255 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:6').execute()
256 self.assertEquals('G:108242092577082601423:6', group['id'], group)
257
ade@google.com9d821b02010-10-11 03:33:51 -0700258 def test_can_delete_activity(self):
259 buzz = build('buzz', 'v1', http=self.http)
Joe Gregorio8963ff92010-10-11 13:14:43 -0400260
ade@google.com9d821b02010-10-11 03:33:51 -0700261 activity = buzz.activities().insert(userId='@me', body={
Joe Gregoriobc2ff9b2010-11-08 09:20:48 -0500262 'data': {
263 'title': 'Activity to be deleted',
264 'object': {
265 'content': u'Created this activity so that it can be deleted.',
266 'type': 'note'}
267 }
268 }
ade@google.com9d821b02010-10-11 03:33:51 -0700269 ).execute()
270 id = activity['id']
271
272 buzz.activities().delete(scope='@self', userId='@me', postId=id).execute()
Joe Gregorio8963ff92010-10-11 13:14:43 -0400273 time.sleep(2)
ade@google.com9d821b02010-10-11 03:33:51 -0700274
275 activity_url = activity['links']['self'][0]['href']
276 resp, content = self.http.request(activity_url, 'GET')
277 self.assertEquals(404, resp.status)
ade@google.com75fdddd2010-09-29 16:44:00 +0100278
Joe Gregoriodb849af2010-09-22 16:53:59 -0400279if __name__ == '__main__':
280 unittest.main()