Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -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 | |
Tres Seaver | b096a3d | 2017-10-30 16:12:37 -0400 | [diff] [blame^] | 17 | import pytest |
| 18 | |
Jon Wayne Parrott | e60c124 | 2017-03-23 16:00:24 -0700 | [diff] [blame] | 19 | from google.auth import _helpers |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 20 | from google.auth import credentials |
| 21 | |
| 22 | |
| 23 | class CredentialsImpl(credentials.Credentials): |
| 24 | def refresh(self, request): |
| 25 | self.token = request |
| 26 | |
| 27 | |
| 28 | def 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 | |
| 36 | def test_expired_and_valid(): |
| 37 | credentials = CredentialsImpl() |
| 38 | credentials.token = 'token' |
| 39 | |
| 40 | assert credentials.valid |
| 41 | assert not credentials.expired |
| 42 | |
Jon Wayne Parrott | 7af9f66 | 2017-05-08 09:40:56 -0700 | [diff] [blame] | 43 | # Set the expiration to one second more than now plus the clock skew |
| 44 | # accomodation. These credentials should be valid. |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 45 | credentials.expiry = ( |
Jon Wayne Parrott | 7af9f66 | 2017-05-08 09:40:56 -0700 | [diff] [blame] | 46 | datetime.datetime.utcnow() + |
| 47 | _helpers.CLOCK_SKEW + |
Jon Wayne Parrott | e60c124 | 2017-03-23 16:00:24 -0700 | [diff] [blame] | 48 | datetime.timedelta(seconds=1)) |
| 49 | |
| 50 | assert credentials.valid |
| 51 | assert not credentials.expired |
| 52 | |
Jon Wayne Parrott | 7af9f66 | 2017-05-08 09:40:56 -0700 | [diff] [blame] | 53 | # 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 Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 56 | |
| 57 | assert not credentials.valid |
| 58 | assert credentials.expired |
| 59 | |
| 60 | |
| 61 | def 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 Seaver | b096a3d | 2017-10-30 16:12:37 -0400 | [diff] [blame^] | 82 | def 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 | |
| 90 | def test_anonymous_credentials_refresh(): |
| 91 | anon = credentials.AnonymousCredentials() |
| 92 | request = object() |
| 93 | with pytest.raises(ValueError): |
| 94 | anon.refresh(request) |
| 95 | |
| 96 | |
| 97 | def 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 | |
| 106 | def 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 Parrott | 4460a96 | 2017-09-12 10:01:23 -0700 | [diff] [blame] | 116 | class ReadOnlyScopedCredentialsImpl( |
| 117 | credentials.ReadOnlyScoped, CredentialsImpl): |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 118 | @property |
| 119 | def requires_scopes(self): |
Jon Wayne Parrott | 4460a96 | 2017-09-12 10:01:23 -0700 | [diff] [blame] | 120 | return super(ReadOnlyScopedCredentialsImpl, self).requires_scopes |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 121 | |
| 122 | |
Tres Seaver | 4246832 | 2017-09-11 15:36:53 -0400 | [diff] [blame] | 123 | def test_readonly_scoped_credentials_constructor(): |
Jon Wayne Parrott | 4460a96 | 2017-09-12 10:01:23 -0700 | [diff] [blame] | 124 | credentials = ReadOnlyScopedCredentialsImpl() |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 125 | assert credentials._scopes is None |
| 126 | |
| 127 | |
Tres Seaver | 4246832 | 2017-09-11 15:36:53 -0400 | [diff] [blame] | 128 | def test_readonly_scoped_credentials_scopes(): |
Jon Wayne Parrott | 4460a96 | 2017-09-12 10:01:23 -0700 | [diff] [blame] | 129 | credentials = ReadOnlyScopedCredentialsImpl() |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 130 | 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 Seaver | 4246832 | 2017-09-11 15:36:53 -0400 | [diff] [blame] | 138 | def test_readonly_scoped_credentials_requires_scopes(): |
Jon Wayne Parrott | 4460a96 | 2017-09-12 10:01:23 -0700 | [diff] [blame] | 139 | credentials = ReadOnlyScopedCredentialsImpl() |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 140 | assert not credentials.requires_scopes |
Jon Wayne Parrott | f89a3cf | 2016-10-31 10:52:57 -0700 | [diff] [blame] | 141 | |
| 142 | |
| 143 | class 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 | |
| 156 | def 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 | |
| 166 | def 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 |