Пример #1
0
def test_get_sca_checks(mock_agent, mock_sca_agent):
    """
    Checks sca checks data are properly loaded from database
    """
    with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb:
        mock_wdb.return_value = InitWDBSocketMock(
            sql_schema_file='schema_sca_test.sql')
        result = get_sca_checks('cis_debian', agent_list=['000'])
        assert isinstance(result, AffectedItemsWazuhResult)
        result = result.to_dict()
        assert isinstance(result['total_affected_items'], int)
        sca = result['affected_items']
        assert isinstance(sca, list)
        assert len(sca) > 0
        assert set(sca[0].keys()).issubset(
            set(fields_translation_sca_check.keys()) | {'compliance', 'rules'})

        compliance = sca[0]['compliance']
        assert isinstance(compliance, list)
        assert len(compliance) > 0
        assert set(compliance[0].keys()) == set(
            fields_translation_sca_check_compliance.values())

        # Check 0 result
        result = get_sca_checks('not_exists', agent_list=['000'])
        assert isinstance(result, AffectedItemsWazuhResult)
        result = result.to_dict()
        assert isinstance(result['total_affected_items'], int)
        sca = result['affected_items']
        assert isinstance(sca, list)
        assert len(sca) == 0

        result = get_sca_checks('cis_debian', agent_list=['999'])
        assert isinstance(result, AffectedItemsWazuhResult)
        result = result.to_dict()
        assert isinstance(result['total_affected_items'], int)
        assert result['total_affected_items'] == 0
        assert len(result['affected_items']) == 0
        assert result['total_failed_items'] == 1
        assert len(result['failed_items']) == 1
        failed = result['failed_items']
        assert isinstance(list(failed.keys())[0], WazuhResourceNotFound)
        assert list(failed.keys())[0].to_dict()['code'] == 1701
        assert failed[list(failed.keys())[0]] == {'999'}
Пример #2
0
    finally:
        sca_db.close()


# Aliases and ` are lost when sqlite db answers...
cols_returned_from_db_sca = [
    field.replace('`', '').replace('si.', '')
    for field in fields_translation_sca.keys()
]
cols_returned_from_db_sca = [
    field.split(' as ')[1] if ' as ' in field else field
    for field in cols_returned_from_db_sca
]
cols_returned_from_db_sca_check = [
    field.replace('`', '').replace('sca.', '')
    for field in fields_translation_sca_check.keys()
]


@patch("wazuh.core.sca.Agent.get_basic_information")
@patch("wazuh.sca.get_agents_info", return_value={'000'})
def test_get_sca_list(mock_agent, mock_sca_agent):
    """
    Checks data are properly loaded from database
    """
    with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb:
        mock_wdb.return_value = InitWDBSocketMock(
            sql_schema_file='schema_sca_test.sql')
        result = get_sca_list(agent_list=['000'])
        assert isinstance(result, AffectedItemsWazuhResult)
        result = result.to_dict()