From 6575680f01245f62de1a7eaa3d3359bf8f4fac7b Mon Sep 17 00:00:00 2001 From: HugoPBrito Date: Mon, 16 Dec 2024 12:10:04 +0000 Subject: [PATCH] feat: added testing --- ...y_enforces_admin_branch_protection_test.py | 116 ++++++++++++++++++ .../repository/repository_service_test.py | 4 + 2 files changed, 120 insertions(+) create mode 100644 tests/providers/github/services/repository/repository_enforces_admin_branch_protection/repository_enforces_admin_branch_protection_test.py diff --git a/tests/providers/github/services/repository/repository_enforces_admin_branch_protection/repository_enforces_admin_branch_protection_test.py b/tests/providers/github/services/repository/repository_enforces_admin_branch_protection/repository_enforces_admin_branch_protection_test.py new file mode 100644 index 0000000000..346f8ccd7e --- /dev/null +++ b/tests/providers/github/services/repository/repository_enforces_admin_branch_protection/repository_enforces_admin_branch_protection_test.py @@ -0,0 +1,116 @@ +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_enforces_admin_branch_protection_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_enforces_admin_branch_protection.repository_enforces_admin_branch_protection.repository_client", + new=repository_client, + ), + ): + from prowler.providers.github.services.repository.repository_enforces_admin_branch_protection.repository_enforces_admin_branch_protection import ( + repository_enforces_admin_branch_protection, + ) + + check = repository_enforces_admin_branch_protection() + result = check.execute() + assert len(result) == 0 + + def test_enforce_status_checks_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, + 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_enforces_admin_branch_protection.repository_enforces_admin_branch_protection.repository_client", + new=repository_client, + ), + ): + from prowler.providers.github.services.repository.repository_enforces_admin_branch_protection.repository_enforces_admin_branch_protection import ( + repository_enforces_admin_branch_protection, + ) + + check = repository_enforces_admin_branch_protection() + 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 enforce administrators to be subject to the same branch protection rules as other users." + ) + + def test_enforce_status_checks_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, + enforce_admins=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_enforces_admin_branch_protection.repository_enforces_admin_branch_protection.repository_client", + new=repository_client, + ), + ): + from prowler.providers.github.services.repository.repository_enforces_admin_branch_protection.repository_enforces_admin_branch_protection import ( + repository_enforces_admin_branch_protection, + ) + + check = repository_enforces_admin_branch_protection() + 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 enforce administrators to be subject to the same branch protection rules as other users." + ) diff --git a/tests/providers/github/services/repository/repository_service_test.py b/tests/providers/github/services/repository/repository_service_test.py index bf5c22e12e..48b55edf48 100644 --- a/tests/providers/github/services/repository/repository_service_test.py +++ b/tests/providers/github/services/repository/repository_service_test.py @@ -23,6 +23,7 @@ def mock_list_repositories(_): allow_force_push=False, allow_branch_deletion=False, enforce_status_checks=True, + enforce_admins=True, ), securitymd=True, ), @@ -69,5 +70,8 @@ class Test_Repository_Service: assert repository_service.repositories[ 1 ].default_branch_protection.enforce_status_checks + assert repository_service.repositories[ + 1 + ].default_branch_protection.enforce_admins # Repo assert repository_service.repositories[1].securitymd