def test_save_credentials_errors(self): """Test save credentials when get_credentials raises exception.""" event = choice(EVENT_LIST) msg = msg_generator(event) processor = KafkaMessageProcessor(msg, event, COST_MGMT_APP_TYPE_ID) with patch.multiple( SourcesHTTPClient, _get_aws_credentials=MagicMock( side_effect=SourcesHTTPClientError), _get_azure_credentials=MagicMock( side_effect=SourcesHTTPClientError), _get_gcp_credentials=MagicMock( side_effect=SourcesHTTPClientError), _get_ocp_credentials=MagicMock( side_effect=SourcesHTTPClientError), ): for provider in Provider.PROVIDER_LIST: with self.subTest(test=provider): with patch("sources.storage.get_source_type", return_value=provider): with patch.object(SourcesHTTPClient, "set_source_status") as mock_set: with patch( "sources.storage.add_provider_sources_auth_info" ) as mock_add: with self.assertRaises(SourcesHTTPClientError): result = processor.save_credentials() self.assertIsNone(result) mock_set.assert_called_once() mock_add.assert_not_called()
def test_save_credentials(self): """Test save credentials calls add_provider_sources_auth_info.""" event = choice(EVENT_LIST) msg = msg_generator(event) processor = KafkaMessageProcessor(msg, event, COST_MGMT_APP_TYPE_ID) with patch.multiple( # Add all new providers here SourcesHTTPClient, _get_aws_credentials=MagicMock( return_value=self.valid_creds.get(Provider.PROVIDER_AWS)), _get_azure_credentials=MagicMock(return_value=self.valid_creds. get(Provider.PROVIDER_AZURE)), _get_gcp_credentials=MagicMock( return_value=self.valid_creds.get(Provider.PROVIDER_GCP)), _get_ocp_credentials=MagicMock( return_value=self.valid_creds.get(Provider.PROVIDER_OCP)), ): table = [{ "return": None, "expected": False }, { "return": True, "expected": True }] for test, provider in product(table, PROVIDER_LIST): with self.subTest(test=(test, provider)): with patch("sources.storage.get_source_type", return_value=provider): with patch( "sources.storage.add_provider_sources_auth_info", return_value=test.get("return")) as mock_add: result = processor.save_credentials() self.assertEqual(result, test.get("expected")) mock_add.assert_called_with( None, { "credentials": self.valid_creds.get(provider) })