blob: 5b458a5f95bed4368c3fcd97daca3bd54b066fe6 [file] [log] [blame]
Joe Gregorio0bc70912011-05-24 15:30:49 -04001#!/usr/bin/python2.4
2#
3# Copyright 2010 Google Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18"""Oauth tests
19
20Unit tests for apiclient.oauth.
21"""
22
23__author__ = 'jcgregorio@google.com (Joe Gregorio)'
24
Joe Gregorio7cbceab2011-06-27 10:46:54 -040025# Do not remove the httplib2 import
26import httplib2
Joe Gregorio0bc70912011-05-24 15:30:49 -040027import unittest
28
29from apiclient.http import HttpMockSequence
30from apiclient.oauth import CredentialsInvalidError
31from apiclient.oauth import MissingParameter
32from apiclient.oauth import TwoLeggedOAuthCredentials
33
34
35class TwoLeggedOAuthCredentialsTests(unittest.TestCase):
36
37 def setUp(self):
38 client_id = "some_client_id"
39 client_secret = "cOuDdkfjxxnv+"
40 user_agent = "sample/1.0"
41 self.credentials = TwoLeggedOAuthCredentials(client_id, client_secret,
42 user_agent)
43 self.credentials.requestor = 'test@example.org'
44
45 def test_invalid_token(self):
46 http = HttpMockSequence([
47 ({'status': '401'}, ''),
48 ])
49 http = self.credentials.authorize(http)
50 try:
51 resp, content = http.request("http://example.com")
52 self.fail('should raise CredentialsInvalidError')
53 except CredentialsInvalidError:
54 pass
55
56 def test_no_requestor(self):
57 self.credentials.requestor = None
58 http = HttpMockSequence([
59 ({'status': '401'}, ''),
60 ])
61 http = self.credentials.authorize(http)
62 try:
63 resp, content = http.request("http://example.com")
64 self.fail('should raise MissingParameter')
65 except MissingParameter:
66 pass
67
68 def test_add_requestor_to_uri(self):
69 http = HttpMockSequence([
70 ({'status': '200'}, 'echo_request_uri'),
71 ])
72 http = self.credentials.authorize(http)
73 resp, content = http.request("http://example.com")
74 self.assertEqual('http://example.com?xoauth_requestor_id=test%40example.org',
75 content)
76
77if __name__ == '__main__':
78 unittest.main()