Skip to content

Commit 9b642a6

Browse files
authored
Merge pull request #202 from github/auth-testing
test: add more tests to increase auth test coverage
2 parents caded81 + 753b2f3 commit 9b642a6

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
@@ -4,6 +4,7 @@
44
from unittest.mock import MagicMock, patch
55

66
import auth
7+
import requests
78

89

910
class TestAuth(unittest.TestCase):
@@ -91,6 +92,42 @@ def test_get_github_app_installation_token(self, mock_post):
9192

9293
self.assertEqual(result, dummy_token)
9394

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

95132
if __name__ == "__main__":
96133
unittest.main()

0 commit comments

Comments
 (0)