def test_cost_usage_source_is_reachable_bad_iam_token(self, usage_reports): """Test that cost_usage_source_is_reachable raises ValidationError on invalid IAM token.""" service = MagicMock() service.get_resource_usage_report.side_effect = ApiException( code=400, message="API key is wrong") usage_reports.return_value = service provider = IBMProvider() creds = {"iam_token": FAKE.word()} billing = {"enterprise_id": FAKE.word()} with self.assertRaises(ValidationError) as ve: provider.cost_usage_source_is_reachable(creds, billing) self.assertIsNotNone(ve.exception.get_full_details().get( "credentials.iam_token", None))
def test_page_downloader_factory_no_more_pages(self, get_sdk_headers, base_service_mock): iam_token = FAKE.sha1() enterprise_id = FAKE.uuid4() credentials = dict(iam_token=iam_token) data_source = dict(enterprise_id=enterprise_id) get_sdk_headers.return_value = None service_mock = MagicMock() service_mock.prepare_request.return_value = None service_mock.send.side_effect = ApiException( code=400, message="Error: Total pages is 4") base_service_mock.return_value = service_mock page_downloader = page_downloader_factory(credentials, data_source, "2020-12") with self.assertRaises(NoMoreReportPages): page_downloader(page=100)
def test_service_callable_raise_api_exception_strict_mode_on(self): # patch where the class is located. with mock.patch('watson_transformer.service.nlu.IAMAuthenticator'): with mock.patch('watson_transformer.service.stt.SpeechToTextV1' ) as mock_stt_api: # arrange mock_stt_api.return_value.recognize.side_effect = ApiException( 'STT API raise exception.' ) # mock stt.recognize().get_result() stt = STT(token='foo', endpoint='http://www.foo.com/bar', reader=lambda x: "foo is speaking to bar.", strict_mode=False, features='foo') for value in ['none_exist.wav', 'invalid.wav']: # act response = stt(value) # assert assert 'api_error_message' in response assert 'STT API raise exception.' in response assert stt.strict_mode == False