Skip to content

Commit 2ab9714

Browse files
authored
Merge pull request #426 from github/auth-test-coverage
2 parents 69267c5 + d50bf91 commit 2ab9714

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

test_auth.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from unittest.mock import MagicMock, patch
1313

1414
import github3
15+
import requests
1516
from auth import auth_to_github, get_github_app_installation_token
1617

1718

@@ -87,3 +88,39 @@ def test_get_github_app_installation_token(self, mock_post):
8788
)
8889

8990
self.assertEqual(result, dummy_token)
91+
92+
@patch("github3.apps.create_jwt_headers", MagicMock(return_value="gh_token"))
93+
@patch("auth.requests.post")
94+
def test_get_github_app_installation_token_request_failure(self, mock_post):
95+
"""
96+
Test the get_github_app_installation_token function returns None when the request fails.
97+
"""
98+
# Mock the post request to raise a RequestException
99+
mock_post.side_effect = requests.exceptions.RequestException("Request failed")
100+
101+
# Call the function with test data
102+
result = get_github_app_installation_token(
103+
ghe="https://api.github.com",
104+
gh_app_id=12345,
105+
gh_app_private_key_bytes=b"private_key",
106+
gh_app_installation_id=678910,
107+
)
108+
109+
# Assert that the result is None
110+
self.assertIsNone(result)
111+
112+
@patch("github3.login")
113+
def test_auth_to_github_invalid_credentials(self, mock_login):
114+
"""
115+
Test the auth_to_github function raises correct ValueError
116+
when credentials are present but incorrect.
117+
"""
118+
mock_login.return_value = None
119+
with self.assertRaises(ValueError) as context_manager:
120+
auth_to_github("not_a_valid_token", "", "", b"", "", False)
121+
122+
the_exception = context_manager.exception
123+
self.assertEqual(
124+
str(the_exception),
125+
"Unable to authenticate to GitHub",
126+
)

0 commit comments

Comments
 (0)