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. |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 8 | """ |
| 9 | |
| 10 | __author__ = 'ade@google.com (Ade Oshineye)' |
| 11 | |
ade@google.com | a9907a2 | 2010-12-11 02:44:37 +0000 | [diff] [blame] | 12 | import httplib2 |
| 13 | import pprint |
| 14 | |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 15 | from apiclient.discovery import build |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 16 | import httplib2 |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 17 | import logging |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 18 | import pickle |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 19 | import os |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 20 | import time |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 21 | import unittest |
| 22 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 23 | # TODO(ade) Remove this mock once the bug in the discovery document is fixed |
| 24 | DATA_DIR = os.path.join(logging.os.path.dirname(__file__), '../tests/data') |
| 25 | class HttpMock(object): |
| 26 | |
| 27 | def __init__(self, filename, headers): |
| 28 | f = file(os.path.join(DATA_DIR, filename), 'r') |
| 29 | self.data = f.read() |
| 30 | f.close() |
| 31 | self.headers = headers |
| 32 | |
| 33 | def request(self, uri, method="GET", body=None, headers=None, redirections=1, connection_type=None): |
| 34 | return httplib2.Response(self.headers), self.data |
| 35 | |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 36 | class BuzzFunctionalTest(unittest.TestCase): |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 37 | def test_can_get_specific_activity(self): |
| 38 | buzz = build('buzz', 'v1') |
| 39 | activity = buzz.activities().get(userId='105037104815911535953', |
| 40 | postId='B:z12sspviqyakfvye123wehng0muwz5jzq04').execute() |
| 41 | |
| 42 | self.assertTrue(activity is not None) |
| 43 | |
| 44 | def test_can_get_specific_activity_with_tag_id(self): |
| 45 | buzz = build('buzz', 'v1') |
| 46 | activity = buzz.activities().get(userId='105037104815911535953', |
| 47 | postId='tag:google.com,2010:buzz:z13ptnw5usmnv15ey22fzlswnuqoebasu').execute() |
| 48 | |
| 49 | self.assertTrue(activity is not None) |
| 50 | |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 51 | def test_can_get_buzz_activities_with_many_params(self): |
| 52 | buzz = build('buzz', 'v1') |
| 53 | max_results = 2 |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 54 | activities_command = buzz.activities() |
| 55 | activities = activities_command.list(userId='googlebuzz', scope='@self', |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 56 | max_comments=max_results*2 ,max_liked=max_results*3, |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 57 | max_results=max_results).execute() |
| 58 | activity_count = len(activities['items']) |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 59 | self.assertEquals(max_results, activity_count) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 60 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 61 | activities = activities_command.list_next(activities).execute() |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 62 | activity_count = len(activities['items']) |
| 63 | self.assertEquals(max_results, activity_count) |
| 64 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 65 | def test_can_get_multiple_pages_of_buzz_activities(self): |
ade@google.com | 973d1a6 | 2010-09-23 21:21:21 +0100 | [diff] [blame] | 66 | buzz = build('buzz', 'v1') |
| 67 | max_results = 2 |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 68 | activities_command = buzz.activities() |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 69 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 70 | activities = activities_command.list(userId='adewale', scope='@self', |
ade@google.com | 973d1a6 | 2010-09-23 21:21:21 +0100 | [diff] [blame] | 71 | max_results=max_results).execute() |
| 72 | for count in range(10): |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 73 | activities = activities_command.list_next(activities).execute() |
ade@google.com | 973d1a6 | 2010-09-23 21:21:21 +0100 | [diff] [blame] | 74 | activity_count = len(activities['items']) |
| 75 | self.assertEquals(max_results, activity_count, 'Failed after %s pages' % str(count)) |
| 76 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 77 | def IGNORE__test_can_get_multiple_pages_of_buzz_likers(self): |
| 78 | # Ignore this test until the Buzz API fixes the bug with next links |
| 79 | # http://code.google.com/p/google-buzz-api/issues/detail?id=114 |
| 80 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 81 | buzz = build('buzz', 'v1', self.http) |
| 82 | max_results = 1 |
| 83 | people_cmd = buzz.people() |
ade@google.com | f21095c | 2010-12-10 20:44:13 +0000 | [diff] [blame] | 84 | # The post https://www.googleapis.com/buzz/v1/activities/111062888259659218284/@self/B:z13nh535yk2syfob004cdjyb3mjeulcwv3c?alt=json# |
| 85 | #Perform this call https://www.googleapis.com/buzz/v1/activities/111062888259659218284/@self/B:z13nh535yk2syfob004cdjyb3mjeulcwv3c/@liked?alt=json&max-results=1 |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 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.com | d6d6f2f | 2010-09-28 07:37:25 +0100 | [diff] [blame] | 94 | 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.com | 5185853 | 2010-10-14 12:09:22 -0500 | [diff] [blame] | 104 | def test_can_get_user_profile_using_numeric_identifier(self): |
| 105 | buzz = build('buzz', 'v1') |
| 106 | person = buzz.people().get(userId='108242092577082601423').execute() |
| 107 | |
| 108 | self.assertTrue(person is not None) |
| 109 | self.assertEquals('buzz#person', person['kind']) |
| 110 | self.assertEquals('Test Account', person['displayName']) |
| 111 | self.assertEquals('108242092577082601423', person['id']) |
| 112 | self.assertEquals('http://www.google.com/profiles/108242092577082601423', person['profileUrl']) |
| 113 | |
ade@google.com | 567ee8d | 2010-10-06 06:13:18 -0700 | [diff] [blame] | 114 | def test_can_get_followees_of_user(self): |
| 115 | buzz = build('buzz', 'v1') |
| 116 | expected_followees = 30 |
| 117 | following = buzz.people().list(userId='googlebuzz', groupId='@following', max_results=expected_followees).execute() |
| 118 | |
| 119 | self.assertEquals(expected_followees, following['totalResults']) |
| 120 | self.assertEquals(expected_followees, len(following['entry'])) |
| 121 | |
| 122 | def test_can_efficiently_get_follower_count_of_user(self): |
| 123 | buzz = build('buzz', 'v1') |
| 124 | |
| 125 | # Restricting max_results to 1 means only a tiny amount of data comes back but the totalResults still has the total. |
ade@google.com | e45ea6c | 2011-01-11 18:15:57 +0000 | [diff] [blame] | 126 | followers = buzz.people().list(userId='googlebuzz', groupId='@followers', |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 127 | max_results='1').execute() |
ade@google.com | 567ee8d | 2010-10-06 06:13:18 -0700 | [diff] [blame] | 128 | |
| 129 | # @googlebuzz has a large but fluctuating number of followers |
| 130 | # It is sufficient if the result is bigger than 10, 000 |
ade@google.com | e45ea6c | 2011-01-11 18:15:57 +0000 | [diff] [blame] | 131 | follower_count = followers['totalResults'] |
ade@google.com | 567ee8d | 2010-10-06 06:13:18 -0700 | [diff] [blame] | 132 | self.assertTrue(follower_count > 10000, follower_count) |
| 133 | |
ade@google.com | d9427cf | 2011-01-03 19:18:44 +0000 | [diff] [blame] | 134 | def test_follower_count_is_missing_for_user_with_hidden_follower_count(self): |
ade@google.com | 567ee8d | 2010-10-06 06:13:18 -0700 | [diff] [blame] | 135 | buzz = build('buzz', 'v1') |
ade@google.com | e45ea6c | 2011-01-11 18:15:57 +0000 | [diff] [blame] | 136 | followers = buzz.people().list(userId='adewale', groupId='@followers').execute() |
Joe Gregorio | c935907 | 2010-10-25 15:26:13 -0400 | [diff] [blame] | 137 | |
ade@google.com | e45ea6c | 2011-01-11 18:15:57 +0000 | [diff] [blame] | 138 | self.assertFalse('totalResults' in followers) |
ade@google.com | 567ee8d | 2010-10-06 06:13:18 -0700 | [diff] [blame] | 139 | |
ade@google.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 140 | |
| 141 | class BuzzAuthenticatedFunctionalTest(unittest.TestCase): |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 142 | 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.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 148 | |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 149 | self.http = credentials.authorize(httplib2.Http()) |
| 150 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 151 | def test_can_create_activity(self): |
| 152 | buzz = build('buzz', 'v1', http=self.http) |
| 153 | |
| 154 | activity = buzz.activities().insert(userId='@me', body={ |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 155 | 'data': { |
| 156 | 'title': 'Testing insert', |
| 157 | 'object': { |
| 158 | 'content': u'Just a short note to show that insert is working. ?', |
| 159 | 'type': 'note'} |
| 160 | } |
| 161 | } |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 162 | ).execute() |
| 163 | self.assertTrue(activity is not None) |
| 164 | |
ade@google.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame] | 165 | def test_can_create_private_activity(self): |
| 166 | buzz = build('buzz', 'v1', http=self.http) |
| 167 | |
| 168 | activity = buzz.activities().insert(userId='@me', body={ |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 169 | 'data': { |
| 170 | 'title': 'Testing insert', |
| 171 | 'object': { |
| 172 | 'content': 'This is a private post.' |
| 173 | }, |
| 174 | 'visibility': { |
| 175 | 'entries': [ |
| 176 | { 'id': 'tag:google.com,2010:buzz-group:108242092577082601423:13' } |
| 177 | ] |
| 178 | } |
| 179 | } |
| 180 | } |
ade@google.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame] | 181 | ).execute() |
| 182 | self.assertTrue(activity is not None) |
| 183 | |
ade@google.com | 41b2603 | 2010-10-20 17:37:18 -0700 | [diff] [blame] | 184 | def test_can_create_and_delete_new_group(self): |
| 185 | buzz = build('buzz', 'v1', http=self.http) |
| 186 | group_name = 'New Group Created At' + str(time.time()) |
| 187 | group = buzz.groups().insert(userId='@me', body = { |
| 188 | 'data': { |
| 189 | 'title': group_name |
| 190 | } |
| 191 | }).execute() |
| 192 | self.assertTrue(group is not None) |
| 193 | |
| 194 | result = buzz.groups().delete(userId='@me', groupId=group['id']).execute() |
| 195 | self.assertEquals({}, result) |
| 196 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 197 | def test_can_identify_number_of_groups_belonging_to_user(self): |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 198 | buzz = build('buzz', 'v1', http=self.http) |
| 199 | groups = buzz.groups().list(userId='108242092577082601423').execute() |
| 200 | |
ade@google.com | 41b2603 | 2010-10-20 17:37:18 -0700 | [diff] [blame] | 201 | # 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] | 202 | expected_default_number_of_groups = 4 |
ade@google.com | 41b2603 | 2010-10-20 17:37:18 -0700 | [diff] [blame] | 203 | self.assertTrue(len(groups['items']) > expected_default_number_of_groups) |
ade@google.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 204 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 205 | def IGNORE__test_can_like_activity(self): |
| 206 | buzz = build('buzz', 'v1', http=self.http) |
| 207 | activity = buzz.activities().insert(userId='@me', body={ |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 208 | 'data': { |
| 209 | 'title': 'Testing insert', |
| 210 | 'object': { |
| 211 | 'content': u'Just a short note to show that insert is working. ?', |
| 212 | 'type': 'note'} |
| 213 | } |
| 214 | } |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 215 | ).execute() |
| 216 | pprint.pprint(activity) |
| 217 | id = activity['id'] |
| 218 | likers = buzz.people().liked(userId='105037104815911535953', postId=id, groupId='@liked', scope='@self').execute() |
| 219 | # Todo(ade) Insert the new liker once the Buzz back-end bug is fixed |
| 220 | |
| 221 | def test_can_comment_on_activity(self): |
| 222 | buzz = build('buzz', 'v1', http=self.http) |
| 223 | |
| 224 | activity = buzz.activities().insert(userId='@me', body={ |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 225 | 'data': { |
| 226 | 'title': 'A new activity', |
| 227 | 'object': { |
| 228 | 'content': u'The body of the new activity', |
| 229 | 'type': 'note'} |
| 230 | } |
| 231 | } |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 232 | ).execute() |
| 233 | |
| 234 | id = activity['id'] |
| 235 | comment = buzz.comments().insert(userId='@me', postId=id, body={ |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 236 | 'data': { |
| 237 | 'content': 'A comment on the new activity' |
| 238 | } |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 239 | }).execute() |
| 240 | |
ade@google.com | c36ac53 | 2010-10-25 15:22:50 -0400 | [diff] [blame] | 241 | def test_can_list_groups_belonging_to_user(self): |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 242 | buzz = build('buzz', 'v1', http=self.http) |
| 243 | groups = buzz.groups().list(userId='108242092577082601423').execute() |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 244 | |
| 245 | group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:15').execute() |
| 246 | self.assertEquals('G:108242092577082601423:15', group['id'], group) |
| 247 | |
| 248 | group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:14').execute() |
| 249 | self.assertEquals('G:108242092577082601423:14', group['id'], group) |
| 250 | |
| 251 | group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:13').execute() |
| 252 | self.assertEquals('G:108242092577082601423:13', group['id'], group) |
| 253 | |
| 254 | group = buzz.groups().get(userId='108242092577082601423', groupId='G:108242092577082601423:6').execute() |
| 255 | self.assertEquals('G:108242092577082601423:6', group['id'], group) |
| 256 | |
ade@google.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame] | 257 | def test_can_delete_activity(self): |
| 258 | buzz = build('buzz', 'v1', http=self.http) |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 259 | |
ade@google.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame] | 260 | activity = buzz.activities().insert(userId='@me', body={ |
Joe Gregorio | bc2ff9b | 2010-11-08 09:20:48 -0500 | [diff] [blame] | 261 | 'data': { |
| 262 | 'title': 'Activity to be deleted', |
| 263 | 'object': { |
| 264 | 'content': u'Created this activity so that it can be deleted.', |
| 265 | 'type': 'note'} |
| 266 | } |
| 267 | } |
ade@google.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame] | 268 | ).execute() |
| 269 | id = activity['id'] |
| 270 | |
| 271 | buzz.activities().delete(scope='@self', userId='@me', postId=id).execute() |
Joe Gregorio | 8963ff9 | 2010-10-11 13:14:43 -0400 | [diff] [blame] | 272 | time.sleep(2) |
ade@google.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame] | 273 | |
| 274 | activity_url = activity['links']['self'][0]['href'] |
| 275 | resp, content = self.http.request(activity_url, 'GET') |
| 276 | self.assertEquals(404, resp.status) |
ade@google.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 277 | |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 278 | if __name__ == '__main__': |
| 279 | unittest.main() |