-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
test_password_manager.py
371 lines (260 loc) · 11 KB
/
test_password_manager.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from __future__ import annotations
import logging
import os
from typing import TYPE_CHECKING
from unittest.mock import MagicMock
import pytest
from poetry.utils.password_manager import PasswordManager
from poetry.utils.password_manager import PoetryKeyring
from poetry.utils.password_manager import PoetryKeyringError
if TYPE_CHECKING:
from _pytest.logging import LogCaptureFixture
from pytest_mock import MockerFixture
from tests.conftest import Config
from tests.conftest import DummyBackend
def test_set_http_password(
config: Config, with_simple_keyring: None, dummy_keyring: DummyBackend
) -> None:
manager = PasswordManager(config)
assert PoetryKeyring.is_available()
manager.set_http_password("foo", "bar", "baz")
assert dummy_keyring.get_password("poetry-repository-foo", "bar") == "baz"
auth = config.get("http-basic.foo")
assert auth["username"] == "bar"
assert "password" not in auth
def test_get_http_auth(
config: Config, with_simple_keyring: None, dummy_keyring: DummyBackend
) -> None:
dummy_keyring.set_password("poetry-repository-foo", "bar", "baz")
config.auth_config_source.add_property("http-basic.foo", {"username": "bar"})
manager = PasswordManager(config)
assert PoetryKeyring.is_available()
auth = manager.get_http_auth("foo")
assert auth is not None
assert auth["username"] == "bar"
assert auth["password"] == "baz"
def test_delete_http_password(
config: Config, with_simple_keyring: None, dummy_keyring: DummyBackend
) -> None:
dummy_keyring.set_password("poetry-repository-foo", "bar", "baz")
config.auth_config_source.add_property("http-basic.foo", {"username": "bar"})
manager = PasswordManager(config)
assert PoetryKeyring.is_available()
manager.delete_http_password("foo")
assert dummy_keyring.get_password("poetry-repository-foo", "bar") is None
assert config.get("http-basic.foo") is None
def test_set_pypi_token(
config: Config, with_simple_keyring: None, dummy_keyring: DummyBackend
) -> None:
manager = PasswordManager(config)
assert PoetryKeyring.is_available()
manager.set_pypi_token("foo", "baz")
assert config.get("pypi-token.foo") is None
assert dummy_keyring.get_password("poetry-repository-foo", "__token__") == "baz"
def test_get_pypi_token(
config: Config, with_simple_keyring: None, dummy_keyring: DummyBackend
) -> None:
dummy_keyring.set_password("poetry-repository-foo", "__token__", "baz")
manager = PasswordManager(config)
assert PoetryKeyring.is_available()
assert manager.get_pypi_token("foo") == "baz"
def test_delete_pypi_token(
config: Config, with_simple_keyring: None, dummy_keyring: DummyBackend
) -> None:
dummy_keyring.set_password("poetry-repository-foo", "__token__", "baz")
manager = PasswordManager(config)
assert PoetryKeyring.is_available()
manager.delete_pypi_token("foo")
assert dummy_keyring.get_password("poetry-repository-foo", "__token__") is None
def test_set_http_password_with_unavailable_backend(
config: Config, with_fail_keyring: None
) -> None:
manager = PasswordManager(config)
assert not PoetryKeyring.is_available()
manager.set_http_password("foo", "bar", "baz")
auth = config.get("http-basic.foo")
assert auth["username"] == "bar"
assert auth["password"] == "baz"
def test_get_http_auth_with_unavailable_backend(
config: Config, with_fail_keyring: None
) -> None:
config.auth_config_source.add_property(
"http-basic.foo", {"username": "bar", "password": "baz"}
)
manager = PasswordManager(config)
assert not PoetryKeyring.is_available()
auth = manager.get_http_auth("foo")
assert auth is not None
assert auth["username"] == "bar"
assert auth["password"] == "baz"
def test_delete_http_password_with_unavailable_backend(
config: Config, with_fail_keyring: None
) -> None:
config.auth_config_source.add_property(
"http-basic.foo", {"username": "bar", "password": "baz"}
)
manager = PasswordManager(config)
assert not PoetryKeyring.is_available()
manager.delete_http_password("foo")
assert config.get("http-basic.foo") is None
def test_set_pypi_token_with_unavailable_backend(
config: Config, with_fail_keyring: None
) -> None:
manager = PasswordManager(config)
assert not PoetryKeyring.is_available()
manager.set_pypi_token("foo", "baz")
assert config.get("pypi-token.foo") == "baz"
def test_get_pypi_token_with_unavailable_backend(
config: Config, with_fail_keyring: None
) -> None:
config.auth_config_source.add_property("pypi-token.foo", "baz")
manager = PasswordManager(config)
assert not PoetryKeyring.is_available()
assert manager.get_pypi_token("foo") == "baz"
def test_delete_pypi_token_with_unavailable_backend(
config: Config, with_fail_keyring: None
) -> None:
config.auth_config_source.add_property("pypi-token.foo", "baz")
manager = PasswordManager(config)
assert not PoetryKeyring.is_available()
manager.delete_pypi_token("foo")
assert config.get("pypi-token.foo") is None
def test_keyring_raises_errors_on_keyring_errors(
mocker: MockerFixture, with_fail_keyring: None
) -> None:
mocker.patch("poetry.utils.password_manager.PoetryKeyring.is_available")
key_ring = PoetryKeyring("poetry")
with pytest.raises(PoetryKeyringError):
key_ring.set_password("foo", "bar", "baz")
with pytest.raises(PoetryKeyringError):
key_ring.get_password("foo", "bar")
with pytest.raises(PoetryKeyringError):
key_ring.delete_password("foo", "bar")
def test_keyring_returns_none_on_locked_keyring(
with_locked_keyring: None,
caplog: LogCaptureFixture,
) -> None:
caplog.set_level(logging.DEBUG, logger="poetry.utils.password_manager")
key_ring = PoetryKeyring("poetry")
cred = key_ring.get_credential("foo")
assert cred.password is None
assert "Keyring foo is locked" in caplog.messages
def test_keyring_returns_none_on_erroneous_keyring(
with_erroneous_keyring: None,
caplog: LogCaptureFixture,
) -> None:
caplog.set_level(logging.DEBUG, logger="poetry.utils.password_manager")
key_ring = PoetryKeyring("poetry")
cred = key_ring.get_credential("foo")
assert cred.password is None
assert "Accessing keyring foo failed" in caplog.messages
def test_keyring_with_chainer_backend_and_fail_keyring_should_be_unavailable(
with_chained_fail_keyring: None,
) -> None:
key_ring = PoetryKeyring("poetry")
assert not key_ring.is_available()
def test_keyring_with_chainer_backend_and_null_keyring_should_be_unavailable(
with_chained_null_keyring: None,
) -> None:
key_ring = PoetryKeyring("poetry")
assert not key_ring.is_available()
def test_null_keyring_should_be_unavailable(
with_null_keyring: None,
) -> None:
key_ring = PoetryKeyring("poetry")
assert not key_ring.is_available()
def test_fail_keyring_should_be_unavailable(
with_fail_keyring: None,
) -> None:
key_ring = PoetryKeyring("poetry")
assert not key_ring.is_available()
def test_locked_keyring_should_be_available(with_locked_keyring: None) -> None:
key_ring = PoetryKeyring("poetry")
assert key_ring.is_available()
def test_erroneous_keyring_should_be_available(with_erroneous_keyring: None) -> None:
key_ring = PoetryKeyring("poetry")
assert key_ring.is_available()
def test_get_http_auth_from_environment_variables(
environ: None, config: Config
) -> None:
os.environ["POETRY_HTTP_BASIC_FOO_USERNAME"] = "bar"
os.environ["POETRY_HTTP_BASIC_FOO_PASSWORD"] = "baz"
manager = PasswordManager(config)
auth = manager.get_http_auth("foo")
assert auth == {"username": "bar", "password": "baz"}
def test_get_http_auth_does_not_call_keyring_when_credentials_in_environment_variables(
environ: None, config: Config
) -> None:
os.environ["POETRY_HTTP_BASIC_FOO_USERNAME"] = "bar"
os.environ["POETRY_HTTP_BASIC_FOO_PASSWORD"] = "baz"
manager = PasswordManager(config)
manager.keyring = MagicMock()
auth = manager.get_http_auth("foo")
assert auth == {"username": "bar", "password": "baz"}
manager.keyring.get_password.assert_not_called()
def test_get_http_auth_does_not_call_keyring_when_password_in_environment_variables(
environ: None, config: Config
) -> None:
config.merge(
{
"http-basic": {"foo": {"username": "bar"}},
}
)
os.environ["POETRY_HTTP_BASIC_FOO_PASSWORD"] = "baz"
manager = PasswordManager(config)
manager.keyring = MagicMock()
auth = manager.get_http_auth("foo")
assert auth == {"username": "bar", "password": "baz"}
manager.keyring.get_password.assert_not_called()
def test_get_pypi_token_with_env_var_positive(
mocker: MockerFixture,
config: Config,
with_simple_keyring: None,
dummy_keyring: DummyBackend,
) -> None:
sample_token = "sampletoken-1234"
repo_name = "foo"
manager = PasswordManager(config)
mocker.patch.dict(
os.environ,
{f"POETRY_PYPI_TOKEN_{repo_name.upper()}": sample_token},
)
assert manager.get_pypi_token(repo_name) == sample_token
def test_get_pypi_token_with_env_var_not_available(
config: Config, with_simple_keyring: None, dummy_keyring: DummyBackend
) -> None:
repo_name = "foo"
manager = PasswordManager(config)
result_token = manager.get_pypi_token(repo_name)
assert result_token is None
def test_disabled_keyring_never_called(
config: Config, with_simple_keyring: None, dummy_keyring: DummyBackend
) -> None:
config.config["keyring"]["enabled"] = False
config.config["http-basic"] = {"onlyuser": {"username": "user"}}
manager = PasswordManager(config)
num_public_functions = len([f for f in dir(manager) if not f.startswith("_")])
if num_public_functions != 10:
pytest.fail(
f"A function was added to or removed from the {PasswordManager.__name__} "
"class without reflecting this change in this test."
)
with pytest.raises(PoetryKeyringError) as e:
_ = manager.keyring
assert str(e.value) == "Access to keyring was requested, but it is not available"
# We made sure that accessing a disabled keyring raises an exception.
# Now we call the PasswordManager functions that do access the keyring to
# make sure that they never do so when the keyring is disabled.
manager.set_pypi_token(repo_name="exists", token="token")
manager.get_pypi_token(repo_name="exists")
manager.get_pypi_token(repo_name="doesn't exist")
manager.delete_pypi_token(repo_name="exists")
manager.delete_pypi_token(repo_name="doesn't exist")
manager.set_http_password(repo_name="exists", username="user", password="password")
manager.get_http_auth(repo_name="exists")
manager.get_http_auth(repo_name="doesn't exist")
manager.get_http_auth(repo_name="onlyuser")
manager.delete_http_password(repo_name="exits")
manager.delete_http_password(repo_name="doesn't exist")
manager.delete_http_password(repo_name="onlyuser")
manager.get_credential("a", "b", "c", username="user")