From 56274ebaee51f6b70c2cd60e5555adc90d80d6b0 Mon Sep 17 00:00:00 2001 From: Zack Koppert Date: Tue, 5 Nov 2024 08:46:10 -0800 Subject: [PATCH 1/2] test: add more tests to increase auth test coverage --- test_auth.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test_auth.py b/test_auth.py index 7a595fe..a9d6c66 100644 --- a/test_auth.py +++ b/test_auth.py @@ -3,6 +3,7 @@ import unittest from unittest.mock import MagicMock, patch +import requests import auth @@ -91,6 +92,41 @@ 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_invalid_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", + ) if __name__ == "__main__": unittest.main() From 753b2f3bb22858e5a0a85ecc3badab5e3006acdd Mon Sep 17 00:00:00 2001 From: Zack Koppert Date: Tue, 5 Nov 2024 09:38:37 -0800 Subject: [PATCH 2/2] fix: use proper prefix for function calls Signed-off-by: Zack Koppert --- test_auth.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test_auth.py b/test_auth.py index a9d6c66..9337abc 100644 --- a/test_auth.py +++ b/test_auth.py @@ -3,8 +3,8 @@ import unittest from unittest.mock import MagicMock, patch -import requests import auth +import requests class TestAuth(unittest.TestCase): @@ -102,7 +102,7 @@ def test_get_github_app_installation_token_request_failure(self, mock_post): mock_post.side_effect = requests.exceptions.RequestException("Request failed") # Call the function with test data - result = get_github_app_installation_token( + result = auth.get_github_app_installation_token( ghe="https://api.github.com", gh_app_id=12345, gh_app_private_key_bytes=b"private_key", @@ -120,7 +120,7 @@ def test_auth_to_github_invalid_credentials(self, mock_login): """ mock_login.return_value = None with self.assertRaises(ValueError) as context_manager: - auth_to_github("not_a_valid_token", "", "", b"", "", False) + auth.auth_to_github("not_a_valid_token", "", "", b"", "", False) the_exception = context_manager.exception self.assertEqual( @@ -128,5 +128,6 @@ def test_auth_to_github_invalid_credentials(self, mock_login): "Unable to authenticate to GitHub", ) + if __name__ == "__main__": unittest.main()