From 8197448b300a7ecd6ed430b8693a0596c9ed4461 Mon Sep 17 00:00:00 2001 From: Zack Koppert Date: Mon, 4 Nov 2024 20:53:11 -0800 Subject: [PATCH 1/2] test: improve auth test coverage by adding missing and failed app install tests Signed-off-by: Zack Koppert --- test_auth.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test_auth.py b/test_auth.py index e264938..791c6f5 100644 --- a/test_auth.py +++ b/test_auth.py @@ -12,6 +12,7 @@ from unittest.mock import MagicMock, patch import github3 +import requests from auth import auth_to_github, get_github_app_installation_token @@ -87,3 +88,39 @@ def test_get_github_app_installation_token(self, mock_post): ) self.assertEqual(result, dummy_token) + + @patch("github3.apps.create_jwt_headers", MagicMock(return_value="gh_token")) + @patch("auth.requests.post") + def test_get_github_app_installation_token_request_failure(self, mock_post): + """ + Test the get_github_app_installation_token function returns None when the request fails. + """ + # Mock the post request to raise a RequestException + mock_post.side_effect = requests.exceptions.RequestException("Request failed") + + # Call the function with test data + result = get_github_app_installation_token( + ghe="https://api.github.com", + gh_app_id=12345, + gh_app_private_key_bytes=b"private_key", + gh_app_installation_id=678910, + ) + + # Assert that the result is None + self.assertIsNone(result) + + @patch("github3.login") + def test_auth_to_github_missing_credentials(self, mock_login): + """ + Test the auth_to_github function raises correct ValueError + when credentials are present but incorrect. + """ + mock_login.return_value = None + with self.assertRaises(ValueError) as context_manager: + auth_to_github("not_a_valid_token", "", "", b"", "", False) + + the_exception = context_manager.exception + self.assertEqual( + str(the_exception), + "Unable to authenticate to GitHub", + ) From d50bf917c6463c4688b13bf403fcb3f61e174d98 Mon Sep 17 00:00:00 2001 From: Zack Koppert Date: Mon, 4 Nov 2024 21:07:48 -0800 Subject: [PATCH 2/2] chore: fix naming to match test case Signed-off-by: Zack Koppert --- test_auth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_auth.py b/test_auth.py index 791c6f5..9788df0 100644 --- a/test_auth.py +++ b/test_auth.py @@ -110,7 +110,7 @@ def test_get_github_app_installation_token_request_failure(self, mock_post): self.assertIsNone(result) @patch("github3.login") - def test_auth_to_github_missing_credentials(self, mock_login): + def test_auth_to_github_invalid_credentials(self, mock_login): """ Test the auth_to_github function raises correct ValueError when credentials are present but incorrect.