From 350739ae7192d4d0bea6b8632487b938b0745872 Mon Sep 17 00:00:00 2001 From: angelolmn Date: Thu, 11 Sep 2025 11:59:50 +0200 Subject: [PATCH] test: add test for CheckID, filename and classname verification. --- tests/lib/check/models_test.py | 106 ++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) diff --git a/tests/lib/check/models_test.py b/tests/lib/check/models_test.py index a992da946c..b31849800f 100644 --- a/tests/lib/check/models_test.py +++ b/tests/lib/check/models_test.py @@ -1,9 +1,10 @@ +import sys from unittest import mock import pytest from pydantic.v1 import ValidationError -from prowler.lib.check.models import CheckMetadata +from prowler.lib.check.models import Check, CheckMetadata from tests.lib.check.compliance_check_test import custom_compliance_metadata mock_metadata = CheckMetadata( @@ -716,3 +717,106 @@ class TestCheckMetada: ) # Should contain the validation error we set in the validator assert "AdditionalURLs must be a list" in str(exc_info.value) + + +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): + """Case where everything matches: CheckID == class_name == file_name""" + mock_parse_file.return_value = mock_metadata.copy( + update={ + "CheckID": "accessanalyzer_enabled", + "ServiceName": "accessanalyzer", + } + ) + + class accessanalyzer_enabled(Check): + def execute(self): + pass + + fake_module = mock.Mock() + fake_module.__file__ = "/path/to/accessanalyzer_enabled.py" + sys.modules[accessanalyzer_enabled.__module__] = fake_module + + 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 + ): + """CheckID != class name, but matches file_name""" + mock_parse_file.return_value = mock_metadata.copy( + update={ + "CheckID": "accessanalyzer_enabled", + "ServiceName": "accessanalyzer", + } + ) + + class WrongClass(Check): + def execute(self): + pass + + fake_module = mock.Mock() + fake_module.__file__ = "/path/to/accessanalyzer_enabled.py" + sys.modules[WrongClass.__module__] = fake_module + + WrongClass() + + mock_logger.error.assert_called() + msg = mock_logger.error.call_args[0][0] + assert "!= class name" in msg + + @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): + """CheckID == class name, but != file_name""" + mock_parse_file.return_value = mock_metadata.copy( + update={ + "CheckID": "accessanalyzer_enabled", + "ServiceName": "accessanalyzer", + } + ) + + class accessanalyzer_enabled(Check): + def execute(self): + pass + + fake_module = mock.Mock() + fake_module.__file__ = "/path/to/OtherFile.py" + sys.modules[accessanalyzer_enabled.__module__] = fake_module + + accessanalyzer_enabled() + + mock_logger.error.assert_called() + msg = mock_logger.error.call_args[0][0] + assert "!= file name" in msg + + @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): + """Neither class name nor file name match the CheckID""" + mock_parse_file.return_value = mock_metadata.copy( + update={ + "CheckID": "accessanalyzer_enabled", + "ServiceName": "accessanalyzer", + } + ) + + class WrongClass(Check): + def execute(self): + pass + + fake_module = mock.Mock() + fake_module.__file__ = "/path/to/OtherFile.py" + sys.modules[WrongClass.__module__] = fake_module + + WrongClass() + + mock_logger.error.assert_called() + msg = mock_logger.error.call_args[0][0] + assert "!= class name" in msg + assert "!= file name" in msg