From 3bb6662e8e514f2025b2cbeb351f67f8306dd20d Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Tue, 9 Sep 2014 17:48:36 +0200 Subject: [PATCH] Add testcase --- tests/utils/test_main.py | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/tests/utils/test_main.py b/tests/utils/test_main.py index 11a2e62..6310702 100644 --- a/tests/utils/test_main.py +++ b/tests/utils/test_main.py @@ -102,19 +102,10 @@ def test_get_password_from_system_keyring(monkeypatch): monkeypatch.setattr(utils, 'keyring', KeyringMock()) - netrc_calls = [] - - class Netrc(object): - def authenticators(self, hostname): - netrc_calls.append(hostname) - return None - - monkeypatch.setattr('netrc.netrc', Netrc) monkeypatch.setattr('getpass.getpass', blow_up) _password = utils.get_password(username, resource) assert _password == password - assert netrc_calls == [hostname] def test_get_password_from_prompt(): @@ -137,6 +128,37 @@ def test_get_password_from_prompt(): ] +def test_set_keyring_password(monkeypatch): + class KeyringMock(object): + def get_password(self, resource, username): + assert resource == utils.password_key_prefix + 'example.com' + assert username == 'foouser' + return None + + def set_password(self, resource, username, password): + assert resource == utils.password_key_prefix + 'example.com' + assert username == 'foouser' + assert password == 'hunter2' + + monkeypatch.setattr(utils, 'keyring', KeyringMock()) + + @doubleclick.click.command() + @doubleclick.click.pass_context + def fake_app(ctx): + ctx.obj = {} + x = utils.get_password('foouser', 'http://example.com/a/b') + click.echo('password is ' + x) + + runner = CliRunner() + result = runner.invoke(fake_app, input='hunter2\ny\n') + assert not result.exception + assert result.output == ( + 'Server password for foouser at host example.com: \n' + 'Save this password in the keyring? [y/N]: y\n' + 'password is hunter2' + ) + + def test_get_password_from_cache(monkeypatch): user = 'my_user' resource = 'http://example.com'