Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -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 | |
| 17 | import mock |
| 18 | import pytest |
| 19 | |
| 20 | from google.auth import _helpers |
| 21 | from google.oauth2 import credentials |
| 22 | |
| 23 | |
| 24 | class TestCredentials(object): |
| 25 | TOKEN_URI = 'https://example.com/oauth2/token' |
| 26 | REFRESH_TOKEN = 'refresh_token' |
| 27 | CLIENT_ID = 'client_id' |
| 28 | CLIENT_SECRET = 'client_secret' |
Danny Hermes | 9779181 | 2016-11-01 12:43:01 -0700 | [diff] [blame] | 29 | credentials = None |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 30 | |
| 31 | @pytest.fixture(autouse=True) |
Danny Hermes | 9779181 | 2016-11-01 12:43:01 -0700 | [diff] [blame] | 32 | def credentials_fixture(self): |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 33 | self.credentials = credentials.Credentials( |
| 34 | token=None, refresh_token=self.REFRESH_TOKEN, |
| 35 | token_uri=self.TOKEN_URI, client_id=self.CLIENT_ID, |
| 36 | client_secret=self.CLIENT_SECRET) |
| 37 | |
| 38 | def test_default_state(self): |
| 39 | assert not self.credentials.valid |
| 40 | # Expiration hasn't been set yet |
| 41 | assert not self.credentials.expired |
| 42 | # Scopes aren't required for these credentials |
| 43 | assert not self.credentials.requires_scopes |
Jon Wayne Parrott | 2d0549a | 2017-03-01 09:27:16 -0800 | [diff] [blame] | 44 | # Test properties |
| 45 | assert self.credentials.refresh_token == self.REFRESH_TOKEN |
| 46 | assert self.credentials.token_uri == self.TOKEN_URI |
| 47 | assert self.credentials.client_id == self.CLIENT_ID |
| 48 | assert self.credentials.client_secret == self.CLIENT_SECRET |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 49 | |
| 50 | def test_create_scoped(self): |
| 51 | with pytest.raises(NotImplementedError): |
| 52 | self.credentials.with_scopes(['email']) |
| 53 | |
Jon Wayne Parrott | 8784b23 | 2016-11-10 12:53:55 -0800 | [diff] [blame] | 54 | @mock.patch('google.oauth2._client.refresh_grant', autospec=True) |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 55 | @mock.patch( |
Jon Wayne Parrott | e60c124 | 2017-03-23 16:00:24 -0700 | [diff] [blame] | 56 | 'google.auth._helpers.utcnow', |
| 57 | return_value=datetime.datetime.min + _helpers.CLOCK_SKEW) |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 58 | def test_refresh_success(self, now_mock, refresh_grant_mock): |
| 59 | token = 'token' |
| 60 | expiry = _helpers.utcnow() + datetime.timedelta(seconds=500) |
Jon Wayne Parrott | 26a1637 | 2017-03-28 13:03:33 -0700 | [diff] [blame] | 61 | grant_response = {'id_token': mock.sentinel.id_token} |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 62 | refresh_grant_mock.return_value = ( |
| 63 | # Access token |
| 64 | token, |
| 65 | # New refresh token |
| 66 | None, |
| 67 | # Expiry, |
| 68 | expiry, |
| 69 | # Extra data |
Jon Wayne Parrott | 26a1637 | 2017-03-28 13:03:33 -0700 | [diff] [blame] | 70 | grant_response) |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 71 | request_mock = mock.Mock() |
| 72 | |
| 73 | # Refresh credentials |
| 74 | self.credentials.refresh(request_mock) |
| 75 | |
| 76 | # Check jwt grant call. |
| 77 | refresh_grant_mock.assert_called_with( |
| 78 | request_mock, self.TOKEN_URI, self.REFRESH_TOKEN, self.CLIENT_ID, |
| 79 | self.CLIENT_SECRET) |
| 80 | |
| 81 | # Check that the credentials have the token and expiry |
| 82 | assert self.credentials.token == token |
| 83 | assert self.credentials.expiry == expiry |
Jon Wayne Parrott | 26a1637 | 2017-03-28 13:03:33 -0700 | [diff] [blame] | 84 | assert self.credentials.id_token == mock.sentinel.id_token |
Jon Wayne Parrott | 10ec7e9 | 2016-10-17 10:46:38 -0700 | [diff] [blame] | 85 | |
| 86 | # Check that the credentials are valid (have a token and are not |
| 87 | # expired) |
| 88 | assert self.credentials.valid |