From 580ecad331fe6c4877ffea52f5c7a950bd2a4bf5 Mon Sep 17 00:00:00 2001 From: Andreas Schmidt Date: Fri, 18 Oct 2024 09:36:39 +0200 Subject: [PATCH] tests: add tests for hotkey handling Signed-off-by: Andreas Schmidt Signed-off-by: Andreas Schmidt --- tests/test_args | 43 ++++++++++++--------- tests/test_get_field | 16 ++++++++ tests/test_get_passname | 84 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 18 deletions(-) diff --git a/tests/test_args b/tests/test_args index 4441afb..7e0f6ae 100644 --- a/tests/test_args +++ b/tests/test_args @@ -2,12 +2,16 @@ argument_testing() { local -r short_arg="${1}" local -r long_arg="${2}" local -r flag_name="${3}" - local -r cmd_arg="${4:-""}" + local -r expected_value="${4}" + local -r cmd_arg="${5:-""}" + local init_value=0 - flags=("FLAG_AUTOTYPE" "FLAG_COPY" "FLAG_FILEISUSER" "FLAG_HELP" "FLAG_SQUASH" "FLAG_TYPE") + flags=("FLAG_AUTOTYPE" "FLAG_COPY" "FLAG_FILEISUSER" "FLAG_HELP" "FLAG_SQUASH" "FLAG_TYPE" "FLAG_HOTKEYS_SUPPORTED") + + [ ${expected_value} -eq 0 ] && init_value=1 for flag in "${flags[@]}"; do - declare "${flag}"=0 + declare "${flag}"=${init_value} done CMD_COPY="wl-copy" CMD_TYPE="wtype -" @@ -16,53 +20,56 @@ argument_testing() { [[ "${flag_name}" == "FLAG_COPY" ]] && EXPECTED_CMD_COPY="${cmd_arg:-"wl-copy"}" [[ "${flag_name}" == "FLAG_TYPE" ]] && EXPECTED_CMD_TYPE="${cmd_arg:-"wtype -"}" - + parse_arguments "-${short_arg}${cmd_arg}" - assert_equals 1 ${!flag_name} "args: ${flag_name} was not set" + assert_equals ${expected_value} ${!flag_name} "args: ${flag_name} was not set" - declare "${flag_name}"=0 + declare "${flag_name}"=${init_value} arg="${long_arg}" [ -n "${cmd_arg}" ] && arg="${long_arg}=${cmd_arg}" parse_arguments "--${arg}" - assert_equals 1 ${!flag_name} "args: ${flag_name} was not set" + assert_equals ${expected_value} ${!flag_name} "args: ${flag_name} was not set" for flag in "${flags[@]}"; do - value=0 + value=${init_value} if [[ "${flag_name}" == "${flag}" ]]; then - value=1 + value=${expected_value} fi assert_equals ${value} ${!flag} "args: ${flag} is not ${value}" - done assert_equals "${EXPECTED_CMD_COPY}" "${CMD_COPY}" "args: CMD_COPY is not \"${EXPECTED_CMD_COPY}\"" assert_equals "${EXPECTED_CMD_TYPE}" "${CMD_TYPE}" "args: CMD_TYPE is not \"${EXPECTED_CMD_TYPE}\"" } test_args_autotype() { - argument_testing "a" "autotype" "FLAG_AUTOTYPE" + argument_testing "a" "autotype" "FLAG_AUTOTYPE" 1 } test_args_copy() { - argument_testing "c" "copy" "FLAG_COPY" - argument_testing "c" "copy" "FLAG_COPY" "new-copy-cmd" + argument_testing "c" "copy" "FLAG_COPY" 1 + argument_testing "c" "copy" "FLAG_COPY" 1 "new-copy-cmd" } test_args_fileisuser() { - argument_testing "f" "fileisuser" "FLAG_FILEISUSER" + argument_testing "f" "fileisuser" "FLAG_FILEISUSER" 1 } test_args_help() { - argument_testing "h" "help" "FLAG_HELP" + argument_testing "h" "help" "FLAG_HELP" 1 } test_args_squash() { - argument_testing "s" "squash" "FLAG_SQUASH" + argument_testing "s" "squash" "FLAG_SQUASH" 1 } test_args_type() { - argument_testing "t" "type" "FLAG_TYPE" - argument_testing "t" "type" "FLAG_TYPE" "new-type-cmd" + argument_testing "t" "type" "FLAG_TYPE" 1 + argument_testing "t" "type" "FLAG_TYPE" 1 "new-type-cmd" +} + +test_args_nohotkey() { + argument_testing "k" "nohotkey" "FLAG_HOTKEYS_SUPPORTED" 0 } test_args_unknown() { diff --git a/tests/test_get_field b/tests/test_get_field index d39cd36..9b50a8e 100644 --- a/tests/test_get_field +++ b/tests/test_get_field @@ -42,6 +42,22 @@ test_get_field_mulitfields() { local output="$(get_field_from_menu "passname")" assert_equals "${fields[${select_field}]}" "${output}" "get field: unexpected output" + output="$(get_field_from_menu "passname" ${HOTKEY_USERNAME_RET})" + assert_equals "username" "${output}" "get field: unexpected hotkey output" + + output="$(get_field_from_menu "passname" ${HOTKEY_PASSWORD_RET})" + assert_equals "password" "${output}" "get field: unexpected hotkey output" + + output="$(get_field_from_menu "passname" ${HOTKEY_AUTOTYPE_RET})" + assert_equals "${fields[${select_field}]}" "${output}" "get field: unexpected hotkey output" + + FLAG_TYPE=1 + output="$(get_field_from_menu "passname" ${HOTKEY_AUTOTYPE_RET})" + assert_equals "autotype" "${output}" "get field: unexpected hotkey output" + + output="$(get_field_from_menu "passname" ${HOTKEY_OTP_RET})" + assert_equals "${FIELD_OTP_TAG}" "${output}" "get field: unexpected hotkey output" + select_field=2 fake _wofi fake_wofi ${select_field} output="$(get_field_from_menu "passname")" diff --git a/tests/test_get_passname b/tests/test_get_passname index ddf090d..adfa7a7 100644 --- a/tests/test_get_passname +++ b/tests/test_get_passname @@ -4,6 +4,8 @@ fake_wofi() { input+=("${line}") done echo "${input[${1}]}" + + return ${2:-0} } fake_find() { @@ -36,10 +38,92 @@ test_passname_change_pass_store_dir() { local -r items=("item0" "item1" "item2") fake find fake_find "${PASSWORD_STORE_DIR}" "${items[@]}" fake _wofi fake_wofi ${select_item} + local output="$(get_passname_from_menu)" assert_equals "${items[${select_item}]}" "${output}" "passname: not find valid item" } +test_passname_return_hotkey() { + local output + local ret + local -r select_item=1 + local -r items=("item0" "item1" "item2") + fake find fake_find "${HOME}/.password-store" "${items[@]}" + + hotkeys_ret=() + hotkeys_ret+=(${HOTKEY_USERNAME_RET}) + hotkeys_ret+=(${HOTKEY_PASSWORD_RET}) + hotkeys_ret+=(${HOTKEY_AUTOTYPE_RET}) + hotkeys_ret+=(${HOTKEY_OTP_RET}) + + for hotkey_ret in "${hotkeys_ret[@]}"; do + fake _wofi fake_wofi ${select_item} ${hotkey_ret} + ret=0 + output="$(get_passname_from_menu)" || ret=${?} + assert_equals "${items[${select_item}]}" "${output}" "passname: not find valid item" + assert_equals ${hotkey_ret} ${ret} "passname: return value not expected" + done +} + +wofi_args() { + echo "${FAKE_PARAMS[@]}" +} + +is_hotkey_set() { + local regex=".*key_custom_${2}.*" + [[ "${1}" =~ ${regex} ]] && return 0 || return 1 +} + +test_passname_hotkey_args() { + local result + fake find echo "" + fake _wofi wofi_args + local output="$(get_passname_from_menu)" + + result=true + for key in {0..3}; do + is_hotkey_set "${output}" "${key}" || result=false + done + assert ${result} "passname: hotkey argument not found" + + HOTKEY_USERNAME="" + output="$(get_passname_from_menu)" + result=true + is_hotkey_set "${output}" "$((HOTKEY_USERNAME_RET - 10))" && result=false + is_hotkey_set "${output}" "$((HOTKEY_PASSWORD_RET - 10))" || result=false + is_hotkey_set "${output}" "$((HOTKEY_AUTOTYPE_RET - 10))" || result=false + is_hotkey_set "${output}" "$((HOTKEY_OTP_RET - 10))" || result=false + assert ${result} "passname: hotkey argument not found" + + HOTKEY_AUTOTYPE="" + output="$(get_passname_from_menu)" + result=true + is_hotkey_set "${output}" "$((HOTKEY_USERNAME_RET - 10))" && result=false + is_hotkey_set "${output}" "$((HOTKEY_PASSWORD_RET - 10))" || result=false + is_hotkey_set "${output}" "$((HOTKEY_AUTOTYPE_RET - 10))" && result=false + is_hotkey_set "${output}" "$((HOTKEY_OTP_RET - 10))" || result=false + assert ${result} "passname: hotkey argument not found" + + HOTKEY_OTP="" + output="$(get_passname_from_menu)" + result=true + is_hotkey_set "${output}" "$((HOTKEY_USERNAME_RET - 10))" && result=false + is_hotkey_set "${output}" "$((HOTKEY_PASSWORD_RET - 10))" || result=false + is_hotkey_set "${output}" "$((HOTKEY_AUTOTYPE_RET - 10))" && result=false + is_hotkey_set "${output}" "$((HOTKEY_OTP_RET - 10))" && result=false + assert ${result} "passname: hotkey argument not found" + + HOTKEY_PASSWORD="" + output="$(get_passname_from_menu)" + result=true + is_hotkey_set "${output}" "$((HOTKEY_USERNAME_RET - 10))" && result=false + is_hotkey_set "${output}" "$((HOTKEY_PASSWORD_RET - 10))" && result=false + is_hotkey_set "${output}" "$((HOTKEY_AUTOTYPE_RET - 10))" && result=false + is_hotkey_set "${output}" "$((HOTKEY_OTP_RET - 10))" && result=false + assert ${result} "passname: hotkey argument not found" + +} + setup_suite() { WOFI_PASS_TESTING="1" source ../wofi-pass