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 = '''

Commands

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.

  1. desc1: non-deprecated-cmd
  2. desc2: non-deprecated-cmd2

1. non-deprecated-cmd


desc1

Base Command

non-deprecated-cmd

Required Permissions

The following permissions are required for this command.

Input
There are no input arguments for this command.

 

Context Output
There are no context output for this command.

 

Command Example

Human Readable Output

2. non-deprecated-cmd2


desc2.

Base Command

non-deprecated-cmd2

Required Permissions

The following permissions are required for this command.

Input
There are no input arguments for this command.

 

Context Output
There are no context output for this command.

 

Command Example

Human Readable Output

''' 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
! ! !
''' assert to_html_table(['header1', 'header2', 'header3'], data) == expected def test_human_readable_example_to_html(): from HTMLDocsAutomation import human_readable_example_to_html data = [ { 'header1': 'hello', 'header2': 'hello', }, { 'header1': 'world', 'header2': 'world', }, ] md = tableToMarkdown('Title', data, headers=['header1', 'header2']) expected = '''

Title

header1 header2
hello hello
world world
''' assert human_readable_example_to_html(md) == expected md = md + '\n# Headline\nsome text\nanother line of text\n' + md expected = expected + '\n

Headline

\n

\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))