-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_credentials.py
248 lines (211 loc) · 7.88 KB
/
test_credentials.py
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
import io
import os
import random
import pytest
from scmrepo.git.credentials import (
Credential,
CredentialNotFoundError,
GitCredentialHelper,
MemoryCredentialHelper,
)
@pytest.fixture(name="git_helper")
def git_helper_fixture(mocker) -> "GitCredentialHelper":
mocker.patch("shutil.which", return_value="/usr/bin/git-credential-foo")
return GitCredentialHelper("foo")
def test_subprocess_get(git_helper, mocker):
run = mocker.patch(
"subprocess.run",
return_value=mocker.Mock(
stdout="protocol=https\nhost=foo.com\nusername=foo\npassword=bar\n"
),
)
creds = git_helper.get(Credential(protocol="https", host="foo.com", path="foo.git"))
assert run.call_args.args[0] == ["git-credential-foo", "get"]
assert run.call_args.kwargs.get("input") == "protocol=https\nhost=foo.com\n"
assert creds == Credential(url="https://foo:[email protected]")
def test_subprocess_get_use_http_path(git_helper, mocker):
git_helper.use_http_path = True
run = mocker.patch(
"subprocess.run",
return_value=mocker.Mock(stdout="username=foo\npassword=bar\n"),
)
creds = git_helper.get(Credential(protocol="https", host="foo.com", path="foo.git"))
assert run.call_args.args[0] == ["git-credential-foo", "get"]
assert (
run.call_args.kwargs.get("input")
== "protocol=https\nhost=foo.com\npath=foo.git\n"
)
assert creds == Credential(username="foo", password="bar")
def test_subprocess_ignore_unexpected_credential_keys(git_helper, mocker):
git_helper.use_http_path = True
run = mocker.patch(
"subprocess.run",
# Simulate git-credential-osxkeychain (version >=2.45)
return_value=mocker.Mock(
stdout="username=foo\npassword=bar\ncapability[]=state\nstate[]=osxkeychain:seen=1"
),
)
creds = git_helper.get(Credential(protocol="https", host="foo.com", path="foo.git"))
assert run.call_args.args[0] == ["git-credential-foo", "get"]
assert (
run.call_args.kwargs.get("input")
== "protocol=https\nhost=foo.com\npath=foo.git\n"
)
assert creds == Credential(username="foo", password="bar")
def test_subprocess_strip_trailing_garbage_bytes(git_helper, mocker):
"""Garbage bytes were output from git-credential-osxkeychain from 2.45.0 to 2.47.0
so must be removed
https://github.com/git/git/commit/6c3c451fb6e1c3ca83f74e63079d4d0af01b2d69"""
git_helper.use_http_path = True
run = mocker.patch(
"subprocess.run",
# Simulate git-credential-osxkeychain (version 2.45), assuming initial 0-byte
return_value=mocker.Mock(
stdout=f"username=foo\npassword=bar{chr(0)}{random.randbytes(15)}"
),
)
creds = git_helper.get(Credential(protocol="https", host="foo.com", path="foo.git"))
assert run.call_args.args[0] == ["git-credential-foo", "get"]
assert (
run.call_args.kwargs.get("input")
== "protocol=https\nhost=foo.com\npath=foo.git\n"
)
assert creds == Credential(username="foo", password="bar")
def test_subprocess_get_failed(git_helper, mocker):
from subprocess import CalledProcessError
mocker.patch("subprocess.run", side_effect=CalledProcessError(1, "/usr/bin/foo"))
with pytest.raises(CredentialNotFoundError):
git_helper.get(Credential(protocol="https", host="foo.com"))
def test_subprocess_get_no_output(git_helper, mocker):
mocker.patch("subprocess.run", return_value=mocker.Mock(stdout="\n"))
with pytest.raises(CredentialNotFoundError):
git_helper.get(Credential(protocol="https", host="foo.com"))
def test_subprocess_store(git_helper, mocker):
run = mocker.patch("subprocess.run")
git_helper.store(
Credential(protocol="https", host="foo.com", username="foo", password="bar")
)
assert run.call_args.args[0] == ["git-credential-foo", "store"]
assert (
run.call_args.kwargs.get("input")
== "protocol=https\nhost=foo.com\nusername=foo\npassword=bar\n"
)
def test_subprocess_erase(git_helper, mocker):
run = mocker.patch("subprocess.run")
git_helper.erase(Credential(protocol="https", host="foo.com"))
assert run.call_args.args[0] == ["git-credential-foo", "erase"]
assert run.call_args.kwargs.get("input") == "protocol=https\nhost=foo.com\n"
def test_memory_helper_get(mocker):
from getpass import getpass
from scmrepo.git.credentials import _input_tty
helper = MemoryCredentialHelper()
expected = Credential(
protocol="https", host="foo.com", username="foo", password="bar"
)
get_interactive = mocker.patch.object(
helper,
"_get_interactive",
return_value=expected,
)
with pytest.raises(CredentialNotFoundError):
helper.get(Credential(protocol="https", host="foo.com"), interactive=False)
get_interactive.assert_not_called()
assert (
helper.get(Credential(protocol="https", host="foo.com"), interactive=True)
== expected
)
assert get_interactive.call_args.args[0] == Credential(
protocol="https", host="foo.com"
)
assert get_interactive.call_args.args[1:] == (_input_tty, getpass)
def test_memory_helper_get_cached(mocker):
helper = MemoryCredentialHelper()
expected = Credential(
protocol="https", host="foo.com", username="foo", password="bar"
)
helper[expected] = expected
get_interactive = mocker.patch.object(
helper,
"_get_interactive",
return_value=expected,
)
assert (
helper.get(Credential(protocol="https", host="foo.com"), interactive=False)
== expected
)
get_interactive.assert_not_called()
def test_memory_helper_prompt_disabled(mocker):
helper = MemoryCredentialHelper()
get_interactive = mocker.patch.object(
helper,
"_get_interactive",
)
mocker.patch.dict(os.environ, {"GIT_TERMINAL_PROMPT": "0"})
with pytest.raises(CredentialNotFoundError):
helper.get(Credential(protocol="https", host="foo.com"), interactive=True)
get_interactive.assert_not_called()
def test_memory_helper_prompt_askpass(mocker):
helper = MemoryCredentialHelper()
mocker.patch.dict(os.environ, {"GIT_ASKPASS": "/usr/local/bin/my-askpass"})
run = mocker.patch(
"subprocess.run",
side_effect=[
mocker.Mock(stdout="foo"),
mocker.Mock(stdout="bar\n"),
],
)
expected = Credential(
protocol="https", host="foo.com", username="foo", password="bar"
)
assert (
helper.get(Credential(protocol="https", host="foo.com"), interactive=True)
== expected
)
assert len(run.call_args_list) == 2
assert run.call_args_list[0].args[0] == [
"/usr/local/bin/my-askpass",
"Username for 'https://foo.com': ",
]
assert run.call_args_list[1].args[0] == [
"/usr/local/bin/my-askpass",
"Password for 'https://[email protected]': ",
]
def test_get_matching_commands():
from dulwich.config import ConfigFile
config_file = io.BytesIO(
b"""
[credential]
helper = /usr/local/bin/my-helper
UseHttpPath = true
"""
)
config_file.seek(0)
config = ConfigFile.from_file(config_file)
assert list(
GitCredentialHelper.get_matching_commands("https://foo.com/foo.git", config)
) == [("/usr/local/bin/my-helper", True)]
config_file = io.BytesIO(
b"""
[credential]
helper = /usr/local/bin/my-helper
"""
)
config_file.seek(0)
config = ConfigFile.from_file(config_file)
assert list(
GitCredentialHelper.get_matching_commands("https://foo.com/foo.git", config)
) == [("/usr/local/bin/my-helper", False)]
config_file = io.BytesIO(
b"""
[credential]
helper =
"""
)
config_file.seek(0)
config = ConfigFile.from_file(config_file)
assert (
list(
GitCredentialHelper.get_matching_commands("https://foo.com/foo.git", config)
)
== []
)