blob: e1189ed493f0986f3b754e68752a7bc20ee7881d [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
20from google.auth import app_engine
21
22
23@pytest.fixture
24def 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 Parrott2148fde2016-10-24 13:44:25 -070032def 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
37def 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 Parrott04714752016-10-24 10:00:58 -070044class TestCredentials(object):
45 def test_missing_apis(self):
46 with pytest.raises(EnvironmentError) as excinfo:
47 app_engine.Credentials()
48
49 assert excinfo.match(r'App Engine APIs are not available')
50
51 def test_default_state(self, app_identity_mock):
52 credentials = app_engine.Credentials()
53
54 # Not token acquired yet
55 assert not credentials.valid
56 # Expiration hasn't been set yet
57 assert not credentials.expired
58 # Scopes are required
59 assert not credentials.scopes
60 assert credentials.requires_scopes
61
62 def test_with_scopes(self, app_identity_mock):
63 credentials = app_engine.Credentials()
64
65 assert not credentials.scopes
66 assert credentials.requires_scopes
67
68 scoped_credentials = credentials.with_scopes(['email'])
69
70 assert scoped_credentials.has_scopes(['email'])
71 assert not scoped_credentials.requires_scopes
72
73 @mock.patch(
74 'google.auth._helpers.utcnow',
75 return_value=datetime.datetime.min)
76 def test_refresh(self, now_mock, app_identity_mock):
77 token = 'token'
78 ttl = 100
79 app_identity_mock.get_access_token.return_value = token, ttl
80 credentials = app_engine.Credentials(scopes=['email'])
81
82 credentials.refresh(None)
83
84 app_identity_mock.get_access_token.assert_called_with(
85 credentials.scopes, credentials._service_account_id)
86 assert credentials.token == token
87 assert credentials.expiry == (
88 datetime.datetime.min + datetime.timedelta(seconds=ttl))
89 assert credentials.valid
90 assert not credentials.expired
91
92 def test_sign_bytes(self, app_identity_mock):
93 app_identity_mock.sign_blob.return_value = mock.sentinel.signature
94 credentials = app_engine.Credentials()
95 to_sign = b'123'
96
97 signature = credentials.sign_bytes(to_sign)
98
99 assert signature == mock.sentinel.signature
100 app_identity_mock.sign_blob.assert_called_with(to_sign)