blob: b302989fbf1948660224622f70bb1a588c2f9e32 [file] [log] [blame]
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -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
Tres Seaverb096a3d2017-10-30 16:12:37 -040017import pytest
18
Jon Wayne Parrotte60c1242017-03-23 16:00:24 -070019from google.auth import _helpers
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -070020from google.auth import credentials
21
22
23class CredentialsImpl(credentials.Credentials):
24 def refresh(self, request):
25 self.token = request
26
27
28def test_credentials_constructor():
29 credentials = CredentialsImpl()
30 assert not credentials.token
31 assert not credentials.expiry
32 assert not credentials.expired
33 assert not credentials.valid
34
35
36def test_expired_and_valid():
37 credentials = CredentialsImpl()
38 credentials.token = 'token'
39
40 assert credentials.valid
41 assert not credentials.expired
42
Jon Wayne Parrott7af9f662017-05-08 09:40:56 -070043 # Set the expiration to one second more than now plus the clock skew
44 # accomodation. These credentials should be valid.
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -070045 credentials.expiry = (
Jon Wayne Parrott7af9f662017-05-08 09:40:56 -070046 datetime.datetime.utcnow() +
47 _helpers.CLOCK_SKEW +
Jon Wayne Parrotte60c1242017-03-23 16:00:24 -070048 datetime.timedelta(seconds=1))
49
50 assert credentials.valid
51 assert not credentials.expired
52
Jon Wayne Parrott7af9f662017-05-08 09:40:56 -070053 # Set the credentials expiration to now. Because of the clock skew
54 # accomodation, these credentials should report as expired.
55 credentials.expiry = datetime.datetime.utcnow()
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -070056
57 assert not credentials.valid
58 assert credentials.expired
59
60
61def test_before_request():
62 credentials = CredentialsImpl()
63 request = 'token'
64 headers = {}
65
66 # First call should call refresh, setting the token.
67 credentials.before_request(request, 'http://example.com', 'GET', headers)
68 assert credentials.valid
69 assert credentials.token == 'token'
70 assert headers['authorization'] == 'Bearer token'
71
72 request = 'token2'
73 headers = {}
74
75 # Second call shouldn't call refresh.
76 credentials.before_request(request, 'http://example.com', 'GET', headers)
77 assert credentials.valid
78 assert credentials.token == 'token'
79 assert headers['authorization'] == 'Bearer token'
80
81
Tres Seaverb096a3d2017-10-30 16:12:37 -040082def test_anonymous_credentials_ctor():
83 anon = credentials.AnonymousCredentials()
84 assert anon.token is None
85 assert anon.expiry is None
86 assert not anon.expired
87 assert anon.valid
88
89
90def test_anonymous_credentials_refresh():
91 anon = credentials.AnonymousCredentials()
92 request = object()
93 with pytest.raises(ValueError):
94 anon.refresh(request)
95
96
97def test_anonymous_credentials_apply_default():
98 anon = credentials.AnonymousCredentials()
99 headers = {}
100 anon.apply(headers)
101 assert headers == {}
102 with pytest.raises(ValueError):
103 anon.apply(headers, token='TOKEN')
104
105
106def test_anonymous_credentials_before_request():
107 anon = credentials.AnonymousCredentials()
108 request = object()
109 method = 'GET'
110 url = 'https://example.com/api/endpoint'
111 headers = {}
112 anon.before_request(request, method, url, headers)
113 assert headers == {}
114
115
Jon Wayne Parrott4460a962017-09-12 10:01:23 -0700116class ReadOnlyScopedCredentialsImpl(
117 credentials.ReadOnlyScoped, CredentialsImpl):
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -0700118 @property
119 def requires_scopes(self):
Jon Wayne Parrott4460a962017-09-12 10:01:23 -0700120 return super(ReadOnlyScopedCredentialsImpl, self).requires_scopes
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -0700121
122
Tres Seaver42468322017-09-11 15:36:53 -0400123def test_readonly_scoped_credentials_constructor():
Jon Wayne Parrott4460a962017-09-12 10:01:23 -0700124 credentials = ReadOnlyScopedCredentialsImpl()
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -0700125 assert credentials._scopes is None
126
127
Tres Seaver42468322017-09-11 15:36:53 -0400128def test_readonly_scoped_credentials_scopes():
Jon Wayne Parrott4460a962017-09-12 10:01:23 -0700129 credentials = ReadOnlyScopedCredentialsImpl()
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -0700130 credentials._scopes = ['one', 'two']
131 assert credentials.scopes == ['one', 'two']
132 assert credentials.has_scopes(['one'])
133 assert credentials.has_scopes(['two'])
134 assert credentials.has_scopes(['one', 'two'])
135 assert not credentials.has_scopes(['three'])
136
137
Tres Seaver42468322017-09-11 15:36:53 -0400138def test_readonly_scoped_credentials_requires_scopes():
Jon Wayne Parrott4460a962017-09-12 10:01:23 -0700139 credentials = ReadOnlyScopedCredentialsImpl()
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -0700140 assert not credentials.requires_scopes
Jon Wayne Parrottf89a3cf2016-10-31 10:52:57 -0700141
142
143class RequiresScopedCredentialsImpl(credentials.Scoped, CredentialsImpl):
144 def __init__(self, scopes=None):
145 super(RequiresScopedCredentialsImpl, self).__init__()
146 self._scopes = scopes
147
148 @property
149 def requires_scopes(self):
150 return not self.scopes
151
152 def with_scopes(self, scopes):
153 return RequiresScopedCredentialsImpl(scopes=scopes)
154
155
156def test_create_scoped_if_required_scoped():
157 unscoped_credentials = RequiresScopedCredentialsImpl()
158 scoped_credentials = credentials.with_scopes_if_required(
159 unscoped_credentials, ['one', 'two'])
160
161 assert scoped_credentials is not unscoped_credentials
162 assert not scoped_credentials.requires_scopes
163 assert scoped_credentials.has_scopes(['one', 'two'])
164
165
166def test_create_scoped_if_required_not_scopes():
167 unscoped_credentials = CredentialsImpl()
168 scoped_credentials = credentials.with_scopes_if_required(
169 unscoped_credentials, ['one', 'two'])
170
171 assert scoped_credentials is unscoped_credentials