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 | |
| 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 Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame^] | 52 | google.auth.credentials.Credentials, google.auth.credentials.Scoped |
| 53 | ): |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 54 | 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ć | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 68 | authorized_http = _auth.authorized_http(credentials) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 69 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame^] | 70 | self.assertIsInstance(authorized_http, google_auth_httplib2.AuthorizedHttp) |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 71 | 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 Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 75 | |
| 76 | |
Jan van der Lugt | 46164b9 | 2019-01-15 14:20:54 -0800 | [diff] [blame] | 77 | class TestAuthWithOAuth2Client(unittest.TestCase): |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 78 | 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 Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame^] | 88 | "oauth2client.client.GoogleCredentials.get_application_default" |
| 89 | ) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 90 | |
| 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ć | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 118 | authorized_http = _auth.authorized_http(credentials) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 119 | |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 120 | 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 Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 126 | |
| 127 | |
Jan van der Lugt | 46164b9 | 2019-01-15 14:20:54 -0800 | [diff] [blame] | 128 | class TestAuthWithoutAuth(unittest.TestCase): |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 129 | 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 Parrott | 401e886 | 2017-09-19 18:36:35 -0700 | [diff] [blame] | 140 | |
| 141 | |
Jan van der Lugt | 46164b9 | 2019-01-15 14:20:54 -0800 | [diff] [blame] | 142 | class TestGoogleAuthWithoutHttplib2(unittest.TestCase): |
Jon Wayne Parrott | 401e886 | 2017-09-19 18:36:35 -0700 | [diff] [blame] | 143 | 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) |