mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-24 21:11:53 +00:00
feat: added testing
This commit is contained in:
+117
@@ -0,0 +1,117 @@
|
||||
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_linear_history_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_linear_history.repository_merging_requires_linear_history.repository_client",
|
||||
new=repository_client,
|
||||
),
|
||||
):
|
||||
from prowler.providers.github.services.repository.repository_merging_requires_linear_history.repository_merging_requires_linear_history import (
|
||||
repository_merging_requires_linear_history,
|
||||
)
|
||||
|
||||
check = repository_merging_requires_linear_history()
|
||||
result = check.execute()
|
||||
assert len(result) == 0
|
||||
|
||||
def test_linear_history_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_linear_history.repository_merging_requires_linear_history.repository_client",
|
||||
new=repository_client,
|
||||
),
|
||||
):
|
||||
from prowler.providers.github.services.repository.repository_merging_requires_linear_history.repository_merging_requires_linear_history import (
|
||||
repository_merging_requires_linear_history,
|
||||
)
|
||||
|
||||
check = repository_merging_requires_linear_history()
|
||||
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 linear history on default branch ({default_branch})."
|
||||
)
|
||||
|
||||
def test_linear_history_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, linear_history=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_linear_history.repository_merging_requires_linear_history.repository_client",
|
||||
new=repository_client,
|
||||
),
|
||||
):
|
||||
from prowler.providers.github.services.repository.repository_merging_requires_linear_history.repository_merging_requires_linear_history import (
|
||||
repository_merging_requires_linear_history,
|
||||
)
|
||||
|
||||
check = repository_merging_requires_linear_history()
|
||||
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 linear history on default branch ({default_branch})."
|
||||
)
|
||||
@@ -17,7 +17,7 @@ def mock_list_repositories(_):
|
||||
private=False,
|
||||
default_branch="main",
|
||||
default_branch_protection=Protection(
|
||||
require_pull_request=True, approval_count=2
|
||||
require_pull_request=True, approval_count=2, linear_history=True
|
||||
),
|
||||
securitymd=True,
|
||||
),
|
||||
@@ -52,5 +52,8 @@ class Test_Repository_Service:
|
||||
repository_service.repositories[1].default_branch_protection.approval_count
|
||||
== 2
|
||||
)
|
||||
assert repository_service.repositories[
|
||||
1
|
||||
].default_branch_protection.linear_history
|
||||
# Repo
|
||||
assert repository_service.repositories[1].securitymd
|
||||
|
||||
Reference in New Issue
Block a user