def test_send_files(self): files = [ File('file1.txt', 'some text'), File('my_data.dat', b'abcdef12345'), File('movie.vid', path='/home/me/movies/movie.vid'), ] self.send('My title', 'my_body', files) self.assertEqual([('My title', 'my_body', files)], self.get_sent_messages())
def test_send_multiple_files(self): destination = HttpDestination({}) body = {'p1': 5} destination.send('ignored', body, files=[ File('log.txt', 'log content'), File('admin.data', 'something happened'), File('.hidden', '123-345-abc')]) expected_body = body.copy() expected_body['files'] = {'log.txt': 'log content', 'admin.data': 'something happened', '.hidden': '123-345-abc'} self.assert_sent_json(expected_body)
def test_send_single_file(self): destination = HttpDestination({}) body = {'p1': 5} destination.send('ignored', body, files=[File('log.txt', 'log content')]) expected_body = body.copy() expected_body['files'] = {'log.txt': 'log content'} self.assert_sent_json(expected_body)
def test_send_email_alert(self): config = self.create_config(['email']) alerts_service = AlertsService(config) title = 'My test alert' body = 'Test message body' files = [File(filename='log.txt', content='doing X')] self.send_alert(alerts_service, body, files, title) self.assertEqual([(title, body, files)], self.get_communicators()[0].messages)
def test_send_http_alert(self): config = self.create_config(['http']) alerts_service = AlertsService(config) title = 'My test alert' body = 'Test message body' files = [File(filename='log.txt', content='doing X')] self.send_alert(alerts_service, body, files, title) expected_body = json.dumps(OrderedDict([ ('title', title), ('message', body), ('files', { 'log.txt': 'doing X' })])) self.assertEqual([(None, expected_body, None)], self.get_communicators()[0].messages)
def finished(execution_id, user: User): return_code = execution_service.get_exit_code(execution_id) if return_code != 0: script_config = execution_service.get_config(execution_id, user) script = str(script_config.name) audit_name = user.get_audit_name() output_stream = execution_service.get_anonymized_output_stream(execution_id) title = script + ' FAILED' body = 'The script "' + script + '", started by ' + audit_name + \ ' exited with return code ' + str(return_code) + '.' + \ ' Usually this means an error, occurred during the execution.' + \ ' Please check the corresponding logs' output_stream_data = read_until_closed(output_stream) script_output = ''.join(output_stream_data) files = [File(filename='log.txt', content=script_output)] alert_service.send_alert(title, body, files)
def test_send_file_with_string_body(self): destination = HttpDestination({}) self.assertRaisesRegex(Exception, 'Files are supported only for JSON body, was \'test_body\'', destination.send, 'ignored', 'test_body', files=[File('log.txt', 'log content')])
def test_send_when_files(self): self.assertRaisesRegex(Exception, 'Files are not supported for scripts', self.do_send, ['param1'], [File('test_file')])