Skip to content

Commit

Permalink
add test case for no branch protection (#6)
Browse files Browse the repository at this point in the history
* add test case for no branch protection
  • Loading branch information
bhamail authored Oct 22, 2024
1 parent 0ef4782 commit 01767bc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion github_standards/standards.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def check_and_apply_standard_properties_to_branch(repo, branch: Branch, do_actua
branch_protection: Optional[BranchProtection] = None
try:
branch_protection = branch.get_protection()
except GithubException as e:
except GithubException:
# GH returns a 404 when Branch Protection is not yet enabled for the branch in question
print(f' Branch {branch} is not protected in {repo.name}')

Expand Down
30 changes: 30 additions & 0 deletions github_standards/test/test_standards.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from unittest.mock import MagicMock

from github import GithubException
from github.Branch import Branch
from github.BranchProtection import BranchProtection
from github.Repository import Repository
Expand Down Expand Up @@ -163,6 +164,35 @@ def test_props_in_spec_branch_makes_no_change(self):

self.assertEqual(result, "")

def test_props_out_of_spec_branch_makes_a_change_no_branch_protection(self):
repo = self.create_mock_repo()

requester = self.create_mock_requester()
# noinspection PyTypeChecker
branch = Branch(requester='', headers='', attributes={}, completed='')
branch.get_protection = MagicMock()
ghe = GithubException(status=404, data=None)
branch.get_protection.side_effect = ghe

branch.edit_protection = MagicMock()

branch.get_required_pull_request_reviews = MagicMock()
# noinspection PyTypeChecker
rprr = RequiredPullRequestReviews(requester=requester, headers='', attributes={"url": MOCK_REPO_URL,
'require_code_owner_reviews': True,
'required_approving_review_count': 1},
completed='')
branch.get_required_pull_request_reviews.return_value = rprr

branch.get_required_signatures = MagicMock()
branch.get_required_signatures.return_value = True

result = standards.check_and_apply_standard_properties_to_branch(repo, branch, True)

self.assertEqual(result, "")
branch.edit_protection.assert_called_once_with(allow_deletions=False,
allow_force_pushes=False)

def test_props_out_of_spec_branch_makes_a_change(self):
repo = self.create_mock_repo()

Expand Down

0 comments on commit 01767bc

Please sign in to comment.