blob: 6347e42d659ee3ce3680af824bd77eb0d51bd1b5 [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
Bu Sun Kim790e7022020-09-11 20:18:06 -060043 def test_credentials_from_file(self):
44 with mock.patch(
45 "google.auth.load_credentials_from_file", autospec=True
46 ) as default:
47 default.return_value = (mock.sentinel.credentials, mock.sentinel.project)
48
49 credentials = _auth.credentials_from_file("credentials.json")
50
51 self.assertEqual(credentials, mock.sentinel.credentials)
52 default.assert_called_once_with(
53 "credentials.json", scopes=None, quota_project_id=None
54 )
55
56 def test_default_credentials_with_scopes(self):
57 with mock.patch("google.auth.default", autospec=True) as default:
58 default.return_value = (mock.sentinel.credentials, mock.sentinel.project)
59 credentials = _auth.default_credentials(scopes=["1", "2"])
60
61 default.assert_called_once_with(scopes=["1", "2"], quota_project_id=None)
62 self.assertEqual(credentials, mock.sentinel.credentials)
63
64 def test_default_credentials_with_quota_project(self):
65 with mock.patch("google.auth.default", autospec=True) as default:
66 default.return_value = (mock.sentinel.credentials, mock.sentinel.project)
67 credentials = _auth.default_credentials(quota_project_id="my-project")
68
69 default.assert_called_once_with(scopes=None, quota_project_id="my-project")
70 self.assertEqual(credentials, mock.sentinel.credentials)
71
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080072 def test_with_scopes_non_scoped(self):
73 credentials = mock.Mock(spec=google.auth.credentials.Credentials)
74
75 returned = _auth.with_scopes(credentials, mock.sentinel.scopes)
76
77 self.assertEqual(credentials, returned)
78
79 def test_with_scopes_scoped(self):
80 class CredentialsWithScopes(
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070081 google.auth.credentials.Credentials, google.auth.credentials.Scoped
82 ):
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080083 pass
84
85 credentials = mock.Mock(spec=CredentialsWithScopes)
86 credentials.requires_scopes = True
87
88 returned = _auth.with_scopes(credentials, mock.sentinel.scopes)
89
90 self.assertNotEqual(credentials, returned)
91 self.assertEqual(returned, credentials.with_scopes.return_value)
Anthonios Partheniou76657062021-02-22 15:44:11 -050092 credentials.with_scopes.assert_called_once_with(mock.sentinel.scopes, default_scopes=None)
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080093
94 def test_authorized_http(self):
95 credentials = mock.Mock(spec=google.auth.credentials.Credentials)
96
Igor Maravić22435292017-01-19 22:28:22 +010097 authorized_http = _auth.authorized_http(credentials)
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -080098
Bu Sun Kim66bb32c2019-10-30 10:11:58 -070099 self.assertIsInstance(authorized_http, google_auth_httplib2.AuthorizedHttp)
Igor Maravić22435292017-01-19 22:28:22 +0100100 self.assertEqual(authorized_http.credentials, credentials)
101 self.assertIsInstance(authorized_http.http, httplib2.Http)
102 self.assertIsInstance(authorized_http.http.timeout, int)
103 self.assertGreater(authorized_http.http.timeout, 0)
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800104
105
Jan van der Lugt46164b92019-01-15 14:20:54 -0800106class TestAuthWithOAuth2Client(unittest.TestCase):
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800107 def setUp(self):
108 _auth.HAS_GOOGLE_AUTH = False
109 _auth.HAS_OAUTH2CLIENT = True
110
111 def tearDown(self):
112 _auth.HAS_GOOGLE_AUTH = True
113 _auth.HAS_OAUTH2CLIENT = True
114
115 def test_default_credentials(self):
116 default_patch = mock.patch(
Bu Sun Kim66bb32c2019-10-30 10:11:58 -0700117 "oauth2client.client.GoogleCredentials.get_application_default"
118 )
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800119
120 with default_patch as default:
121 default.return_value = mock.sentinel.credentials
122
123 credentials = _auth.default_credentials()
124
125 self.assertEqual(credentials, mock.sentinel.credentials)
126
Bu Sun Kim790e7022020-09-11 20:18:06 -0600127 def test_credentials_from_file(self):
128 with self.assertRaises(EnvironmentError):
129 credentials = _auth.credentials_from_file("credentials.json")
130
131 def test_default_credentials_with_scopes_and_quota_project(self):
132 with self.assertRaises(EnvironmentError):
133 credentials = _auth.default_credentials(
134 scopes=["1", "2"], quota_project_id="my-project"
135 )
136
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800137 def test_with_scopes_non_scoped(self):
138 credentials = mock.Mock(spec=oauth2client.client.Credentials)
139
140 returned = _auth.with_scopes(credentials, mock.sentinel.scopes)
141
142 self.assertEqual(credentials, returned)
143
144 def test_with_scopes_scoped(self):
145 credentials = mock.Mock(spec=oauth2client.client.GoogleCredentials)
146 credentials.create_scoped_required.return_value = True
147
148 returned = _auth.with_scopes(credentials, mock.sentinel.scopes)
149
150 self.assertNotEqual(credentials, returned)
151 self.assertEqual(returned, credentials.create_scoped.return_value)
152 credentials.create_scoped.assert_called_once_with(mock.sentinel.scopes)
153
154 def test_authorized_http(self):
155 credentials = mock.Mock(spec=oauth2client.client.Credentials)
156
Igor Maravić22435292017-01-19 22:28:22 +0100157 authorized_http = _auth.authorized_http(credentials)
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800158
Igor Maravić22435292017-01-19 22:28:22 +0100159 http = credentials.authorize.call_args[0][0]
160
161 self.assertEqual(authorized_http, credentials.authorize.return_value)
162 self.assertIsInstance(http, httplib2.Http)
163 self.assertIsInstance(http.timeout, int)
164 self.assertGreater(http.timeout, 0)
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800165
166
Jan van der Lugt46164b92019-01-15 14:20:54 -0800167class TestAuthWithoutAuth(unittest.TestCase):
Jon Wayne Parrott85c2c6d2017-01-05 12:34:49 -0800168 def setUp(self):
169 _auth.HAS_GOOGLE_AUTH = False
170 _auth.HAS_OAUTH2CLIENT = False
171
172 def tearDown(self):
173 _auth.HAS_GOOGLE_AUTH = True
174 _auth.HAS_OAUTH2CLIENT = True
175
176 def test_default_credentials(self):
177 with self.assertRaises(EnvironmentError):
178 print(_auth.default_credentials())
Jon Wayne Parrott401e8862017-09-19 18:36:35 -0700179
180
Jan van der Lugt46164b92019-01-15 14:20:54 -0800181class TestGoogleAuthWithoutHttplib2(unittest.TestCase):
Jon Wayne Parrott401e8862017-09-19 18:36:35 -0700182 def setUp(self):
183 _auth.google_auth_httplib2 = None
184
185 def tearDown(self):
186 _auth.google_auth_httplib2 = google_auth_httplib2
187
188 def test_default_credentials(self):
189 credentials = mock.Mock(spec=google.auth.credentials.Credentials)
190 with self.assertRaises(ValueError):
191 _auth.authorized_http(credentials)