C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -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 | |
| 17 | import mock |
| 18 | import pytest |
| 19 | |
| 20 | from google.auth import app_engine |
| 21 | |
| 22 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 23 | class _AppIdentityModule(object): |
| 24 | """The interface of the App Idenity app engine module. |
| 25 | See https://cloud.google.com/appengine/docs/standard/python/refdocs |
| 26 | /google.appengine.api.app_identity.app_identity |
| 27 | """ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 28 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 29 | def get_application_id(self): |
| 30 | raise NotImplementedError() |
| 31 | |
| 32 | def sign_blob(self, bytes_to_sign, deadline=None): |
| 33 | raise NotImplementedError() |
| 34 | |
| 35 | def get_service_account_name(self, deadline=None): |
| 36 | raise NotImplementedError() |
| 37 | |
| 38 | def get_access_token(self, scopes, service_account_id=None): |
| 39 | raise NotImplementedError() |
| 40 | |
| 41 | |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 42 | @pytest.fixture |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 43 | def app_identity(monkeypatch): |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 44 | """Mocks the app_identity module for google.auth.app_engine.""" |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 45 | app_identity_module = mock.create_autospec(_AppIdentityModule, instance=True) |
| 46 | monkeypatch.setattr(app_engine, "app_identity", app_identity_module) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 47 | yield app_identity_module |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 48 | |
| 49 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 50 | def test_get_project_id(app_identity): |
| 51 | app_identity.get_application_id.return_value = mock.sentinel.project |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 52 | assert app_engine.get_project_id() == mock.sentinel.project |
| 53 | |
| 54 | |
| 55 | def test_get_project_id_missing_apis(): |
| 56 | with pytest.raises(EnvironmentError) as excinfo: |
| 57 | assert app_engine.get_project_id() |
| 58 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 59 | assert excinfo.match(r"App Engine APIs are not available") |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 60 | |
| 61 | |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 62 | class TestSigner(object): |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 63 | def test_key_id(self, app_identity): |
| 64 | app_identity.sign_blob.return_value = ( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 65 | mock.sentinel.key_id, |
| 66 | mock.sentinel.signature, |
| 67 | ) |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 68 | |
| 69 | signer = app_engine.Signer() |
| 70 | |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame] | 71 | assert signer.key_id is None |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 72 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 73 | def test_sign(self, app_identity): |
| 74 | app_identity.sign_blob.return_value = ( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 75 | mock.sentinel.key_id, |
| 76 | mock.sentinel.signature, |
| 77 | ) |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 78 | |
| 79 | signer = app_engine.Signer() |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 80 | to_sign = b"123" |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 81 | |
| 82 | signature = signer.sign(to_sign) |
| 83 | |
| 84 | assert signature == mock.sentinel.signature |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 85 | app_identity.sign_blob.assert_called_with(to_sign) |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 86 | |
| 87 | |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 88 | class TestCredentials(object): |
| 89 | def test_missing_apis(self): |
| 90 | with pytest.raises(EnvironmentError) as excinfo: |
| 91 | app_engine.Credentials() |
| 92 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 93 | assert excinfo.match(r"App Engine APIs are not available") |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 94 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 95 | def test_default_state(self, app_identity): |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 96 | credentials = app_engine.Credentials() |
| 97 | |
| 98 | # Not token acquired yet |
| 99 | assert not credentials.valid |
| 100 | # Expiration hasn't been set yet |
| 101 | assert not credentials.expired |
| 102 | # Scopes are required |
| 103 | assert not credentials.scopes |
| 104 | assert credentials.requires_scopes |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 105 | assert not credentials.quota_project_id |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 106 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 107 | def test_with_scopes(self, app_identity): |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 108 | credentials = app_engine.Credentials() |
| 109 | |
| 110 | assert not credentials.scopes |
| 111 | assert credentials.requires_scopes |
| 112 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 113 | scoped_credentials = credentials.with_scopes(["email"]) |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 114 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 115 | assert scoped_credentials.has_scopes(["email"]) |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 116 | assert not scoped_credentials.requires_scopes |
| 117 | |
Bu Sun Kim | 3dda7b2 | 2020-07-09 10:39:39 -0700 | [diff] [blame] | 118 | def test_with_quota_project(self, app_identity): |
| 119 | credentials = app_engine.Credentials() |
| 120 | |
| 121 | assert not credentials.scopes |
| 122 | assert not credentials.quota_project_id |
| 123 | |
| 124 | quota_project_creds = credentials.with_quota_project("project-foo") |
| 125 | |
| 126 | assert quota_project_creds.quota_project_id == "project-foo" |
| 127 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 128 | def test_service_account_email_implicit(self, app_identity): |
| 129 | app_identity.get_service_account_name.return_value = ( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 130 | mock.sentinel.service_account_email |
| 131 | ) |
Jon Wayne Parrott | 61ffb05 | 2016-11-08 09:30:30 -0800 | [diff] [blame] | 132 | credentials = app_engine.Credentials() |
| 133 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 134 | assert credentials.service_account_email == mock.sentinel.service_account_email |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 135 | assert app_identity.get_service_account_name.called |
Jon Wayne Parrott | 61ffb05 | 2016-11-08 09:30:30 -0800 | [diff] [blame] | 136 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 137 | def test_service_account_email_explicit(self, app_identity): |
Jon Wayne Parrott | 61ffb05 | 2016-11-08 09:30:30 -0800 | [diff] [blame] | 138 | credentials = app_engine.Credentials( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 139 | service_account_id=mock.sentinel.service_account_email |
| 140 | ) |
Jon Wayne Parrott | 61ffb05 | 2016-11-08 09:30:30 -0800 | [diff] [blame] | 141 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 142 | assert credentials.service_account_email == mock.sentinel.service_account_email |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 143 | assert not app_identity.get_service_account_name.called |
Jon Wayne Parrott | 61ffb05 | 2016-11-08 09:30:30 -0800 | [diff] [blame] | 144 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 145 | @mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min) |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 146 | def test_refresh(self, utcnow, app_identity): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 147 | token = "token" |
Jon Wayne Parrott | 1cba0f8 | 2017-09-12 10:21:02 -0700 | [diff] [blame] | 148 | ttl = 643942923 |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 149 | app_identity.get_access_token.return_value = token, ttl |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 150 | credentials = app_engine.Credentials(scopes=["email"]) |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 151 | |
| 152 | credentials.refresh(None) |
| 153 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 154 | app_identity.get_access_token.assert_called_with( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 155 | credentials.scopes, credentials._service_account_id |
| 156 | ) |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 157 | assert credentials.token == token |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 158 | assert credentials.expiry == datetime.datetime(1990, 5, 29, 1, 2, 3) |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 159 | assert credentials.valid |
| 160 | assert not credentials.expired |
| 161 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 162 | def test_sign_bytes(self, app_identity): |
| 163 | app_identity.sign_blob.return_value = ( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 164 | mock.sentinel.key_id, |
| 165 | mock.sentinel.signature, |
| 166 | ) |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 167 | credentials = app_engine.Credentials() |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 168 | to_sign = b"123" |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 169 | |
| 170 | signature = credentials.sign_bytes(to_sign) |
| 171 | |
| 172 | assert signature == mock.sentinel.signature |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 173 | app_identity.sign_blob.assert_called_with(to_sign) |
Jon Wayne Parrott | 4c883f0 | 2016-12-02 14:26:33 -0800 | [diff] [blame] | 174 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 175 | def test_signer(self, app_identity): |
Jon Wayne Parrott | d722167 | 2017-02-16 09:05:11 -0800 | [diff] [blame] | 176 | credentials = app_engine.Credentials() |
| 177 | assert isinstance(credentials.signer, app_engine.Signer) |
| 178 | |
Jon Wayne Parrott | 78fec2c | 2017-06-30 10:25:08 -0700 | [diff] [blame] | 179 | def test_signer_email(self, app_identity): |
Jon Wayne Parrott | 4c883f0 | 2016-12-02 14:26:33 -0800 | [diff] [blame] | 180 | credentials = app_engine.Credentials() |
| 181 | assert credentials.signer_email == credentials.service_account_email |