commit 5ca2713b2fdfec612275d55ba6e8ff4673bf3281
parent 107da8936f54929adbdc1600d55aacb8b96aeb91
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date: Tue, 29 Oct 2024 12:29:09 +0000
Generate command is fully covered by integrated suites
Diffstat:
2 files changed, 123 insertions(+), 1 deletion(-)
diff --git a/spec/pashage_extra_spec.sh b/spec/pashage_extra_spec.sh
@@ -138,11 +138,14 @@ Describe 'Integrated Command Functions'
AfterEach cleanup
cat() { @cat "$@"; }
+ dd() { @dd "$@"; }
diff() { @diff "$@"; }
dirname() { @dirname "$@"; }
git() { @git "$@"; }
+ mkdir() { @mkdir "$@"; }
mktemp() { @mktemp "$@"; }
rm() { @rm "$@"; }
+ tr() { @tr "$@"; }
platform_tmpdir() {
SECURE_TMPDIR="${SHELLSPEC_WORKDIR}/secure"
@@ -328,7 +331,63 @@ Describe 'Integrated Command Functions'
End
End
-# Describe 'cmd_generate'
+ Describe 'cmd_generate'
+ DECISION=default
+ OVERWRITE=no
+ SHOW=text
+
+ random_chars() { %- 0123456789 ; }
+
+ It 'overwrites after asking for confirmation'
+ expected_out() { %text
+ #|An entry already exists for subdir/file. Overwrite it? [y/n](B)The generated password for (U)subdir/file(!U) is:(N)
+ #|0123456789
+ }
+ Data 'y'
+ When call cmd_generate subdir/file 10
+ The status should be success
+ The output should equal "$(expected_out)"
+ The error should be blank
+ expected_file() { %text:expand
+ #|ageRecipient:myself
+ #|age:0123456789
+ }
+ The contents of file "${PREFIX}/subdir/file.age" should \
+ equal "$(expected_file)"
+ expected_log() { %text
+ #|Add generated password for subdir/file.
+ #|
+ #| subdir/file.age | 2 +-
+ #| 1 file changed, 1 insertion(+), 1 deletion(-)
+ setup_log
+ }
+ The result of function check_git_log should be successful
+ End
+
+ It 'does nothing without confirmation'
+ Data 'n'
+ When call cmd_generate subdir/file 10
+ The status should be success
+ The output should equal \
+ 'An entry already exists for subdir/file. Overwrite it? [y/n]'
+ The error should be blank
+ The result of function check_git_log should be successful
+ End
+
+ It 'cannot overwrite a directory'
+ run_test() {
+ mkdir -p "${PREFIX}/new-secret.age" && \
+ cmd_generate -f new-secret 10
+ }
+ When run run_test
+ The status should equal 1
+ The output should be blank
+ The error should equal 'Cannot replace directory new-secret.age'
+ The result of function git_log should be successful
+ The contents of file "${GITLOG}" should equal "$(setup_log)"
+ End
+ End
+
# Describe 'cmd_git'
# Describe 'cmd_grep'
# Describe 'cmd_gitconfig'
diff --git a/spec/pass_spec.sh b/spec/pass_spec.sh
@@ -1515,6 +1515,29 @@ Describe 'Pass-like command'
The contents of file "${GITLOG}" should equal "$(expected_log $3)"
End
+ It 'reports a generation error'
+ Mock tr
+ if [ "$1" = '-dc' ]; then
+ %putsn '0123456789'
+ else
+ @tr "$@"
+ fi
+ End
+ expected_err() {
+ if [ "$1" = pashage ]; then
+ %putsn 'Error while generating password: 10/12 bytes read'
+ else
+ %putsn 'Could not generate password from /dev/urandom.'
+ fi
+ }
+ When run script $1 generate -f new-secret 12
+ The status should equal 1
+ The output should be blank
+ The error should equal "$(expected_err $2)"
+ The result of function git_log should be successful
+ The contents of file "${GITLOG}" should equal "$(setup_log)"
+ End
+
It 'displays usage when called without argument'
Skip if 'pass(age) needs bash' check_skip $2
When run script $1 generate
@@ -1526,6 +1549,46 @@ Describe 'Pass-like command'
The contents of file "${GITLOG}" should equal "$(setup_log)"
End
+ It 'displays usage when called with incompatible display options'
+ When run script $1 generate --clip --qrcode new-secret
+ The status should equal 1
+ The output should be blank
+ The error should include 'Usage:'
+ The error should include ' generate '
+ The result of function git_log should be successful
+ The contents of file "${GITLOG}" should equal "$(setup_log)"
+ End
+
+ It 'displays usage when called with reversed incompatible display options'
+ When run script $1 generate --qrcode --clip new-secret
+ The status should equal 1
+ The output should be blank
+ The error should include 'Usage:'
+ The error should include ' generate '
+ The result of function git_log should be successful
+ The contents of file "${GITLOG}" should equal "$(setup_log)"
+ End
+
+ It 'displays usage when called with incompatible overwriting options'
+ When run script $1 generate --force --inplace new-secret
+ The status should equal 1
+ The output should be blank
+ The error should include 'Usage:'
+ The error should include ' generate '
+ The result of function git_log should be successful
+ The contents of file "${GITLOG}" should equal "$(setup_log)"
+ End
+
+ It 'displays usage when called with reversed incompatible overwriting opt'
+ When run script $1 generate --inplace --force new-secret
+ The status should equal 1
+ The output should be blank
+ The error should include 'Usage:'
+ The error should include ' generate '
+ The result of function git_log should be successful
+ The contents of file "${GITLOG}" should equal "$(setup_log)"
+ End
+
It 'rejects a path containing ..'
Skip if 'pass(age) needs bash' check_skip $2
When run script $1 generate subdir/../new-secret