fix: replace logger.error with ValidationError and update tests

This commit is contained in:
OlmeNav
2025-09-11 12:50:42 +02:00
parent 8286531a9a
commit e9f528ce27
2 changed files with 18 additions and 27 deletions
+5 -4
View File
@@ -8,6 +8,7 @@ from enum import Enum
from typing import Any, Dict, Optional, Set
from pydantic.v1 import BaseModel, Field, ValidationError, validator
from pydantic.v1.error_wrappers import ErrorWrapper
from prowler.config.config import Provider
from prowler.lib.check.compliance_models import Compliance
@@ -457,10 +458,10 @@ class Check(ABC, CheckMetadata):
errors.append(f"CheckID '{check_id}' != file name '{file_name}'")
if errors:
logger.error(
"Name consistency error. The CheckID in the metadata, the class name "
"and the file name must all match. -- " + " | ".join(errors)
)
formatted_errors = [
ErrorWrapper(ValueError(err), loc=("CheckID",)) for err in errors
]
raise ValidationError(formatted_errors, model=CheckMetadata)
def metadata(self) -> dict:
"""Return the JSON representation of the check's metadata"""
+13 -23
View File
@@ -720,9 +720,8 @@ class TestCheckMetada:
class TestCheck:
@mock.patch("prowler.lib.check.models.logger")
@mock.patch("prowler.lib.check.models.CheckMetadata.parse_file")
def test_verify_names_consistency_all_match(self, mock_parse_file, mock_logger):
def test_verify_names_consistency_all_match(self, mock_parse_file):
"""Case where everything matches: CheckID == class_name == file_name"""
mock_parse_file.return_value = mock_metadata.copy(
update={
@@ -741,13 +740,8 @@ class TestCheck:
accessanalyzer_enabled()
mock_logger.error.assert_not_called()
@mock.patch("prowler.lib.check.models.logger")
@mock.patch("prowler.lib.check.models.CheckMetadata.parse_file")
def test_verify_names_consistency_class_mismatch(
self, mock_parse_file, mock_logger
):
def test_verify_names_consistency_class_mismatch(self, mock_parse_file):
"""CheckID != class name, but matches file_name"""
mock_parse_file.return_value = mock_metadata.copy(
update={
@@ -764,15 +758,13 @@ class TestCheck:
fake_module.__file__ = "/path/to/accessanalyzer_enabled.py"
sys.modules[WrongClass.__module__] = fake_module
WrongClass()
with pytest.raises(ValidationError) as excinfo:
WrongClass()
mock_logger.error.assert_called()
msg = mock_logger.error.call_args[0][0]
assert "!= class name" in msg
assert "!= class name" in str(excinfo.value)
@mock.patch("prowler.lib.check.models.logger")
@mock.patch("prowler.lib.check.models.CheckMetadata.parse_file")
def test_verify_names_consistency_file_mismatch(self, mock_parse_file, mock_logger):
def test_verify_names_consistency_file_mismatch(self, mock_parse_file):
"""CheckID == class name, but != file_name"""
mock_parse_file.return_value = mock_metadata.copy(
update={
@@ -789,15 +781,13 @@ class TestCheck:
fake_module.__file__ = "/path/to/OtherFile.py"
sys.modules[accessanalyzer_enabled.__module__] = fake_module
accessanalyzer_enabled()
with pytest.raises(ValidationError) as excinfo:
accessanalyzer_enabled()
mock_logger.error.assert_called()
msg = mock_logger.error.call_args[0][0]
assert "!= file name" in msg
assert "!= file name" in str(excinfo.value)
@mock.patch("prowler.lib.check.models.logger")
@mock.patch("prowler.lib.check.models.CheckMetadata.parse_file")
def test_verify_names_consistency_both_mismatch(self, mock_parse_file, mock_logger):
def test_verify_names_consistency_both_mismatch(self, mock_parse_file):
"""Neither class name nor file name match the CheckID"""
mock_parse_file.return_value = mock_metadata.copy(
update={
@@ -814,9 +804,9 @@ class TestCheck:
fake_module.__file__ = "/path/to/OtherFile.py"
sys.modules[WrongClass.__module__] = fake_module
WrongClass()
with pytest.raises(ValidationError) as excinfo:
WrongClass()
mock_logger.error.assert_called()
msg = mock_logger.error.call_args[0][0]
msg = str(excinfo.value)
assert "!= class name" in msg
assert "!= file name" in msg