| 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 unittest |
| 16 |
from unittest import mock |
| 17 |
|
| 18 |
import google.auth.credentials |
| 19 |
import google_auth_httplib2 |
| 20 |
import httplib2 |
| 21 |
|
| 22 |
try: |
| 23 |
import oauth2client.client |
| 24 |
|
| 25 |
HAS_OAUTH2CLIENT = True |
| 26 |
except ImportError: |
| 27 |
HAS_OAUTH2CLIENT = False |
| 28 |
|
| 29 |
from googleapiclient import _auth |
| 30 |
|
| 31 |
|
| 32 |
class TestAuthWithGoogleAuth(unittest.TestCase): |
| 33 |
def setUp(self): |
| 34 |
_auth.HAS_GOOGLE_AUTH = True |
| 35 |
_auth.HAS_OAUTH2CLIENT = False |
| 36 |
|
| 37 |
def tearDown(self): |
| 38 |
_auth.HAS_GOOGLE_AUTH = True |
| 39 |
_auth.HAS_OAUTH2CLIENT = True |
| 40 |
|
| 41 |
def test_default_credentials(self): |
| 42 |
with mock.patch("google.auth.default", autospec=True) as default: |
| 43 |
default.return_value = (mock.sentinel.credentials, mock.sentinel.project) |
| 44 |
|
| 45 |
credentials = _auth.default_credentials() |
| 46 |
|
| 47 |
self.assertEqual(credentials, mock.sentinel.credentials) |
| 48 |
|
| 49 |
def test_credentials_from_file(self): |
| 50 |
with mock.patch( |
| 51 |
"google.auth.load_credentials_from_file", autospec=True |
| 52 |
) as default: |
| 53 |
default.return_value = (mock.sentinel.credentials, mock.sentinel.project) |
| 54 |
|
| 55 |
credentials = _auth.credentials_from_file("credentials.json") |
| 56 |
|
| 57 |
self.assertEqual(credentials, mock.sentinel.credentials) |
| 58 |
default.assert_called_once_with( |
| 59 |
"credentials.json", scopes=None, quota_project_id=None |
| 60 |
) |
| 61 |
|
| 62 |
def test_default_credentials_with_scopes(self): |
| 63 |
with mock.patch("google.auth.default", autospec=True) as default: |
| 64 |
default.return_value = (mock.sentinel.credentials, mock.sentinel.project) |
| 65 |
credentials = _auth.default_credentials(scopes=["1", "2"]) |
| 66 |
|
| 67 |
default.assert_called_once_with(scopes=["1", "2"], quota_project_id=None) |
| 68 |
self.assertEqual(credentials, mock.sentinel.credentials) |
| 69 |
|
| 70 |
def test_default_credentials_with_quota_project(self): |
| 71 |
with mock.patch("google.auth.default", autospec=True) as default: |
| 72 |
default.return_value = (mock.sentinel.credentials, mock.sentinel.project) |
| 73 |
credentials = _auth.default_credentials(quota_project_id="my-project") |
| 74 |
|
| 75 |
default.assert_called_once_with(scopes=None, quota_project_id="my-project") |
| 76 |
self.assertEqual(credentials, mock.sentinel.credentials) |
| 77 |
|
| 78 |
def test_with_scopes_non_scoped(self): |
| 79 |
credentials = mock.Mock(spec=google.auth.credentials.Credentials) |
| 80 |
|
| 81 |
returned = _auth.with_scopes(credentials, mock.sentinel.scopes) |
| 82 |
|
| 83 |
self.assertEqual(credentials, returned) |
| 84 |
|
| 85 |
def test_with_scopes_scoped(self): |
| 86 |
class CredentialsWithScopes( |
| 87 |
google.auth.credentials.Credentials, google.auth.credentials.Scoped |
| 88 |
): |
| 89 |
pass |
| 90 |
|
| 91 |
credentials = mock.Mock(spec=CredentialsWithScopes) |
| 92 |
credentials.requires_scopes = True |
| 93 |
|
| 94 |
returned = _auth.with_scopes(credentials, mock.sentinel.scopes) |
| 95 |
|
| 96 |
self.assertNotEqual(credentials, returned) |
| 97 |
self.assertEqual(returned, credentials.with_scopes.return_value) |
| 98 |
credentials.with_scopes.assert_called_once_with( |
| 99 |
mock.sentinel.scopes, default_scopes=None |
| 100 |
) |
| 101 |
|
| 102 |
def test_authorized_http(self): |
| 103 |
credentials = mock.Mock(spec=google.auth.credentials.Credentials) |
| 104 |
|
| 105 |
authorized_http = _auth.authorized_http(credentials) |
| 106 |
|
| 107 |
self.assertIsInstance(authorized_http, google_auth_httplib2.AuthorizedHttp) |
| 108 |
self.assertEqual(authorized_http.credentials, credentials) |
| 109 |
self.assertIsInstance(authorized_http.http, httplib2.Http) |
| 110 |
self.assertIsInstance(authorized_http.http.timeout, int) |
| 111 |
self.assertGreater(authorized_http.http.timeout, 0) |
| 112 |
|
| 113 |
|
| 114 |
@unittest.skipIf(not HAS_OAUTH2CLIENT, "oauth2client unavailable.") |
| 115 |
class TestAuthWithOAuth2Client(unittest.TestCase): |
| 116 |
def setUp(self): |
| 117 |
_auth.HAS_GOOGLE_AUTH = False |
| 118 |
_auth.HAS_OAUTH2CLIENT = True |
| 119 |
|
| 120 |
def tearDown(self): |
| 121 |
_auth.HAS_GOOGLE_AUTH = True |
| 122 |
_auth.HAS_OAUTH2CLIENT = True |
| 123 |
|
| 124 |
def test_default_credentials(self): |
| 125 |
default_patch = mock.patch( |
| 126 |
"oauth2client.client.GoogleCredentials.get_application_default" |
| 127 |
) |
| 128 |
|
| 129 |
with default_patch as default: |
| 130 |
default.return_value = mock.sentinel.credentials |
| 131 |
|
| 132 |
credentials = _auth.default_credentials() |
| 133 |
|
| 134 |
self.assertEqual(credentials, mock.sentinel.credentials) |
| 135 |
|
| 136 |
def test_credentials_from_file(self): |
| 137 |
with self.assertRaises(EnvironmentError): |
| 138 |
credentials = _auth.credentials_from_file("credentials.json") |
| 139 |
|
| 140 |
def test_default_credentials_with_scopes_and_quota_project(self): |
| 141 |
with self.assertRaises(EnvironmentError): |
| 142 |
credentials = _auth.default_credentials( |
| 143 |
scopes=["1", "2"], quota_project_id="my-project" |
| 144 |
) |
| 145 |
|
| 146 |
def test_with_scopes_non_scoped(self): |
| 147 |
credentials = mock.Mock(spec=oauth2client.client.Credentials) |
| 148 |
|
| 149 |
returned = _auth.with_scopes(credentials, mock.sentinel.scopes) |
| 150 |
|
| 151 |
self.assertEqual(credentials, returned) |
| 152 |
|
| 153 |
def test_with_scopes_scoped(self): |
| 154 |
credentials = mock.Mock(spec=oauth2client.client.GoogleCredentials) |
| 155 |
credentials.create_scoped_required.return_value = True |
| 156 |
|
| 157 |
returned = _auth.with_scopes(credentials, mock.sentinel.scopes) |
| 158 |
|
| 159 |
self.assertNotEqual(credentials, returned) |
| 160 |
self.assertEqual(returned, credentials.create_scoped.return_value) |
| 161 |
credentials.create_scoped.assert_called_once_with(mock.sentinel.scopes) |
| 162 |
|
| 163 |
def test_authorized_http(self): |
| 164 |
credentials = mock.Mock(spec=oauth2client.client.Credentials) |
| 165 |
|
| 166 |
authorized_http = _auth.authorized_http(credentials) |
| 167 |
|
| 168 |
http = credentials.authorize.call_args[0][0] |
| 169 |
|
| 170 |
self.assertEqual(authorized_http, credentials.authorize.return_value) |
| 171 |
self.assertIsInstance(http, httplib2.Http) |
| 172 |
self.assertIsInstance(http.timeout, int) |
| 173 |
self.assertGreater(http.timeout, 0) |
| 174 |
|
| 175 |
|
| 176 |
class TestAuthWithoutAuth(unittest.TestCase): |
| 177 |
def setUp(self): |
| 178 |
_auth.HAS_GOOGLE_AUTH = False |
| 179 |
_auth.HAS_OAUTH2CLIENT = False |
| 180 |
|
| 181 |
def tearDown(self): |
| 182 |
_auth.HAS_GOOGLE_AUTH = True |
| 183 |
_auth.HAS_OAUTH2CLIENT = True |
| 184 |
|
| 185 |
def test_default_credentials(self): |
| 186 |
with self.assertRaises(EnvironmentError): |
| 187 |
print(_auth.default_credentials()) |
| 188 |
|
| 189 |
|
| 190 |
class TestGoogleAuthWithoutHttplib2(unittest.TestCase): |
| 191 |
def setUp(self): |
| 192 |
_auth.google_auth_httplib2 = None |
| 193 |
|
| 194 |
def tearDown(self): |
| 195 |
_auth.google_auth_httplib2 = google_auth_httplib2 |
| 196 |
|
| 197 |
def test_default_credentials(self): |
| 198 |
credentials = mock.Mock(spec=google.auth.credentials.Credentials) |
| 199 |
with self.assertRaises(ValueError): |
| 200 |
_auth.authorized_http(credentials) |