|
12 | 12 | from unittest.mock import MagicMock, patch
|
13 | 13 |
|
14 | 14 | import github3
|
| 15 | +import requests |
15 | 16 | from auth import auth_to_github, get_github_app_installation_token
|
16 | 17 |
|
17 | 18 |
|
@@ -87,3 +88,39 @@ def test_get_github_app_installation_token(self, mock_post):
|
87 | 88 | )
|
88 | 89 |
|
89 | 90 | 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