forked from pypa/twine
/
test_auth.py
245 lines (171 loc) · 7.34 KB
/
test_auth.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
import getpass
import logging
import pytest
from twine import auth
from twine import exceptions
from twine import utils
@pytest.fixture
def config() -> utils.RepositoryConfig:
return dict(repository="system")
def test_get_password_keyring_overrides_prompt(monkeypatch, config):
class MockKeyring:
@staticmethod
def get_password(system, user):
return f"{user}@{system} sekure pa55word"
monkeypatch.setattr(auth, "keyring", MockKeyring)
pw = auth.Resolver(config, auth.CredentialInput("user")).password
assert pw == "user@system sekure pa55word"
def test_get_password_keyring_defers_to_prompt(monkeypatch, entered_password, config):
class MockKeyring:
@staticmethod
def get_password(system, user):
return
monkeypatch.setattr(auth, "keyring", MockKeyring)
pw = auth.Resolver(config, auth.CredentialInput("user")).password
assert pw == "entered pw"
def test_no_password_defers_to_prompt(monkeypatch, entered_password, config):
config.update(password=None)
pw = auth.Resolver(config, auth.CredentialInput("user")).password
assert pw == "entered pw"
def test_empty_password_bypasses_prompt(monkeypatch, entered_password, config):
config.update(password="")
pw = auth.Resolver(config, auth.CredentialInput("user")).password
assert pw == ""
def test_no_username_non_interactive_aborts(config):
with pytest.raises(exceptions.NonInteractive):
auth.Private(config, auth.CredentialInput("user")).password
def test_no_password_non_interactive_aborts(config):
with pytest.raises(exceptions.NonInteractive):
auth.Private(config, auth.CredentialInput("user")).password
def test_get_username_and_password_keyring_overrides_prompt(
monkeypatch, config, caplog
):
caplog.set_level(logging.INFO, "twine")
class MockKeyring:
@staticmethod
def get_credential(system, user):
return auth.CredentialInput(
"real_user", f"real_user@{system} sekure pa55word"
)
@staticmethod
def get_password(system, user):
cred = MockKeyring.get_credential(system, user)
if user != cred.username:
raise RuntimeError("unexpected username")
return cred.password
monkeypatch.setattr(auth, "keyring", MockKeyring)
res = auth.Resolver(config, auth.CredentialInput())
assert res.username == "real_user"
assert res.password == "real_user@system sekure pa55word"
assert caplog.messages == [
"Querying keyring for username",
"username set from keyring",
"Querying keyring for password",
"password set from keyring",
]
@pytest.fixture
def keyring_missing_get_credentials(monkeypatch):
"""Simulate keyring prior to 15.2 that does not have the 'get_credential' API."""
monkeypatch.delattr(auth.keyring, "get_credential")
@pytest.fixture
def entered_username(monkeypatch):
monkeypatch.setattr(auth, "input", lambda prompt: "entered user", raising=False)
def test_get_username_keyring_missing_get_credentials_prompts(
entered_username, keyring_missing_get_credentials, config
):
assert auth.Resolver(config, auth.CredentialInput()).username == "entered user"
def test_get_username_keyring_missing_non_interactive_aborts(
entered_username, keyring_missing_get_credentials, config
):
with pytest.raises(exceptions.NonInteractive):
auth.Private(config, auth.CredentialInput()).username
def test_get_password_keyring_missing_non_interactive_aborts(
entered_username, keyring_missing_get_credentials, config
):
with pytest.raises(exceptions.NonInteractive):
auth.Private(config, auth.CredentialInput("user")).password
@pytest.fixture
def keyring_no_backends(monkeypatch):
"""Simulate missing keyring backend raising RuntimeError on get_password."""
class FailKeyring:
@staticmethod
def get_password(system, username):
raise RuntimeError("fail!")
monkeypatch.setattr(auth, "keyring", FailKeyring())
@pytest.fixture
def keyring_no_backends_get_credential(monkeypatch):
"""Simulate missing keyring backend raising RuntimeError on get_credential."""
class FailKeyring:
@staticmethod
def get_credential(system, username):
raise RuntimeError("fail!")
monkeypatch.setattr(auth, "keyring", FailKeyring())
def test_get_username_runtime_error_suppressed(
entered_username, keyring_no_backends_get_credential, caplog, config
):
assert auth.Resolver(config, auth.CredentialInput()).username == "entered user"
assert caplog.messages == ["Error from keyring"] and "fail!" in caplog.text
def test_get_password_runtime_error_suppressed(
entered_password, keyring_no_backends, caplog, config
):
assert auth.Resolver(config, auth.CredentialInput("user")).password == "entered pw"
assert caplog.messages == ["Error from keyring"] and "fail!" in caplog.text
def test_get_username_return_none(entered_username, monkeypatch, config):
"""Prompt for username when it's not in keyring."""
class FailKeyring:
@staticmethod
def get_credential(system, username):
return None
monkeypatch.setattr(auth, "keyring", FailKeyring())
assert auth.Resolver(config, auth.CredentialInput()).username == "entered user"
def test_logs_cli_values(caplog):
caplog.set_level(logging.INFO, "twine")
res = auth.Resolver(config, auth.CredentialInput("username", "password"))
assert res.username == "username"
assert res.password == "password"
assert caplog.messages == [
"username set by command options",
"password set by command options",
]
def test_logs_config_values(config, caplog):
caplog.set_level(logging.INFO, "twine")
config.update(username="username", password="password")
res = auth.Resolver(config, auth.CredentialInput())
assert res.username == "username"
assert res.password == "password"
assert caplog.messages == [
"username set from config file",
"password set from config file",
]
@pytest.mark.parametrize(
"password, warning",
[
("", "Your password is empty"),
("\x16", "Your password contains control characters"),
("entered\x16pw", "Your password contains control characters"),
],
)
def test_warns_for_empty_password(
password,
warning,
monkeypatch,
entered_username,
config,
caplog,
):
# Avoiding additional warning "No recommended backend was available"
monkeypatch.setattr(auth.keyring, "get_password", lambda system, user: None)
monkeypatch.setattr(getpass, "getpass", lambda prompt: password)
assert auth.Resolver(config, auth.CredentialInput()).password == password
assert caplog.messages[0].startswith(warning)
def test_log_exception_on_keyring_failure(config, monkeypatch, caplog):
class FailKeyring:
@staticmethod
def get_credential(system, username):
# Simulate the error from https://github.com/pypa/twine/issues/889
environ = {}
environ["HOME"]
monkeypatch.setattr(auth, "keyring", FailKeyring())
assert not auth.Resolver(config, auth.CredentialInput()).get_username_from_keyring()
assert not auth.Resolver(config, auth.CredentialInput()).get_password_from_keyring()
assert caplog.messages[0] == "Error from keyring" and "KeyError: 'HOME'" in caplog.text