C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 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 | |
| 15 | import datetime |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 16 | import http.client |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 17 | import json |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 18 | import os |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 19 | import urllib |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 20 | |
| 21 | import mock |
| 22 | import pytest |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 23 | |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 24 | from google.auth import _helpers |
| 25 | from google.auth import crypt |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 26 | from google.auth import exceptions |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 27 | from google.auth import jwt |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 28 | from google.auth import transport |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 29 | from google.oauth2 import _client |
| 30 | |
| 31 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 32 | DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data") |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 33 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 34 | with open(os.path.join(DATA_DIR, "privatekey.pem"), "rb") as fh: |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 35 | PRIVATE_KEY_BYTES = fh.read() |
| 36 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 37 | SIGNER = crypt.RSASigner.from_string(PRIVATE_KEY_BYTES, "1") |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 38 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 39 | SCOPES_AS_LIST = [ |
| 40 | "https://www.googleapis.com/auth/pubsub", |
| 41 | "https://www.googleapis.com/auth/logging.write", |
| 42 | ] |
| 43 | SCOPES_AS_STRING = ( |
| 44 | "https://www.googleapis.com/auth/pubsub" |
| 45 | " https://www.googleapis.com/auth/logging.write" |
| 46 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 47 | |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 48 | |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 49 | def test__handle_error_response(): |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 50 | response_data = {"error": "help", "error_description": "I'm alive"} |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 51 | |
| 52 | with pytest.raises(exceptions.RefreshError) as excinfo: |
| 53 | _client._handle_error_response(response_data) |
| 54 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 55 | assert excinfo.match(r"help: I\'m alive") |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 56 | |
| 57 | |
| 58 | def test__handle_error_response_non_json(): |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 59 | response_data = {"foo": "bar"} |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 60 | |
| 61 | with pytest.raises(exceptions.RefreshError) as excinfo: |
| 62 | _client._handle_error_response(response_data) |
| 63 | |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 64 | assert excinfo.match(r"{\"foo\": \"bar\"}") |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 65 | |
| 66 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 67 | @mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 68 | def test__parse_expiry(unused_utcnow): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 69 | result = _client._parse_expiry({"expires_in": 500}) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 70 | assert result == datetime.datetime.min + datetime.timedelta(seconds=500) |
| 71 | |
| 72 | |
| 73 | def test__parse_expiry_none(): |
| 74 | assert _client._parse_expiry({}) is None |
| 75 | |
| 76 | |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 77 | def make_request(response_data, status=http.client.OK): |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 78 | response = mock.create_autospec(transport.Response, instance=True) |
| 79 | response.status = status |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 80 | response.data = json.dumps(response_data).encode("utf-8") |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 81 | request = mock.create_autospec(transport.Request) |
| 82 | request.return_value = response |
| 83 | return request |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 84 | |
| 85 | |
| 86 | def test__token_endpoint_request(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 87 | request = make_request({"test": "response"}) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 88 | |
| 89 | result = _client._token_endpoint_request( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 90 | request, "http://example.com", {"test": "params"} |
| 91 | ) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 92 | |
| 93 | # Check request call |
| 94 | request.assert_called_with( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 95 | method="POST", |
| 96 | url="http://example.com", |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 97 | headers={"Content-Type": "application/x-www-form-urlencoded"}, |
Bu Sun Kim | a57a770 | 2020-01-10 13:17:34 -0800 | [diff] [blame] | 98 | body="test=params".encode("utf-8"), |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 99 | ) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 100 | |
| 101 | # Check result |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 102 | assert result == {"test": "response"} |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 103 | |
| 104 | |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 105 | def test__token_endpoint_request_use_json(): |
| 106 | request = make_request({"test": "response"}) |
| 107 | |
| 108 | result = _client._token_endpoint_request( |
| 109 | request, |
| 110 | "http://example.com", |
| 111 | {"test": "params"}, |
| 112 | access_token="access_token", |
| 113 | use_json=True, |
| 114 | ) |
| 115 | |
| 116 | # Check request call |
| 117 | request.assert_called_with( |
| 118 | method="POST", |
| 119 | url="http://example.com", |
| 120 | headers={ |
| 121 | "Content-Type": "application/json", |
| 122 | "Authorization": "Bearer access_token", |
| 123 | }, |
| 124 | body=b'{"test": "params"}', |
| 125 | ) |
| 126 | |
| 127 | # Check result |
| 128 | assert result == {"test": "response"} |
| 129 | |
| 130 | |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 131 | def test__token_endpoint_request_error(): |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 132 | request = make_request({}, status=http.client.BAD_REQUEST) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 133 | |
| 134 | with pytest.raises(exceptions.RefreshError): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 135 | _client._token_endpoint_request(request, "http://example.com", {}) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 136 | |
| 137 | |
Anjali Doneria | eae1dcb | 2019-09-09 16:36:10 -0700 | [diff] [blame] | 138 | def test__token_endpoint_request_internal_failure_error(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 139 | request = make_request( |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 140 | {"error_description": "internal_failure"}, status=http.client.BAD_REQUEST |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 141 | ) |
Anjali Doneria | eae1dcb | 2019-09-09 16:36:10 -0700 | [diff] [blame] | 142 | |
| 143 | with pytest.raises(exceptions.RefreshError): |
| 144 | _client._token_endpoint_request( |
Georgy Savva | 46bb58e | 2019-11-13 22:21:57 +0300 | [diff] [blame] | 145 | request, "http://example.com", {"error_description": "internal_failure"} |
| 146 | ) |
| 147 | |
| 148 | request = make_request( |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 149 | {"error": "internal_failure"}, status=http.client.BAD_REQUEST |
Georgy Savva | 46bb58e | 2019-11-13 22:21:57 +0300 | [diff] [blame] | 150 | ) |
| 151 | |
| 152 | with pytest.raises(exceptions.RefreshError): |
| 153 | _client._token_endpoint_request( |
| 154 | request, "http://example.com", {"error": "internal_failure"} |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 155 | ) |
Anjali Doneria | eae1dcb | 2019-09-09 16:36:10 -0700 | [diff] [blame] | 156 | |
| 157 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 158 | def verify_request_params(request, params): |
Bu Sun Kim | a57a770 | 2020-01-10 13:17:34 -0800 | [diff] [blame] | 159 | request_body = request.call_args[1]["body"].decode("utf-8") |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 160 | request_params = urllib.parse.parse_qs(request_body) |
| 161 | |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 162 | for key, value in params.items(): |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 163 | assert request_params[key][0] == value |
| 164 | |
| 165 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 166 | @mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 167 | def test_jwt_grant(utcnow): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 168 | request = make_request( |
| 169 | {"access_token": "token", "expires_in": 500, "extra": "data"} |
| 170 | ) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 171 | |
| 172 | token, expiry, extra_data = _client.jwt_grant( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 173 | request, "http://example.com", "assertion_value" |
| 174 | ) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 175 | |
| 176 | # Check request call |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 177 | verify_request_params( |
| 178 | request, {"grant_type": _client._JWT_GRANT_TYPE, "assertion": "assertion_value"} |
| 179 | ) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 180 | |
| 181 | # Check result |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 182 | assert token == "token" |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 183 | assert expiry == utcnow() + datetime.timedelta(seconds=500) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 184 | assert extra_data["extra"] == "data" |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 185 | |
| 186 | |
| 187 | def test_jwt_grant_no_access_token(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 188 | request = make_request( |
| 189 | { |
| 190 | # No access token. |
| 191 | "expires_in": 500, |
| 192 | "extra": "data", |
| 193 | } |
| 194 | ) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 195 | |
| 196 | with pytest.raises(exceptions.RefreshError): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 197 | _client.jwt_grant(request, "http://example.com", "assertion_value") |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 198 | |
| 199 | |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 200 | def test_id_token_jwt_grant(): |
| 201 | now = _helpers.utcnow() |
| 202 | id_token_expiry = _helpers.datetime_to_secs(now) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 203 | id_token = jwt.encode(SIGNER, {"exp": id_token_expiry}).decode("utf-8") |
| 204 | request = make_request({"id_token": id_token, "extra": "data"}) |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 205 | |
| 206 | token, expiry, extra_data = _client.id_token_jwt_grant( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 207 | request, "http://example.com", "assertion_value" |
| 208 | ) |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 209 | |
| 210 | # Check request call |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 211 | verify_request_params( |
| 212 | request, {"grant_type": _client._JWT_GRANT_TYPE, "assertion": "assertion_value"} |
| 213 | ) |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 214 | |
| 215 | # Check result |
| 216 | assert token == id_token |
| 217 | # JWT does not store microseconds |
| 218 | now = now.replace(microsecond=0) |
| 219 | assert expiry == now |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 220 | assert extra_data["extra"] == "data" |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 221 | |
| 222 | |
| 223 | def test_id_token_jwt_grant_no_access_token(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 224 | request = make_request( |
| 225 | { |
| 226 | # No access token. |
| 227 | "expires_in": 500, |
| 228 | "extra": "data", |
| 229 | } |
| 230 | ) |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 231 | |
| 232 | with pytest.raises(exceptions.RefreshError): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 233 | _client.id_token_jwt_grant(request, "http://example.com", "assertion_value") |
Christophe Taton | b649b43 | 2018-02-08 14:12:23 -0800 | [diff] [blame] | 234 | |
| 235 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 236 | @mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 237 | def test_refresh_grant(unused_utcnow): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 238 | request = make_request( |
| 239 | { |
| 240 | "access_token": "token", |
| 241 | "refresh_token": "new_refresh_token", |
| 242 | "expires_in": 500, |
| 243 | "extra": "data", |
| 244 | } |
| 245 | ) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 246 | |
| 247 | token, refresh_token, expiry, extra_data = _client.refresh_grant( |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 248 | request, |
| 249 | "http://example.com", |
| 250 | "refresh_token", |
| 251 | "client_id", |
| 252 | "client_secret", |
| 253 | rapt_token="rapt_token", |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 254 | ) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 255 | |
| 256 | # Check request call |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 257 | verify_request_params( |
| 258 | request, |
| 259 | { |
| 260 | "grant_type": _client._REFRESH_GRANT_TYPE, |
| 261 | "refresh_token": "refresh_token", |
| 262 | "client_id": "client_id", |
| 263 | "client_secret": "client_secret", |
arithmetic1728 | 82293fe | 2021-04-14 11:22:13 -0700 | [diff] [blame] | 264 | "rapt": "rapt_token", |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 265 | }, |
| 266 | ) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 267 | |
| 268 | # Check result |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 269 | assert token == "token" |
| 270 | assert refresh_token == "new_refresh_token" |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 271 | assert expiry == datetime.datetime.min + datetime.timedelta(seconds=500) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 272 | assert extra_data["extra"] == "data" |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 273 | |
| 274 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 275 | @mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 276 | def test_refresh_grant_with_scopes(unused_utcnow): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 277 | request = make_request( |
| 278 | { |
| 279 | "access_token": "token", |
| 280 | "refresh_token": "new_refresh_token", |
| 281 | "expires_in": 500, |
| 282 | "extra": "data", |
| 283 | "scope": SCOPES_AS_STRING, |
| 284 | } |
| 285 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 286 | |
| 287 | token, refresh_token, expiry, extra_data = _client.refresh_grant( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 288 | request, |
| 289 | "http://example.com", |
| 290 | "refresh_token", |
| 291 | "client_id", |
| 292 | "client_secret", |
| 293 | SCOPES_AS_LIST, |
| 294 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 295 | |
| 296 | # Check request call. |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 297 | verify_request_params( |
| 298 | request, |
| 299 | { |
| 300 | "grant_type": _client._REFRESH_GRANT_TYPE, |
| 301 | "refresh_token": "refresh_token", |
| 302 | "client_id": "client_id", |
| 303 | "client_secret": "client_secret", |
| 304 | "scope": SCOPES_AS_STRING, |
| 305 | }, |
| 306 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 307 | |
| 308 | # Check result. |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 309 | assert token == "token" |
| 310 | assert refresh_token == "new_refresh_token" |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 311 | assert expiry == datetime.datetime.min + datetime.timedelta(seconds=500) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 312 | assert extra_data["extra"] == "data" |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 313 | |
| 314 | |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 315 | def test_refresh_grant_no_access_token(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 316 | request = make_request( |
| 317 | { |
| 318 | # No access token. |
| 319 | "refresh_token": "new_refresh_token", |
| 320 | "expires_in": 500, |
| 321 | "extra": "data", |
| 322 | } |
| 323 | ) |
Jon Wayne Parrott | 123a48b | 2016-10-07 15:32:49 -0700 | [diff] [blame] | 324 | |
| 325 | with pytest.raises(exceptions.RefreshError): |
| 326 | _client.refresh_grant( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 327 | request, "http://example.com", "refresh_token", "client_id", "client_secret" |
| 328 | ) |