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 | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 13 | |
| 14 | __author__ = 'ade@google.com (Ade Oshineye)' |
| 15 | |
| 16 | from apiclient.discovery import build |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 17 | import httplib2 |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 18 | import logging |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 19 | import pickle |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 20 | import os |
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): |
| 37 | def test_can_get_buzz_activities_with_many_params(self): |
| 38 | buzz = build('buzz', 'v1') |
| 39 | max_results = 2 |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 40 | actcol = buzz.activities() |
| 41 | activities = actcol.list(userId='googlebuzz', scope='@self', |
| 42 | max_comments=max_results*2 ,max_liked=max_results*3, |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 43 | max_results=max_results).execute() |
| 44 | activity_count = len(activities['items']) |
ade@google.com | 46179d3 | 2010-08-21 00:00:03 +0100 | [diff] [blame] | 45 | self.assertEquals(max_results, activity_count) |
Joe Gregorio | c5c5a37 | 2010-09-22 11:42:32 -0400 | [diff] [blame] | 46 | |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 47 | activities = actcol.list_next(activities).execute() |
| 48 | activity_count = len(activities['items']) |
| 49 | self.assertEquals(max_results, activity_count) |
| 50 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 51 | def test_can_get_multiple_pages_of_buzz_activities(self): |
ade@google.com | 973d1a6 | 2010-09-23 21:21:21 +0100 | [diff] [blame] | 52 | buzz = build('buzz', 'v1') |
| 53 | max_results = 2 |
| 54 | actcol = buzz.activities() |
| 55 | |
| 56 | activities = actcol.list(userId='adewale', scope='@self', |
| 57 | max_results=max_results).execute() |
| 58 | for count in range(10): |
| 59 | activities = actcol.list_next(activities).execute() |
| 60 | activity_count = len(activities['items']) |
| 61 | self.assertEquals(max_results, activity_count, 'Failed after %s pages' % str(count)) |
| 62 | |
ade@google.com | 2ab0de7 | 2010-09-27 23:26:54 +0100 | [diff] [blame] | 63 | def IGNORE__test_can_get_multiple_pages_of_buzz_likers(self): |
| 64 | # Ignore this test until the Buzz API fixes the bug with next links |
| 65 | # http://code.google.com/p/google-buzz-api/issues/detail?id=114 |
| 66 | self.http = HttpMock('buzz.json', {'status': '200'}) |
| 67 | buzz = build('buzz', 'v1', self.http) |
| 68 | max_results = 1 |
| 69 | people_cmd = buzz.people() |
| 70 | #https://www.googleapis.com/buzz/v1/activities/111062888259659218284/@self/B:z13nh535yk2syfob004cdjyb3mjeulcwv3c?alt=json# |
| 71 | people = people_cmd.liked(groupId='@liked', userId='googlebuzz', scope='@self', |
| 72 | postId='B:z13nh535yk2syfob004cdjyb3mjeulcwv3c', max_results=max_results).execute() |
| 73 | |
| 74 | for count in range(10): |
| 75 | people = people_cmd.liked_next(people).execute() |
| 76 | people_count = len(people['items']) |
| 77 | self.assertEquals(max_results, people_count, 'Failed after %s pages' % str(count)) |
| 78 | |
ade@google.com | d6d6f2f | 2010-09-28 07:37:25 +0100 | [diff] [blame] | 79 | def test_can_get_user_profile(self): |
| 80 | buzz = build('buzz', 'v1') |
| 81 | person = buzz.people().get(userId='googlebuzz').execute() |
| 82 | |
| 83 | self.assertTrue(person is not None) |
| 84 | self.assertEquals('buzz#person', person['kind']) |
| 85 | self.assertEquals('Google Buzz Team', person['displayName']) |
| 86 | self.assertEquals('111062888259659218284', person['id']) |
| 87 | self.assertEquals('http://www.google.com/profiles/googlebuzz', person['profileUrl']) |
| 88 | |
ade@google.com | 567ee8d | 2010-10-06 06:13:18 -0700 | [diff] [blame^] | 89 | def test_can_get_followees_of_user(self): |
| 90 | buzz = build('buzz', 'v1') |
| 91 | expected_followees = 30 |
| 92 | following = buzz.people().list(userId='googlebuzz', groupId='@following', max_results=expected_followees).execute() |
| 93 | |
| 94 | self.assertEquals(expected_followees, following['totalResults']) |
| 95 | self.assertEquals(expected_followees, len(following['entry'])) |
| 96 | |
| 97 | def test_can_efficiently_get_follower_count_of_user(self): |
| 98 | buzz = build('buzz', 'v1') |
| 99 | |
| 100 | # Restricting max_results to 1 means only a tiny amount of data comes back but the totalResults still has the total. |
| 101 | following = buzz.people().list(userId='googlebuzz', groupId='@followers', max_results=1).execute() |
| 102 | |
| 103 | # @googlebuzz has a large but fluctuating number of followers |
| 104 | # It is sufficient if the result is bigger than 10, 000 |
| 105 | follower_count = following['totalResults'] |
| 106 | self.assertTrue(follower_count > 10000, follower_count) |
| 107 | |
| 108 | def test_follower_count_is_zero_for_user_with_hidden_follower_count(self): |
| 109 | buzz = build('buzz', 'v1') |
| 110 | following = buzz.people().list(userId='adewale', groupId='@followers').execute() |
| 111 | |
| 112 | self.assertEquals(0, following['totalResults']) |
| 113 | |
ade@google.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 114 | |
| 115 | class BuzzAuthenticatedFunctionalTest(unittest.TestCase): |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 116 | def __init__(self, method_name): |
| 117 | unittest.TestCase.__init__(self, method_name) |
| 118 | credentials_dir = os.path.join(logging.os.path.dirname(__file__), './data') |
| 119 | f = file(os.path.join(credentials_dir, 'buzz_credentials.dat'), 'r') |
| 120 | credentials = pickle.loads(f.read()) |
| 121 | f.close() |
ade@google.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 122 | |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 123 | self.http = credentials.authorize(httplib2.Http()) |
| 124 | |
| 125 | def test_can_list_groups_belonging_to_user(self): |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame] | 126 | buzz = build('buzz', 'v1', http=self.http) |
| 127 | groups = buzz.groups().list(userId='108242092577082601423').execute() |
| 128 | |
| 129 | # This should work as long as no-one edits the groups for this test account |
| 130 | expected_default_number_of_groups = 4 |
| 131 | self.assertEquals(expected_default_number_of_groups, len(groups['items'])) |
ade@google.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 132 | |
ade@google.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 133 | |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 134 | if __name__ == '__main__': |
| 135 | unittest.main() |