C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -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 |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 16 | import json |
| 17 | import os |
Bu Sun Kim | 32d71a5 | 2019-12-18 11:30:46 -0800 | [diff] [blame] | 18 | import pickle |
| 19 | import sys |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 20 | |
| 21 | import mock |
Thea Flowers | 118c048 | 2018-05-24 13:34:07 -0700 | [diff] [blame] | 22 | import pytest |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 23 | |
| 24 | from google.auth import _helpers |
Thea Flowers | 118c048 | 2018-05-24 13:34:07 -0700 | [diff] [blame] | 25 | from google.auth import exceptions |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 26 | from google.auth import transport |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 27 | from google.oauth2 import credentials |
| 28 | |
| 29 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 30 | DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data") |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 31 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 32 | AUTH_USER_JSON_FILE = os.path.join(DATA_DIR, "authorized_user.json") |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 33 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 34 | with open(AUTH_USER_JSON_FILE, "r") as fh: |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 35 | AUTH_USER_INFO = json.load(fh) |
| 36 | |
| 37 | |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 38 | class TestCredentials(object): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 39 | TOKEN_URI = "https://example.com/oauth2/token" |
| 40 | REFRESH_TOKEN = "refresh_token" |
| 41 | CLIENT_ID = "client_id" |
| 42 | CLIENT_SECRET = "client_secret" |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 43 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 44 | @classmethod |
| 45 | def make_credentials(cls): |
| 46 | return credentials.Credentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 47 | token=None, |
| 48 | refresh_token=cls.REFRESH_TOKEN, |
| 49 | token_uri=cls.TOKEN_URI, |
| 50 | client_id=cls.CLIENT_ID, |
| 51 | client_secret=cls.CLIENT_SECRET, |
| 52 | ) |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 53 | |
| 54 | def test_default_state(self): |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 55 | credentials = self.make_credentials() |
| 56 | assert not credentials.valid |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 57 | # Expiration hasn't been set yet |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 58 | assert not credentials.expired |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 59 | # Scopes aren't required for these credentials |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 60 | assert not credentials.requires_scopes |
Jon Wayne Parrott | 2d0549a | 2017-03-01 09:27:16 -0800 | [diff] [blame] | 61 | # Test properties |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 62 | assert credentials.refresh_token == self.REFRESH_TOKEN |
| 63 | assert credentials.token_uri == self.TOKEN_URI |
| 64 | assert credentials.client_id == self.CLIENT_ID |
| 65 | assert credentials.client_secret == self.CLIENT_SECRET |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 66 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 67 | @mock.patch("google.oauth2._client.refresh_grant", autospec=True) |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 68 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 69 | "google.auth._helpers.utcnow", |
| 70 | return_value=datetime.datetime.min + _helpers.CLOCK_SKEW, |
| 71 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 72 | def test_refresh_success(self, unused_utcnow, refresh_grant): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 73 | token = "token" |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 74 | expiry = _helpers.utcnow() + datetime.timedelta(seconds=500) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 75 | grant_response = {"id_token": mock.sentinel.id_token} |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 76 | refresh_grant.return_value = ( |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 77 | # Access token |
| 78 | token, |
| 79 | # New refresh token |
| 80 | None, |
| 81 | # Expiry, |
| 82 | expiry, |
| 83 | # Extra data |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 84 | grant_response, |
| 85 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 86 | |
| 87 | request = mock.create_autospec(transport.Request) |
| 88 | credentials = self.make_credentials() |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 89 | |
| 90 | # Refresh credentials |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 91 | credentials.refresh(request) |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 92 | |
| 93 | # Check jwt grant call. |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 94 | refresh_grant.assert_called_with( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 95 | request, |
| 96 | self.TOKEN_URI, |
| 97 | self.REFRESH_TOKEN, |
| 98 | self.CLIENT_ID, |
| 99 | self.CLIENT_SECRET, |
| 100 | None, |
| 101 | ) |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 102 | |
| 103 | # Check that the credentials have the token and expiry |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 104 | assert credentials.token == token |
| 105 | assert credentials.expiry == expiry |
| 106 | assert credentials.id_token == mock.sentinel.id_token |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 107 | |
| 108 | # Check that the credentials are valid (have a token and are not |
| 109 | # expired) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 110 | assert credentials.valid |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 111 | |
Thea Flowers | 118c048 | 2018-05-24 13:34:07 -0700 | [diff] [blame] | 112 | def test_refresh_no_refresh_token(self): |
| 113 | request = mock.create_autospec(transport.Request) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 114 | credentials_ = credentials.Credentials(token=None, refresh_token=None) |
Thea Flowers | 118c048 | 2018-05-24 13:34:07 -0700 | [diff] [blame] | 115 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 116 | with pytest.raises(exceptions.RefreshError, match="necessary fields"): |
Thea Flowers | 118c048 | 2018-05-24 13:34:07 -0700 | [diff] [blame] | 117 | credentials_.refresh(request) |
| 118 | |
| 119 | request.assert_not_called() |
| 120 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 121 | @mock.patch("google.oauth2._client.refresh_grant", autospec=True) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 122 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 123 | "google.auth._helpers.utcnow", |
| 124 | return_value=datetime.datetime.min + _helpers.CLOCK_SKEW, |
| 125 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 126 | def test_credentials_with_scopes_requested_refresh_success( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 127 | self, unused_utcnow, refresh_grant |
| 128 | ): |
| 129 | scopes = ["email", "profile"] |
| 130 | token = "token" |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 131 | expiry = _helpers.utcnow() + datetime.timedelta(seconds=500) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 132 | grant_response = {"id_token": mock.sentinel.id_token} |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 133 | refresh_grant.return_value = ( |
| 134 | # Access token |
| 135 | token, |
| 136 | # New refresh token |
| 137 | None, |
| 138 | # Expiry, |
| 139 | expiry, |
| 140 | # Extra data |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 141 | grant_response, |
| 142 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 143 | |
| 144 | request = mock.create_autospec(transport.Request) |
| 145 | creds = credentials.Credentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 146 | token=None, |
| 147 | refresh_token=self.REFRESH_TOKEN, |
| 148 | token_uri=self.TOKEN_URI, |
| 149 | client_id=self.CLIENT_ID, |
| 150 | client_secret=self.CLIENT_SECRET, |
| 151 | scopes=scopes, |
| 152 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 153 | |
| 154 | # Refresh credentials |
| 155 | creds.refresh(request) |
| 156 | |
| 157 | # Check jwt grant call. |
| 158 | refresh_grant.assert_called_with( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 159 | request, |
| 160 | self.TOKEN_URI, |
| 161 | self.REFRESH_TOKEN, |
| 162 | self.CLIENT_ID, |
| 163 | self.CLIENT_SECRET, |
| 164 | scopes, |
| 165 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 166 | |
| 167 | # Check that the credentials have the token and expiry |
| 168 | assert creds.token == token |
| 169 | assert creds.expiry == expiry |
| 170 | assert creds.id_token == mock.sentinel.id_token |
| 171 | assert creds.has_scopes(scopes) |
| 172 | |
| 173 | # Check that the credentials are valid (have a token and are not |
| 174 | # expired.) |
| 175 | assert creds.valid |
| 176 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 177 | @mock.patch("google.oauth2._client.refresh_grant", autospec=True) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 178 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 179 | "google.auth._helpers.utcnow", |
| 180 | return_value=datetime.datetime.min + _helpers.CLOCK_SKEW, |
| 181 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 182 | def test_credentials_with_scopes_returned_refresh_success( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 183 | self, unused_utcnow, refresh_grant |
| 184 | ): |
| 185 | scopes = ["email", "profile"] |
| 186 | token = "token" |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 187 | expiry = _helpers.utcnow() + datetime.timedelta(seconds=500) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 188 | grant_response = { |
| 189 | "id_token": mock.sentinel.id_token, |
| 190 | "scopes": " ".join(scopes), |
| 191 | } |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 192 | refresh_grant.return_value = ( |
| 193 | # Access token |
| 194 | token, |
| 195 | # New refresh token |
| 196 | None, |
| 197 | # Expiry, |
| 198 | expiry, |
| 199 | # Extra data |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 200 | grant_response, |
| 201 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 202 | |
| 203 | request = mock.create_autospec(transport.Request) |
| 204 | creds = credentials.Credentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 205 | token=None, |
| 206 | refresh_token=self.REFRESH_TOKEN, |
| 207 | token_uri=self.TOKEN_URI, |
| 208 | client_id=self.CLIENT_ID, |
| 209 | client_secret=self.CLIENT_SECRET, |
| 210 | scopes=scopes, |
| 211 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 212 | |
| 213 | # Refresh credentials |
| 214 | creds.refresh(request) |
| 215 | |
| 216 | # Check jwt grant call. |
| 217 | refresh_grant.assert_called_with( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 218 | request, |
| 219 | self.TOKEN_URI, |
| 220 | self.REFRESH_TOKEN, |
| 221 | self.CLIENT_ID, |
| 222 | self.CLIENT_SECRET, |
| 223 | scopes, |
| 224 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 225 | |
| 226 | # Check that the credentials have the token and expiry |
| 227 | assert creds.token == token |
| 228 | assert creds.expiry == expiry |
| 229 | assert creds.id_token == mock.sentinel.id_token |
| 230 | assert creds.has_scopes(scopes) |
| 231 | |
| 232 | # Check that the credentials are valid (have a token and are not |
| 233 | # expired.) |
| 234 | assert creds.valid |
| 235 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 236 | @mock.patch("google.oauth2._client.refresh_grant", autospec=True) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 237 | @mock.patch( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 238 | "google.auth._helpers.utcnow", |
| 239 | return_value=datetime.datetime.min + _helpers.CLOCK_SKEW, |
| 240 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 241 | def test_credentials_with_scopes_refresh_failure_raises_refresh_error( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 242 | self, unused_utcnow, refresh_grant |
| 243 | ): |
| 244 | scopes = ["email", "profile"] |
| 245 | scopes_returned = ["email"] |
| 246 | token = "token" |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 247 | expiry = _helpers.utcnow() + datetime.timedelta(seconds=500) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 248 | grant_response = { |
| 249 | "id_token": mock.sentinel.id_token, |
| 250 | "scopes": " ".join(scopes_returned), |
| 251 | } |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 252 | refresh_grant.return_value = ( |
| 253 | # Access token |
| 254 | token, |
| 255 | # New refresh token |
| 256 | None, |
| 257 | # Expiry, |
| 258 | expiry, |
| 259 | # Extra data |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 260 | grant_response, |
| 261 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 262 | |
| 263 | request = mock.create_autospec(transport.Request) |
| 264 | creds = credentials.Credentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 265 | token=None, |
| 266 | refresh_token=self.REFRESH_TOKEN, |
| 267 | token_uri=self.TOKEN_URI, |
| 268 | client_id=self.CLIENT_ID, |
| 269 | client_secret=self.CLIENT_SECRET, |
| 270 | scopes=scopes, |
| 271 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 272 | |
| 273 | # Refresh credentials |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 274 | with pytest.raises( |
| 275 | exceptions.RefreshError, match="Not all requested scopes were granted" |
| 276 | ): |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 277 | creds.refresh(request) |
| 278 | |
| 279 | # Check jwt grant call. |
| 280 | refresh_grant.assert_called_with( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 281 | request, |
| 282 | self.TOKEN_URI, |
| 283 | self.REFRESH_TOKEN, |
| 284 | self.CLIENT_ID, |
| 285 | self.CLIENT_SECRET, |
| 286 | scopes, |
| 287 | ) |
Eugene W. Foley | 49a18c4 | 2019-05-22 13:50:38 -0400 | [diff] [blame] | 288 | |
| 289 | # Check that the credentials have the token and expiry |
| 290 | assert creds.token == token |
| 291 | assert creds.expiry == expiry |
| 292 | assert creds.id_token == mock.sentinel.id_token |
| 293 | assert creds.has_scopes(scopes) |
| 294 | |
| 295 | # Check that the credentials are valid (have a token and are not |
| 296 | # expired.) |
| 297 | assert creds.valid |
| 298 | |
Bu Sun Kim | 32d71a5 | 2019-12-18 11:30:46 -0800 | [diff] [blame] | 299 | def test_apply_with_quota_project_id(self): |
| 300 | creds = credentials.Credentials( |
| 301 | token="token", |
| 302 | refresh_token=self.REFRESH_TOKEN, |
| 303 | token_uri=self.TOKEN_URI, |
| 304 | client_id=self.CLIENT_ID, |
| 305 | client_secret=self.CLIENT_SECRET, |
| 306 | quota_project_id="quota-project-123", |
| 307 | ) |
| 308 | |
| 309 | headers = {} |
| 310 | creds.apply(headers) |
| 311 | assert headers["x-goog-user-project"] == "quota-project-123" |
| 312 | |
| 313 | def test_apply_with_no_quota_project_id(self): |
| 314 | creds = credentials.Credentials( |
| 315 | token="token", |
| 316 | refresh_token=self.REFRESH_TOKEN, |
| 317 | token_uri=self.TOKEN_URI, |
| 318 | client_id=self.CLIENT_ID, |
| 319 | client_secret=self.CLIENT_SECRET, |
| 320 | ) |
| 321 | |
| 322 | headers = {} |
| 323 | creds.apply(headers) |
| 324 | assert "x-goog-user-project" not in headers |
| 325 | |
Bu Sun Kim | b12488c | 2020-06-10 13:44:07 -0700 | [diff] [blame] | 326 | def test_with_quota_project(self): |
| 327 | creds = credentials.Credentials( |
| 328 | token="token", |
| 329 | refresh_token=self.REFRESH_TOKEN, |
| 330 | token_uri=self.TOKEN_URI, |
| 331 | client_id=self.CLIENT_ID, |
| 332 | client_secret=self.CLIENT_SECRET, |
| 333 | quota_project_id="quota-project-123", |
| 334 | ) |
| 335 | |
| 336 | new_creds = creds.with_quota_project("new-project-456") |
| 337 | assert new_creds.quota_project_id == "new-project-456" |
| 338 | headers = {} |
| 339 | creds.apply(headers) |
| 340 | assert "x-goog-user-project" in headers |
| 341 | |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 342 | def test_from_authorized_user_info(self): |
| 343 | info = AUTH_USER_INFO.copy() |
| 344 | |
| 345 | creds = credentials.Credentials.from_authorized_user_info(info) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 346 | assert creds.client_secret == info["client_secret"] |
| 347 | assert creds.client_id == info["client_id"] |
| 348 | assert creds.refresh_token == info["refresh_token"] |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 349 | assert creds.token_uri == credentials._GOOGLE_OAUTH2_TOKEN_ENDPOINT |
| 350 | assert creds.scopes is None |
| 351 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 352 | scopes = ["email", "profile"] |
| 353 | creds = credentials.Credentials.from_authorized_user_info(info, scopes) |
| 354 | assert creds.client_secret == info["client_secret"] |
| 355 | assert creds.client_id == info["client_id"] |
| 356 | assert creds.refresh_token == info["refresh_token"] |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 357 | assert creds.token_uri == credentials._GOOGLE_OAUTH2_TOKEN_ENDPOINT |
| 358 | assert creds.scopes == scopes |
| 359 | |
| 360 | def test_from_authorized_user_file(self): |
| 361 | info = AUTH_USER_INFO.copy() |
| 362 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 363 | creds = credentials.Credentials.from_authorized_user_file(AUTH_USER_JSON_FILE) |
| 364 | assert creds.client_secret == info["client_secret"] |
| 365 | assert creds.client_id == info["client_id"] |
| 366 | assert creds.refresh_token == info["refresh_token"] |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 367 | assert creds.token_uri == credentials._GOOGLE_OAUTH2_TOKEN_ENDPOINT |
| 368 | assert creds.scopes is None |
| 369 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 370 | scopes = ["email", "profile"] |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 371 | creds = credentials.Credentials.from_authorized_user_file( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 372 | AUTH_USER_JSON_FILE, scopes |
| 373 | ) |
| 374 | assert creds.client_secret == info["client_secret"] |
| 375 | assert creds.client_id == info["client_id"] |
| 376 | assert creds.refresh_token == info["refresh_token"] |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 377 | assert creds.token_uri == credentials._GOOGLE_OAUTH2_TOKEN_ENDPOINT |
| 378 | assert creds.scopes == scopes |
patkasper | bfb1f8c | 2019-12-05 22:03:44 +0100 | [diff] [blame] | 379 | |
| 380 | def test_to_json(self): |
| 381 | info = AUTH_USER_INFO.copy() |
| 382 | creds = credentials.Credentials.from_authorized_user_info(info) |
| 383 | |
| 384 | # Test with no `strip` arg |
| 385 | json_output = creds.to_json() |
| 386 | json_asdict = json.loads(json_output) |
| 387 | assert json_asdict.get("token") == creds.token |
| 388 | assert json_asdict.get("refresh_token") == creds.refresh_token |
| 389 | assert json_asdict.get("token_uri") == creds.token_uri |
| 390 | assert json_asdict.get("client_id") == creds.client_id |
| 391 | assert json_asdict.get("scopes") == creds.scopes |
| 392 | assert json_asdict.get("client_secret") == creds.client_secret |
| 393 | |
| 394 | # Test with a `strip` arg |
| 395 | json_output = creds.to_json(strip=["client_secret"]) |
| 396 | json_asdict = json.loads(json_output) |
| 397 | assert json_asdict.get("token") == creds.token |
| 398 | assert json_asdict.get("refresh_token") == creds.refresh_token |
| 399 | assert json_asdict.get("token_uri") == creds.token_uri |
| 400 | assert json_asdict.get("client_id") == creds.client_id |
| 401 | assert json_asdict.get("scopes") == creds.scopes |
| 402 | assert json_asdict.get("client_secret") is None |
Bu Sun Kim | 32d71a5 | 2019-12-18 11:30:46 -0800 | [diff] [blame] | 403 | |
| 404 | def test_pickle_and_unpickle(self): |
| 405 | creds = self.make_credentials() |
| 406 | unpickled = pickle.loads(pickle.dumps(creds)) |
| 407 | |
| 408 | # make sure attributes aren't lost during pickling |
| 409 | assert list(creds.__dict__).sort() == list(unpickled.__dict__).sort() |
| 410 | |
| 411 | for attr in list(creds.__dict__): |
| 412 | assert getattr(creds, attr) == getattr(unpickled, attr) |
| 413 | |
| 414 | def test_pickle_with_missing_attribute(self): |
| 415 | creds = self.make_credentials() |
| 416 | |
| 417 | # remove an optional attribute before pickling |
| 418 | # this mimics a pickle created with a previous class definition with |
| 419 | # fewer attributes |
| 420 | del creds.__dict__["_quota_project_id"] |
| 421 | |
| 422 | unpickled = pickle.loads(pickle.dumps(creds)) |
| 423 | |
| 424 | # Attribute should be initialized by `__setstate__` |
| 425 | assert unpickled.quota_project_id is None |
| 426 | |
| 427 | # pickles are not compatible across versions |
| 428 | @pytest.mark.skipif( |
| 429 | sys.version_info < (3, 5), |
| 430 | reason="pickle file can only be loaded with Python >= 3.5", |
| 431 | ) |
| 432 | def test_unpickle_old_credentials_pickle(self): |
| 433 | # make sure a credentials file pickled with an older |
| 434 | # library version (google-auth==1.5.1) can be unpickled |
| 435 | with open( |
| 436 | os.path.join(DATA_DIR, "old_oauth_credentials_py3.pickle"), "rb" |
| 437 | ) as f: |
| 438 | credentials = pickle.load(f) |
| 439 | assert credentials.quota_project_id is None |
arithmetic1728 | 772dac6 | 2020-03-27 14:34:13 -0700 | [diff] [blame] | 440 | |
| 441 | |
| 442 | class TestUserAccessTokenCredentials(object): |
| 443 | def test_instance(self): |
| 444 | cred = credentials.UserAccessTokenCredentials() |
| 445 | assert cred._account is None |
| 446 | |
| 447 | cred = cred.with_account("account") |
| 448 | assert cred._account == "account" |
| 449 | |
| 450 | @mock.patch("google.auth._cloud_sdk.get_auth_access_token", autospec=True) |
| 451 | def test_refresh(self, get_auth_access_token): |
| 452 | get_auth_access_token.return_value = "access_token" |
| 453 | cred = credentials.UserAccessTokenCredentials() |
| 454 | cred.refresh(None) |
| 455 | assert cred.token == "access_token" |
| 456 | |
| 457 | @mock.patch( |
| 458 | "google.oauth2.credentials.UserAccessTokenCredentials.apply", autospec=True |
| 459 | ) |
| 460 | @mock.patch( |
| 461 | "google.oauth2.credentials.UserAccessTokenCredentials.refresh", autospec=True |
| 462 | ) |
| 463 | def test_before_request(self, refresh, apply): |
| 464 | cred = credentials.UserAccessTokenCredentials() |
| 465 | cred.before_request(mock.Mock(), "GET", "https://example.com", {}) |
| 466 | refresh.assert_called() |
| 467 | apply.assert_called() |