blob: 50a1a4d8779352b8acec9b323990c490242ded46 [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
ade@google.com46179d32010-08-21 00:00:03 +010022import unittest
23
ade@google.com2ab0de72010-09-27 23:26:54 +010024# TODO(ade) Remove this mock once the bug in the discovery document is fixed
25DATA_DIR = os.path.join(logging.os.path.dirname(__file__), '../tests/data')
26class HttpMock(object):
27
28 def __init__(self, filename, headers):
29 f = file(os.path.join(DATA_DIR, filename), 'r')
30 self.data = f.read()
31 f.close()
32 self.headers = headers
33
34 def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None):
35 return httplib2.Response(self.headers), self.data
36
ade@google.com46179d32010-08-21 00:00:03 +010037class BuzzFunctionalTest(unittest.TestCase):
ade@google.com4f2794e2010-10-11 02:11:48 -070038 def test_can_get_specific_activity(self):
39 buzz = build('buzz', 'v1')
40 activity = buzz.activities().get(userId='105037104815911535953',
41 postId='B:z12sspviqyakfvye123wehng0muwz5jzq04').execute()
42
43 self.assertTrue(activity is not None)
44
45 def test_can_get_specific_activity_with_tag_id(self):
46 buzz = build('buzz', 'v1')
47 activity = buzz.activities().get(userId='105037104815911535953',
48 postId='tag:google.com,2010:buzz:z13ptnw5usmnv15ey22fzlswnuqoebasu').execute()
49
50 self.assertTrue(activity is not None)
51
ade@google.com46179d32010-08-21 00:00:03 +010052 def test_can_get_buzz_activities_with_many_params(self):
53 buzz = build('buzz', 'v1')
54 max_results = 2
ade@google.com4f2794e2010-10-11 02:11:48 -070055 activities_command = buzz.activities()
56 activities = activities_command.list(userId='googlebuzz', scope='@self',
Joe Gregorioc5c5a372010-09-22 11:42:32 -040057 max_comments=max_results*2 ,max_liked=max_results*3,
Joe Gregoriodb849af2010-09-22 16:53:59 -040058 max_results=max_results).execute()
59 activity_count = len(activities['items'])
ade@google.com46179d32010-08-21 00:00:03 +010060 self.assertEquals(max_results, activity_count)
Joe Gregorioc5c5a372010-09-22 11:42:32 -040061
ade@google.com4f2794e2010-10-11 02:11:48 -070062 activities = activities_command.list_next(activities).execute()
Joe Gregoriodb849af2010-09-22 16:53:59 -040063 activity_count = len(activities['items'])
64 self.assertEquals(max_results, activity_count)
65
ade@google.com2ab0de72010-09-27 23:26:54 +010066 def test_can_get_multiple_pages_of_buzz_activities(self):
ade@google.com973d1a62010-09-23 21:21:21 +010067 buzz = build('buzz', 'v1')
68 max_results = 2
ade@google.com4f2794e2010-10-11 02:11:48 -070069 activities_command = buzz.activities()
ade@google.com973d1a62010-09-23 21:21:21 +010070
ade@google.com4f2794e2010-10-11 02:11:48 -070071 activities = activities_command.list(userId='adewale', scope='@self',
ade@google.com973d1a62010-09-23 21:21:21 +010072 max_results=max_results).execute()
73 for count in range(10):
ade@google.com4f2794e2010-10-11 02:11:48 -070074 activities = activities_command.list_next(activities).execute()
ade@google.com973d1a62010-09-23 21:21:21 +010075 activity_count = len(activities['items'])
76 self.assertEquals(max_results, activity_count, 'Failed after %s pages' % str(count))
77
ade@google.com2ab0de72010-09-27 23:26:54 +010078 def IGNORE__test_can_get_multiple_pages_of_buzz_likers(self):
79 # Ignore this test until the Buzz API fixes the bug with next links
80 # http://code.google.com/p/google-buzz-api/issues/detail?id=114
81 self.http = HttpMock('buzz.json', {'status': '200'})
82 buzz = build('buzz', 'v1', self.http)
83 max_results = 1
84 people_cmd = buzz.people()
85 #https://www.googleapis.com/buzz/v1/activities/111062888259659218284/@self/B:z13nh535yk2syfob004cdjyb3mjeulcwv3c?alt=json#
86 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.com567ee8d2010-10-06 06:13:18 -0700104 def test_can_get_followees_of_user(self):
105 buzz = build('buzz', 'v1')
106 expected_followees = 30
107 following = buzz.people().list(userId='googlebuzz', groupId='@following', max_results=expected_followees).execute()
108
109 self.assertEquals(expected_followees, following['totalResults'])
110 self.assertEquals(expected_followees, len(following['entry']))
111
112 def test_can_efficiently_get_follower_count_of_user(self):
113 buzz = build('buzz', 'v1')
114
115 # Restricting max_results to 1 means only a tiny amount of data comes back but the totalResults still has the total.
116 following = buzz.people().list(userId='googlebuzz', groupId='@followers', max_results=1).execute()
117
118 # @googlebuzz has a large but fluctuating number of followers
119 # It is sufficient if the result is bigger than 10, 000
120 follower_count = following['totalResults']
121 self.assertTrue(follower_count > 10000, follower_count)
122
123 def test_follower_count_is_zero_for_user_with_hidden_follower_count(self):
124 buzz = build('buzz', 'v1')
125 following = buzz.people().list(userId='adewale', groupId='@followers').execute()
126
127 self.assertEquals(0, following['totalResults'])
128
ade@google.com75fdddd2010-09-29 16:44:00 +0100129
130class BuzzAuthenticatedFunctionalTest(unittest.TestCase):
ade@google.com6a05e7d2010-10-05 00:29:51 -0700131 def __init__(self, method_name):
132 unittest.TestCase.__init__(self, method_name)
133 credentials_dir = os.path.join(logging.os.path.dirname(__file__), './data')
134 f = file(os.path.join(credentials_dir, 'buzz_credentials.dat'), 'r')
135 credentials = pickle.loads(f.read())
136 f.close()
ade@google.com75fdddd2010-09-29 16:44:00 +0100137
ade@google.com6a05e7d2010-10-05 00:29:51 -0700138 self.http = credentials.authorize(httplib2.Http())
139
ade@google.com4f2794e2010-10-11 02:11:48 -0700140 def test_can_create_activity(self):
141 buzz = build('buzz', 'v1', http=self.http)
142
143 activity = buzz.activities().insert(userId='@me', body={
144 'title': 'Testing insert',
145 'object': {
146 'content': u'Just a short note to show that insert is working. ?',
147 'type': 'note'}
148 }
149 ).execute()
150 self.assertTrue(activity is not None)
151
ade@google.com9d821b02010-10-11 03:33:51 -0700152 def test_can_create_private_activity(self):
153 buzz = build('buzz', 'v1', http=self.http)
154
155 activity = buzz.activities().insert(userId='@me', body={
156 'title': 'Testing insert',
157 'object': {
158 'content': 'This is a private post.'
159 },
160 'visibility': {
161 'entries': [
162 { 'id': 'tag:google.com,2010:buzz-group:108242092577082601423:13' }
163 ]
164 }
165 }
166 ).execute()
167 self.assertTrue(activity is not None)
168
ade@google.com4f2794e2010-10-11 02:11:48 -0700169 def test_can_identify_number_of_groups_belonging_to_user(self):
ade@google.com6a05e7d2010-10-05 00:29:51 -0700170 buzz = build('buzz', 'v1', http=self.http)
171 groups = buzz.groups().list(userId='108242092577082601423').execute()
172
173 # This should work as long as no-one edits the groups for this test account
174 expected_default_number_of_groups = 4
175 self.assertEquals(expected_default_number_of_groups, len(groups['items']))
ade@google.com75fdddd2010-09-29 16:44:00 +0100176
ade@google.com4f2794e2010-10-11 02:11:48 -0700177 def IGNORE__test_can_like_activity(self):
178 buzz = build('buzz', 'v1', http=self.http)
179 activity = buzz.activities().insert(userId='@me', body={
180 'title': 'Testing insert',
181 'object': {
182 'content': u'Just a short note to show that insert is working. ?',
183 'type': 'note'}
184 }
185 ).execute()
186 pprint.pprint(activity)
187 id = activity['id']
188 likers = buzz.people().liked(userId='105037104815911535953', postId=id, groupId='@liked', scope='@self').execute()
189 # Todo(ade) Insert the new liker once the Buzz back-end bug is fixed
190
191 def test_can_comment_on_activity(self):
192 buzz = build('buzz', 'v1', http=self.http)
193
194 activity = buzz.activities().insert(userId='@me', body={
195 'title': 'A new activity',
196 'object': {
197 'content': u'The body of the new activity',
198 'type': 'note'}
199 }
200 ).execute()
201
202 id = activity['id']
203 comment = buzz.comments().insert(userId='@me', postId=id, body={
204 "content": "A comment on the new activity"
205 }).execute()
206
207 def IGNORE__test_can_list_groups_belonging_to_user(self):
208 # TODO(ade) Uncomment this test once the related Buzz back-end bug is fixed
209 buzz = build('buzz', 'v1', http=self.http)
210 groups = buzz.groups().list(userId='108242092577082601423').execute()
211 pprint.pprint(groups)
212
213 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:15').execute()
214 self.assertEquals('G:108242092577082601423:15', group['id'], group)
215
216 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:14').execute()
217 self.assertEquals('G:108242092577082601423:14', group['id'], group)
218
219 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:13').execute()
220 self.assertEquals('G:108242092577082601423:13', group['id'], group)
221
222 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:6').execute()
223 self.assertEquals('G:108242092577082601423:6', group['id'], group)
224
225 group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:9999999').execute()
226 self.assertEquals(None, group, group)
227
ade@google.com9d821b02010-10-11 03:33:51 -0700228 def test_can_delete_activity(self):
229 buzz = build('buzz', 'v1', http=self.http)
230
231 activity = buzz.activities().insert(userId='@me', body={
232 'title': 'Activity to be deleted',
233 'object': {
234 'content': u'Created this activity so that it can be deleted.',
235 'type': 'note'}
236 }
237 ).execute()
238 id = activity['id']
239
240 buzz.activities().delete(scope='@self', userId='@me', postId=id).execute()
241
242 activity_url = activity['links']['self'][0]['href']
243 resp, content = self.http.request(activity_url, 'GET')
244 self.assertEquals(404, resp.status)
ade@google.com75fdddd2010-09-29 16:44:00 +0100245
Joe Gregoriodb849af2010-09-22 16:53:59 -0400246if __name__ == '__main__':
247 unittest.main()