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 15import mock 16 17import google.auth.credentials 18import google_auth_httplib2 19import httplib2 20import oauth2client.client 21import unittest2 as unittest 22 23from googleapiclient import _auth 24 25 26class TestAuthWithGoogleAuth(unittest.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 = (mock.sentinel.credentials, mock.sentinel.project) 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( 52 google.auth.credentials.Credentials, google.auth.credentials.Scoped 53 ): 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 68 authorized_http = _auth.authorized_http(credentials) 69 70 self.assertIsInstance(authorized_http, google_auth_httplib2.AuthorizedHttp) 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) 75 76 77class TestAuthWithOAuth2Client(unittest.TestCase): 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( 88 "oauth2client.client.GoogleCredentials.get_application_default" 89 ) 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 118 authorized_http = _auth.authorized_http(credentials) 119 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) 126 127 128class TestAuthWithoutAuth(unittest.TestCase): 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()) 140 141 142class TestGoogleAuthWithoutHttplib2(unittest.TestCase): 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) 153