diff --git a/tests/lib/all.sh b/tests/lib/all.sh index 0fa22f6c..ba246f89 100644 --- a/tests/lib/all.sh +++ b/tests/lib/all.sh @@ -16,4 +16,5 @@ . "$1/populate.sh" || exit 7 . "$1/installed-state.sh" || exit 8 +. "$1/totp.sh" || exit 9 diff --git a/tests/lib/totp.sh b/tests/lib/totp.sh new file mode 100644 index 00000000..4e419597 --- /dev/null +++ b/tests/lib/totp.sh @@ -0,0 +1,21 @@ +# +# requires: +# mailinabox's python installed with pyotp module at +# /usr/local/lib/mailinabox/env +# + +totp_current_token() { + # given a secret, get the current token + # token written to stdout + # error message to stderr + # return 0 if successful + # non-zero if an error occured + local secret="$1" + /usr/local/lib/mailinabox/env/bin/python -c "import pyotp; totp=pyotp.TOTP(r'$secret'); print(totp.now());" + if [ $? -ne 0 ]; then + return 1 + else + return 0 + fi +} + diff --git a/tests/suites/_mgmt-functions.sh b/tests/suites/_mgmt-functions.sh index 466275cf..a384481e 100644 --- a/tests/suites/_mgmt-functions.sh +++ b/tests/suites/_mgmt-functions.sh @@ -195,9 +195,9 @@ mgmt_get_totp_token() { local count=0 while [ -z "$TOTP_TOKEN" -a $count -lt 10 ]; do - TOTP_TOKEN="$(/usr/local/lib/mailinabox/env/bin/python -c "import pyotp; totp=pyotp.TOTP(r'$secret'); print(totp.now());" 2>>"$TEST_OF")" + TOTP_TOKEN="$(totp_current_token "$secret" 2>>"$TEST_OF")" if [ $? -ne 0 ]; then - record "Failed: Could not generate a TOTP token !" + record "Failed: Could not get the TOTP token !" return 1 fi