blob: b65ed81d46a0a56faa07816196367e88499a2f47 [file] [log] [blame]
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -08001# Copyright 2016 Google Inc. All Rights Reserved.
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 mock
16
17import google.auth.credentials
18import google_auth_httplib2
19import httplib2
20import oauth2client.client
Jan van der Lugt46164b92019-01-15 14:20:54 -080021import unittest2 as unittest
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080022
23from googleapiclient import _auth
24
25
Jan van der Lugt46164b92019-01-15 14:20:54 -080026class TestAuthWithGoogleAuth(unittest.TestCase):
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080027 def setUp(self):
28 _auth.HAS_GOOGLE_AUTH = True
29 _auth.HAS_OAUTH2CLIENT = False
30
31 def tearDown(self):
32 _auth.HAS_GOOGLE_AUTH = True
33 _auth.HAS_OAUTH2CLIENT = True
34
35 def test_default_credentials(self):
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070036 with mock.patch("google.auth.default", autospec=True) as default:
37 default.return_value = (mock.sentinel.credentials, mock.sentinel.project)
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080038
39 credentials = _auth.default_credentials()
40
41 self.assertEqual(credentials, mock.sentinel.credentials)
42
43 def test_with_scopes_non_scoped(self):
44 credentials = mock.Mock(spec=google.auth.credentials.Credentials)
45
46 returned = _auth.with_scopes(credentials, mock.sentinel.scopes)
47
48 self.assertEqual(credentials, returned)
49
50 def test_with_scopes_scoped(self):
51 class CredentialsWithScopes(
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070052 google.auth.credentials.Credentials, google.auth.credentials.Scoped
53 ):
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080054 pass
55
56 credentials = mock.Mock(spec=CredentialsWithScopes)
57 credentials.requires_scopes = True
58
59 returned = _auth.with_scopes(credentials, mock.sentinel.scopes)
60
61 self.assertNotEqual(credentials, returned)
62 self.assertEqual(returned, credentials.with_scopes.return_value)
63 credentials.with_scopes.assert_called_once_with(mock.sentinel.scopes)
64
65 def test_authorized_http(self):
66 credentials = mock.Mock(spec=google.auth.credentials.Credentials)
67
Igor Maravić22435292017-01-19 22:28:22 +010068 authorized_http = _auth.authorized_http(credentials)
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080069
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070070 self.assertIsInstance(authorized_http, google_auth_httplib2.AuthorizedHttp)
Igor Maravić22435292017-01-19 22:28:22 +010071 self.assertEqual(authorized_http.credentials, credentials)
72 self.assertIsInstance(authorized_http.http, httplib2.Http)
73 self.assertIsInstance(authorized_http.http.timeout, int)
74 self.assertGreater(authorized_http.http.timeout, 0)
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080075
76
Jan van der Lugt46164b92019-01-15 14:20:54 -080077class TestAuthWithOAuth2Client(unittest.TestCase):
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080078 def setUp(self):
79 _auth.HAS_GOOGLE_AUTH = False
80 _auth.HAS_OAUTH2CLIENT = True
81
82 def tearDown(self):
83 _auth.HAS_GOOGLE_AUTH = True
84 _auth.HAS_OAUTH2CLIENT = True
85
86 def test_default_credentials(self):
87 default_patch = mock.patch(
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070088 "oauth2client.client.GoogleCredentials.get_application_default"
89 )
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080090
91 with default_patch as default:
92 default.return_value = mock.sentinel.credentials
93
94 credentials = _auth.default_credentials()
95
96 self.assertEqual(credentials, mock.sentinel.credentials)
97
98 def test_with_scopes_non_scoped(self):
99 credentials = mock.Mock(spec=oauth2client.client.Credentials)
100
101 returned = _auth.with_scopes(credentials, mock.sentinel.scopes)
102
103 self.assertEqual(credentials, returned)
104
105 def test_with_scopes_scoped(self):
106 credentials = mock.Mock(spec=oauth2client.client.GoogleCredentials)
107 credentials.create_scoped_required.return_value = True
108
109 returned = _auth.with_scopes(credentials, mock.sentinel.scopes)
110
111 self.assertNotEqual(credentials, returned)
112 self.assertEqual(returned, credentials.create_scoped.return_value)
113 credentials.create_scoped.assert_called_once_with(mock.sentinel.scopes)
114
115 def test_authorized_http(self):
116 credentials = mock.Mock(spec=oauth2client.client.Credentials)
117
Igor Maravić22435292017-01-19 22:28:22 +0100118 authorized_http = _auth.authorized_http(credentials)
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800119
Igor Maravić22435292017-01-19 22:28:22 +0100120 http = credentials.authorize.call_args[0][0]
121
122 self.assertEqual(authorized_http, credentials.authorize.return_value)
123 self.assertIsInstance(http, httplib2.Http)
124 self.assertIsInstance(http.timeout, int)
125 self.assertGreater(http.timeout, 0)
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800126
127
Jan van der Lugt46164b92019-01-15 14:20:54 -0800128class TestAuthWithoutAuth(unittest.TestCase):
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800129 def setUp(self):
130 _auth.HAS_GOOGLE_AUTH = False
131 _auth.HAS_OAUTH2CLIENT = False
132
133 def tearDown(self):
134 _auth.HAS_GOOGLE_AUTH = True
135 _auth.HAS_OAUTH2CLIENT = True
136
137 def test_default_credentials(self):
138 with self.assertRaises(EnvironmentError):
139 print(_auth.default_credentials())
Jon Wayne Parrott401e8862017-09-19 18:36:35 -0700140
141
Jan van der Lugt46164b92019-01-15 14:20:54 -0800142class TestGoogleAuthWithoutHttplib2(unittest.TestCase):
Jon Wayne Parrott401e8862017-09-19 18:36:35 -0700143 def setUp(self):
144 _auth.google_auth_httplib2 = None
145
146 def tearDown(self):
147 _auth.google_auth_httplib2 = google_auth_httplib2
148
149 def test_default_credentials(self):
150 credentials = mock.Mock(spec=google.auth.credentials.Credentials)
151 with self.assertRaises(ValueError):
152 _auth.authorized_http(credentials)