blob: fa86a16d5b02f40dd9a19d7f4fc62efd119239b4 [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
21from google.oauth2 import credentials
22
23
24class 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 Hermes97791812016-11-01 12:43:01 -070029 credentials = None
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070030
31 @pytest.fixture(autouse=True)
Danny Hermes97791812016-11-01 12:43:01 -070032 def credentials_fixture(self):
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070033 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 Parrott2d0549a2017-03-01 09:27:16 -080044 # 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 Parrott10ec7e92016-10-17 10:46:38 -070049
50 def test_create_scoped(self):
51 with pytest.raises(NotImplementedError):
52 self.credentials.with_scopes(['email'])
53
Jon Wayne Parrott8784b232016-11-10 12:53:55 -080054 @mock.patch('google.oauth2._client.refresh_grant', autospec=True)
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070055 @mock.patch(
56 'google.auth._helpers.utcnow', return_value=datetime.datetime.min)
57 def test_refresh_success(self, now_mock, refresh_grant_mock):
58 token = 'token'
59 expiry = _helpers.utcnow() + datetime.timedelta(seconds=500)
60 refresh_grant_mock.return_value = (
61 # Access token
62 token,
63 # New refresh token
64 None,
65 # Expiry,
66 expiry,
67 # Extra data
68 {})
69 request_mock = mock.Mock()
70
71 # Refresh credentials
72 self.credentials.refresh(request_mock)
73
74 # Check jwt grant call.
75 refresh_grant_mock.assert_called_with(
76 request_mock, self.TOKEN_URI, self.REFRESH_TOKEN, self.CLIENT_ID,
77 self.CLIENT_SECRET)
78
79 # Check that the credentials have the token and expiry
80 assert self.credentials.token == token
81 assert self.credentials.expiry == expiry
82
83 # Check that the credentials are valid (have a token and are not
84 # expired)
85 assert self.credentials.valid