From d99fd1968a3f714428161ae7ae1bc40ab65cefd5 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 13 Jan 2021 12:23:46 +0100 Subject: dataclasses: Be more explicit about FakeOutput See #6023 --- tests/unit/misc/userscripts/test_qute_lastpass.py | 56 +++++++++++------------ 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/tests/unit/misc/userscripts/test_qute_lastpass.py b/tests/unit/misc/userscripts/test_qute_lastpass.py index bcff913d7..14f7e8e4a 100644 --- a/tests/unit/misc/userscripts/test_qute_lastpass.py +++ b/tests/unit/misc/userscripts/test_qute_lastpass.py @@ -43,13 +43,14 @@ default_lpass_match = [ @dataclasses.dataclass class FakeOutput: - stdout: str = '' - stderr: str = '' - def __post_init__(self): - # FIXME - self.stdout = self.stdout.encode() - self.stderr = self.stderr.encode() + stdout: bytes = b'' + stderr: bytes = b'' + + @classmethod + def json(cls, obj): + """Get a FakeOutput for a json-encoded object.""" + return cls(stdout=json.dumps(obj).encode('ascii')) @pytest.fixture @@ -101,7 +102,7 @@ class TestQuteLastPassComponents: "2345 | example2.com | https://www.example2.com | jane.doe@example.com", ] - subprocess_mock.return_value = FakeOutput(stdout=entries[1]) + subprocess_mock.return_value = FakeOutput(stdout=entries[1].encode('ascii')) selected = qute_lastpass.dmenu(entries, 'rofi -dmenu', 'UTF-8') @@ -114,7 +115,7 @@ class TestQuteLastPassComponents: def test_pass_subprocess_args(self, subprocess_mock): """Test if pass_ calls subprocess with correct arguments.""" - subprocess_mock.return_value = FakeOutput(stdout='[{}]') + subprocess_mock.return_value = FakeOutput(stdout=b'[{}]') qute_lastpass.pass_('example.com', 'utf-8') @@ -124,8 +125,7 @@ class TestQuteLastPassComponents: def test_pass_returns_candidates(self, subprocess_mock): """Test if pass_ returns expected lpass site entry.""" - subprocess_mock.return_value = FakeOutput( - stdout=json.dumps(default_lpass_match)) + subprocess_mock.return_value = FakeOutput.json(default_lpass_match) response = qute_lastpass.pass_('www.example.com', 'utf-8') assert response[1] == '' @@ -137,7 +137,7 @@ class TestQuteLastPassComponents: def test_pass_no_accounts(self, subprocess_mock): """Test if pass_ handles no accounts as an empty lpass result.""" - error_message = 'Error: Could not find specified account(s).' + error_message = b'Error: Could not find specified account(s).' subprocess_mock.return_value = FakeOutput(stderr=error_message) response = qute_lastpass.pass_('www.example.com', 'utf-8') @@ -146,9 +146,9 @@ class TestQuteLastPassComponents: def test_pass_returns_error(self, subprocess_mock): """Test if pass_ returns error from lpass.""" - # pylint: disable=line-too-long - error_message = 'Error: Could not find decryption key. Perhaps you need to login with `lpass login`.' - subprocess_mock.return_value = FakeOutput(stderr=error_message) + error_message = ('Error: Could not find decryption key. ' + 'Perhaps you need to login with `lpass login`.') + subprocess_mock.return_value = FakeOutput(stderr=error_message.encode('ascii')) response = qute_lastpass.pass_('www.example.com', 'utf-8') assert response[0] == [] @@ -161,8 +161,7 @@ class TestQuteLastPassMain: def test_main_happy_path(self, subprocess_mock, arguments_mock, qutecommand_mock): """Test sending username/password to qutebrowser on *single* match.""" - subprocess_mock.return_value = FakeOutput( - stdout=json.dumps(default_lpass_match)) + subprocess_mock.return_value = FakeOutput.json(default_lpass_match) arguments_mock.url = default_lpass_match[0]['url'] exit_code = qute_lastpass.main(arguments_mock) @@ -180,7 +179,7 @@ class TestQuteLastPassMain: stderr_mock, qutecommand_mock): """Test correct exit code and message returned on no entries.""" - error_message = 'Error: Could not find specified account(s).' + error_message = b'Error: Could not find specified account(s).' subprocess_mock.return_value = FakeOutput(stderr=error_message) arguments_mock.url = default_lpass_match[0]['url'] @@ -195,8 +194,8 @@ class TestQuteLastPassMain: stderr_mock, qutecommand_mock): """Test correct exit code and message on lpass failure.""" - # pylint: disable=line-too-long - error_message = 'Error: Could not find decryption key. Perhaps you need to login with `lpass login`.' + error_message = (b'Error: Could not find decryption key. ' + b'Perhaps you need to login with `lpass login`.') subprocess_mock.return_value = FakeOutput(stderr=error_message) arguments_mock.url = default_lpass_match[0]['url'] @@ -211,8 +210,7 @@ class TestQuteLastPassMain: def test_main_username_only_flag(self, subprocess_mock, arguments_mock, qutecommand_mock): """Test if --username-only flag sends username only.""" - subprocess_mock.return_value = FakeOutput( - stdout=json.dumps(default_lpass_match)) + subprocess_mock.return_value = FakeOutput.json(default_lpass_match) arguments_mock.url = default_lpass_match[0]['url'] arguments_mock.username_only = True @@ -226,8 +224,7 @@ class TestQuteLastPassMain: def test_main_password_only_flag(self, subprocess_mock, arguments_mock, qutecommand_mock): """Test if --password-only flag sends password only.""" - subprocess_mock.return_value = FakeOutput( - stdout=json.dumps(default_lpass_match)) + subprocess_mock.return_value = FakeOutput.json(default_lpass_match) arguments_mock.url = default_lpass_match[0]['url'] arguments_mock.password_only = True @@ -252,9 +249,9 @@ class TestQuteLastPassMain: } ) - lpass_response = FakeOutput(stdout=json.dumps(multiple_matches)) + lpass_response = FakeOutput.json(multiple_matches) dmenu_response = FakeOutput( - stdout='23456 | Sites/www.example.com | https://www.example.com | john.doe@fake.com') + stdout=b'23456 | Sites/www.example.com | https://www.example.com | john.doe@fake.com') subprocess_mock.side_effect = [lpass_response, dmenu_response] @@ -310,12 +307,11 @@ class TestQuteLastPassMain: } ] - fqdn_response = FakeOutput(stdout=json.dumps(fqdn_matches)) - domain_response = FakeOutput(stdout=json.dumps(domain_matches)) - no_response = FakeOutput( - stderr='Error: Could not find specified account(s).') + fqdn_response = FakeOutput.json(fqdn_matches) + domain_response = FakeOutput.json(domain_matches) + no_response = FakeOutput(stderr=b'Error: Could not find specified account(s).') dmenu_response = FakeOutput( - stdout='23456 | Sites/www.example.com | https://www.example.com | john.doe@fake.com') + stdout=b'23456 | Sites/www.example.com | https://www.example.com | john.doe@fake.com') # lpass command will return results for search against # www.example.com, example.com, but not wwwexample.com and its ipv4 -- cgit v1.2.3-54-g00ecf