blob: 6403946d251ffb24e83dfb80315c436686615ecb [file] [log] [blame]
ade@google.com46179d32010-08-21 00:00:03 +01001#!/usr/bin/python2.4
2#
3# Copyright 2010 Google Inc. All Rights Reserved.
4
5"""Discovery document tests
6
7Functional tests that verify we can retrieve data from existing services.
8
9These tests are read-only in order to ensure they're repeatable. They also
10only work with publicly visible data in order to avoid dealing with OAuth.
11"""
ade@google.com2ab0de72010-09-27 23:26:54 +010012import httplib2
ade@google.com46179d32010-08-21 00:00:03 +010013
14__author__ = 'ade@google.com (Ade Oshineye)'
15
16from apiclient.discovery import build
ade@google.com2ab0de72010-09-27 23:26:54 +010017import httplib2
ade@google.com46179d32010-08-21 00:00:03 +010018import logging
ade@google.com6a05e7d2010-10-05 00:29:51 -070019import pickle
ade@google.com2ab0de72010-09-27 23:26:54 +010020import os
ade@google.com46179d32010-08-21 00:00:03 +010021import unittest
22
ade@google.com2ab0de72010-09-27 23:26:54 +010023# TODO(ade) Remove this mock once the bug in the discovery document is fixed
24DATA_DIR = os.path.join(logging.os.path.dirname(__file__), '../tests/data')
25class 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.com46179d32010-08-21 00:00:03 +010036class BuzzFunctionalTest(unittest.TestCase):
37 def test_can_get_buzz_activities_with_many_params(self):
38 buzz = build('buzz', 'v1')
39 max_results = 2
Joe Gregorioc5c5a372010-09-22 11:42:32 -040040 actcol = buzz.activities()
41 activities = actcol.list(userId='googlebuzz', scope='@self',
42 max_comments=max_results*2 ,max_liked=max_results*3,
Joe Gregoriodb849af2010-09-22 16:53:59 -040043 max_results=max_results).execute()
44 activity_count = len(activities['items'])
ade@google.com46179d32010-08-21 00:00:03 +010045 self.assertEquals(max_results, activity_count)
Joe Gregorioc5c5a372010-09-22 11:42:32 -040046
Joe Gregoriodb849af2010-09-22 16:53:59 -040047 activities = actcol.list_next(activities).execute()
48 activity_count = len(activities['items'])
49 self.assertEquals(max_results, activity_count)
50
ade@google.com2ab0de72010-09-27 23:26:54 +010051 def test_can_get_multiple_pages_of_buzz_activities(self):
ade@google.com973d1a62010-09-23 21:21:21 +010052 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.com2ab0de72010-09-27 23:26:54 +010063 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.comd6d6f2f2010-09-28 07:37:25 +010079 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.com75fdddd2010-09-29 16:44:00 +010089
90class BuzzAuthenticatedFunctionalTest(unittest.TestCase):
ade@google.com6a05e7d2010-10-05 00:29:51 -070091 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.com75fdddd2010-09-29 16:44:00 +010097
ade@google.com6a05e7d2010-10-05 00:29:51 -070098 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.com75fdddd2010-09-29 16:44:00 +0100108
109 def IGNORE__test_can_get_followees_of_user(self):
ade@google.com6a05e7d2010-10-05 00:29:51 -0700110 # 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.com75fdddd2010-09-29 16:44:00 +0100114 following = buzz.groups().get(userId='googlebuzz', groupId='@following').execute()
115
116 self.assertEquals(17, len(following))
117
Joe Gregoriodb849af2010-09-22 16:53:59 -0400118if __name__ == '__main__':
119 unittest.main()