blob: 4d56c2663a61595e0dca528667b5011cdec9c53a [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
25import unittest
26
27from apiclient.http import HttpMockSequence
28from apiclient.oauth import CredentialsInvalidError
29from apiclient.oauth import MissingParameter
30from apiclient.oauth import TwoLeggedOAuthCredentials
31
32
33class TwoLeggedOAuthCredentialsTests(unittest.TestCase):
34
35 def setUp(self):
36 client_id = "some_client_id"
37 client_secret = "cOuDdkfjxxnv+"
38 user_agent = "sample/1.0"
39 self.credentials = TwoLeggedOAuthCredentials(client_id, client_secret,
40 user_agent)
41 self.credentials.requestor = 'test@example.org'
42
43 def test_invalid_token(self):
44 http = HttpMockSequence([
45 ({'status': '401'}, ''),
46 ])
47 http = self.credentials.authorize(http)
48 try:
49 resp, content = http.request("http://example.com")
50 self.fail('should raise CredentialsInvalidError')
51 except CredentialsInvalidError:
52 pass
53
54 def test_no_requestor(self):
55 self.credentials.requestor = None
56 http = HttpMockSequence([
57 ({'status': '401'}, ''),
58 ])
59 http = self.credentials.authorize(http)
60 try:
61 resp, content = http.request("http://example.com")
62 self.fail('should raise MissingParameter')
63 except MissingParameter:
64 pass
65
66 def test_add_requestor_to_uri(self):
67 http = HttpMockSequence([
68 ({'status': '200'}, 'echo_request_uri'),
69 ])
70 http = self.credentials.authorize(http)
71 resp, content = http.request("http://example.com")
72 self.assertEqual('http://example.com?xoauth_requestor_id=test%40example.org',
73 content)
74
75if __name__ == '__main__':
76 unittest.main()