blob: 6aeb3d13ba33e331bb1de4697d08890b2d242ad8 [file] [log] [blame]
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -07001# Copyright 2016 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import datetime
16import json
17
18import mock
19import pytest
20import six
21from six.moves import http_client
22from six.moves import urllib
23
24from google.auth import exceptions
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070025from google.auth import transport
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -070026from google.oauth2 import _client
27
28
29def test__handle_error_response():
30 response_data = json.dumps({
31 'error': 'help',
32 'error_description': 'I\'m alive'})
33
34 with pytest.raises(exceptions.RefreshError) as excinfo:
35 _client._handle_error_response(response_data)
36
37 assert excinfo.match(r'help: I\'m alive')
38
39
40def test__handle_error_response_non_json():
41 response_data = 'Help, I\'m alive'
42
43 with pytest.raises(exceptions.RefreshError) as excinfo:
44 _client._handle_error_response(response_data)
45
46 assert excinfo.match(r'Help, I\'m alive')
47
48
49@mock.patch('google.auth._helpers.utcnow', return_value=datetime.datetime.min)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070050def test__parse_expiry(unused_utcnow):
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -070051 result = _client._parse_expiry({'expires_in': 500})
52 assert result == datetime.datetime.min + datetime.timedelta(seconds=500)
53
54
55def test__parse_expiry_none():
56 assert _client._parse_expiry({}) is None
57
58
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070059def make_request(response_data, status=http_client.OK):
60 response = mock.create_autospec(transport.Response, instance=True)
61 response.status = status
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -070062 response.data = json.dumps(response_data).encode('utf-8')
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070063 request = mock.create_autospec(transport.Request)
64 request.return_value = response
65 return request
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -070066
67
68def test__token_endpoint_request():
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070069 request = make_request({'test': 'response'})
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -070070
71 result = _client._token_endpoint_request(
72 request, 'http://example.com', {'test': 'params'})
73
74 # Check request call
75 request.assert_called_with(
76 method='POST',
77 url='http://example.com',
78 headers={'content-type': 'application/x-www-form-urlencoded'},
79 body='test=params')
80
81 # Check result
82 assert result == {'test': 'response'}
83
84
85def test__token_endpoint_request_error():
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070086 request = make_request({}, status=http_client.BAD_REQUEST)
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -070087
88 with pytest.raises(exceptions.RefreshError):
89 _client._token_endpoint_request(request, 'http://example.com', {})
90
91
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070092def verify_request_params(request, params):
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -070093 request_body = request.call_args[1]['body']
94 request_params = urllib.parse.parse_qs(request_body)
95
96 for key, value in six.iteritems(params):
97 assert request_params[key][0] == value
98
99
100@mock.patch('google.auth._helpers.utcnow', return_value=datetime.datetime.min)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700101def test_jwt_grant(utcnow):
102 request = make_request({
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -0700103 'access_token': 'token',
104 'expires_in': 500,
105 'extra': 'data'})
106
107 token, expiry, extra_data = _client.jwt_grant(
108 request, 'http://example.com', 'assertion_value')
109
110 # Check request call
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700111 verify_request_params(request, {
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -0700112 'grant_type': _client._JWT_GRANT_TYPE,
113 'assertion': 'assertion_value'
114 })
115
116 # Check result
117 assert token == 'token'
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700118 assert expiry == utcnow() + datetime.timedelta(seconds=500)
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -0700119 assert extra_data['extra'] == 'data'
120
121
122def test_jwt_grant_no_access_token():
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700123 request = make_request({
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -0700124 # No access token.
125 'expires_in': 500,
126 'extra': 'data'})
127
128 with pytest.raises(exceptions.RefreshError):
129 _client.jwt_grant(request, 'http://example.com', 'assertion_value')
130
131
132@mock.patch('google.auth._helpers.utcnow', return_value=datetime.datetime.min)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700133def test_refresh_grant(unused_utcnow):
134 request = make_request({
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -0700135 'access_token': 'token',
136 'refresh_token': 'new_refresh_token',
137 'expires_in': 500,
138 'extra': 'data'})
139
140 token, refresh_token, expiry, extra_data = _client.refresh_grant(
141 request, 'http://example.com', 'refresh_token', 'client_id',
142 'client_secret')
143
144 # Check request call
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700145 verify_request_params(request, {
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -0700146 'grant_type': _client._REFRESH_GRANT_TYPE,
147 'refresh_token': 'refresh_token',
148 'client_id': 'client_id',
149 'client_secret': 'client_secret'
150 })
151
152 # Check result
153 assert token == 'token'
154 assert refresh_token == 'new_refresh_token'
155 assert expiry == datetime.datetime.min + datetime.timedelta(seconds=500)
156 assert extra_data['extra'] == 'data'
157
158
159def test_refresh_grant_no_access_token():
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700160 request = make_request({
Jon Wayne Parrott123a48b2016-10-07 15:32:49 -0700161 # No access token.
162 'refresh_token': 'new_refresh_token',
163 'expires_in': 500,
164 'extra': 'data'})
165
166 with pytest.raises(exceptions.RefreshError):
167 _client.refresh_grant(
168 request, 'http://example.com', 'refresh_token', 'client_id',
169 'client_secret')