From e9f528ce271e0d2a2e975e779970b174ed650a9a Mon Sep 17 00:00:00 2001 From: OlmeNav Date: Thu, 11 Sep 2025 12:50:42 +0200 Subject: [PATCH] fix: replace logger.error with ValidationError and update tests --- prowler/lib/check/models.py | 9 +++++---- tests/lib/check/models_test.py | 36 ++++++++++++---------------------- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/prowler/lib/check/models.py b/prowler/lib/check/models.py index 9e31cbe5bb..10a3b2166e 100644 --- a/prowler/lib/check/models.py +++ b/prowler/lib/check/models.py @@ -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""" diff --git a/tests/lib/check/models_test.py b/tests/lib/check/models_test.py index b31849800f..3c490e20c5 100644 --- a/tests/lib/check/models_test.py +++ b/tests/lib/check/models_test.py @@ -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