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
|
import getpass
import itertools
import sys
from unittest import mock
import pytest
from keyring import cli
from keyring import credentials
flatten = itertools.chain.from_iterable
class PasswordEmitter:
"""
Replacement for getpass() to emit passwords:
>>> pe = PasswordEmitter('foo', 'bar')
>>> pe()
'foo'
>>> pe()
'bar'
>>> pe()
'foo'
"""
def __init__(self, *passwords):
self.passwords = flatten(itertools.repeat(passwords))
def __call__(self, unused_prompt=None):
return next(self.passwords)
@pytest.fixture
def mocked_set():
with mock.patch('keyring.cli.set_password') as set_password:
yield set_password
@pytest.fixture
def mocked_get_credential():
with mock.patch('keyring.cli.get_credential') as get_credential:
yield get_credential
def test_set_interactive(monkeypatch, mocked_set):
tool = cli.CommandLineTool()
tool.service = 'svc'
tool.username = 'usr'
monkeypatch.setattr(sys.stdin, 'isatty', lambda: True)
monkeypatch.setattr(getpass, 'getpass', PasswordEmitter('foo123'))
tool.do_set()
mocked_set.assert_called_once_with('svc', 'usr', 'foo123')
def test_set_pipe(monkeypatch, mocked_set):
tool = cli.CommandLineTool()
tool.service = 'svc'
tool.username = 'usr'
monkeypatch.setattr(sys.stdin, 'isatty', lambda: False)
monkeypatch.setattr(sys.stdin, 'read', lambda: 'foo123')
tool.do_set()
mocked_set.assert_called_once_with('svc', 'usr', 'foo123')
def test_set_pipe_newline(monkeypatch, mocked_set):
tool = cli.CommandLineTool()
tool.service = 'svc'
tool.username = 'usr'
monkeypatch.setattr(sys.stdin, 'isatty', lambda: False)
monkeypatch.setattr(sys.stdin, 'read', lambda: 'foo123\n')
tool.do_set()
mocked_set.assert_called_once_with('svc', 'usr', 'foo123')
@pytest.mark.parametrize('format', ['json', 'plain'])
def test_get_anonymous(monkeypatch, mocked_get_credential, format, capsys):
mocked_get_credential.return_value = credentials.AnonymousCredential('s3cret')
tool = cli.CommandLineTool()
tool.service = 'svc'
tool.username = None
tool.get_mode = 'creds'
tool.output_format = format
tool.do_get()
assert 's3cret' in capsys.readouterr().out
@pytest.mark.parametrize('format', ['json', 'plain'])
def test_get(monkeypatch, mocked_get_credential, format, capsys):
mocked_get_credential.return_value = credentials.SimpleCredential('alice', 's3cret')
tool = cli.CommandLineTool()
tool.service = 'svc'
tool.username = 'alice'
tool.get_mode = 'creds'
tool.output_format = format
tool.do_get()
assert 's3cret' in capsys.readouterr().out
|