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" |
Bu Sun Kim | 18d5ae6 | 2020-07-21 13:44:03 -0700 | [diff] [blame] | 312 | assert "token" in headers["authorization"] |
Bu Sun Kim | 32d71a5 | 2019-12-18 11:30:46 -0800 | [diff] [blame] | 313 | |
| 314 | def test_apply_with_no_quota_project_id(self): |
| 315 | creds = credentials.Credentials( |
| 316 | token="token", |
| 317 | refresh_token=self.REFRESH_TOKEN, |
| 318 | token_uri=self.TOKEN_URI, |
| 319 | client_id=self.CLIENT_ID, |
| 320 | client_secret=self.CLIENT_SECRET, |
| 321 | ) |
| 322 | |
| 323 | headers = {} |
| 324 | creds.apply(headers) |
| 325 | assert "x-goog-user-project" not in headers |
Bu Sun Kim | 18d5ae6 | 2020-07-21 13:44:03 -0700 | [diff] [blame] | 326 | assert "token" in headers["authorization"] |
Bu Sun Kim | 32d71a5 | 2019-12-18 11:30:46 -0800 | [diff] [blame] | 327 | |
Bu Sun Kim | b12488c | 2020-06-10 13:44:07 -0700 | [diff] [blame] | 328 | def test_with_quota_project(self): |
| 329 | creds = credentials.Credentials( |
| 330 | token="token", |
| 331 | refresh_token=self.REFRESH_TOKEN, |
| 332 | token_uri=self.TOKEN_URI, |
| 333 | client_id=self.CLIENT_ID, |
| 334 | client_secret=self.CLIENT_SECRET, |
| 335 | quota_project_id="quota-project-123", |
| 336 | ) |
| 337 | |
| 338 | new_creds = creds.with_quota_project("new-project-456") |
| 339 | assert new_creds.quota_project_id == "new-project-456" |
| 340 | headers = {} |
| 341 | creds.apply(headers) |
| 342 | assert "x-goog-user-project" in headers |
| 343 | |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 344 | def test_from_authorized_user_info(self): |
| 345 | info = AUTH_USER_INFO.copy() |
| 346 | |
| 347 | creds = credentials.Credentials.from_authorized_user_info(info) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 348 | assert creds.client_secret == info["client_secret"] |
| 349 | assert creds.client_id == info["client_id"] |
| 350 | assert creds.refresh_token == info["refresh_token"] |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 351 | assert creds.token_uri == credentials._GOOGLE_OAUTH2_TOKEN_ENDPOINT |
| 352 | assert creds.scopes is None |
| 353 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 354 | scopes = ["email", "profile"] |
| 355 | creds = credentials.Credentials.from_authorized_user_info(info, scopes) |
| 356 | assert creds.client_secret == info["client_secret"] |
| 357 | assert creds.client_id == info["client_id"] |
| 358 | assert creds.refresh_token == info["refresh_token"] |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 359 | assert creds.token_uri == credentials._GOOGLE_OAUTH2_TOKEN_ENDPOINT |
| 360 | assert creds.scopes == scopes |
| 361 | |
wesley chun | d0e0aba | 2020-09-17 09:18:55 -0700 | [diff] [blame] | 362 | info["scopes"] = "email" # single non-array scope from file |
| 363 | creds = credentials.Credentials.from_authorized_user_info(info) |
| 364 | assert creds.scopes == [info["scopes"]] |
| 365 | |
| 366 | info["scopes"] = ["email", "profile"] # array scope from file |
| 367 | creds = credentials.Credentials.from_authorized_user_info(info) |
| 368 | assert creds.scopes == info["scopes"] |
| 369 | |
| 370 | expiry = datetime.datetime(2020, 8, 14, 15, 54, 1) |
| 371 | info["expiry"] = expiry.isoformat() + "Z" |
| 372 | creds = credentials.Credentials.from_authorized_user_info(info) |
| 373 | assert creds.expiry == expiry |
| 374 | assert creds.expired |
| 375 | |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 376 | def test_from_authorized_user_file(self): |
| 377 | info = AUTH_USER_INFO.copy() |
| 378 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 379 | creds = credentials.Credentials.from_authorized_user_file(AUTH_USER_JSON_FILE) |
| 380 | assert creds.client_secret == info["client_secret"] |
| 381 | assert creds.client_id == info["client_id"] |
| 382 | assert creds.refresh_token == info["refresh_token"] |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 383 | assert creds.token_uri == credentials._GOOGLE_OAUTH2_TOKEN_ENDPOINT |
| 384 | assert creds.scopes is None |
| 385 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 386 | scopes = ["email", "profile"] |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 387 | creds = credentials.Credentials.from_authorized_user_file( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 388 | AUTH_USER_JSON_FILE, scopes |
| 389 | ) |
| 390 | assert creds.client_secret == info["client_secret"] |
| 391 | assert creds.client_id == info["client_id"] |
| 392 | assert creds.refresh_token == info["refresh_token"] |
Hiranya Jayathilaka | 23c88f7 | 2017-12-05 09:29:59 -0800 | [diff] [blame] | 393 | assert creds.token_uri == credentials._GOOGLE_OAUTH2_TOKEN_ENDPOINT |
| 394 | assert creds.scopes == scopes |
patkasper | bfb1f8c | 2019-12-05 22:03:44 +0100 | [diff] [blame] | 395 | |
| 396 | def test_to_json(self): |
| 397 | info = AUTH_USER_INFO.copy() |
wesley chun | d0e0aba | 2020-09-17 09:18:55 -0700 | [diff] [blame] | 398 | expiry = datetime.datetime(2020, 8, 14, 15, 54, 1) |
| 399 | info["expiry"] = expiry.isoformat() + "Z" |
patkasper | bfb1f8c | 2019-12-05 22:03:44 +0100 | [diff] [blame] | 400 | creds = credentials.Credentials.from_authorized_user_info(info) |
wesley chun | d0e0aba | 2020-09-17 09:18:55 -0700 | [diff] [blame] | 401 | assert creds.expiry == expiry |
patkasper | bfb1f8c | 2019-12-05 22:03:44 +0100 | [diff] [blame] | 402 | |
| 403 | # Test with no `strip` arg |
| 404 | json_output = creds.to_json() |
| 405 | json_asdict = json.loads(json_output) |
| 406 | assert json_asdict.get("token") == creds.token |
| 407 | assert json_asdict.get("refresh_token") == creds.refresh_token |
| 408 | assert json_asdict.get("token_uri") == creds.token_uri |
| 409 | assert json_asdict.get("client_id") == creds.client_id |
| 410 | assert json_asdict.get("scopes") == creds.scopes |
| 411 | assert json_asdict.get("client_secret") == creds.client_secret |
wesley chun | d0e0aba | 2020-09-17 09:18:55 -0700 | [diff] [blame] | 412 | assert json_asdict.get("expiry") == info["expiry"] |
patkasper | bfb1f8c | 2019-12-05 22:03:44 +0100 | [diff] [blame] | 413 | |
| 414 | # Test with a `strip` arg |
| 415 | json_output = creds.to_json(strip=["client_secret"]) |
| 416 | json_asdict = json.loads(json_output) |
| 417 | assert json_asdict.get("token") == creds.token |
| 418 | assert json_asdict.get("refresh_token") == creds.refresh_token |
| 419 | assert json_asdict.get("token_uri") == creds.token_uri |
| 420 | assert json_asdict.get("client_id") == creds.client_id |
| 421 | assert json_asdict.get("scopes") == creds.scopes |
| 422 | assert json_asdict.get("client_secret") is None |
Bu Sun Kim | 32d71a5 | 2019-12-18 11:30:46 -0800 | [diff] [blame] | 423 | |
wesley chun | d0e0aba | 2020-09-17 09:18:55 -0700 | [diff] [blame] | 424 | # Test with no expiry |
| 425 | creds.expiry = None |
| 426 | json_output = creds.to_json() |
| 427 | json_asdict = json.loads(json_output) |
| 428 | assert json_asdict.get("expiry") is None |
| 429 | |
Bu Sun Kim | 32d71a5 | 2019-12-18 11:30:46 -0800 | [diff] [blame] | 430 | def test_pickle_and_unpickle(self): |
| 431 | creds = self.make_credentials() |
| 432 | unpickled = pickle.loads(pickle.dumps(creds)) |
| 433 | |
| 434 | # make sure attributes aren't lost during pickling |
| 435 | assert list(creds.__dict__).sort() == list(unpickled.__dict__).sort() |
| 436 | |
| 437 | for attr in list(creds.__dict__): |
| 438 | assert getattr(creds, attr) == getattr(unpickled, attr) |
| 439 | |
| 440 | def test_pickle_with_missing_attribute(self): |
| 441 | creds = self.make_credentials() |
| 442 | |
| 443 | # remove an optional attribute before pickling |
| 444 | # this mimics a pickle created with a previous class definition with |
| 445 | # fewer attributes |
| 446 | del creds.__dict__["_quota_project_id"] |
| 447 | |
| 448 | unpickled = pickle.loads(pickle.dumps(creds)) |
| 449 | |
| 450 | # Attribute should be initialized by `__setstate__` |
| 451 | assert unpickled.quota_project_id is None |
| 452 | |
| 453 | # pickles are not compatible across versions |
| 454 | @pytest.mark.skipif( |
| 455 | sys.version_info < (3, 5), |
| 456 | reason="pickle file can only be loaded with Python >= 3.5", |
| 457 | ) |
| 458 | def test_unpickle_old_credentials_pickle(self): |
| 459 | # make sure a credentials file pickled with an older |
| 460 | # library version (google-auth==1.5.1) can be unpickled |
| 461 | with open( |
| 462 | os.path.join(DATA_DIR, "old_oauth_credentials_py3.pickle"), "rb" |
| 463 | ) as f: |
| 464 | credentials = pickle.load(f) |
| 465 | assert credentials.quota_project_id is None |
arithmetic1728 | 772dac6 | 2020-03-27 14:34:13 -0700 | [diff] [blame] | 466 | |
| 467 | |
| 468 | class TestUserAccessTokenCredentials(object): |
| 469 | def test_instance(self): |
| 470 | cred = credentials.UserAccessTokenCredentials() |
| 471 | assert cred._account is None |
| 472 | |
| 473 | cred = cred.with_account("account") |
| 474 | assert cred._account == "account" |
| 475 | |
| 476 | @mock.patch("google.auth._cloud_sdk.get_auth_access_token", autospec=True) |
| 477 | def test_refresh(self, get_auth_access_token): |
| 478 | get_auth_access_token.return_value = "access_token" |
| 479 | cred = credentials.UserAccessTokenCredentials() |
| 480 | cred.refresh(None) |
| 481 | assert cred.token == "access_token" |
| 482 | |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 483 | def test_with_quota_project(self): |
| 484 | cred = credentials.UserAccessTokenCredentials() |
| 485 | quota_project_cred = cred.with_quota_project("project-foo") |
| 486 | |
| 487 | assert quota_project_cred._quota_project_id == "project-foo" |
| 488 | assert quota_project_cred._account == cred._account |
| 489 | |
arithmetic1728 | 772dac6 | 2020-03-27 14:34:13 -0700 | [diff] [blame] | 490 | @mock.patch( |
| 491 | "google.oauth2.credentials.UserAccessTokenCredentials.apply", autospec=True |
| 492 | ) |
| 493 | @mock.patch( |
| 494 | "google.oauth2.credentials.UserAccessTokenCredentials.refresh", autospec=True |
| 495 | ) |
| 496 | def test_before_request(self, refresh, apply): |
| 497 | cred = credentials.UserAccessTokenCredentials() |
| 498 | cred.before_request(mock.Mock(), "GET", "https://example.com", {}) |
| 499 | refresh.assert_called() |
| 500 | apply.assert_called() |