blob: 136a914c99336e9a3b36d0c8c40c0ed50da54a3e [file] [log] [blame]
Jon Wayne Parrott04714752016-10-24 10:00:58 -07001# Copyright 2016 Google Inc.
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
15import datetime
16
17import mock
18import pytest
19
Jon Wayne Parrotte60c1242017-03-23 16:00:24 -070020from google.auth import _helpers
Jon Wayne Parrott04714752016-10-24 10:00:58 -070021from google.auth import app_engine
22
23
24@pytest.fixture
25def app_identity_mock(monkeypatch):
26 """Mocks the app_identity module for google.auth.app_engine."""
27 app_identity_mock = mock.Mock()
28 monkeypatch.setattr(
29 app_engine, 'app_identity', app_identity_mock)
30 yield app_identity_mock
31
32
Jon Wayne Parrott2148fde2016-10-24 13:44:25 -070033def test_get_project_id(app_identity_mock):
34 app_identity_mock.get_application_id.return_value = mock.sentinel.project
35 assert app_engine.get_project_id() == mock.sentinel.project
36
37
38def test_get_project_id_missing_apis():
39 with pytest.raises(EnvironmentError) as excinfo:
40 assert app_engine.get_project_id()
41
42 assert excinfo.match(r'App Engine APIs are not available')
43
44
Jon Wayne Parrott5b4e9c82017-02-15 16:44:00 -080045class TestSigner(object):
46 def test_key_id(self, app_identity_mock):
47 app_identity_mock.sign_blob.return_value = (
48 mock.sentinel.key_id, mock.sentinel.signature)
49
50 signer = app_engine.Signer()
51
Jon Wayne Parrott254befe2017-02-22 14:37:31 -080052 assert signer.key_id is None
Jon Wayne Parrott5b4e9c82017-02-15 16:44:00 -080053
54 def test_sign(self, app_identity_mock):
55 app_identity_mock.sign_blob.return_value = (
56 mock.sentinel.key_id, mock.sentinel.signature)
57
58 signer = app_engine.Signer()
59 to_sign = b'123'
60
61 signature = signer.sign(to_sign)
62
63 assert signature == mock.sentinel.signature
64 app_identity_mock.sign_blob.assert_called_with(to_sign)
65
66
Jon Wayne Parrott04714752016-10-24 10:00:58 -070067class TestCredentials(object):
68 def test_missing_apis(self):
69 with pytest.raises(EnvironmentError) as excinfo:
70 app_engine.Credentials()
71
72 assert excinfo.match(r'App Engine APIs are not available')
73
74 def test_default_state(self, app_identity_mock):
75 credentials = app_engine.Credentials()
76
77 # Not token acquired yet
78 assert not credentials.valid
79 # Expiration hasn't been set yet
80 assert not credentials.expired
81 # Scopes are required
82 assert not credentials.scopes
83 assert credentials.requires_scopes
84
85 def test_with_scopes(self, app_identity_mock):
86 credentials = app_engine.Credentials()
87
88 assert not credentials.scopes
89 assert credentials.requires_scopes
90
91 scoped_credentials = credentials.with_scopes(['email'])
92
93 assert scoped_credentials.has_scopes(['email'])
94 assert not scoped_credentials.requires_scopes
95
Jon Wayne Parrott61ffb052016-11-08 09:30:30 -080096 def test_service_account_email_implicit(self, app_identity_mock):
97 app_identity_mock.get_service_account_name.return_value = (
98 mock.sentinel.service_account_email)
99 credentials = app_engine.Credentials()
100
101 assert (credentials.service_account_email ==
102 mock.sentinel.service_account_email)
103 assert app_identity_mock.get_service_account_name.called
104
105 def test_service_account_email_explicit(self, app_identity_mock):
106 credentials = app_engine.Credentials(
107 service_account_id=mock.sentinel.service_account_email)
108
109 assert (credentials.service_account_email ==
110 mock.sentinel.service_account_email)
111 assert not app_identity_mock.get_service_account_name.called
112
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700113 @mock.patch(
114 'google.auth._helpers.utcnow',
Jon Wayne Parrotte60c1242017-03-23 16:00:24 -0700115 return_value=datetime.datetime.min + _helpers.CLOCK_SKEW)
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700116 def test_refresh(self, now_mock, app_identity_mock):
117 token = 'token'
118 ttl = 100
119 app_identity_mock.get_access_token.return_value = token, ttl
120 credentials = app_engine.Credentials(scopes=['email'])
121
122 credentials.refresh(None)
123
124 app_identity_mock.get_access_token.assert_called_with(
125 credentials.scopes, credentials._service_account_id)
126 assert credentials.token == token
127 assert credentials.expiry == (
Jon Wayne Parrotte60c1242017-03-23 16:00:24 -0700128 now_mock() + datetime.timedelta(seconds=ttl))
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700129 assert credentials.valid
130 assert not credentials.expired
131
132 def test_sign_bytes(self, app_identity_mock):
Jon Wayne Parrott5b4e9c82017-02-15 16:44:00 -0800133 app_identity_mock.sign_blob.return_value = (
134 mock.sentinel.key_id, mock.sentinel.signature)
Jon Wayne Parrott04714752016-10-24 10:00:58 -0700135 credentials = app_engine.Credentials()
136 to_sign = b'123'
137
138 signature = credentials.sign_bytes(to_sign)
139
140 assert signature == mock.sentinel.signature
141 app_identity_mock.sign_blob.assert_called_with(to_sign)
Jon Wayne Parrott4c883f02016-12-02 14:26:33 -0800142
Jon Wayne Parrottd7221672017-02-16 09:05:11 -0800143 def test_signer(self, app_identity_mock):
144 credentials = app_engine.Credentials()
145 assert isinstance(credentials.signer, app_engine.Signer)
146
Jon Wayne Parrott4c883f02016-12-02 14:26:33 -0800147 def test_signer_email(self, app_identity_mock):
148 credentials = app_engine.Credentials()
149 assert credentials.signer_email == credentials.service_account_email