blob: 417807a9ca531983d81817ee6c961b47fec32629 [file] [log] [blame]
Joe Gregorioccc79542011-02-19 00:05:26 -05001#!/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
Joe Gregorio0bc70912011-05-24 15:30:49 -040018"""Oauth2client tests
Joe Gregorioccc79542011-02-19 00:05:26 -050019
Joe Gregorio0bc70912011-05-24 15:30:49 -040020Unit tests for oauth2client.
Joe Gregorioccc79542011-02-19 00:05:26 -050021"""
22
23__author__ = 'jcgregorio@google.com (Joe Gregorio)'
24
Joe Gregorioe1de4162011-02-23 11:30:29 -050025import httplib2
Joe Gregorioccc79542011-02-19 00:05:26 -050026import unittest
27import urlparse
Joe Gregorioe1de4162011-02-23 11:30:29 -050028
Joe Gregorioccc79542011-02-19 00:05:26 -050029try:
30 from urlparse import parse_qs
31except ImportError:
32 from cgi import parse_qs
33
34from apiclient.http import HttpMockSequence
35from oauth2client.client import AccessTokenCredentials
36from oauth2client.client import AccessTokenCredentialsError
37from oauth2client.client import AccessTokenRefreshError
JacobMoshenko8e905102011-06-20 09:53:10 -040038from oauth2client.client import AssertionCredentials
Joe Gregorioccc79542011-02-19 00:05:26 -050039from oauth2client.client import FlowExchangeError
40from oauth2client.client import OAuth2Credentials
41from oauth2client.client import OAuth2WebServerFlow
42
43
44class OAuth2CredentialsTests(unittest.TestCase):
45
46 def setUp(self):
47 access_token = "foo"
48 client_id = "some_client_id"
49 client_secret = "cOuDdkfjxxnv+"
50 refresh_token = "1/0/a.df219fjls0"
51 token_expiry = "ignored"
52 token_uri = "https://www.google.com/accounts/o8/oauth2/token"
53 user_agent = "refresh_checker/1.0"
54 self.credentials = OAuth2Credentials(
55 access_token, client_id, client_secret,
56 refresh_token, token_expiry, token_uri,
57 user_agent)
58
59 def test_token_refresh_success(self):
60 http = HttpMockSequence([
61 ({'status': '401'}, ''),
62 ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'),
63 ({'status': '200'}, 'echo_request_headers'),
64 ])
65 http = self.credentials.authorize(http)
66 resp, content = http.request("http://example.com")
67 self.assertEqual(content['authorization'], 'OAuth 1/3w')
68
69 def test_token_refresh_failure(self):
70 http = HttpMockSequence([
71 ({'status': '401'}, ''),
72 ({'status': '400'}, '{"error":"access_denied"}'),
73 ])
74 http = self.credentials.authorize(http)
75 try:
76 http.request("http://example.com")
77 self.fail("should raise AccessTokenRefreshError exception")
78 except AccessTokenRefreshError:
79 pass
80
81 def test_non_401_error_response(self):
82 http = HttpMockSequence([
83 ({'status': '400'}, ''),
84 ])
85 http = self.credentials.authorize(http)
86 resp, content = http.request("http://example.com")
87 self.assertEqual(400, resp.status)
88
89
90class AccessTokenCredentialsTests(unittest.TestCase):
91
92 def setUp(self):
93 access_token = "foo"
94 user_agent = "refresh_checker/1.0"
95 self.credentials = AccessTokenCredentials(access_token, user_agent)
96
97 def test_token_refresh_success(self):
98 http = HttpMockSequence([
99 ({'status': '401'}, ''),
100 ])
101 http = self.credentials.authorize(http)
102 try:
103 resp, content = http.request("http://example.com")
104 self.fail("should throw exception if token expires")
105 except AccessTokenCredentialsError:
106 pass
107 except Exception:
108 self.fail("should only throw AccessTokenCredentialsError")
109
110 def test_non_401_error_response(self):
111 http = HttpMockSequence([
112 ({'status': '400'}, ''),
113 ])
114 http = self.credentials.authorize(http)
115 resp, content = http.request("http://example.com")
116 self.assertEqual(400, resp.status)
117
118
JacobMoshenko8e905102011-06-20 09:53:10 -0400119class TestAssertionCredentials(unittest.TestCase):
120 assertion_text = "This is the assertion"
121 assertion_type = "http://www.google.com/assertionType"
122
123 class AssertionCredentialsTestImpl(AssertionCredentials):
124
125 def _generate_assertion(self):
126 return TestAssertionCredentials.assertion_text
127
128 def setUp(self):
129 user_agent = "fun/2.0"
130 self.credentials = self.AssertionCredentialsTestImpl(self.assertion_type,
131 user_agent)
132
133 def test_assertion_body(self):
134 body = urlparse.parse_qs(self.credentials._generate_refresh_request_body())
135 self.assertEqual(body['assertion'][0], self.assertion_text)
136 self.assertEqual(body['assertion_type'][0], self.assertion_type)
137
138 def test_assertion_refresh(self):
139 http = HttpMockSequence([
140 ({'status': '200'}, '{"access_token":"1/3w"}'),
141 ({'status': '200'}, 'echo_request_headers'),
142 ])
143 http = self.credentials.authorize(http)
144 resp, content = http.request("http://example.com")
145 self.assertEqual(content['authorization'], 'OAuth 1/3w')
146
147
Joe Gregorioccc79542011-02-19 00:05:26 -0500148class OAuth2WebServerFlowTest(unittest.TestCase):
149
150 def setUp(self):
151 self.flow = OAuth2WebServerFlow(
152 client_id='client_id+1',
153 client_secret='secret+1',
154 scope='foo',
155 user_agent='unittest-sample/1.0',
156 )
157
158 def test_construct_authorize_url(self):
159 authorize_url = self.flow.step1_get_authorize_url('oob')
160
161 parsed = urlparse.urlparse(authorize_url)
162 q = parse_qs(parsed[4])
163 self.assertEqual(q['client_id'][0], 'client_id+1')
164 self.assertEqual(q['response_type'][0], 'code')
165 self.assertEqual(q['scope'][0], 'foo')
166 self.assertEqual(q['redirect_uri'][0], 'oob')
167
168 def test_exchange_failure(self):
169 http = HttpMockSequence([
JacobMoshenko8e905102011-06-20 09:53:10 -0400170 ({'status': '400'}, '{"error":"invalid_request"}'),
Joe Gregorioccc79542011-02-19 00:05:26 -0500171 ])
172
173 try:
174 credentials = self.flow.step2_exchange('some random code', http)
175 self.fail("should raise exception if exchange doesn't get 200")
176 except FlowExchangeError:
177 pass
178
179 def test_exchange_success(self):
180 http = HttpMockSequence([
181 ({'status': '200'},
182 """{ "access_token":"SlAV32hkKG",
183 "expires_in":3600,
184 "refresh_token":"8xLOxBtZp8" }"""),
185 ])
186
187 credentials = self.flow.step2_exchange('some random code', http)
188 self.assertEqual(credentials.access_token, 'SlAV32hkKG')
189 self.assertNotEqual(credentials.token_expiry, None)
190 self.assertEqual(credentials.refresh_token, '8xLOxBtZp8')
191
Joe Gregorioccc79542011-02-19 00:05:26 -0500192 def test_exchange_no_expires_in(self):
193 http = HttpMockSequence([
194 ({'status': '200'}, """{ "access_token":"SlAV32hkKG",
195 "refresh_token":"8xLOxBtZp8" }"""),
196 ])
197
198 credentials = self.flow.step2_exchange('some random code', http)
199 self.assertEqual(credentials.token_expiry, None)
200
201
202if __name__ == '__main__':
203 unittest.main()