|
4 | 4 | from unittest.mock import MagicMock, patch |
5 | 5 |
|
6 | 6 | import auth |
7 | | -import github3.github |
8 | 7 |
|
9 | 8 |
|
10 | 9 | class TestAuth(unittest.TestCase): |
11 | 10 | """ |
12 | 11 | Test case for the auth module. |
13 | 12 | """ |
14 | 13 |
|
15 | | - @patch("github3.github.GitHub.login_as_app_installation") |
16 | | - def test_auth_to_github_with_github_app(self, mock_login): |
17 | | - """ |
18 | | - Test the auth_to_github function when GitHub app |
19 | | - parameters provided. |
20 | | - """ |
21 | | - mock_login.return_value = MagicMock() |
22 | | - result = auth.auth_to_github(12345, 678910, b"hello", "", "") |
23 | | - |
24 | | - self.assertIsInstance(result, github3.github.GitHub) |
25 | | - |
26 | | - def test_auth_to_github_with_token(self): |
| 14 | + @patch("github3.login") |
| 15 | + def test_auth_to_github_with_token(self, mock_login): |
27 | 16 | """ |
28 | 17 | Test the auth_to_github function when the token is provided. |
29 | 18 | """ |
30 | | - result = auth.auth_to_github(None, None, b"", "token", "") |
| 19 | + mock_login.return_value = "Authenticated to GitHub.com" |
31 | 20 |
|
32 | | - self.assertIsInstance(result, github3.github.GitHub) |
| 21 | + result = auth.auth_to_github("token", "", "", b"", "", False) |
| 22 | + |
| 23 | + self.assertEqual(result, "Authenticated to GitHub.com") |
33 | 24 |
|
34 | 25 | def test_auth_to_github_without_token(self): |
35 | 26 | """ |
36 | 27 | Test the auth_to_github function when the token is not provided. |
37 | 28 | Expect a ValueError to be raised. |
38 | 29 | """ |
39 | | - with self.assertRaises(ValueError): |
40 | | - auth.auth_to_github(None, None, b"", "", "") |
| 30 | + with self.assertRaises(ValueError) as context_manager: |
| 31 | + auth.auth_to_github("", "", "", b"", "", False) |
| 32 | + the_exception = context_manager.exception |
| 33 | + self.assertEqual( |
| 34 | + str(the_exception), |
| 35 | + "GH_TOKEN or the set of [GH_APP_ID, GH_APP_INSTALLATION_ID, GH_APP_PRIVATE_KEY] environment variables are not set", |
| 36 | + ) |
41 | 37 |
|
42 | | - def test_auth_to_github_with_ghe(self): |
| 38 | + @patch("github3.github.GitHubEnterprise") |
| 39 | + def test_auth_to_github_with_ghe(self, mock_ghe): |
43 | 40 | """ |
44 | 41 | Test the auth_to_github function when the GitHub Enterprise URL is provided. |
45 | 42 | """ |
| 43 | + mock_ghe.return_value = "Authenticated to GitHub Enterprise" |
| 44 | + result = auth.auth_to_github( |
| 45 | + "token", "", "", b"", "https://github.example.com", False |
| 46 | + ) |
| 47 | + |
| 48 | + self.assertEqual(result, "Authenticated to GitHub Enterprise") |
| 49 | + |
| 50 | + @patch("github3.github.GitHubEnterprise") |
| 51 | + def test_auth_to_github_with_ghe_and_ghe_app(self, mock_ghe): |
| 52 | + """ |
| 53 | + Test the auth_to_github function when the GitHub Enterprise URL is provided and the app was created in GitHub Enterprise URL. |
| 54 | + """ |
| 55 | + mock = mock_ghe.return_value |
| 56 | + mock.login_as_app_installation = MagicMock(return_value=True) |
46 | 57 | result = auth.auth_to_github( |
47 | | - None, None, b"", "token", "https://github.example.com" |
| 58 | + "", "123", "123", b"123", "https://github.example.com", True |
48 | 59 | ) |
| 60 | + mock.login_as_app_installation.assert_called_once() |
| 61 | + self.assertEqual(result, mock) |
49 | 62 |
|
50 | | - self.assertIsInstance(result, github3.github.GitHubEnterprise) |
| 63 | + @patch("github3.github.GitHub") |
| 64 | + def test_auth_to_github_with_app(self, mock_gh): |
| 65 | + """ |
| 66 | + Test the auth_to_github function when app credentials are provided |
| 67 | + """ |
| 68 | + mock = mock_gh.return_value |
| 69 | + mock.login_as_app_installation = MagicMock(return_value=True) |
| 70 | + result = auth.auth_to_github( |
| 71 | + "", "123", "123", b"123", "https://github.example.com", False |
| 72 | + ) |
| 73 | + mock.login_as_app_installation.assert_called_once() |
| 74 | + self.assertEqual(result, mock) |
51 | 75 |
|
52 | 76 |
|
53 | 77 | if __name__ == "__main__": |
|
0 commit comments