From 6b27126c37a460fffdf375c887db181231d1f91d Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Tue, 2 Mar 2021 21:09:33 -0800 Subject: [PATCH] Implement password prompt steps in pytest-bdd - Scaffold some tests that will be added later - Add fixtures for journal name and keyring type - Add "we should be prompted for a password" step - Add "we should not be prompted for a password" step Co-authored-by: Micah Jerome Ellison --- tests/features/password.feature | 38 +++++++++++++++++++++++++++++++++ tests/step_defs/conftest.py | 38 +++++++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/tests/features/password.feature b/tests/features/password.feature index cccb94ab..c8d885f6 100644 --- a/tests/features/password.feature +++ b/tests/features/password.feature @@ -22,3 +22,41 @@ Feature: Using the installed keyring Then we should get no error And the output should not contain "Failed to retrieve keyring" + + Scenario: Encrypt journal with no keyring backend and do store in keyring + Given we use the config "simple.yaml" + When we run "jrnl test entry" + And we run "jrnl --encrypt" and enter + password + password + y + Then we should get no error + And the output should not contain "Failed to retrieve keyring" + # @todo add step to check contents of keyring + + + @todo + Scenario: Open an encrypted journal with wrong password in keyring + # This should ask the user for the password after the keyring fails + + + @todo + Scenario: Decrypt journal with password in keyring + + + @todo + Scenario: Decrypt journal without a keyring + + + Scenario: Encrypt journal when keyring exists but fails + Given we use the config "simple.yaml" + And we have a failed keyring + When we run "jrnl --encrypt" and enter + this password will not be saved in keyring + this password will not be saved in keyring + y + Then we should see the message "Failed to retrieve keyring" + And we should get no error + And we should be prompted for a password + And the config for journal "default" should have "encrypt" set to "bool:True" + diff --git a/tests/step_defs/conftest.py b/tests/step_defs/conftest.py index 2544c470..e8309b69 100644 --- a/tests/step_defs/conftest.py +++ b/tests/step_defs/conftest.py @@ -133,15 +133,28 @@ def keyring(): set_keyring(NoKeyring()) +@fixture +def keyring_type(): + return "default" + + @fixture def config_data(config_path): return load_config(config_path) +@fixture +def journal_name(): + return None + # ----- STEPS ----- # @given("we have a keyring", target_fixture="keyring") -def we_have_keyring(): - set_keyring(FailedKeyring()) +@given(parse("we have a {keyring_type} keyring"), target_fixture="keyring") +def we_have_type_of_keyring(keyring_type): + if keyring_type == "failed": + set_keyring(FailedKeyring()) + else: + set_keyring(TestKeyring()) @given(parse('we use the config "{config_file}"'), target_fixture="config_path") @@ -270,15 +283,26 @@ def should_see_the_message(text, cli_run): assert text in out, [text, out] -@then(parse('the config should have "{key}" set to')) +@then(parse('the config should have "{key}" set to\n{value}')) @then(parse('the config should have "{key}" set to "{value}"')) -@then(parse('the config for journal "{journal}" should have "{key}" set to "{value}"')) -def config_var(config_data, key, value="", journal=None): +@then(parse('the config for journal "{journal_name}" should have "{key}" set to "{value}"')) +def config_var(config_data, key, value, journal_name): value = read_value_from_string(value) configuration = config_data - if journal: - configuration = configuration["journals"][journal] + if journal_name: + configuration = configuration["journals"][journal_name] assert key in configuration assert configuration[key] == value + + +@then("we should be prompted for a password") +def password_was_called(cli_run): + assert cli_run["mocks"]["getpass"].called + + +@then("we should not be prompted for a password") +def password_was_not_called(cli_run): + assert not cli_run["mocks"]["getpass"].called +