From c73e9da442e3627ba3d395983743ea541c1d0f8e Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 19 Nov 2023 18:23:39 -0500 Subject: [PATCH] Add test for do_set --- tests/test_cli.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/test_cli.py diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 00000000..0af97349 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,67 @@ +import itertools +import getpass +import sys +from unittest import mock + +import pytest + +from keyring import cli + + +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 + + +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() + assert mocked_set.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() + assert mocked_set.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() + assert mocked_set.called_once_with('svc', 'usr', 'foo123')