-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_github_auth.py
More file actions
262 lines (215 loc) · 9.52 KB
/
test_github_auth.py
File metadata and controls
262 lines (215 loc) · 9.52 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# tests/test_github_auth.py
import platform
import subprocess
import unittest
from unittest.mock import patch
from githubauthlib import (
CredentialHelperError,
InvalidTokenError,
PlatformNotSupportedError,
TokenNotFoundError,
get_github_token,
)
class TestGitHubAuth(unittest.TestCase):
"""Test cases for GitHub authentication functionality."""
def setUp(self):
"""Set up test cases."""
self.test_token = "ghp_1234567890abcdef1234567890abcdef123456"
self.current_platform = platform.system() # Store current platform for tests
@patch("platform.system")
@patch("subprocess.check_output")
def test_macos_token_retrieval(self, mock_subprocess, mock_platform):
"""Test successful token retrieval on macOS."""
mock_platform.return_value = "Darwin"
mock_subprocess.return_value = f"password={self.test_token}\n"
token = get_github_token()
self.assertEqual(token, self.test_token)
mock_subprocess.assert_called_with(
["git", "credential-osxkeychain", "get"],
input="protocol=https\nhost=github.com\n",
universal_newlines=True,
stderr=subprocess.DEVNULL,
)
@patch("platform.system")
@patch("subprocess.check_output")
def test_macos_no_token(self, mock_subprocess, mock_platform):
"""Test macOS behavior when no token is found."""
mock_platform.return_value = "Darwin"
mock_subprocess.side_effect = subprocess.CalledProcessError(1, "git")
with self.assertRaises(CredentialHelperError):
get_github_token()
@patch("platform.system")
@patch("subprocess.check_output")
def test_windows_token_retrieval(self, mock_subprocess, mock_platform):
"""Test successful token retrieval on Windows."""
mock_platform.return_value = "Windows"
mock_subprocess.side_effect = [
"manager\n",
f"protocol=https\nhost=github.com\npassword={self.test_token}\n",
]
token = get_github_token()
self.assertEqual(token, self.test_token)
@patch("platform.system")
@patch("subprocess.check_output")
def test_windows_no_manager(self, mock_subprocess, mock_platform):
"""Test Windows behavior when credential manager is not configured."""
mock_platform.return_value = "Windows"
mock_subprocess.return_value = "wincred\n"
with self.assertRaises(TokenNotFoundError):
get_github_token()
@patch("platform.system")
@patch("subprocess.check_output")
def test_windows_credential_error(self, mock_subprocess, mock_platform):
"""Test Windows behavior when credential helper fails."""
mock_platform.return_value = "Windows"
mock_subprocess.side_effect = subprocess.CalledProcessError(1, "git")
with self.assertRaises(CredentialHelperError):
get_github_token()
@patch("platform.system")
@patch("subprocess.check_output")
def test_linux_libsecret_token(self, mock_subprocess, mock_platform):
"""Test successful token retrieval on Linux using libsecret."""
mock_platform.return_value = "Linux"
mock_subprocess.return_value = self.test_token
token = get_github_token()
self.assertEqual(token, self.test_token)
mock_subprocess.assert_called_with(
["secret-tool", "lookup", "host", "github.com"],
universal_newlines=True,
stderr=subprocess.DEVNULL,
)
@patch("platform.system")
@patch("subprocess.check_output")
def test_linux_fallback_token(self, mock_subprocess, mock_platform):
"""Test Linux fallback to git credential store."""
mock_platform.return_value = "Linux"
mock_subprocess.side_effect = [
FileNotFoundError(), # secret-tool not found
f"protocol=https\nhost=github.com\npassword={self.test_token}\n",
]
token = get_github_token()
self.assertEqual(token, self.test_token)
@patch("platform.system")
@patch("subprocess.check_output")
def test_linux_all_methods_fail(self, mock_subprocess, mock_platform):
"""Test Linux behavior when both libsecret and git credential store fail."""
mock_platform.return_value = "Linux"
mock_subprocess.side_effect = [
subprocess.CalledProcessError(1, "secret-tool"), # libsecret fails
subprocess.CalledProcessError(1, "git"), # git credential store fails
]
with self.assertRaises(CredentialHelperError):
get_github_token()
@patch("platform.system")
@patch("subprocess.check_output")
def test_linux_credential_store_no_password(self, mock_subprocess, mock_platform):
"""Test Linux git credential store with no password in output."""
mock_platform.return_value = "Linux"
mock_subprocess.side_effect = [
FileNotFoundError(), # secret-tool not found
"protocol=https\nhost=github.com\n", # no password in output
]
with self.assertRaises(TokenNotFoundError):
get_github_token()
@patch("platform.system")
def test_unsupported_platform(self, mock_platform):
"""Test behavior with unsupported platform."""
mock_platform.return_value = "SomeOS"
with self.assertRaises(PlatformNotSupportedError):
get_github_token()
@patch("platform.system")
@patch("subprocess.check_output")
def test_macos_invalid_token(self, mock_subprocess, mock_platform):
"""Test macOS behavior with invalid token format."""
mock_platform.return_value = "Darwin"
mock_subprocess.return_value = "password=invalid_token\n"
with self.assertRaises(InvalidTokenError):
get_github_token()
@patch("platform.system")
@patch("subprocess.check_output")
def test_windows_invalid_token(self, mock_subprocess, mock_platform):
"""Test Windows behavior with invalid token format."""
mock_platform.return_value = "Windows"
mock_subprocess.side_effect = [
"manager\n",
"protocol=https\nhost=github.com\npassword=invalid_token\n",
]
with self.assertRaises(InvalidTokenError):
get_github_token()
@patch("platform.system")
@patch("subprocess.check_output")
def test_linux_invalid_token(self, mock_subprocess, mock_platform):
"""Test Linux behavior with invalid token format."""
mock_platform.return_value = "Linux"
mock_subprocess.return_value = "invalid_token"
with self.assertRaises(InvalidTokenError):
get_github_token()
@patch("platform.system")
@patch("subprocess.check_output")
def test_macos_empty_output(self, mock_subprocess, mock_platform):
"""Test macOS behavior with empty credential output."""
mock_platform.return_value = "Darwin"
mock_subprocess.return_value = ""
with self.assertRaises(TokenNotFoundError):
get_github_token()
@patch("platform.system")
@patch("subprocess.check_output")
def test_windows_empty_output(self, mock_subprocess, mock_platform):
"""Test Windows behavior with empty credential output."""
mock_platform.return_value = "Windows"
mock_subprocess.side_effect = [
"manager\n",
"",
]
with self.assertRaises(TokenNotFoundError):
get_github_token()
def test_validate_token_invalid_types(self):
"""Test token validation with invalid types."""
from githubauthlib.github_auth import _validate_token
# Test None
self.assertFalse(_validate_token(None))
# Test empty string
self.assertFalse(_validate_token(""))
# Test non-string
self.assertFalse(_validate_token(123))
def test_validate_token_fine_grained(self):
"""Test token validation with fine-grained token."""
from githubauthlib.github_auth import _validate_token
# Test fine-grained token
fine_grained_token = (
"github_pat_1234567890abcdef1234567890abcdef1234567890abcdef"
"1234567890abcdef"
)
self.assertTrue(_validate_token(fine_grained_token))
def test_validate_token_organization(self):
"""Test token validation with organization token."""
from githubauthlib.github_auth import _validate_token
# Test organization token
org_token = "gho_1234567890abcdef1234567890abcdef123456"
self.assertTrue(_validate_token(org_token))
@patch("platform.system")
@patch("subprocess.check_output")
def test_linux_libsecret_empty_output(self, mock_subprocess, mock_platform):
"""Test Linux libsecret with empty output."""
mock_platform.return_value = "Linux"
mock_subprocess.side_effect = [
"", # Empty output from libsecret
f"protocol=https\nhost=github.com\npassword={self.test_token}\n",
]
token = get_github_token()
self.assertEqual(token, self.test_token)
@patch("platform.system")
@patch("subprocess.check_output")
def test_linux_git_credential_store_invalid_token(
self, mock_subprocess, mock_platform
):
"""Test Linux git credential store with invalid token."""
mock_platform.return_value = "Linux"
mock_subprocess.side_effect = [
FileNotFoundError(), # secret-tool not found
"protocol=https\nhost=github.com\npassword=invalid_token\n",
]
with self.assertRaises(InvalidTokenError):
get_github_token()
if __name__ == "__main__":
unittest.main()