blob: 14984a1dbaa3ce3c2ea81c22275c3d6af4b74aa4 [file] [log] [blame]
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -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
17import mock
18import pytest
19
20from google.auth import _helpers
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070021from google.auth import transport
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070022from google.oauth2 import credentials
23
24
25class TestCredentials(object):
26 TOKEN_URI = 'https://example.com/oauth2/token'
27 REFRESH_TOKEN = 'refresh_token'
28 CLIENT_ID = 'client_id'
29 CLIENT_SECRET = 'client_secret'
30
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070031 @classmethod
32 def make_credentials(cls):
33 return credentials.Credentials(
34 token=None, refresh_token=cls.REFRESH_TOKEN,
35 token_uri=cls.TOKEN_URI, client_id=cls.CLIENT_ID,
36 client_secret=cls.CLIENT_SECRET)
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070037
38 def test_default_state(self):
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070039 credentials = self.make_credentials()
40 assert not credentials.valid
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070041 # Expiration hasn't been set yet
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070042 assert not credentials.expired
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070043 # Scopes aren't required for these credentials
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070044 assert not credentials.requires_scopes
Jon Wayne Parrott2d0549a2017-03-01 09:27:16 -080045 # Test properties
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070046 assert credentials.refresh_token == self.REFRESH_TOKEN
47 assert credentials.token_uri == self.TOKEN_URI
48 assert credentials.client_id == self.CLIENT_ID
49 assert credentials.client_secret == self.CLIENT_SECRET
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070050
51 def test_create_scoped(self):
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070052 credentials = self.make_credentials()
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070053 with pytest.raises(NotImplementedError):
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070054 credentials.with_scopes(['email'])
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070055
Jon Wayne Parrott8784b232016-11-10 12:53:55 -080056 @mock.patch('google.oauth2._client.refresh_grant', autospec=True)
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070057 @mock.patch(
Jon Wayne Parrotte60c1242017-03-23 16:00:24 -070058 'google.auth._helpers.utcnow',
59 return_value=datetime.datetime.min + _helpers.CLOCK_SKEW)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070060 def test_refresh_success(self, unused_utcnow, refresh_grant):
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070061 token = 'token'
62 expiry = _helpers.utcnow() + datetime.timedelta(seconds=500)
Jon Wayne Parrott26a16372017-03-28 13:03:33 -070063 grant_response = {'id_token': mock.sentinel.id_token}
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070064 refresh_grant.return_value = (
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070065 # Access token
66 token,
67 # New refresh token
68 None,
69 # Expiry,
70 expiry,
71 # Extra data
Jon Wayne Parrott26a16372017-03-28 13:03:33 -070072 grant_response)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070073
74 request = mock.create_autospec(transport.Request)
75 credentials = self.make_credentials()
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070076
77 # Refresh credentials
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070078 credentials.refresh(request)
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070079
80 # Check jwt grant call.
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070081 refresh_grant.assert_called_with(
82 request, self.TOKEN_URI, self.REFRESH_TOKEN, self.CLIENT_ID,
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070083 self.CLIENT_SECRET)
84
85 # Check that the credentials have the token and expiry
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070086 assert credentials.token == token
87 assert credentials.expiry == expiry
88 assert credentials.id_token == mock.sentinel.id_token
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070089
90 # Check that the credentials are valid (have a token and are not
91 # expired)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070092 assert credentials.valid