from CommonServerPython import * import os import demistomock as demisto RETURN_ERROR_TARGET = 'HTMLDocsAutomation.return_error' def test_get_yaml_obj(mocker): from HTMLDocsAutomation import get_yaml_obj return_error_mock = mocker.patch(RETURN_ERROR_TARGET) # sanity file_path = os.path.join('test_data', 'ANYRUN_yml.txt') mocker.patch.object(demisto, 'getFilePath', return_value={'path': file_path}) data = get_yaml_obj('12345') # error count should not change assert return_error_mock.call_count == 0 # call_args last call with a tuple of args list and kwargs assert data['commonfields']['id'] == 'ANYRUN' # invalid yml mocker.patch.object(demisto, 'getFilePath', return_value={'path': os.path.join('test_data', 'not_yml_file.txt')}) get_yaml_obj('234') assert return_error_mock.call_count == 1 # call_args last call with a tuple of args list and kwargs err_msg = return_error_mock.call_args[0][0] assert err_msg == 'Failed to open integration file: not a yml file' # no such file mocker.patch.object(demisto, 'getFilePath', side_effect=ValueError('no such file')) get_yaml_obj('234') assert return_error_mock.call_count == 2 # call_args last call with a tuple of args list and kwargs err_msg = return_error_mock.call_args[0][0] assert err_msg == 'Failed to open integration file: no such file' def test_extract_command(): from HTMLDocsAutomation import extract_command # no args cmd, args = extract_command('!no-args-command') assert cmd == '!no-args-command' assert args == {} # sanity cmd, args = extract_command('!command ip=8.8.8.8') expected = {'ip': '8.8.8.8'} assert cmd == '!command' assert len(expected) == len(args) for k, v in expected.items(): assert args[k] == v # edge cases cmd, args = extract_command('!command SomeParam=8.8.8.8 dash-arg="args" special_chars="1qazxsw2 EW3- *3d" ' 'backTick=`hello "hello" \'hello\'` triple_quotes="""this is a multi quotes"""') expected = { 'SomeParam': '8.8.8.8', 'dash-arg': 'args', 'special_chars': '1qazxsw2 EW3- *3d', 'backTick': 'hello "hello" \'hello\'', 'triple_quotes': 'this is a multi quotes' } assert cmd == '!command' assert len(expected) == len(args) for k, v in expected.items(): assert args[k] == v cmd, args = extract_command('!command SomeParam="""hello\nthis is multiline"""') expected = { 'SomeParam': 'hello\nthis is multiline', } assert cmd == '!command' assert len(expected) == len(args) for k, v in expected.items(): assert args[k] == v def test_generate_commands_section(): from HTMLDocsAutomation import generate_commands_section yml_data = { 'script': { 'commands': [ {'deprecated': True, 'name': 'deprecated-cmd', 'description': 'desc'}, {'deprecated': False, 'name': 'non-deprecated-cmd', 'description': 'desc1'}, {'name': 'non-deprecated-cmd2', 'description': 'desc2.'} ] } } section, errors = generate_commands_section(yml_data, {}, True) expected_section = '''
You can execute these commands from the Demisto CLI, as part of an automation, or in a playbook. After you successfully execute a command, a DBot message appears in the War Room with the command details.
desc1
non-deprecated-cmd
The following permissions are required for this command.
desc2.
non-deprecated-cmd2
The following permissions are required for this command.
''' assert section == expected_section assert len(errors) == 2 # no example for both commands def test_to_html_table(): from HTMLDocsAutomation import to_html_table data = [ ['hello', 'hello', 'hello'], ['world', 'world', 'world'], ['!', '!', '!'], ] expected = '''
| header1 | header2 | header3 |
|---|---|---|
| hello | hello | hello |
| world | world | world |
| ! | ! | ! |
| header1 | header2 |
|---|---|
| hello | hello |
| world | world |
\nsome text\nanother line of text\n
\n' + expected assert human_readable_example_to_html(md) == expected # md = '''Key | Value # - | - # city | Mountain View # country | US # hostname | dns.google # ip | 8.8.8.8 # loc | 37.3860,-122.0838 # org | AS15169 Google LLC # postal | 94035 # readme | https://ipinfo.io/missingauth # region | California # {"lat": 37.386, "lng": -122.0838}''' # # print(human_readable_example_to_html(md))