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 |
| 21 | import unittest2 |
| 22 | |
| 23 | from googleapiclient import _auth |
| 24 | |
| 25 | |
| 26 | class TestAuthWithGoogleAuth(unittest2.TestCase): |
| 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): |
| 36 | with mock.patch('google.auth.default', autospec=True) as default: |
| 37 | default.return_value = ( |
| 38 | mock.sentinel.credentials, mock.sentinel.project) |
| 39 | |
| 40 | credentials = _auth.default_credentials() |
| 41 | |
| 42 | self.assertEqual(credentials, mock.sentinel.credentials) |
| 43 | |
| 44 | def test_with_scopes_non_scoped(self): |
| 45 | credentials = mock.Mock(spec=google.auth.credentials.Credentials) |
| 46 | |
| 47 | returned = _auth.with_scopes(credentials, mock.sentinel.scopes) |
| 48 | |
| 49 | self.assertEqual(credentials, returned) |
| 50 | |
| 51 | def test_with_scopes_scoped(self): |
| 52 | class CredentialsWithScopes( |
| 53 | google.auth.credentials.Credentials, |
| 54 | google.auth.credentials.Scoped): |
| 55 | pass |
| 56 | |
| 57 | credentials = mock.Mock(spec=CredentialsWithScopes) |
| 58 | credentials.requires_scopes = True |
| 59 | |
| 60 | returned = _auth.with_scopes(credentials, mock.sentinel.scopes) |
| 61 | |
| 62 | self.assertNotEqual(credentials, returned) |
| 63 | self.assertEqual(returned, credentials.with_scopes.return_value) |
| 64 | credentials.with_scopes.assert_called_once_with(mock.sentinel.scopes) |
| 65 | |
| 66 | def test_authorized_http(self): |
| 67 | credentials = mock.Mock(spec=google.auth.credentials.Credentials) |
| 68 | |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame^] | 69 | authorized_http = _auth.authorized_http(credentials) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 70 | |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame^] | 71 | self.assertIsInstance(authorized_http, google_auth_httplib2.AuthorizedHttp) |
| 72 | self.assertEqual(authorized_http.credentials, credentials) |
| 73 | self.assertIsInstance(authorized_http.http, httplib2.Http) |
| 74 | self.assertIsInstance(authorized_http.http.timeout, int) |
| 75 | self.assertGreater(authorized_http.http.timeout, 0) |
Jon Wayne Parrott | 85c2c6d | 2017-01-05 12:34:49 -0800 | [diff] [blame] | 76 | |
| 77 | |
| 78 | class TestAuthWithOAuth2Client(unittest2.TestCase): |
| 79 | def setUp(self): |
| 80 | _auth.HAS_GOOGLE_AUTH = False |
| 81 | _auth.HAS_OAUTH2CLIENT = True |
| 82 | |
| 83 | def tearDown(self): |
| 84 | _auth.HAS_GOOGLE_AUTH = True |
| 85 | _auth.HAS_OAUTH2CLIENT = True |
| 86 | |
| 87 | def test_default_credentials(self): |
| 88 | default_patch = mock.patch( |
| 89 | 'oauth2client.client.GoogleCredentials.get_application_default') |
| 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 | |
| 128 | class TestAuthWithoutAuth(unittest2.TestCase): |
| 129 | |
| 130 | def setUp(self): |
| 131 | _auth.HAS_GOOGLE_AUTH = False |
| 132 | _auth.HAS_OAUTH2CLIENT = False |
| 133 | |
| 134 | def tearDown(self): |
| 135 | _auth.HAS_GOOGLE_AUTH = True |
| 136 | _auth.HAS_OAUTH2CLIENT = True |
| 137 | |
| 138 | def test_default_credentials(self): |
| 139 | with self.assertRaises(EnvironmentError): |
| 140 | print(_auth.default_credentials()) |