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 | |
Jon Wayne Parrott | e60c124 | 2017-03-23 16:00:24 -0700 | [diff] [blame^] | 17 | from google.auth import _helpers |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 18 | from google.auth import credentials |
| 19 | |
| 20 | |
| 21 | class CredentialsImpl(credentials.Credentials): |
| 22 | def refresh(self, request): |
| 23 | self.token = request |
| 24 | |
| 25 | |
| 26 | def 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 | |
| 34 | def test_expired_and_valid(): |
| 35 | credentials = CredentialsImpl() |
| 36 | credentials.token = 'token' |
| 37 | |
| 38 | assert credentials.valid |
| 39 | assert not credentials.expired |
| 40 | |
Jon Wayne Parrott | e60c124 | 2017-03-23 16:00:24 -0700 | [diff] [blame^] | 41 | # Set the expiration in the past, but because of clock skew accomodation |
| 42 | # the credentials should still be valid. |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 43 | credentials.expiry = ( |
Jon Wayne Parrott | e60c124 | 2017-03-23 16:00:24 -0700 | [diff] [blame^] | 44 | 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 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 | |
| 82 | class 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 | |
| 91 | def test_scoped_credentials_constructor(): |
| 92 | credentials = ScopedCredentialsImpl() |
| 93 | assert credentials._scopes is None |
| 94 | |
| 95 | |
| 96 | def 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 | |
| 106 | def test_scoped_credentials_requires_scopes(): |
| 107 | credentials = ScopedCredentialsImpl() |
| 108 | assert not credentials.requires_scopes |
Jon Wayne Parrott | f89a3cf | 2016-10-31 10:52:57 -0700 | [diff] [blame] | 109 | |
| 110 | |
| 111 | class 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 | |
| 124 | def 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 | |
| 134 | def 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 |