From cf369f3f84a7057ab74c9f984b4bc5696837f80d Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 26 Sep 2020 14:52:31 -0700 Subject: [PATCH] Added ability to auto-prompt for password for encrypted journals Only uses password when prompted, and doesn't get in the way of other input prompts. This allows us to run the same scenarios on both encrypted journals and other journal types. Co-authored-by: Micah Jerome Ellison --- features/data/configs/basic_encrypted.yaml | 2 +- features/delete.feature | 4 ++- features/steps/core.py | 39 ++++++++++++++++++---- features/upgrade.feature | 2 +- features/write.feature | 23 +++---------- 5 files changed, 42 insertions(+), 28 deletions(-) diff --git a/features/data/configs/basic_encrypted.yaml b/features/data/configs/basic_encrypted.yaml index c7bdae6a..1d9e5710 100644 --- a/features/data/configs/basic_encrypted.yaml +++ b/features/data/configs/basic_encrypted.yaml @@ -6,7 +6,7 @@ colors: default_hour: 9 default_minute: 0 editor: "" -encrypt: false +encrypt: true highlight: true journals: default: features/journals/basic_encrypted.journal diff --git a/features/delete.feature b/features/delete.feature index fc117f05..df51a494 100644 --- a/features/delete.feature +++ b/features/delete.feature @@ -1,7 +1,8 @@ Feature: Delete entries from journal Scenario Outline: Delete flag allows deletion of single entry Given we use the config ".yaml" - When we run "jrnl -n 1" + And we use the password "test" if prompted + When we run "jrnl -1" Then the output should contain "2020-09-24 09:14 The third entry finally" When we run "jrnl --delete" and enter """ @@ -20,6 +21,7 @@ Feature: Delete entries from journal Examples: Configs | config | | basic_onefile | + | basic_encrypted | # | basic_folder | @todo # | basic_dayone | @todo diff --git a/features/steps/core.py b/features/steps/core.py index 43a3bad7..28093415 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -130,6 +130,16 @@ def set_config(context, config_file): cf.write("version: {}".format(__version__)) +@given('we use the password "{password}" if prompted') +def use_password_forever(context, password): + context.password = password + + +@given('we use the password "{password}" {num:d} times if prompted') +def use_password(context, password, num=1): + context.password = iter([password] * num) + + @given("we have a keyring") def set_keyring(context): keyring.set_keyring(TestKeyring()) @@ -210,8 +220,9 @@ def matches_editor_arg(context, regex): def _mock_getpass(inputs): - def prompt_return(prompt="Password: "): - print(prompt) + def prompt_return(prompt=""): + if type(inputs) == str: + return inputs try: return next(inputs) except StopIteration: @@ -251,11 +262,16 @@ def run_with_input(context, command, inputs=""): context.editor_file = tmpfile Path(tmpfile).touch() + if "password" in context: + password = context.password + else: + password = text + # fmt: off # see: https://github.com/psf/black/issues/664 with \ patch("builtins.input", side_effect=_mock_input(text)) as mock_input, \ - patch("getpass.getpass", side_effect=_mock_getpass(text)) as mock_getpass, \ + patch("getpass.getpass", side_effect=_mock_getpass(password)) as mock_getpass, \ patch("sys.stdin.read", side_effect=text) as mock_read, \ patch("subprocess.call", side_effect=_mock_editor) as mock_editor \ : @@ -323,12 +339,23 @@ def run(context, command, text="", cache_dir=None): def _mock_editor(command): context.editor_command = command + if "password" in context: + password = context.password + else: + password = iter(text) + try: - with patch("sys.argv", args), patch( - "subprocess.call", side_effect=_mock_editor - ), patch("sys.stdin.read", side_effect=lambda: text): + # fmt: off + # see: https://github.com/psf/black/issues/664 + with \ + patch("sys.argv", args), \ + patch("getpass.getpass", side_effect=_mock_getpass(password)), \ + patch("subprocess.call", side_effect=_mock_editor), \ + patch("sys.stdin.read", side_effect=lambda: text) \ + : cli(args[1:]) context.exit_status = 0 + # fmt: on except SystemExit as e: context.exit_status = e.code diff --git a/features/upgrade.feature b/features/upgrade.feature index b8452894..7de61362 100644 --- a/features/upgrade.feature +++ b/features/upgrade.feature @@ -21,7 +21,7 @@ Feature: Upgrading Journals from 1.x.x to 2.x.x bad doggie no biscuit bad doggie no biscuit """ - Then the output should contain "Password" + Then we should be prompted for a password And the output should contain "2013-06-10 15:40 Life is good" Scenario: Upgrading a config without colors to colors diff --git a/features/write.feature b/features/write.feature index 37cb1cd7..8ee47a51 100644 --- a/features/write.feature +++ b/features/write.feature @@ -93,6 +93,7 @@ Feature: Writing new entries. Scenario Outline: Writing an empty entry from the editor should yield nothing Given we use the config ".yaml" + And we use the password "bad doggie no biscuit" if prompted When we run "jrnl" and enter nothing Then the output should be empty And the error output should contain "[Nothing saved to file]" @@ -103,13 +104,7 @@ Feature: Writing new entries. | editor | | editor_empty_folder | | dayone | - - Scenario: Writing an empty entry from the editor should yield nothing in encrypted journal - Given we use the config "editor_encrypted.yaml" - When we run "jrnl" and enter "bad doggie no biscuit" - Then the output should be "Password:" - And the error output should contain "[Nothing saved to file]" - And the editor should have been called + | editor_encrypted | Scenario Outline: Writing an entry does not print the entire journal # https://github.com/jrnl-org/jrnl/issues/87 @@ -184,6 +179,7 @@ Feature: Writing new entries. Scenario Outline: Writing an entry at the prompt (no editor) should store the entry Given we use the config ".yaml" + And we use the password "bad doggie no biscuit" if prompted When we run "jrnl" and enter "25 jul 2013: I saw Elvis. He's alive." Then we should get no error When we run "jrnl -on '2013-07-25'" @@ -194,22 +190,11 @@ Feature: Writing new entries. | config_file | | simple | | empty_folder | + | encrypted | @todo # Need to test DayOne w/out an editor Scenario: Writing an entry at the prompt (no editor) in DayOne journal - Scenario: Writing an entry at the prompt (no editor) in encrypted journal - Given we use the config "encrypted.yaml" - When we run "jrnl" and enter - """ - bad doggie no biscuit - 25 jul 2013: I saw Elvis. He's alive. - """ - Then we should get no error - When we run "jrnl -on '2013-07-25'" and enter "bad doggie no biscuit" - Then the output should contain "2013-07-25 09:00 I saw Elvis." - And the output should contain "| He's alive." - Scenario: Writing into Dayone Given we use the config "dayone.yaml" When we run "jrnl 01 may 1979: Being born hurts."