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 |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 22 | import unittest |
| 23 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 24 | # TODO(ade) Remove this mock once the bug in the discovery document is fixed |
| 25 | DATA_DIR = os.path.join(logging.os.path.dirname(__file__), '../tests/data') |
| 26 | class 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.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 37 | class BuzzFunctionalTest(unittest.TestCase): |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 38 | 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.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 52 | def test_can_get_buzz_activities_with_many_params(self): |
| 53 | buzz = build('buzz', 'v1') |
| 54 | max_results = 2 |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 55 | activities_command = buzz.activities() |
| 56 | activities = activities_command.list(userId='googlebuzz', scope='@self', |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 57 | max_comments=max_results*2 ,max_liked=max_results*3, |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 58 | max_results=max_results).execute() |
| 59 | activity_count = len(activities['items']) |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 60 | self.assertEquals(max_results, activity_count) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 61 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 62 | activities = activities_command.list_next(activities).execute() |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 63 | activity_count = len(activities['items']) |
| 64 | self.assertEquals(max_results, activity_count) |
| 65 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 66 | def test_can_get_multiple_pages_of_buzz_activities(self): |
ade@google.com | 973d1a6 | 2010-09-23 21:21:21 +0100 | [diff] [blame] | 67 | buzz = build('buzz', 'v1') |
| 68 | max_results = 2 |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 69 | activities_command = buzz.activities() |
ade@google.com | 973d1a6 | 2010-09-23 21:21:21 +0100 | [diff] [blame] | 70 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 71 | activities = activities_command.list(userId='adewale', scope='@self', |
ade@google.com | 973d1a6 | 2010-09-23 21:21:21 +0100 | [diff] [blame] | 72 | max_results=max_results).execute() |
| 73 | for count in range(10): |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 74 | activities = activities_command.list_next(activities).execute() |
ade@google.com | 973d1a6 | 2010-09-23 21:21:21 +0100 | [diff] [blame] | 75 | activity_count = len(activities['items']) |
| 76 | self.assertEquals(max_results, activity_count, 'Failed after %s pages' % str(count)) |
| 77 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 78 | 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.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 | 567ee8d | 2010-10-06 06:13:18 -0700 | [diff] [blame] | 104 | 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.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 129 | |
| 130 | class BuzzAuthenticatedFunctionalTest(unittest.TestCase): |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 131 | 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.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 137 | |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 138 | self.http = credentials.authorize(httplib2.Http()) |
| 139 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 140 | 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.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame^] | 152 | 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.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 169 | def test_can_identify_number_of_groups_belonging_to_user(self): |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 170 | 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.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 176 | |
ade@google.com | 4f2794e | 2010-10-11 02:11:48 -0700 | [diff] [blame] | 177 | 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.com | 9d821b0 | 2010-10-11 03:33:51 -0700 | [diff] [blame^] | 228 | 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.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 245 | |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 246 | if __name__ == '__main__': |
| 247 | unittest.main() |