From 8b88938eaa97e93dd31905be18e45b7e98704349 Mon Sep 17 00:00:00 2001 From: "Andoni A." <14891798+andoniaf@users.noreply.github.com> Date: Mon, 23 Jun 2025 14:47:00 +0200 Subject: [PATCH] chore(iac): update tests --- tests/lib/outputs/finding_test.py | 49 +++++++++++++++++++ tests/providers/iac/iac_provider_test.py | 60 ++++++++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/tests/lib/outputs/finding_test.py b/tests/lib/outputs/finding_test.py index cdd0be701a..cb5d365e95 100644 --- a/tests/lib/outputs/finding_test.py +++ b/tests/lib/outputs/finding_test.py @@ -506,6 +506,55 @@ class TestFinding: assert finding_output.metadata.Notes == "mock_notes" assert finding_output.metadata.Compliance == [] + def test_generate_output_iac_remote(self): + # Mock provider + provider = MagicMock() + provider.type = "iac" + provider.scan_repository_url = "https://github.com/user/repo" + + # Mock check result + check_output = MagicMock() + check_output.file_path = "/path/to/iac/file.tf" + check_output.resource_path = "/path/to/iac/file.tf" + check_output.file_line_range = [1, 5] + check_output.resource = { + "resource": "aws_s3_bucket.example", + "value": {}, + } + check_output.resource_details = "test_resource_details" + check_output.status = Status.PASS + check_output.status_extended = "mock_status_extended" + check_output.muted = False + check_output.check_metadata = mock_check_metadata(provider="iac") + check_output.compliance = {} + + # Mock output options + output_options = MagicMock() + output_options.unix_timestamp = False + + # Generate the finding + finding_output = Finding.generate_output(provider, check_output, output_options) + + # Finding + assert isinstance(finding_output, Finding) + assert finding_output.auth_method == "None" + assert finding_output.resource_name == "aws_s3_bucket.example" + assert finding_output.resource_uid == "aws_s3_bucket.example" + assert finding_output.region == "/path/to/iac/file.tf" + assert finding_output.status == Status.PASS + assert finding_output.status_extended == "mock_status_extended" + assert finding_output.muted is False + + # Metadata + assert finding_output.metadata.Provider == "iac" + assert finding_output.metadata.CheckID == "mock_check_id" + assert finding_output.metadata.CheckTitle == "mock_check_title" + assert finding_output.metadata.CheckType == [] + assert finding_output.metadata.CheckAliases == [] + assert finding_output.metadata.ServiceName == "mock_service_name" + assert finding_output.metadata.SubServiceName == "" + assert finding_output.metadata.ResourceIdTemplate == "" + def assert_keys_lowercase(self, d): for k, v in d.items(): assert k.islower() diff --git a/tests/providers/iac/iac_provider_test.py b/tests/providers/iac/iac_provider_test.py index e2f3469569..5ebe9d333e 100644 --- a/tests/providers/iac/iac_provider_test.py +++ b/tests/providers/iac/iac_provider_test.py @@ -1,3 +1,5 @@ +import tempfile +from unittest import mock from unittest.mock import MagicMock, patch import pytest @@ -130,3 +132,61 @@ class TestIacProvider: reports = provider.run_scan("/test/directory") assert len(reports) == 0 + + def test_provider_run_local_scan(self): + scan_path = "." + provider = IacProvider(scan_path=scan_path) + with mock.patch( + "prowler.providers.iac.iac_provider.IacProvider.run_scan", + ) as mock_run_scan: + provider.run() + mock_run_scan.assert_called_with(scan_path) + + def test_provider_run_remote_scan(self): + scan_repository_url = "https://github.com/user/repo" + provider = IacProvider(scan_repository_url=scan_repository_url) + + with tempfile.TemporaryDirectory() as temp_dir: + with ( + mock.patch( + "prowler.providers.iac.iac_provider.tempfile.mkdtemp", + return_value=temp_dir, + ), + mock.patch( + "prowler.providers.iac.iac_provider.IacProvider._clone_repository" + ) as mock_clone, + mock.patch( + "prowler.providers.iac.iac_provider.IacProvider.run_scan" + ) as mock_run_scan, + ): + provider.run() + mock_clone.assert_called_with(scan_repository_url, temp_dir) + mock_run_scan.assert_called_with(temp_dir) + + def test_print_credentials_local(self): + scan_path = "/path/to/scan" + provider = IacProvider(scan_path=scan_path) + with mock.patch("builtins.print") as mock_print: + provider.print_credentials() + assert any( + f"Directory: \x1b[33m{scan_path}\x1b[0m" in call.args[0] + for call in mock_print.call_args_list + ) + assert any( + "Scanning local IaC directory:" in call.args[0] + for call in mock_print.call_args_list + ) + + def test_print_credentials_remote(self): + repo_url = "https://github.com/user/repo" + provider = IacProvider(scan_repository_url=repo_url) + with mock.patch("builtins.print") as mock_print: + provider.print_credentials() + assert any( + f"Repository: \x1b[33m{repo_url}\x1b[0m" in call.args[0] + for call in mock_print.call_args_list + ) + assert any( + "Scanning remote IaC repository:" in call.args[0] + for call in mock_print.call_args_list + )