def test_large_output_list_command(client, requests_mock, command, command_api_url, mock_response_file, override_nodes, expected_results): """Unit test Given - a command that returns a list - file path of mocked response When - running the command Then - validate the entry context """ mock_topdesk_node = util_load_json(mock_response_file) mock_topdesk_response = [] for node_override in override_nodes: response_node = mock_topdesk_node.copy() response_node['id'] = node_override['id'] mock_topdesk_response.append(response_node) requests_mock.get( command_api_url, json=mock_topdesk_response) command_results = command(client, {}) assert command_results.outputs_prefix == expected_results['outputs_prefix'] assert command_results.outputs_key_field == expected_results['outputs_key_field'] assert command_results.outputs == capitalize_for_outputs(mock_topdesk_response)
def test_capitalize_outputs(outputs, expected_capitalized_output): """Unit test Given - output of API command When - returning output to XSOAR Then - validate the output is capitalized. """ assert capitalize_for_outputs(outputs) == expected_capitalized_output
def test_list_command(client, requests_mock, command, command_api_url, mock_response, expected_results): """Unit test Given - A command that returns a list When - running the command Then - validate the entry context """ requests_mock.get( command_api_url, json=mock_response) command_results = command(client, {}) assert command_results.outputs_prefix == expected_results['outputs_prefix'] assert command_results.outputs_key_field == expected_results['outputs_key_field'] assert command_results.outputs == capitalize_for_outputs(mock_response)
def test_non_registered_caller_incident_touch_commands(client, requests_mock, create_func, command_args, command_api_url, mock_response_file, expected_last_request_body): """Unit test Given - whether the command is Create or Update - command args When - running the command with a caller as a non registered caller. Then - validate 2 requests were called. - validate the entry context. """ client_func = client.update_incident request_method = "put" action = "updating" if create_func: client_func = client.create_incident request_method = "post" action = "creating" mock_topdesk_node = util_load_json(mock_response_file) response_incident = mock_topdesk_node.copy() request_command = getattr(requests_mock, request_method) def callback_func(request, _): if 'callerLookup' in request.json(): return {"message": "The value for the field 'callerLookup.id' cannot be parsed."} else: return response_incident request_command(command_api_url, json=callback_func) command_results = incident_touch_command(client=client, args=command_args, client_func=client_func, action=action) assert requests_mock.call_count == 3 assert requests_mock.last_request.json() == expected_last_request_body assert command_results.outputs_prefix == f'{INTEGRATION_NAME}.Incident' assert command_results.outputs_key_field == 'Id' assert command_results.outputs == capitalize_for_outputs([response_incident])
def test_caller_lookup_incident_touch_commands(client, requests_mock, create_func, command_args, command_api_url, mock_response_file, expected_last_request_body): """Unit test Given - whether the command is Create or Update - command args When - running the command with a caller as a registered caller. Then - validate 1 request was called. - validate the correct request was called. - validate the entry context. """ client_func = client.update_incident request_method = "put" action = "updating" if create_func: client_func = client.create_incident request_method = "post" action = "creating" mock_topdesk_node = util_load_json(mock_response_file) response_incident = mock_topdesk_node.copy() request_command = getattr(requests_mock, request_method) request_command(command_api_url, json=response_incident) command_results = incident_touch_command(client=client, args=command_args, client_func=client_func, action=action) assert requests_mock.call_count == 2 assert requests_mock.last_request.json() == expected_last_request_body assert command_results.outputs_prefix == f'{INTEGRATION_NAME}.Incident' assert command_results.outputs_key_field == 'Id' assert command_results.outputs == capitalize_for_outputs([response_incident])
def test_attachment_list_command(client, requests_mock, command_args, command_api_url, response_override): """Unit test Given - command args: id, file, description, invisible_for_caller When - running attachment_upload_command with the command args Then - validate the correct request was called. - validate the file is in the request. - validate the entry context. """ mock_topdesk_node = util_load_json('test_data/topdesk_attachment.json') response = [] expected = [] for attachment_override in response_override: response_attachment = mock_topdesk_node.copy() response_attachment["id"] = attachment_override["id"] response_attachment["downloadUrl"] = attachment_override["downloadUrl"] response.append(response_attachment) if attachment_override["expected"]: expected_attachment = mock_topdesk_node.copy() expected_attachment["id"] = attachment_override["id"] expected_attachment["downloadUrl"] = f'https://test.com{attachment_override["downloadUrl"]}' expected.append(expected_attachment) requests_mock.get(command_api_url, json=response) command_results = list_attachments_command(client=client, args=command_args) assert command_results.outputs_prefix == f'{INTEGRATION_NAME}.Attachment' assert command_results.outputs_key_field == 'Id' assert command_results.outputs == capitalize_for_outputs(expected)
def test_incident_do_commands(client, requests_mock, action, command_args, command_api_url, mock_response_file, override_node): """Unit test Given - action: archive, unarchive, escalate, deescalate - command args: id, number, reason_id When - running incident_do_command with the action and args Then - validate the correct request was called. - validate the entry context. """ mock_topdesk_node = util_load_json(mock_response_file) response_incident = mock_topdesk_node.copy() if override_node.get('id', None): response_incident['id'] = override_node['id'] elif override_node.get('number', None): response_incident['number'] = override_node['number'] requests_mock.put( command_api_url, json=response_incident) command_results = incident_do_command(client=client, args=command_args, action=action) assert requests_mock.called if command_args.get(f"{action}_reason_id", None): assert requests_mock.last_request.json() == {'id': command_args.get(f"{action}_reason_id", None)} else: assert requests_mock.last_request.json() == {} assert command_results.outputs_prefix == f'{INTEGRATION_NAME}.Incident' assert command_results.outputs_key_field == 'Id' assert command_results.outputs == capitalize_for_outputs([response_incident])
def test_attachment_upload_command(client, mocker, requests_mock, command_args, command_api_url, command_api_body): """Unit test Given - command args: id, file, description, invisible_for_caller When - running attachment_upload_command with the command args Then - validate the correct request was called. - validate the file is in the request. - validate the entry context. """ mock_topdesk_node = util_load_json('test_data/topdesk_attachment.json') response_attachment = mock_topdesk_node.copy() requests_mock.post( command_api_url, json=response_attachment) mocker.patch.object(demisto, 'dt', return_value="made_up_file.txt") mocker.patch.object(demisto, 'getFilePath', return_value={'path': 'test_data/mock_upload_file.txt'}) command_results = attachment_upload_command(client=client, args=command_args) output_attachment = response_attachment output_attachment['downloadUrl'] = 'https://test.com/api/incidents/id/incident_id/attachments/some-id/download' assert requests_mock.called assert b'mock text file for attachment up' in requests_mock.last_request._request.body assert command_results.outputs_prefix == f'{INTEGRATION_NAME}.Attachment' assert command_results.outputs_key_field == 'Id' assert command_results.outputs == capitalize_for_outputs([output_attachment])