Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 1 | # 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 | |
| 15 | import datetime |
| 16 | |
| 17 | import mock |
| 18 | import pytest |
| 19 | |
| 20 | from google.auth import app_engine |
| 21 | |
| 22 | |
| 23 | @pytest.fixture |
| 24 | def app_identity_mock(monkeypatch): |
| 25 | """Mocks the app_identity module for google.auth.app_engine.""" |
| 26 | app_identity_mock = mock.Mock() |
| 27 | monkeypatch.setattr( |
| 28 | app_engine, 'app_identity', app_identity_mock) |
| 29 | yield app_identity_mock |
| 30 | |
| 31 | |
Jon Wayne Parrott | 2148fde | 2016-10-24 13:44:25 -0700 | [diff] [blame] | 32 | def test_get_project_id(app_identity_mock): |
| 33 | app_identity_mock.get_application_id.return_value = mock.sentinel.project |
| 34 | assert app_engine.get_project_id() == mock.sentinel.project |
| 35 | |
| 36 | |
| 37 | def test_get_project_id_missing_apis(): |
| 38 | with pytest.raises(EnvironmentError) as excinfo: |
| 39 | assert app_engine.get_project_id() |
| 40 | |
| 41 | assert excinfo.match(r'App Engine APIs are not available') |
| 42 | |
| 43 | |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 44 | class TestSigner(object): |
| 45 | def test_key_id(self, app_identity_mock): |
| 46 | app_identity_mock.sign_blob.return_value = ( |
| 47 | mock.sentinel.key_id, mock.sentinel.signature) |
| 48 | |
| 49 | signer = app_engine.Signer() |
| 50 | |
Jon Wayne Parrott | 254befe | 2017-02-22 14:37:31 -0800 | [diff] [blame^] | 51 | assert signer.key_id is None |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 52 | |
| 53 | def test_sign(self, app_identity_mock): |
| 54 | app_identity_mock.sign_blob.return_value = ( |
| 55 | mock.sentinel.key_id, mock.sentinel.signature) |
| 56 | |
| 57 | signer = app_engine.Signer() |
| 58 | to_sign = b'123' |
| 59 | |
| 60 | signature = signer.sign(to_sign) |
| 61 | |
| 62 | assert signature == mock.sentinel.signature |
| 63 | app_identity_mock.sign_blob.assert_called_with(to_sign) |
| 64 | |
| 65 | |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 66 | class TestCredentials(object): |
| 67 | def test_missing_apis(self): |
| 68 | with pytest.raises(EnvironmentError) as excinfo: |
| 69 | app_engine.Credentials() |
| 70 | |
| 71 | assert excinfo.match(r'App Engine APIs are not available') |
| 72 | |
| 73 | def test_default_state(self, app_identity_mock): |
| 74 | credentials = app_engine.Credentials() |
| 75 | |
| 76 | # Not token acquired yet |
| 77 | assert not credentials.valid |
| 78 | # Expiration hasn't been set yet |
| 79 | assert not credentials.expired |
| 80 | # Scopes are required |
| 81 | assert not credentials.scopes |
| 82 | assert credentials.requires_scopes |
| 83 | |
| 84 | def test_with_scopes(self, app_identity_mock): |
| 85 | credentials = app_engine.Credentials() |
| 86 | |
| 87 | assert not credentials.scopes |
| 88 | assert credentials.requires_scopes |
| 89 | |
| 90 | scoped_credentials = credentials.with_scopes(['email']) |
| 91 | |
| 92 | assert scoped_credentials.has_scopes(['email']) |
| 93 | assert not scoped_credentials.requires_scopes |
| 94 | |
Jon Wayne Parrott | 61ffb05 | 2016-11-08 09:30:30 -0800 | [diff] [blame] | 95 | def test_service_account_email_implicit(self, app_identity_mock): |
| 96 | app_identity_mock.get_service_account_name.return_value = ( |
| 97 | mock.sentinel.service_account_email) |
| 98 | credentials = app_engine.Credentials() |
| 99 | |
| 100 | assert (credentials.service_account_email == |
| 101 | mock.sentinel.service_account_email) |
| 102 | assert app_identity_mock.get_service_account_name.called |
| 103 | |
| 104 | def test_service_account_email_explicit(self, app_identity_mock): |
| 105 | credentials = app_engine.Credentials( |
| 106 | service_account_id=mock.sentinel.service_account_email) |
| 107 | |
| 108 | assert (credentials.service_account_email == |
| 109 | mock.sentinel.service_account_email) |
| 110 | assert not app_identity_mock.get_service_account_name.called |
| 111 | |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 112 | @mock.patch( |
| 113 | 'google.auth._helpers.utcnow', |
| 114 | return_value=datetime.datetime.min) |
| 115 | def test_refresh(self, now_mock, app_identity_mock): |
| 116 | token = 'token' |
| 117 | ttl = 100 |
| 118 | app_identity_mock.get_access_token.return_value = token, ttl |
| 119 | credentials = app_engine.Credentials(scopes=['email']) |
| 120 | |
| 121 | credentials.refresh(None) |
| 122 | |
| 123 | app_identity_mock.get_access_token.assert_called_with( |
| 124 | credentials.scopes, credentials._service_account_id) |
| 125 | assert credentials.token == token |
| 126 | assert credentials.expiry == ( |
| 127 | datetime.datetime.min + datetime.timedelta(seconds=ttl)) |
| 128 | assert credentials.valid |
| 129 | assert not credentials.expired |
| 130 | |
| 131 | def test_sign_bytes(self, app_identity_mock): |
Jon Wayne Parrott | 5b4e9c8 | 2017-02-15 16:44:00 -0800 | [diff] [blame] | 132 | app_identity_mock.sign_blob.return_value = ( |
| 133 | mock.sentinel.key_id, mock.sentinel.signature) |
Jon Wayne Parrott | 0471475 | 2016-10-24 10:00:58 -0700 | [diff] [blame] | 134 | credentials = app_engine.Credentials() |
| 135 | to_sign = b'123' |
| 136 | |
| 137 | signature = credentials.sign_bytes(to_sign) |
| 138 | |
| 139 | assert signature == mock.sentinel.signature |
| 140 | app_identity_mock.sign_blob.assert_called_with(to_sign) |
Jon Wayne Parrott | 4c883f0 | 2016-12-02 14:26:33 -0800 | [diff] [blame] | 141 | |
Jon Wayne Parrott | d722167 | 2017-02-16 09:05:11 -0800 | [diff] [blame] | 142 | def test_signer(self, app_identity_mock): |
| 143 | credentials = app_engine.Credentials() |
| 144 | assert isinstance(credentials.signer, app_engine.Signer) |
| 145 | |
Jon Wayne Parrott | 4c883f0 | 2016-12-02 14:26:33 -0800 | [diff] [blame] | 146 | def test_signer_email(self, app_identity_mock): |
| 147 | credentials = app_engine.Credentials() |
| 148 | assert credentials.signer_email == credentials.service_account_email |