1# -*- coding: utf-8 -*- 2# Copyright 2014 Google Inc. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15"""Tests for various combinations of configured credentials.""" 16 17from gslib.cred_types import CredTypes 18from gslib.exception import CommandException 19from gslib.gcs_json_api import GcsJsonApi 20from gslib.tests.mock_logging_handler import MockLoggingHandler 21import gslib.tests.testcase as testcase 22from gslib.tests.util import SetBotoConfigForTest 23 24 25class TestCredsConfig(testcase.GsUtilUnitTestCase): 26 """Tests for various combinations of configured credentials.""" 27 28 def setUp(self): 29 super(TestCredsConfig, self).setUp() 30 self.log_handler = MockLoggingHandler() 31 self.logger.addHandler(self.log_handler) 32 33 def testMultipleConfiguredCreds(self): 34 with SetBotoConfigForTest([ 35 ('Credentials', 'gs_oauth2_refresh_token', 'foo'), 36 ('Credentials', 'gs_service_client_id', 'bar'), 37 ('Credentials', 'gs_service_key_file', 'baz')]): 38 39 try: 40 GcsJsonApi(None, self.logger) 41 self.fail('Succeeded with multiple types of configured creds.') 42 except CommandException, e: 43 msg = str(e) 44 self.assertIn('types of configured credentials', msg) 45 self.assertIn(CredTypes.OAUTH2_USER_ACCOUNT, msg) 46 self.assertIn(CredTypes.OAUTH2_SERVICE_ACCOUNT, msg) 47 48 def testExactlyOneInvalid(self): 49 with SetBotoConfigForTest([ 50 ('Credentials', 'gs_oauth2_refresh_token', 'foo'), 51 ('Credentials', 'gs_service_client_id', None), 52 ('Credentials', 'gs_service_key_file', None)]): 53 succeeded = False 54 try: 55 GcsJsonApi(None, self.logger) 56 succeeded = True # If we self.fail() here, the except below will catch 57 except: # pylint: disable=bare-except 58 warning_messages = self.log_handler.messages['warning'] 59 self.assertEquals(1, len(warning_messages)) 60 self.assertIn('credentials are invalid', warning_messages[0]) 61 self.assertIn(CredTypes.OAUTH2_USER_ACCOUNT, warning_messages[0]) 62 if succeeded: 63 self.fail('Succeeded with invalid credentials, one configured.') 64