From 7e947ca2cdd8e66ea49822acbad81e7d35289c0a Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Fri, 8 May 2020 15:02:07 -0300 Subject: [PATCH] Add functions for getting and comparing balances to integration tests --- integrationtests/common.sh | 23 ++++++++++++++++++++++- integrationtests/test-double-link.sh | 8 ++++---- integrationtests/test-double-spend.sh | 6 ++---- integrationtests/test-refund.sh | 13 ++++++++----- integrationtests/test-retries.sh | 14 ++++++++------ 5 files changed, 44 insertions(+), 20 deletions(-) diff --git a/integrationtests/common.sh b/integrationtests/common.sh index 57dc1a863..86158d153 100644 --- a/integrationtests/common.sh +++ b/integrationtests/common.sh @@ -13,6 +13,7 @@ function setup_config() { # TODO "taler-merchant-httpd -v" should not return an error [[ "$(taler-merchant-httpd -v)" =~ "taler-merchant-httpd v" ]] || exit_skip " MISSING" echo " FOUND" + bc -v >/dev/null >"$LOG" +} + +function assert_less_than() { + AMOUNT_1=${1//TESTKUDOS:/} + AMOUNT_2=${2//TESTKUDOS:/} + if (($(echo "$AMOUNT_1 >= $AMOUNT_2" | bc -l))); then + exit_error "$1 is not lower than $2" + fi +} + +function assert_greater_than() { + AMOUNT_1=${1//TESTKUDOS:/} + AMOUNT_2=${2//TESTKUDOS:/} + if (($(echo "$AMOUNT_1 <= $AMOUNT_2" | bc -l))); then + exit_error "$1 is not greater than $2" + fi +} + function shutdown_services() { echo "Shutting down services" jobs -p | xargs --no-run-if-empty kill || true @@ -171,7 +192,7 @@ function exit_skip() { } function exit_error() { - echo "\033[0;31mError: $1\033[0m" + echo -e "\033[0;31mError: $1\033[0m" exit 1 } diff --git a/integrationtests/test-double-link.sh b/integrationtests/test-double-link.sh index ee4af891a..d5f5d3329 100755 --- a/integrationtests/test-double-link.sh +++ b/integrationtests/test-double-link.sh @@ -7,13 +7,13 @@ normal_start_and_wait "double-link" echo "Getting pay taler:// Uri" PAY_URI=$(taler-wallet-cli testing gen-pay-uri -m "$MERCHANT_URL" -k sandbox -a "TESTKUDOS:1" -s "foo" | grep -E -o 'taler://.*') echo "Trying to pay without balance" -taler-wallet-cli --wallet-db=$WALLET_DB --no-throttle handle-uri --yes "$PAY_URI" 2>&1 | grep -q "insufficient balance" || exit_error "not reporting insufficient balance" +taler-wallet-cli --wallet-db="$WALLET_DB" --no-throttle handle-uri --yes "$PAY_URI" 2>&1 | grep -q "insufficient balance" || exit_error "not reporting insufficient balance" echo "Withdrawing" -taler-wallet-cli --wallet-db=$WALLET_DB --no-throttle testing withdraw -e "$EXCHANGE_URL" -b "$BANK_URL" -a "TESTKUDOS:10" > /dev/null +taler-wallet-cli --wallet-db="$WALLET_DB" --no-throttle testing withdraw -e "$EXCHANGE_URL" -b "$BANK_URL" -a "TESTKUDOS:10" > /dev/null echo "Trying to pay again, should work this time" -taler-wallet-cli --wallet-db=$WALLET_DB --no-throttle handle-uri --yes "$PAY_URI" > /dev/null +taler-wallet-cli --wallet-db="$WALLET_DB" --no-throttle handle-uri --yes "$PAY_URI" > /dev/null echo "Trying to pay what was paid already should throw error" -taler-wallet-cli --wallet-db=$WALLET_DB --no-throttle handle-uri --yes "$PAY_URI" 2>&1 | grep -q "already paid" || exit_error "not reporting already paid" +taler-wallet-cli --wallet-db="$WALLET_DB" --no-throttle handle-uri --yes "$PAY_URI" 2>&1 | grep -q "already paid" || exit_error "not reporting already paid" echo "Already paid properly detected" exit_success diff --git a/integrationtests/test-double-spend.sh b/integrationtests/test-double-spend.sh index 5987ad494..81a4563a6 100755 --- a/integrationtests/test-double-spend.sh +++ b/integrationtests/test-double-spend.sh @@ -10,12 +10,10 @@ taler-wallet-cli --wallet-db="$WALLET_DB" --no-throttle testing withdraw -e "$EX cp "$WALLET_DB" "$WALLET_DB.bak" echo "Spend all the money" taler-wallet-cli --wallet-db="$WALLET_DB" testing test-pay -m "$MERCHANT_URL" -k sandbox -a "TESTKUDOS:9.5" -s "foo" -echo "New balance:" -taler-wallet-cli --wallet-db="$WALLET_DB" balance +echo "New balance: $(get_balance)" # Restore old wallet database mv "$WALLET_DB.bak" "$WALLET_DB" -echo "Balance after getting old coins back:" -taler-wallet-cli --wallet-db="$WALLET_DB" balance +echo "Balance after getting old coins back: $(get_balance)" echo "Try to double-spend" # TODO this should probably fail more gracefully # "exchange_reply: { hint: 'insufficient funds', code: 1200 } diff --git a/integrationtests/test-refund.sh b/integrationtests/test-refund.sh index d4193dbb7..7b8fbdef7 100755 --- a/integrationtests/test-refund.sh +++ b/integrationtests/test-refund.sh @@ -6,18 +6,21 @@ normal_start_and_wait "refund" echo "Withdraw TESTKUDOS" taler-wallet-cli --wallet-db="$WALLET_DB" --no-throttle testing withdraw -e "$EXCHANGE_URL" -b "$BANK_URL" -a "TESTKUDOS:10" >>"$LOG" 2>>"$LOG" -echo -n "Balance after withdrawal: " -taler-wallet-cli --wallet-db="$WALLET_DB" balance 2>>"$LOG" +BALANCE_1=$(get_balance) +echo "Balance after withdrawal: $BALANCE_1" REFUND_URI=$(taler-wallet-cli --wallet-db="$WALLET_DB" --no-throttle testing gen-refund-uri \ -m "$MERCHANT_URL" -k sandbox \ -s "first refund" -a "TESTKUDOS:8" -r "TESTKUDOS:2" 2>>"$LOG" | grep -E -m 1 -o "taler://refund.*insecure=1") -echo -n "Balance after payment: " +BALANCE_2=$(get_balance) +echo "Balance after payment: $BALANCE_2" +assert_less_than "$BALANCE_2" "$BALANCE_1" taler-wallet-cli --wallet-db="$WALLET_DB" balance 2>>"$LOG" echo "Handling refund: $REFUND_URI" taler-wallet-cli --wallet-db="$WALLET_DB" --no-throttle handle-uri "$REFUND_URI" 2>"$LOG" taler-wallet-cli --wallet-db="$WALLET_DB" run-until-done 2>>"$LOG" >>"$LOG" -echo -n "Balance after first refund: " -taler-wallet-cli --wallet-db="$WALLET_DB" balance 2>>"$LOG" +BALANCE_3=$(get_balance) +echo "Balance after first refund: $BALANCE_3" +assert_greater_than "$BALANCE_3" "$BALANCE_2" # TODO how to test second refund for same purchase? exit_success diff --git a/integrationtests/test-retries.sh b/integrationtests/test-retries.sh index 01bfa5774..3d953eab0 100755 --- a/integrationtests/test-retries.sh +++ b/integrationtests/test-retries.sh @@ -8,8 +8,8 @@ normal_start_and_wait "retries" echo "Withdraw TESTKUDOS" taler-wallet-cli --wallet-db="$WALLET_DB" --no-throttle testing withdraw -e "$EXCHANGE_URL" -b "$BANK_URL" -a "TESTKUDOS:10" 2>>"$LOG" >>"$LOG" -echo -n "Balance after withdrawal: " -taler-wallet-cli --wallet-db="$WALLET_DB" balance 2>>"$LOG" +BALANCE_1=$(get_balance) +echo "Balance after withdrawal: $BALANCE_1" echo "Getting pay taler:// Uri" PAY_URI=$(taler-wallet-cli testing gen-pay-uri -m "$MERCHANT_URL" -k sandbox -a "TESTKUDOS:1" -s "foo" | grep -E -o 'taler://.*') echo "Trying to pay with exchange down, will fail" @@ -23,8 +23,9 @@ echo -n "Wait for exchange to start" wait_for_service "$EXCHANGE_URL" echo "Retrying operations with exchange up" taler-wallet-cli --wallet-db="$WALLET_DB" run-until-done 2>>"$LOG" >>"$LOG" -echo -n "Balance after re-tried payment: " -taler-wallet-cli --wallet-db="$WALLET_DB" balance 2>>"$LOG" +BALANCE_2=$(get_balance) +echo "Balance after re-tried payment: $BALANCE_2" +assert_less_than "$BALANCE_2" "$BALANCE_1" echo "Getting pay taler:// Uri" PAY_URI=$(taler-wallet-cli testing gen-pay-uri -m "$MERCHANT_URL" -k sandbox -a "TESTKUDOS:1" -s "foo" | grep -E -o 'taler://.*') @@ -39,7 +40,8 @@ echo -n "Wait for merchant to start" wait_for_service "$MERCHANT_URL" echo "Retrying payment with merchant up" taler-wallet-cli --wallet-db="$WALLET_DB" --no-throttle handle-uri --yes "$PAY_URI" 2>>"$LOG" >>"$LOG" -echo -n "Balance after re-tried payment: " -taler-wallet-cli --wallet-db="$WALLET_DB" balance 2>>"$LOG" +BALANCE_3=$(get_balance) +echo "Balance after re-tried payment: $BALANCE_3" +assert_less_than "$BALANCE_3" "$BALANCE_2" exit_success