blob: e335ff7ed2d811302be20d8e2791b74bed8059a5 [file] [log] [blame]
C.J. Collier37141e42020-02-13 13:49:49 -08001# Copyright 2016 Google LLC
Jon Wayne Parrott04714752016-10-24 10:00:58 -07002#
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
15import datetime
16
17import mock
18import pytest
19
20from google.auth import app_engine
21
22
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070023class _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 Kim9eec0912019-10-21 17:04:21 -070028
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070029 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 Parrott04714752016-10-24 10:00:58 -070042@pytest.fixture
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070043def app_identity(monkeypatch):
Jon Wayne Parrott04714752016-10-24 10:00:58 -070044 """Mocks the app_identity module for google.auth.app_engine."""
Bu Sun Kim9eec0912019-10-21 17:04:21 -070045 app_identity_module = mock.create_autospec(_AppIdentityModule, instance=True)
46 monkeypatch.setattr(app_engine, "app_identity", app_identity_module)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070047 yield app_identity_module
Jon Wayne Parrott04714752016-10-24 10:00:58 -070048
49
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070050def test_get_project_id(app_identity):
51 app_identity.get_application_id.return_value = mock.sentinel.project
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -070052 assert app_engine.get_project_id() == mock.sentinel.project
53
54
55def test_get_project_id_missing_apis():
56 with pytest.raises(EnvironmentError) as excinfo:
57 assert app_engine.get_project_id()
58
Bu Sun Kim9eec0912019-10-21 17:04:21 -070059 assert excinfo.match(r"App Engine APIs are not available")
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -070060
61
Jon Wayne Parrott5b4e9c82017-02-15 16:44:00 -080062class TestSigner(object):
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070063 def test_key_id(self, app_identity):
64 app_identity.sign_blob.return_value = (
Bu Sun Kim9eec0912019-10-21 17:04:21 -070065 mock.sentinel.key_id,
66 mock.sentinel.signature,
67 )
Jon Wayne Parrott5b4e9c82017-02-15 16:44:00 -080068
69 signer = app_engine.Signer()
70
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080071 assert signer.key_id is None
Jon Wayne Parrott5b4e9c82017-02-15 16:44:00 -080072
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070073 def test_sign(self, app_identity):
74 app_identity.sign_blob.return_value = (
Bu Sun Kim9eec0912019-10-21 17:04:21 -070075 mock.sentinel.key_id,
76 mock.sentinel.signature,
77 )
Jon Wayne Parrott5b4e9c82017-02-15 16:44:00 -080078
79 signer = app_engine.Signer()
Bu Sun Kim9eec0912019-10-21 17:04:21 -070080 to_sign = b"123"
Jon Wayne Parrott5b4e9c82017-02-15 16:44:00 -080081
82 signature = signer.sign(to_sign)
83
84 assert signature == mock.sentinel.signature
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070085 app_identity.sign_blob.assert_called_with(to_sign)
Jon Wayne Parrott5b4e9c82017-02-15 16:44:00 -080086
87
Jon Wayne Parrott04714752016-10-24 10:00:58 -070088class TestCredentials(object):
89 def test_missing_apis(self):
90 with pytest.raises(EnvironmentError) as excinfo:
91 app_engine.Credentials()
92
Bu Sun Kim9eec0912019-10-21 17:04:21 -070093 assert excinfo.match(r"App Engine APIs are not available")
Jon Wayne Parrott04714752016-10-24 10:00:58 -070094
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070095 def test_default_state(self, app_identity):
Jon Wayne Parrott04714752016-10-24 10:00:58 -070096 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
Bu Sun Kimbf5ce0c2021-02-01 15:17:49 -0700104 assert not credentials.default_scopes
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700105 assert credentials.requires_scopes
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700106 assert not credentials.quota_project_id
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700107
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700108 def test_with_scopes(self, app_identity):
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700109 credentials = app_engine.Credentials()
110
111 assert not credentials.scopes
112 assert credentials.requires_scopes
113
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700114 scoped_credentials = credentials.with_scopes(["email"])
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700115
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700116 assert scoped_credentials.has_scopes(["email"])
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700117 assert not scoped_credentials.requires_scopes
118
Bu Sun Kimbf5ce0c2021-02-01 15:17:49 -0700119 def test_with_default_scopes(self, app_identity):
120 credentials = app_engine.Credentials()
121
122 assert not credentials.scopes
123 assert not credentials.default_scopes
124 assert credentials.requires_scopes
125
126 scoped_credentials = credentials.with_scopes(
127 scopes=None, default_scopes=["email"]
128 )
129
130 assert scoped_credentials.has_scopes(["email"])
131 assert not scoped_credentials.requires_scopes
132
Bu Sun Kim3dda7b22020-07-09 10:39:39 -0700133 def test_with_quota_project(self, app_identity):
134 credentials = app_engine.Credentials()
135
136 assert not credentials.scopes
137 assert not credentials.quota_project_id
138
139 quota_project_creds = credentials.with_quota_project("project-foo")
140
141 assert quota_project_creds.quota_project_id == "project-foo"
142
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700143 def test_service_account_email_implicit(self, app_identity):
144 app_identity.get_service_account_name.return_value = (
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700145 mock.sentinel.service_account_email
146 )
Jon Wayne Parrott61ffb052016-11-08 09:30:30 -0800147 credentials = app_engine.Credentials()
148
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700149 assert credentials.service_account_email == mock.sentinel.service_account_email
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700150 assert app_identity.get_service_account_name.called
Jon Wayne Parrott61ffb052016-11-08 09:30:30 -0800151
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700152 def test_service_account_email_explicit(self, app_identity):
Jon Wayne Parrott61ffb052016-11-08 09:30:30 -0800153 credentials = app_engine.Credentials(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700154 service_account_id=mock.sentinel.service_account_email
155 )
Jon Wayne Parrott61ffb052016-11-08 09:30:30 -0800156
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700157 assert credentials.service_account_email == mock.sentinel.service_account_email
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700158 assert not app_identity.get_service_account_name.called
Jon Wayne Parrott61ffb052016-11-08 09:30:30 -0800159
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700160 @mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700161 def test_refresh(self, utcnow, app_identity):
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700162 token = "token"
Jon Wayne Parrott1cba0f82017-09-12 10:21:02 -0700163 ttl = 643942923
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700164 app_identity.get_access_token.return_value = token, ttl
Bu Sun Kimbf5ce0c2021-02-01 15:17:49 -0700165 credentials = app_engine.Credentials(
166 scopes=["email"], default_scopes=["profile"]
167 )
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700168
169 credentials.refresh(None)
170
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700171 app_identity.get_access_token.assert_called_with(
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700172 credentials.scopes, credentials._service_account_id
173 )
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700174 assert credentials.token == token
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700175 assert credentials.expiry == datetime.datetime(1990, 5, 29, 1, 2, 3)
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700176 assert credentials.valid
177 assert not credentials.expired
178
Bu Sun Kimbf5ce0c2021-02-01 15:17:49 -0700179 @mock.patch("google.auth._helpers.utcnow", return_value=datetime.datetime.min)
180 def test_refresh_with_default_scopes(self, utcnow, app_identity):
181 token = "token"
182 ttl = 643942923
183 app_identity.get_access_token.return_value = token, ttl
184 credentials = app_engine.Credentials(default_scopes=["email"])
185
186 credentials.refresh(None)
187
188 app_identity.get_access_token.assert_called_with(
189 credentials.default_scopes, credentials._service_account_id
190 )
191 assert credentials.token == token
192 assert credentials.expiry == datetime.datetime(1990, 5, 29, 1, 2, 3)
193 assert credentials.valid
194 assert not credentials.expired
195
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700196 def test_sign_bytes(self, app_identity):
197 app_identity.sign_blob.return_value = (
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700198 mock.sentinel.key_id,
199 mock.sentinel.signature,
200 )
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700201 credentials = app_engine.Credentials()
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700202 to_sign = b"123"
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700203
204 signature = credentials.sign_bytes(to_sign)
205
206 assert signature == mock.sentinel.signature
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700207 app_identity.sign_blob.assert_called_with(to_sign)
Jon Wayne Parrott4c883f02016-12-02 14:26:33 -0800208
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700209 def test_signer(self, app_identity):
Jon Wayne Parrottd7221672017-02-16 09:05:11 -0800210 credentials = app_engine.Credentials()
211 assert isinstance(credentials.signer, app_engine.Signer)
212
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -0700213 def test_signer_email(self, app_identity):
Jon Wayne Parrott4c883f02016-12-02 14:26:33 -0800214 credentials = app_engine.Credentials()
215 assert credentials.signer_email == credentials.service_account_email