From 15d7e107eea8d89f4f60e6ee8da82d4ab8dd4b2a Mon Sep 17 00:00:00 2001 From: HugoPBrito Date: Mon, 16 Dec 2024 13:18:23 +0000 Subject: [PATCH] feat: added testing --- ...g_requires_conversation_resolution_test.py | 119 ++++++++++++++++++ .../repository/repository_service_test.py | 4 + 2 files changed, 123 insertions(+) create mode 100644 tests/providers/github/services/repository/repository_merging_requires_conversation_resolution/repository_merging_requires_conversation_resolution_test.py diff --git a/tests/providers/github/services/repository/repository_merging_requires_conversation_resolution/repository_merging_requires_conversation_resolution_test.py b/tests/providers/github/services/repository/repository_merging_requires_conversation_resolution/repository_merging_requires_conversation_resolution_test.py new file mode 100644 index 0000000000..79415b99f5 --- /dev/null +++ b/tests/providers/github/services/repository/repository_merging_requires_conversation_resolution/repository_merging_requires_conversation_resolution_test.py @@ -0,0 +1,119 @@ +from unittest import mock + +from prowler.providers.github.services.repository.repository_service import ( + Protection, + Repo, +) +from tests.providers.github.github_fixtures import set_mocked_github_provider + + +class Test_repository_merging_requires_conversation_resolution_test: + def test_no_repositories(self): + repository_client = mock.MagicMock + repository_client.repositories = {} + + with ( + mock.patch( + "prowler.providers.common.provider.Provider.get_global_provider", + return_value=set_mocked_github_provider(), + ), + mock.patch( + "prowler.providers.github.services.repository.repository_merging_requires_conversation_resolution.repository_merging_requires_conversation_resolution.repository_client", + new=repository_client, + ), + ): + from prowler.providers.github.services.repository.repository_merging_requires_conversation_resolution.repository_merging_requires_conversation_resolution import ( + repository_merging_requires_conversation_resolution, + ) + + check = repository_merging_requires_conversation_resolution() + result = check.execute() + assert len(result) == 0 + + def test_conversation_resolution_disabled(self): + repository_client = mock.MagicMock + repo_name = "repo1" + default_branch = "main" + repository_client.repositories = { + 1: Repo( + id=1, + name=repo_name, + full_name="account-name/repo1", + default_branch=default_branch, + default_branch_protection=Protection( + require_pull_request=True, approval_count=2, linear_history=False + ), + private=False, + securitymd=False, + ), + } + + with ( + mock.patch( + "prowler.providers.common.provider.Provider.get_global_provider", + return_value=set_mocked_github_provider(), + ), + mock.patch( + "prowler.providers.github.services.repository.repository_merging_requires_conversation_resolution.repository_merging_requires_conversation_resolution.repository_client", + new=repository_client, + ), + ): + from prowler.providers.github.services.repository.repository_merging_requires_conversation_resolution.repository_merging_requires_conversation_resolution import ( + repository_merging_requires_conversation_resolution, + ) + + check = repository_merging_requires_conversation_resolution() + result = check.execute() + assert len(result) == 1 + assert result[0].resource_id == 1 + assert result[0].resource_name == "repo1" + assert result[0].status == "FAIL" + assert ( + result[0].status_extended + == f"Repository {repo_name} does not require conversation resolution." + ) + + def test_conversation_resolution_enabled(self): + repository_client = mock.MagicMock + repo_name = "repo1" + default_branch = "main" + repository_client.repositories = { + 1: Repo( + id=1, + name=repo_name, + full_name="account-name/repo1", + private=False, + default_branch=default_branch, + default_branch_protection=Protection( + require_pull_request=True, + approval_count=2, + conversation_resolution=True, + ), + securitymd=True, + ), + } + + with ( + mock.patch( + "prowler.providers.common.provider.Provider.get_global_provider", + return_value=set_mocked_github_provider(), + ), + mock.patch( + "prowler.providers.github.services.repository.repository_merging_requires_conversation_resolution.repository_merging_requires_conversation_resolution.repository_client", + new=repository_client, + ), + ): + from prowler.providers.github.services.repository.repository_merging_requires_conversation_resolution.repository_merging_requires_conversation_resolution import ( + repository_merging_requires_conversation_resolution, + ) + + check = repository_merging_requires_conversation_resolution() + result = check.execute() + assert len(result) == 1 + assert result[0].resource_id == 1 + assert result[0].resource_name == "repo1" + assert result[0].status == "PASS" + assert ( + result[0].status_extended + == f"Repository {repo_name} does require conversation resolution." + ) diff --git a/tests/providers/github/services/repository/repository_service_test.py b/tests/providers/github/services/repository/repository_service_test.py index 48b55edf48..18a7735bdc 100644 --- a/tests/providers/github/services/repository/repository_service_test.py +++ b/tests/providers/github/services/repository/repository_service_test.py @@ -24,6 +24,7 @@ def mock_list_repositories(_): allow_branch_deletion=False, enforce_status_checks=True, enforce_admins=True, + conversation_resolution=True, ), securitymd=True, ), @@ -73,5 +74,8 @@ class Test_Repository_Service: assert repository_service.repositories[ 1 ].default_branch_protection.enforce_admins + assert repository_service.repositories[ + 1 + ].default_branch_protection.conversation_resolution # Repo assert repository_service.repositories[1].securitymd