ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 4 | |
| 5 | """Discovery document tests |
| 6 | |
| 7 | Functional tests that verify we can retrieve data from existing services. |
| 8 | |
| 9 | These tests are read-only in order to ensure they're repeatable. They also |
| 10 | only work with publicly visible data in order to avoid dealing with OAuth. |
| 11 | """ |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 12 | import httplib2 |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 13 | import pprint |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 14 | |
| 15 | __author__ = 'ade@google.com (Ade Oshineye)' |
| 16 | |
| 17 | from apiclient.discovery import build |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 18 | import httplib2 |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 19 | import logging |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 20 | import pickle |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 21 | import os |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 22 | import time |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 23 | import unittest |
| 24 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 25 | # TODO(ade) Remove this mock once the bug in the discovery document is fixed |
| 26 | DATA_DIR = os.path.join(logging.os.path.dirname(__file__), '../tests/data') |
| 27 | class 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.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 38 | class BuzzFunctionalTest(unittest.TestCase): |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 39 | 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.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 53 | def test_can_get_buzz_activities_with_many_params(self): |
| 54 | buzz = build('buzz', 'v1') |
| 55 | max_results = 2 |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 56 | activities_command = buzz.activities() |
| 57 | activities = activities_command.list(userId='googlebuzz', scope='@self', |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 58 | max_comments=max_results*2 ,max_liked=max_results*3, |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 59 | max_results=max_results).execute() |
| 60 | activity_count = len(activities['items']) |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 61 | self.assertEquals(max_results, activity_count) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 62 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 63 | activities = activities_command.list_next(activities).execute() |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 64 | activity_count = len(activities['items']) |
| 65 | self.assertEquals(max_results, activity_count) |
| 66 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 67 | def test_can_get_multiple_pages_of_buzz_activities(self): |
ade@google.com | 973d1a6 | 2010-09-23 21:21:21 +0100 | [diff] [blame] | 68 | buzz = build('buzz', 'v1') |
| 69 | max_results = 2 |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 70 | activities_command = buzz.activities() |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 71 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 72 | activities = activities_command.list(userId='adewale', scope='@self', |
ade@google.com | 973d1a6 | 2010-09-23 21:21:21 +0100 | [diff] [blame] | 73 | max_results=max_results).execute() |
| 74 | for count in range(10): |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 75 | activities = activities_command.list_next(activities).execute() |
ade@google.com | 973d1a6 | 2010-09-23 21:21:21 +0100 | [diff] [blame] | 76 | activity_count = len(activities['items']) |
| 77 | self.assertEquals(max_results, activity_count, 'Failed after %s pages' % str(count)) |
| 78 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 79 | 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.com | d6d6f2f | 2010-09-28 07:37:25 +0100 | [diff] [blame] | 95 | 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.com | 5185853 | 2010-10-14 12:09:22 -0500 | [diff] [blame] | 105 | 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.com | 567ee8d | 2010-10-06 06:13:18 -0700 | [diff] [blame] | 115 | 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 Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 127 | following = buzz.people().list(userId='googlebuzz', groupId='@followers', |
| 128 | max_results='1').execute() |
ade@google.com | 567ee8d | 2010-10-06 06:13:18 -0700 | [diff] [blame] | 129 | |
| 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 Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 138 | |
ade@google.com | 567ee8d | 2010-10-06 06:13:18 -0700 | [diff] [blame] | 139 | self.assertEquals(0, following['totalResults']) |
| 140 | |
ade@google.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 141 | |
| 142 | class BuzzAuthenticatedFunctionalTest(unittest.TestCase): |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 143 | 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.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 149 | |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 150 | self.http = credentials.authorize(httplib2.Http()) |
| 151 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 152 | def test_can_create_activity(self): |
| 153 | buzz = build('buzz', 'v1', http=self.http) |
| 154 | |
| 155 | activity = buzz.activities().insert(userId='@me', body={ |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 156 | '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.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 163 | ).execute() |
| 164 | self.assertTrue(activity is not None) |
| 165 | |
ade@google.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame] | 166 | 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 Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 170 | '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.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame] | 182 | ).execute() |
| 183 | self.assertTrue(activity is not None) |
| 184 | |
ade@google.com | 41b2603 | 2010-10-20 17:37:18 -0700 | [diff] [blame] | 185 | 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.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 198 | def test_can_identify_number_of_groups_belonging_to_user(self): |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 199 | buzz = build('buzz', 'v1', http=self.http) |
| 200 | groups = buzz.groups().list(userId='108242092577082601423').execute() |
| 201 | |
ade@google.com | 41b2603 | 2010-10-20 17:37:18 -0700 | [diff] [blame] | 202 | # This should work as long as no-one deletes the 4 default groups for this test account |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 203 | expected_default_number_of_groups = 4 |
ade@google.com | 41b2603 | 2010-10-20 17:37:18 -0700 | [diff] [blame] | 204 | self.assertTrue(len(groups['items']) > expected_default_number_of_groups) |
ade@google.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 205 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 206 | def IGNORE__test_can_like_activity(self): |
| 207 | buzz = build('buzz', 'v1', http=self.http) |
| 208 | activity = buzz.activities().insert(userId='@me', body={ |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 209 | '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.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 216 | ).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 Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 226 | 'data': { |
| 227 | 'title': 'A new activity', |
| 228 | 'object': { |
| 229 | 'content': u'The body of the new activity', |
| 230 | 'type': 'note'} |
| 231 | } |
| 232 | } |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 233 | ).execute() |
| 234 | |
| 235 | id = activity['id'] |
| 236 | comment = buzz.comments().insert(userId='@me', postId=id, body={ |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 237 | 'data': { |
| 238 | 'content': 'A comment on the new activity' |
| 239 | } |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 240 | }).execute() |
| 241 | |
ade@google.com | c36ac53 | 2010-10-25 15:22:50 -0400 | [diff] [blame] | 242 | def test_can_list_groups_belonging_to_user(self): |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 243 | buzz = build('buzz', 'v1', http=self.http) |
| 244 | groups = buzz.groups().list(userId='108242092577082601423').execute() |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 245 | |
| 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.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame] | 258 | def test_can_delete_activity(self): |
| 259 | buzz = build('buzz', 'v1', http=self.http) |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 260 | |
ade@google.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame] | 261 | activity = buzz.activities().insert(userId='@me', body={ |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 262 | '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.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame] | 269 | ).execute() |
| 270 | id = activity['id'] |
| 271 | |
| 272 | buzz.activities().delete(scope='@self', userId='@me', postId=id).execute() |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 273 | time.sleep(2) |
ade@google.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame] | 274 | |
| 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.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 278 | |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 279 | if __name__ == '__main__': |
| 280 | unittest.main() |