From 13b7d1c1dffd22ba19081c64236e5622e1bbbfd6 Mon Sep 17 00:00:00 2001 From: Manuel Ebert Date: Thu, 17 Oct 2013 16:26:49 -0700 Subject: [PATCH] Tests for storing password in keychain --- features/data/configs/multiple.json | 1 + features/encryption.feature | 13 +++++++++++-- features/steps/core.py | 26 +++++++++++++++++++------- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/features/data/configs/multiple.json b/features/data/configs/multiple.json index af7a3e15..980c9353 100644 --- a/features/data/configs/multiple.json +++ b/features/data/configs/multiple.json @@ -9,6 +9,7 @@ "password": "", "journals": { "default": "features/journals/simple.journal", + "simple": "features/journals/simple.journal", "work": "features/journals/work.journal", "ideas": "features/journals/nothing.journal" }, diff --git a/features/encryption.feature b/features/encryption.feature index f28660b6..72749f23 100644 --- a/features/encryption.feature +++ b/features/encryption.feature @@ -9,15 +9,24 @@ Scenario: Decrypting a journal Given we use the config "encrypted.json" When we run "jrnl --decrypt" and enter "bad doggie no biscuit" + Then the config for journal "default" should have "encrypt" set to "bool:False" Then we should see the message "Journal decrypted" and the journal should have 2 entries - and the config should have "encrypt" set to "bool:False" Scenario: Encrypting a journal Given we use the config "basic.json" When we run "jrnl --encrypt" and enter "swordfish" Then we should see the message "Journal encrypted" - and the config should have "encrypt" set to "bool:True" + and the config for journal "default" should have "encrypt" set to "bool:True" When we run "jrnl -n 1" and enter "swordfish" Then we should see the message "Password" and the output should contain "2013-06-10 15:40 Life is good" + + Scenario: Storing a password in Keychain + Given we use the config "multiple.json" + When we run "jrnl simple --encrypt" and enter "sabertooth" + When we set the keychain password of "simple" to "sabertooth" + Then the config for journal "simple" should have "encrypt" set to "bool:True" + When we run "jrnl simple -n 1" + Then we should not see the message "Password" + and the output should contain "2013-06-10 15:40 Life is good" diff --git a/features/steps/core.py b/features/steps/core.py index da39387f..83a14017 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -5,6 +5,7 @@ import os import sys import json import pytz +import keyring try: from io import StringIO except ImportError: @@ -34,11 +35,11 @@ def read_journal(journal_name="default"): def open_journal(journal_name="default"): with open(jrnl.CONFIG_PATH) as config_file: config = json.load(config_file) - journals = config['journals'] - if type(journals) is dict: # We can override the default config on a by-journal basis - config['journal'] = journals.get(journal_name) - else: # But also just give them a string to point to the journal file - config['journal'] = journal + journal_conf = config['journals'][journal_name] + if type(journal_conf) is dict: # We can override the default config on a by-journal basis + config.update(journal_conf) + else: # But also just give them a string to point to the journal file + config['journal'] = journal_conf return Journal.Journal(**config) @given('we use the config "{config_file}"') @@ -68,6 +69,10 @@ def run(context, command): except SystemExit as e: context.exit_status = e.code +@when('we set the keychain password of "{journal}" to "{password}"') +def set_keychain(context, journal, password): + keyring.set_password('jrnl', journal, password) + @then('we should get an error') def has_error(context): assert context.exit_status != 0, context.exit_status @@ -134,6 +139,11 @@ def check_message(context, text): out = context.messages.getvalue() assert text in out, [text, out] +@then('we should not see the message "{text}"') +def check_not_message(context, text): + out = context.messages.getvalue() + assert text not in out, [text, out] + @then('the journal should contain "{text}"') @then('journal "{journal_name}" should contain "{text}"') def check_journal_content(context, text, journal_name="default"): @@ -148,7 +158,8 @@ def journal_doesnt_exist(context, journal_name="default"): assert not os.path.exists(journal_path) @then('the config should have "{key}" set to "{value}"') -def config_var(context, key, value): +@then('the config for journal "{journal}" should have "{key}" set to "{value}"') +def config_var(context, key, value, journal=None): t, value = value.split(":") value = { "bool": lambda v: v.lower() == "true", @@ -157,6 +168,8 @@ def config_var(context, key, value): }[t](value) with open(jrnl.CONFIG_PATH) as config_file: config = json.load(config_file) + if journal: + config = config["journals"][journal] assert key in config assert config[key] == value @@ -171,4 +184,3 @@ def check_journal_content(context, number, journal_name="default"): @then('fail') def debug_fail(context): assert False -