From 2f852dac86c10494d4ea951c705a883cdea3cb45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Thu, 13 Oct 2022 13:47:42 +0200 Subject: [PATCH] Add: Add tests for async GitHub branches API --- pontos/github/api/branch.py | 5 +- tests/github/api/test_branch.py | 83 ++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/pontos/github/api/branch.py b/pontos/github/api/branch.py index d69af52a5..b601581bc 100644 --- a/pontos/github/api/branch.py +++ b/pontos/github/api/branch.py @@ -43,7 +43,7 @@ async def delete(self, repo: str, branch: str): branch: Branch to be deleted Raises: - HTTPError if the request was invalid + HTTPStatusError if the request was invalid """ api = f"/repos/{repo}/git/refs/{branch}" response = await self._client.delete(api) @@ -59,10 +59,11 @@ async def protection_rules(self, repo: str, branch: str) -> JSON: branch: Get protection rules for this branch Raises: - HTTPError if the request was invalid + HTTPStatusError if the request was invalid """ api = f"/repos/{repo}/branches/{branch}/protection" response = await self._client.get(api) + response.raise_for_status() return response.json() diff --git a/tests/github/api/test_branch.py b/tests/github/api/test_branch.py index 3ed6b1cdc..0334eb5b7 100644 --- a/tests/github/api/test_branch.py +++ b/tests/github/api/test_branch.py @@ -15,18 +15,97 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -# pylint: disable=too-many-lines - import unittest from pathlib import Path from unittest.mock import MagicMock, patch +from httpx import HTTPStatusError, Response + from pontos.github.api import GitHubRESTApi +from pontos.github.api.branch import GitHubAsyncRESTBranches +from pontos.github.api.client import GitHubAsyncRESTClient +from tests import AsyncMock, IsolatedAsyncioTestCase from tests.github.api import default_request here = Path(__file__).parent +class GitHubAsyncRESTBranchesTestCase(IsolatedAsyncioTestCase): + def setUp(self) -> None: + self.client = AsyncMock(spec=GitHubAsyncRESTClient) + self.api = GitHubAsyncRESTBranches(self.client) + + async def test_exists(self): + response = MagicMock(spec=Response, is_success=True) + self.client.get.return_value = response + + self.assertTrue(await self.api.exists("foo/bar", "baz")) + self.client.get.assert_awaited_once_with("/repos/foo/bar/branches/baz") + + async def test_not_exists(self): + response = MagicMock(spec=Response, is_success=False) + self.client.get.return_value = response + + self.assertFalse(await self.api.exists("foo/bar", "baz")) + self.client.get.assert_awaited_once_with("/repos/foo/bar/branches/baz") + + async def test_delete_branch(self): + response = MagicMock(spec=Response) + self.client.delete.return_value = response + + await self.api.delete("foo/bar", "baz") + + self.client.delete.assert_awaited_once_with( + "/repos/foo/bar/git/refs/baz" + ) + + async def test_delete_branch_failure(self): + response = MagicMock(spec=Response) + error = HTTPStatusError("404", request=MagicMock(), response=response) + response.raise_for_status.side_effect = error + + self.client.delete.return_value = response + + with self.assertRaises(HTTPStatusError): + await self.api.delete("foo/bar", "baz") + + self.client.delete.assert_awaited_once_with( + "/repos/foo/bar/git/refs/baz" + ) + + async def test_protection_rules(self): + rules = { + "required_status_checks": {}, + "enforce_admins": {}, + "required_pull_request_reviews": {}, + } + response = MagicMock(spec=Response) + response.json.return_value = rules + + self.client.get.return_value = response + + data = await self.api.protection_rules("foo/bar", "baz") + + self.client.get.assert_awaited_once_with( + "/repos/foo/bar/branches/baz/protection" + ) + self.assertEqual(data, rules) + + async def test_protection_rules_failure(self): + response = MagicMock(spec=Response) + error = HTTPStatusError("404", request=MagicMock(), response=response) + response.raise_for_status.side_effect = error + + self.client.get.return_value = response + + with self.assertRaises(HTTPStatusError): + await self.api.protection_rules("foo/bar", "baz") + + self.client.get.assert_awaited_once_with( + "/repos/foo/bar/branches/baz/protection" + ) + + class GitHubBranchTestCase(unittest.TestCase): @patch("pontos.github.api.api.httpx.get") def test_branch_exists(self, requests_mock: MagicMock):