|
4 | 4 | from unittest.mock import MagicMock, patch
|
5 | 5 |
|
6 | 6 | import auth
|
| 7 | +import requests |
7 | 8 |
|
8 | 9 |
|
9 | 10 | class TestAuth(unittest.TestCase):
|
@@ -91,6 +92,42 @@ def test_get_github_app_installation_token(self, mock_post):
|
91 | 92 |
|
92 | 93 | self.assertEqual(result, dummy_token)
|
93 | 94 |
|
| 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 | + |
94 | 131 |
|
95 | 132 | if __name__ == "__main__":
|
96 | 133 | unittest.main()
|
0 commit comments