-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_auth.py
More file actions
127 lines (106 loc) · 4.75 KB
/
Copy pathtest_auth.py
File metadata and controls
127 lines (106 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""Test cases for the auth module."""
import unittest
from unittest.mock import MagicMock, patch
import auth
class TestAuth(unittest.TestCase):
"""
Test case for the auth module.
"""
@patch("auth.Github")
@patch("auth.Auth")
def test_auth_to_github_with_token(self, mock_auth, mock_github):
"""
Test the auth_to_github function when the token is provided.
"""
mock_token = MagicMock()
mock_auth.Token.return_value = mock_token
mock_github.return_value = "Authenticated to GitHub.com"
result = auth.auth_to_github("token", None, None, b"", "", False)
mock_auth.Token.assert_called_once_with("token")
mock_github.assert_called_once_with(auth=mock_token)
self.assertEqual(result, "Authenticated to GitHub.com")
def test_auth_to_github_without_token(self):
"""
Test the auth_to_github function when the token is not provided.
Expect a ValueError to be raised.
"""
with self.assertRaises(ValueError) as context_manager:
auth.auth_to_github("", None, None, b"", "", False)
the_exception = context_manager.exception
self.assertEqual(
str(the_exception),
"GH_TOKEN or the set of [GH_APP_ID, GH_APP_INSTALLATION_ID, GH_APP_PRIVATE_KEY] environment variables are not set",
)
@patch("auth.Github")
@patch("auth.Auth")
def test_auth_to_github_with_ghe(self, mock_auth, mock_github):
"""
Test the auth_to_github function when the GitHub Enterprise URL is provided.
"""
mock_token = MagicMock()
mock_auth.Token.return_value = mock_token
mock_github.return_value = "Authenticated to GitHub Enterprise"
result = auth.auth_to_github(
"token", None, None, b"", "https://github.example.com", False
)
mock_auth.Token.assert_called_once_with("token")
mock_github.assert_called_once_with(
base_url="https://github.example.com/api/v3", auth=mock_token
)
self.assertEqual(result, "Authenticated to GitHub Enterprise")
@patch("auth.Github")
@patch("auth.Auth")
def test_auth_to_github_with_ghe_and_ghe_app(self, mock_auth, mock_github):
"""
Test the auth_to_github function when the GitHub Enterprise URL is provided and the app was created in GitHub Enterprise URL.
"""
mock_app_auth = MagicMock()
mock_installation_auth = MagicMock()
mock_auth.AppAuth.return_value = mock_app_auth
mock_app_auth.get_installation_auth.return_value = mock_installation_auth
mock_github.return_value = MagicMock()
result = auth.auth_to_github(
"", 123, 123, b"123", "https://github.example.com", True
)
mock_auth.AppAuth.assert_called_once_with(123, "123")
mock_app_auth.get_installation_auth.assert_called_once_with(123)
mock_github.assert_called_once_with(
base_url="https://github.example.com/api/v3",
auth=mock_installation_auth,
)
self.assertEqual(result, mock_github.return_value)
@patch("auth.Github")
@patch("auth.Auth")
def test_auth_to_github_with_app(self, mock_auth, mock_github):
"""
Test the auth_to_github function when app credentials are provided
"""
mock_app_auth = MagicMock()
mock_installation_auth = MagicMock()
mock_auth.AppAuth.return_value = mock_app_auth
mock_app_auth.get_installation_auth.return_value = mock_installation_auth
mock_github.return_value = MagicMock()
result = auth.auth_to_github(
"", 123, 123, b"123", "https://github.example.com", False
)
mock_auth.AppAuth.assert_called_once_with(123, "123")
mock_app_auth.get_installation_auth.assert_called_once_with(123)
mock_github.assert_called_once_with(auth=mock_installation_auth)
self.assertEqual(result, mock_github.return_value)
@patch("auth.Github")
@patch("auth.Auth")
def test_auth_to_github_with_app_int_app_id(self, mock_auth, mock_github):
"""
Test that an integer app_id is converted properly for Auth.AppAuth.
"""
mock_app_auth = MagicMock()
mock_installation_auth = MagicMock()
mock_auth.AppAuth.return_value = mock_app_auth
mock_app_auth.get_installation_auth.return_value = mock_installation_auth
mock_github.return_value = MagicMock()
result = auth.auth_to_github("", 123, 456, b"private_key", "", False)
mock_auth.AppAuth.assert_called_once_with(123, "private_key")
mock_app_auth.get_installation_auth.assert_called_once_with(456)
self.assertEqual(result, mock_github.return_value)
if __name__ == "__main__":
unittest.main()