def test_get_ciscat_results_select(agents_info_mock, socket_mock, select): """Check that only selected elements are returned Parameters ---------- select : list Fields to be returned. """ valid_fields = { 'scan', 'benchmark', 'profile', 'pass', 'fail', 'error', 'notchecked', 'unknown', 'score' } with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb: mock_wdb.return_value = InitWDBSocketMock(sql_schema_file=db_file) result = get_ciscat_results(agent_list=['001'], select=select).render()['data'] # Check returned keys are the ones specified inside 'select' field for item in result['affected_items']: if select: for select_item in select: if '.' in select_item: key, subkey = select_item.split('.') assert subkey in item[key] else: assert select_item in item for key in item.keys(): assert key in valid_fields if key != 'agent_id' else True
def test_get_ciscat_results_select_ko(agents_info_mock, socket_mock): """Check that expected exception is raised when select field is not allowed.""" with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb: mock_wdb.return_value = InitWDBSocketMock(sql_schema_file=db_file) with pytest.raises(WazuhError, match=r'\b1724\b'): result = get_ciscat_results(agent_list=['001'], select=['random']).render()['data']
def test_get_ciscat_results(agents_info_mock, socket_mock, limit): """Check if limit is correctly applied to get_ciscat_results() function Parameters ---------- limit : int Number of items to be returned. """ with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb: mock_wdb.return_value = InitWDBSocketMock(sql_schema_file=db_file) result = get_ciscat_results(agent_list=['001'], limit=limit).render()['data'] limit = limit if limit else 2 assert len(result['affected_items'] ) == limit and result['total_affected_items'] == 2 assert len( result['failed_items']) == 0 and result['total_failed_items'] == 0
def test_get_ciscat_results_filters(agents_info_mock, socket_mock, filters, expected_scan_id): """Check that filters are correctly applied. Parameters ---------- filters : dict Filters to be applied and their values. expected_scan_id : list Expected IDs of the returned items. """ with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb: mock_wdb.return_value = InitWDBSocketMock(sql_schema_file=db_file) result = get_ciscat_results(agent_list=['001'], filters=filters).render()['data'] for item in result['affected_items']: assert item['scan']['id'] in expected_scan_id
def test_get_ciscat_results_sort(agents_info_mock, socket_mock, sort, first_item): """Check if the the first item returned is expected when using sort parameter Parameters ---------- sort : str Field and order to sort by first_item : int Expected string to be contained in the log of the first returned element. """ with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb: mock_wdb.return_value = InitWDBSocketMock(sql_schema_file=db_file) result = get_ciscat_results(agent_list=['001'], sort=parse_api_param( sort, 'sort')).render()['data'] assert result['affected_items'][0]['scan']['id'] == first_item
def test_get_ciscat_results_search(agents_info_mock, socket_mock, search, total_expected_items): """Check if the number of items returned is as expected when using the search parameter. Parameters ---------- search : str String to be searched in the database. total_expected_items : int Number of expected items to be returned. """ with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb: mock_wdb.return_value = InitWDBSocketMock(sql_schema_file=db_file) result = get_ciscat_results(agent_list=['001'], search=parse_api_param( search, 'search')).render()['data'] assert result['total_affected_items'] == total_expected_items
def test_get_ciscat_results_query(agents_info_mock, socket_mock, query, total_expected_items, expected_scan_id): """Check if the number of items returned is as expected when using query parameter. Parameters ---------- query : str Query to be applied in the database total_expected_items : int Number of expected items to be returned. expected_scan_id : list Expected IDs of the returned items. """ with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb: mock_wdb.return_value = InitWDBSocketMock(sql_schema_file=db_file) result = get_ciscat_results(agent_list=['001'], q=query).render()['data'] assert result['total_affected_items'] == total_expected_items for item in result['affected_items']: assert item['scan']['id'] in expected_scan_id
def test_get_ciscat_results(agents_info_mock, socket_mock, agent_id, exception): """Test function `get_ciscat_results` from ciscat module. Parameters ---------- agent_id : list List of agent IDs. exception : bool True if the code will go through an exception. False otherwise. """ with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb: mock_wdb.return_value = InitWDBSocketMock(sql_schema_file='schema_ciscat_test.sql') result = get_ciscat_results(agent_id) assert isinstance(result, AffectedItemsWazuhResult) if not exception: assert result.affected_items assert result.total_affected_items == 2 assert result.total_failed_items == 0 else: assert not result.affected_items assert result.total_failed_items == 1 assert result.total_affected_items == 0
def test_get_ciscat_results_ko(agents_info_mock, socket_mock): """Check that expected exception is raised when agent does not exist.""" with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb: mock_wdb.return_value = InitWDBSocketMock(sql_schema_file=db_file) result = get_ciscat_results(agent_list=['002']).render()['data'] assert result['total_failed_items'] == 1
def test_get_ciscat_result(mock_response): """ Tests get_packages method """ ciscat.get_ciscat_results()