Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 1 | # 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 | |
| 15 | import mock |
| 16 | |
| 17 | import google.auth.credentials |
| 18 | import google_auth_httplib2 |
| 19 | import httplib2 |
| 20 | import oauth2client.client |
Jan van der Lugt | 46164b9 | 2019-01-15 14:20:54 -0800 | [diff] [blame] | 21 | import unittest2 as unittest |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 22 | |
| 23 | from googleapiclient import _auth |
| 24 | |
| 25 | |
Jan van der Lugt | 46164b9 | 2019-01-15 14:20:54 -0800 | [diff] [blame] | 26 | class TestAuthWithGoogleAuth(unittest.TestCase): |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 27 | 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 Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 36 | with mock.patch("google.auth.default", autospec=True) as default: |
| 37 | default.return_value = (mock.sentinel.credentials, mock.sentinel.project) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 38 | |
| 39 | credentials = _auth.default_credentials() |
| 40 | |
| 41 | self.assertEqual(credentials, mock.sentinel.credentials) |
| 42 | |
Bu Sun Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 43 | 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 Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 72 | 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 Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 81 | google.auth.credentials.Credentials, google.auth.credentials.Scoped |
| 82 | ): |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 83 | 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 Partheniou | 7665706 | 2021-02-22 15:44:11 -0500 | [diff] [blame] | 92 | credentials.with_scopes.assert_called_once_with(mock.sentinel.scopes, default_scopes=None) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 93 | |
| 94 | def test_authorized_http(self): |
| 95 | credentials = mock.Mock(spec=google.auth.credentials.Credentials) |
| 96 | |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 97 | authorized_http = _auth.authorized_http(credentials) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 98 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 99 | self.assertIsInstance(authorized_http, google_auth_httplib2.AuthorizedHttp) |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 100 | 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 Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 104 | |
| 105 | |
Jan van der Lugt | 46164b9 | 2019-01-15 14:20:54 -0800 | [diff] [blame] | 106 | class TestAuthWithOAuth2Client(unittest.TestCase): |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 107 | 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 Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 117 | "oauth2client.client.GoogleCredentials.get_application_default" |
| 118 | ) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 119 | |
| 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 Kim | 790e702 | 2020-09-11 20:18:06 -0600 | [diff] [blame] | 127 | 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 Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 137 | 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ć | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 157 | authorized_http = _auth.authorized_http(credentials) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 158 | |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 159 | 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 Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 165 | |
| 166 | |
Jan van der Lugt | 46164b9 | 2019-01-15 14:20:54 -0800 | [diff] [blame] | 167 | class TestAuthWithoutAuth(unittest.TestCase): |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 168 | 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 Parrott | 401e886 | 2017-09-19 18:36:35 -0700 | [diff] [blame] | 179 | |
| 180 | |
Jan van der Lugt | 46164b9 | 2019-01-15 14:20:54 -0800 | [diff] [blame] | 181 | class TestGoogleAuthWithoutHttplib2(unittest.TestCase): |
Jon Wayne Parrott | 401e886 | 2017-09-19 18:36:35 -0700 | [diff] [blame] | 182 | 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) |