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 | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 89 | |
| 90 | class BuzzAuthenticatedFunctionalTest(unittest.TestCase): |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame^] | 91 | def __init__(self, method_name): |
| 92 | unittest.TestCase.__init__(self, method_name) |
| 93 | credentials_dir = os.path.join(logging.os.path.dirname(__file__), './data') |
| 94 | f = file(os.path.join(credentials_dir, 'buzz_credentials.dat'), 'r') |
| 95 | credentials = pickle.loads(f.read()) |
| 96 | f.close() |
ade@google.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 97 | |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame^] | 98 | self.http = credentials.authorize(httplib2.Http()) |
| 99 | |
| 100 | def test_can_list_groups_belonging_to_user(self): |
| 101 | # TODO(ade) This should not require authentication. It does because we're adding a spurious @self to the URL |
| 102 | buzz = build('buzz', 'v1', http=self.http) |
| 103 | groups = buzz.groups().list(userId='108242092577082601423').execute() |
| 104 | |
| 105 | # This should work as long as no-one edits the groups for this test account |
| 106 | expected_default_number_of_groups = 4 |
| 107 | self.assertEquals(expected_default_number_of_groups, len(groups['items'])) |
ade@google.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 108 | |
| 109 | def IGNORE__test_can_get_followees_of_user(self): |
ade@google.com | 6a05e7d | 2010-10-05 00:29:51 -0700 | [diff] [blame^] | 110 | # This currently fails with: |
| 111 | # Attempting to access self view of a different user. |
| 112 | # and URL: |
| 113 | buzz = build('buzz', 'v1', http=self.http) |
ade@google.com | 75fdddd | 2010-09-29 16:44:00 +0100 | [diff] [blame] | 114 | following = buzz.groups().get(userId='googlebuzz', groupId='@following').execute() |
| 115 | |
| 116 | self.assertEquals(17, len(following)) |
| 117 | |
Joe Gregorio | db849af | 2010-09-22 16:53:59 -0400 | [diff] [blame] | 118 | if __name__ == '__main__': |
| 119 | unittest.main() |