@@ -221,161 +221,6 @@ def test_default_to_environment_action(env_name, default, environ, expected):
221
221
assert action .default == expected
222
222
223
223
224
- def test_get_password_keyring_overrides_prompt (monkeypatch ):
225
- class MockKeyring :
226
- @staticmethod
227
- def get_password (system , user ):
228
- return '{user}@{system} sekure pa55word' .format (** locals ())
229
-
230
- monkeypatch .setattr (utils , 'keyring' , MockKeyring )
231
-
232
- pw = utils .get_password ('system' , 'user' , None , {})
233
- assert pw == 'user@system sekure pa55word'
234
-
235
-
236
- def test_get_password_keyring_defers_to_prompt (monkeypatch ):
237
- monkeypatch .setattr (utils , 'password_prompt' , lambda prompt : 'entered pw' )
238
-
239
- class MockKeyring :
240
- @staticmethod
241
- def get_password (system , user ):
242
- return
243
-
244
- monkeypatch .setattr (utils , 'keyring' , MockKeyring )
245
-
246
- pw = utils .get_password ('system' , 'user' , None , {})
247
- assert pw == 'entered pw'
248
-
249
-
250
- def test_no_password_defers_to_prompt (monkeypatch ):
251
- monkeypatch .setattr (utils , 'password_prompt' , lambda prompt : 'entered pw' )
252
- pw = utils .get_password ('system' , 'user' , None , {'password' : None })
253
- assert pw == 'entered pw'
254
-
255
-
256
- def test_empty_password_bypasses_prompt (monkeypatch ):
257
- monkeypatch .setattr (utils , 'password_prompt' , lambda prompt : 'entered pw' )
258
- pw = utils .get_password ('system' , 'user' , None , {'password' : '' })
259
- assert pw == ''
260
-
261
-
262
- def test_no_username_non_interactive_aborts ():
263
- with pytest .raises (exceptions .NonInteractive ):
264
- utils .get_username ('system' , None , {'username' : None }, True )
265
-
266
-
267
- def test_no_password_non_interactive_aborts ():
268
- with pytest .raises (exceptions .NonInteractive ):
269
- utils .get_password ('system' , 'user' , None , {'password' : None }, True )
270
-
271
-
272
- def test_get_username_and_password_keyring_overrides_prompt (monkeypatch ):
273
- import collections
274
- Credential = collections .namedtuple ('Credential' , 'username password' )
275
-
276
- class MockKeyring :
277
- @staticmethod
278
- def get_credential (system , user ):
279
- return Credential (
280
- 'real_user' ,
281
- 'real_user@{system} sekure pa55word' .format (** locals ())
282
- )
283
-
284
- @staticmethod
285
- def get_password (system , user ):
286
- cred = MockKeyring .get_credential (system , user )
287
- if user != cred .username :
288
- raise RuntimeError ("unexpected username" )
289
- return cred .password
290
-
291
- monkeypatch .setattr (utils , 'keyring' , MockKeyring )
292
-
293
- user = utils .get_username ('system' , None , {})
294
- assert user == 'real_user'
295
- pw = utils .get_password ('system' , user , None , {})
296
- assert pw == 'real_user@system sekure pa55word'
297
-
298
-
299
- @pytest .fixture
300
- def keyring_missing_get_credentials (monkeypatch ):
301
- """
302
- Simulate keyring prior to 15.2 that does not have the
303
- 'get_credential' API.
304
- """
305
- monkeypatch .delattr (utils .keyring , 'get_credential' )
306
-
307
-
308
- @pytest .fixture
309
- def entered_username (monkeypatch ):
310
- monkeypatch .setattr (utils , 'input_func' , lambda prompt : 'entered user' )
311
-
312
-
313
- @pytest .fixture
314
- def entered_password (monkeypatch ):
315
- monkeypatch .setattr (utils , 'password_prompt' , lambda prompt : 'entered pw' )
316
-
317
-
318
- def test_get_username_keyring_missing_get_credentials_prompts (
319
- entered_username , keyring_missing_get_credentials ):
320
- assert utils .get_username ('system' , None , {}) == 'entered user'
321
-
322
-
323
- def test_get_username_keyring_missing_non_interactive_aborts (
324
- entered_username , keyring_missing_get_credentials ):
325
- with pytest .raises (exceptions .NonInteractive ):
326
- utils .get_username ('system' , None , {}, True )
327
-
328
-
329
- def test_get_password_keyring_missing_non_interactive_aborts (
330
- entered_username , keyring_missing_get_credentials ):
331
- with pytest .raises (exceptions .NonInteractive ):
332
- utils .get_password ('system' , 'user' , None , {}, True )
333
-
334
-
335
- @pytest .fixture
336
- def keyring_no_backends (monkeypatch ):
337
- """
338
- Simulate that keyring has no available backends. When keyring
339
- has no backends for the system, the backend will be a
340
- fail.Keyring, which raises RuntimeError on get_password.
341
- """
342
- class FailKeyring :
343
- @staticmethod
344
- def get_password (system , username ):
345
- raise RuntimeError ("fail!" )
346
- monkeypatch .setattr (utils , 'keyring' , FailKeyring ())
347
-
348
-
349
- @pytest .fixture
350
- def keyring_no_backends_get_credential (monkeypatch ):
351
- """
352
- Simulate that keyring has no available backends. When keyring
353
- has no backends for the system, the backend will be a
354
- fail.Keyring, which raises RuntimeError on get_credential.
355
- """
356
- class FailKeyring :
357
- @staticmethod
358
- def get_credential (system , username ):
359
- raise RuntimeError ("fail!" )
360
- monkeypatch .setattr (utils , 'keyring' , FailKeyring ())
361
-
362
-
363
- def test_get_username_runtime_error_suppressed (
364
- entered_username , keyring_no_backends_get_credential , recwarn ):
365
- assert utils .get_username ('system' , None , {}) == 'entered user'
366
- assert len (recwarn ) == 1
367
- warning = recwarn .pop (UserWarning )
368
- assert 'fail!' in str (warning )
369
-
370
-
371
- def test_get_password_runtime_error_suppressed (
372
- entered_password , keyring_no_backends , recwarn ):
373
- assert utils .get_password ('system' , 'user' , None , {}) == 'entered pw'
374
- assert len (recwarn ) == 1
375
- warning = recwarn .pop (UserWarning )
376
- assert 'fail!' in str (warning )
377
-
378
-
379
224
@pytest .mark .parametrize ('repo_url' , [
380
225
"https://pypi.python.org" ,
381
226
"https://testpypi.python.org"
0 commit comments