blob: c2ff844a44415fd46e91e3f91f82543db9f412e9 [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
Jon Wayne Parrotte60c1242017-03-23 16:00:24 -070017from google.auth import _helpers
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -070018from google.auth import credentials
19
20
21class CredentialsImpl(credentials.Credentials):
22 def refresh(self, request):
23 self.token = request
24
25
26def test_credentials_constructor():
27 credentials = CredentialsImpl()
28 assert not credentials.token
29 assert not credentials.expiry
30 assert not credentials.expired
31 assert not credentials.valid
32
33
34def test_expired_and_valid():
35 credentials = CredentialsImpl()
36 credentials.token = 'token'
37
38 assert credentials.valid
39 assert not credentials.expired
40
Jon Wayne Parrotte60c1242017-03-23 16:00:24 -070041 # Set the expiration in the past, but because of clock skew accomodation
42 # the credentials should still be valid.
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -070043 credentials.expiry = (
Jon Wayne Parrotte60c1242017-03-23 16:00:24 -070044 datetime.datetime.utcnow() -
45 datetime.timedelta(seconds=1))
46
47 assert credentials.valid
48 assert not credentials.expired
49
50 # Set the credentials far enough in the past to exceed the clock skew
51 # accomodation. They should now be expired.
52 credentials.expiry = (
53 datetime.datetime.utcnow() -
54 _helpers.CLOCK_SKEW -
55 datetime.timedelta(seconds=1))
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
82class ScopedCredentialsImpl(credentials.Scoped, CredentialsImpl):
83 @property
84 def requires_scopes(self):
85 return super(ScopedCredentialsImpl, self).requires_scopes
86
87 def with_scopes(self, scopes):
88 raise NotImplementedError
89
90
91def test_scoped_credentials_constructor():
92 credentials = ScopedCredentialsImpl()
93 assert credentials._scopes is None
94
95
96def test_scoped_credentials_scopes():
97 credentials = ScopedCredentialsImpl()
98 credentials._scopes = ['one', 'two']
99 assert credentials.scopes == ['one', 'two']
100 assert credentials.has_scopes(['one'])
101 assert credentials.has_scopes(['two'])
102 assert credentials.has_scopes(['one', 'two'])
103 assert not credentials.has_scopes(['three'])
104
105
106def test_scoped_credentials_requires_scopes():
107 credentials = ScopedCredentialsImpl()
108 assert not credentials.requires_scopes
Jon Wayne Parrottf89a3cf2016-10-31 10:52:57 -0700109
110
111class RequiresScopedCredentialsImpl(credentials.Scoped, CredentialsImpl):
112 def __init__(self, scopes=None):
113 super(RequiresScopedCredentialsImpl, self).__init__()
114 self._scopes = scopes
115
116 @property
117 def requires_scopes(self):
118 return not self.scopes
119
120 def with_scopes(self, scopes):
121 return RequiresScopedCredentialsImpl(scopes=scopes)
122
123
124def test_create_scoped_if_required_scoped():
125 unscoped_credentials = RequiresScopedCredentialsImpl()
126 scoped_credentials = credentials.with_scopes_if_required(
127 unscoped_credentials, ['one', 'two'])
128
129 assert scoped_credentials is not unscoped_credentials
130 assert not scoped_credentials.requires_scopes
131 assert scoped_credentials.has_scopes(['one', 'two'])
132
133
134def test_create_scoped_if_required_not_scopes():
135 unscoped_credentials = CredentialsImpl()
136 scoped_credentials = credentials.with_scopes_if_required(
137 unscoped_credentials, ['one', 'two'])
138
139 assert scoped_credentials is unscoped_credentials