C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -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 |
| 16 | import os |
| 17 | import sys |
| 18 | |
| 19 | import mock |
| 20 | import oauth2client.client |
| 21 | import oauth2client.contrib.gce |
| 22 | import oauth2client.service_account |
| 23 | import pytest |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 24 | from six.moves import reload_module |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 25 | |
| 26 | from google.auth import _oauth2client |
| 27 | |
| 28 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 29 | DATA_DIR = os.path.join(os.path.dirname(__file__), "data") |
| 30 | SERVICE_ACCOUNT_JSON_FILE = os.path.join(DATA_DIR, "service_account.json") |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 31 | |
| 32 | |
| 33 | def test__convert_oauth2_credentials(): |
| 34 | old_credentials = oauth2client.client.OAuth2Credentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 35 | "access_token", |
| 36 | "client_id", |
| 37 | "client_secret", |
| 38 | "refresh_token", |
| 39 | datetime.datetime.min, |
| 40 | "token_uri", |
| 41 | "user_agent", |
| 42 | scopes="one two", |
| 43 | ) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 44 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 45 | new_credentials = _oauth2client._convert_oauth2_credentials(old_credentials) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 46 | |
| 47 | assert new_credentials.token == old_credentials.access_token |
| 48 | assert new_credentials._refresh_token == old_credentials.refresh_token |
| 49 | assert new_credentials._client_id == old_credentials.client_id |
| 50 | assert new_credentials._client_secret == old_credentials.client_secret |
| 51 | assert new_credentials._token_uri == old_credentials.token_uri |
| 52 | assert new_credentials.scopes == old_credentials.scopes |
| 53 | |
| 54 | |
| 55 | def test__convert_service_account_credentials(): |
| 56 | old_class = oauth2client.service_account.ServiceAccountCredentials |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 57 | old_credentials = old_class.from_json_keyfile_name(SERVICE_ACCOUNT_JSON_FILE) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 58 | |
| 59 | new_credentials = _oauth2client._convert_service_account_credentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 60 | old_credentials |
| 61 | ) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 62 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 63 | assert ( |
| 64 | new_credentials.service_account_email == old_credentials.service_account_email |
| 65 | ) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 66 | assert new_credentials._signer.key_id == old_credentials._private_key_id |
| 67 | assert new_credentials._token_uri == old_credentials.token_uri |
| 68 | |
| 69 | |
| 70 | def test__convert_service_account_credentials_with_jwt(): |
| 71 | old_class = oauth2client.service_account._JWTAccessCredentials |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 72 | old_credentials = old_class.from_json_keyfile_name(SERVICE_ACCOUNT_JSON_FILE) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 73 | |
| 74 | new_credentials = _oauth2client._convert_service_account_credentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 75 | old_credentials |
| 76 | ) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 77 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 78 | assert ( |
| 79 | new_credentials.service_account_email == old_credentials.service_account_email |
| 80 | ) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 81 | assert new_credentials._signer.key_id == old_credentials._private_key_id |
| 82 | assert new_credentials._token_uri == old_credentials.token_uri |
| 83 | |
| 84 | |
| 85 | def test__convert_gce_app_assertion_credentials(): |
| 86 | old_credentials = oauth2client.contrib.gce.AppAssertionCredentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 87 | email="some_email" |
| 88 | ) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 89 | |
| 90 | new_credentials = _oauth2client._convert_gce_app_assertion_credentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 91 | old_credentials |
| 92 | ) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 93 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 94 | assert ( |
| 95 | new_credentials.service_account_email == old_credentials.service_account_email |
| 96 | ) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 97 | |
| 98 | |
| 99 | @pytest.fixture |
| 100 | def mock_oauth2client_gae_imports(mock_non_existent_module): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 101 | mock_non_existent_module("google.appengine.api.app_identity") |
| 102 | mock_non_existent_module("google.appengine.ext.ndb") |
| 103 | mock_non_existent_module("google.appengine.ext.webapp.util") |
| 104 | mock_non_existent_module("webapp2") |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 105 | |
| 106 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 107 | @mock.patch("google.auth.app_engine.app_identity") |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 108 | def test__convert_appengine_app_assertion_credentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 109 | app_identity, mock_oauth2client_gae_imports |
| 110 | ): |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 111 | |
| 112 | import oauth2client.contrib.appengine |
| 113 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 114 | service_account_id = "service_account_id" |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 115 | old_credentials = oauth2client.contrib.appengine.AppAssertionCredentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 116 | scope="one two", service_account_id=service_account_id |
| 117 | ) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 118 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 119 | new_credentials = _oauth2client._convert_appengine_app_assertion_credentials( |
| 120 | old_credentials |
| 121 | ) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 122 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 123 | assert new_credentials.scopes == ["one", "two"] |
| 124 | assert new_credentials._service_account_id == old_credentials.service_account_id |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 125 | |
| 126 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 127 | class FakeCredentials(object): |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 128 | pass |
| 129 | |
| 130 | |
| 131 | def test_convert_success(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 132 | convert_function = mock.Mock(spec=["__call__"]) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 133 | conversion_map_patch = mock.patch.object( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 134 | _oauth2client, "_CLASS_CONVERSION_MAP", {FakeCredentials: convert_function} |
| 135 | ) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 136 | credentials = FakeCredentials() |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 137 | |
| 138 | with conversion_map_patch: |
| 139 | result = _oauth2client.convert(credentials) |
| 140 | |
| 141 | convert_function.assert_called_once_with(credentials) |
| 142 | assert result == convert_function.return_value |
| 143 | |
| 144 | |
| 145 | def test_convert_not_found(): |
| 146 | with pytest.raises(ValueError) as excinfo: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 147 | _oauth2client.convert("a string is not a real credentials class") |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 148 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 149 | assert excinfo.match("Unable to convert") |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 150 | |
| 151 | |
| 152 | @pytest.fixture |
| 153 | def reset__oauth2client_module(): |
| 154 | """Reloads the _oauth2client module after a test.""" |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 155 | reload_module(_oauth2client) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 156 | |
| 157 | |
| 158 | def test_import_has_app_engine( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 159 | mock_oauth2client_gae_imports, reset__oauth2client_module |
| 160 | ): |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 161 | reload_module(_oauth2client) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 162 | assert _oauth2client._HAS_APPENGINE |
| 163 | |
| 164 | |
| 165 | def test_import_without_oauth2client(monkeypatch, reset__oauth2client_module): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 166 | monkeypatch.setitem(sys.modules, "oauth2client", None) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 167 | with pytest.raises(ImportError) as excinfo: |
arithmetic1728 | 5bd5ccf | 2021-10-21 15:25:46 -0700 | [diff] [blame^] | 168 | reload_module(_oauth2client) |
Jon Wayne Parrott | a896d2a | 2016-11-02 23:42:51 -0700 | [diff] [blame] | 169 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 170 | assert excinfo.match("oauth2client") |