merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

test_merchant_statistics.sh (8048B)


      1 #!/bin/bash
      2 # This file is in the public domain.
      3 
      4 set -eu
      5 
      6 function clean_wallet() {
      7     rm -f "${WALLET_DB}"
      8     exit_cleanup
      9 }
     10 
     11 
     12 # Replace with 0 for nexus...
     13 USE_FAKEBANK=1
     14 if [ 1 = "$USE_FAKEBANK" ]
     15 then
     16     ACCOUNT="exchange-account-2"
     17     BANK_FLAGS="-f -d x-taler-bank -u $ACCOUNT"
     18     BANK_URL="http://localhost:8082/"
     19 else
     20     ACCOUNT="exchange-account-1"
     21     BANK_FLAGS="-ns -d iban -u $ACCOUNT"
     22     BANK_URL="http://localhost:18082/"
     23     echo -n "Testing for libeufin-bank"
     24     libeufin-bank --help >/dev/null </dev/null || exit_skip " MISSING"
     25     echo " FOUND"
     26 
     27 fi
     28 
     29 . setup.sh
     30 
     31 echo -n "Testing for taler-harness"
     32 taler-harness --help >/dev/null </dev/null || exit_skip " MISSING"
     33 echo " FOUND"
     34 
     35 # Launch exchange, merchant and bank.
     36 setup -c "test_template.conf" \
     37       -r "merchant-exchange-default" \
     38       -em \
     39       $BANK_FLAGS
     40 LAST_RESPONSE=$(mktemp -p "${TMPDIR:-/tmp}" test_response.conf-XXXXXX)
     41 CONF="test_template.conf.edited"
     42 WALLET_DB=$(mktemp -p "${TMPDIR:-/tmp}" test_wallet.json-XXXXXX)
     43 EXCHANGE_URL="http://localhost:8081/"
     44 
     45 # Install cleanup handler (except for kill -9)
     46 trap clean_wallet EXIT
     47 
     48 echo -n "First prepare wallet with coins ..."
     49 rm -f "$WALLET_DB"
     50 taler-wallet-cli \
     51     --no-throttle \
     52     --wallet-db="$WALLET_DB" \
     53     api \
     54     --expect-success 'withdrawTestBalance' \
     55   "$(jq -n '
     56     {
     57         amount: "TESTKUDOS:99",
     58         corebankApiBaseUrl: $BANK_URL,
     59         exchangeBaseUrl: $EXCHANGE_URL
     60     }' \
     61     --arg BANK_URL "${BANK_URL}" \
     62     --arg EXCHANGE_URL "$EXCHANGE_URL"
     63   )" 2>wallet-withdraw-1.err >wallet-withdraw-1.out
     64 echo -n "."
     65 # FIXME-MS: add logic to have nexus check immediately here.
     66 # sleep 10
     67 echo -n "."
     68 # NOTE: once libeufin can do long-polling, we should
     69 # be able to reduce the delay here and run wirewatch
     70 # always in the background via setup
     71 taler-exchange-wirewatch \
     72     -a "$ACCOUNT" \
     73     -L "INFO" \
     74     -c "$CONF" \
     75     -t &> taler-exchange-wirewatch.out
     76 echo -n "."
     77 timeout 60 taler-wallet-cli \
     78     --wallet-db="$WALLET_DB" \
     79     run-until-done \
     80     2>wallet-withdraw-finish-1.err \
     81     >wallet-withdraw-finish-1.out
     82 echo " OK"
     83 
     84 CURRENCY_COUNT=$(taler-wallet-cli --wallet-db="$WALLET_DB" balance | jq '.balances|length')
     85 if [ "$CURRENCY_COUNT" = "0" ]
     86 then
     87     exit_fail "Expected least one currency, withdrawal failed. check log."
     88 fi
     89 
     90 #
     91 # CREATE INSTANCE FOR TESTING
     92 #
     93 
     94 
     95 echo -n "Configuring merchant instance ..."
     96 
     97 STATUS=$(curl -H "Content-Type: application/json" -X POST \
     98     -H 'Authorization: Bearer secret-token:super_secret' \
     99     http://localhost:9966/management/instances \
    100     -d '{"auth":{"method":"external"},"id":"admin","name":"default","user_type":"business","address":{},"jurisdiction":{},"use_stefan":true,"default_wire_transfer_delay":{"d_us" : 50000000000},"default_pay_delay":{"d_us": 60000000000}}' \
    101     -w "%{http_code}" -s -o /dev/null)
    102 
    103 if [ "$STATUS" != "204" ]
    104 then
    105     exit_fail "Expected '204 No content' response. Got instead $STATUS"
    106 fi
    107 echo "Ok"
    108 
    109 echo -n "Configuring merchant account ..."
    110 
    111 if [ 1 = "$USE_FAKEBANK" ]
    112 then
    113     FORTYTHREE="payto://x-taler-bank/localhost/fortythree?receiver-name=fortythree"
    114 else
    115     FORTYTHREE=$(get_payto_uri fortythree x)
    116 fi
    117 
    118 # add bank account
    119 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    120     -H 'Authorization: Bearer secret-token:super_secret' \
    121     http://localhost:9966/private/accounts \
    122     -d '{"payto_uri":"'"$FORTYTHREE"'"}' \
    123     -w "%{http_code}" -s -o /dev/null)
    124 
    125 if [ "$STATUS" != "200" ]
    126 then
    127     exit_fail "Expected '200 OK' response. Got instead $STATUS"
    128 fi
    129 echo "OK"
    130 
    131 echo -n "Adding same account again ..."
    132 # add same bank account again (check idempotency)
    133 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    134     -H 'Authorization: Bearer secret-token:super_secret' \
    135     http://localhost:9966/private/accounts \
    136     -d '{"payto_uri":"'"$FORTYTHREE"'"}' \
    137     -w "%{http_code}" -s -o /dev/null)
    138 
    139 if [ "$STATUS" != "200" ]
    140 then
    141     exit_fail "Expected '200 OK' response. Got instead $STATUS"
    142 fi
    143 echo "OK"
    144 
    145 echo -n "Checking idempotency ..."
    146 curl -H "Content-Type: application/json" \
    147      -H 'Authorization: Bearer secret-token:super_secret' \
    148      http://localhost:9966/private/accounts > "$LAST_RESPONSE" 2> /dev/null
    149 NUM=$(jq '.accounts|length' < "$LAST_RESPONSE")
    150 
    151 if [ "$NUM" != "1" ]
    152 then
    153     cat "$LAST_RESPONSE" >&2
    154     exit_fail "Expected 1 account. got: $NUM"
    155 fi
    156 echo " OK"
    157 
    158 
    159 echo -n "Creating order with non-existing POT..."
    160 STATUS=$(curl 'http://localhost:9966/private/orders' \
    161     -d '{"create_token":false,"refund_delay":{"d_us":0},"order":{"amount":"TESTKUDOS:7","summary":"3","order_default_money_pot":1}}' \
    162     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    163 
    164 if [ "$STATUS" != "404" ]
    165 then
    166     cat "$LAST_RESPONSE" >&2
    167     exit_fail "Expected 404, money pot unknown. got: $STATUS"
    168 fi
    169 echo "OK"
    170 
    171 
    172 echo -n "Creating money pot..."
    173 STATUS=$(curl 'http://localhost:9966/private/pots' \
    174     -d '{"description":"First pot","pot_name":"#1"}' \
    175     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    176 
    177 if [ "$STATUS" != "200" ]
    178 then
    179     cat "$LAST_RESPONSE" >&2
    180     exit_fail "Expected 200, pot created. got: $STATUS"
    181 fi
    182 MONEY_POT=$(jq -r .pot_serial_id < "$LAST_RESPONSE")
    183 echo "OK"
    184 
    185 
    186 echo -n "Creating order with money pot..."
    187 STATUS=$(curl 'http://localhost:9966/private/orders' \
    188     -d '{"create_token":false,"refund_delay":{"d_us":0},"order":{"amount":"TESTKUDOS:7","summary":"3","order_default_money_pot":'"$MONEY_POT"'}}' \
    189     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    190 
    191 if [ "$STATUS" != "200" ]
    192 then
    193     cat "$LAST_RESPONSE" >&2
    194     exit_fail "Expected 200, order created. got: $STATUS"
    195 fi
    196 
    197 ORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    198 
    199 echo "OK"
    200 
    201 echo -n "Checking created order without TOKEN..."
    202 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID" \
    203               -w "%{http_code}" -s -o "$LAST_RESPONSE")
    204 PAY_URI=$(jq -r .taler_pay_uri < "$LAST_RESPONSE")
    205 if [ "$PAY_URI" == "null" ]
    206 then
    207     cat "$LAST_RESPONSE" >&2
    208     exit_fail "Expected non-NULL payuri. got $PAY_URI"
    209 fi
    210 echo "OK"
    211 
    212 NOW=$(date +%s)
    213 
    214 echo -n "Pay first order ${PAY_URI} ..."
    215 taler-wallet-cli \
    216     --no-throttle \
    217     --wallet-db="$WALLET_DB" \
    218     handle-uri "${PAY_URI}" \
    219     -y 2> wallet-pay1.err > wallet-pay1.log
    220 timeout 60 taler-wallet-cli \
    221     --no-throttle \
    222     --wallet-db="$WALLET_DB" \
    223     run-until-done \
    224     2> wallet-finish-pay1.err \
    225     > wallet-finish-pay1.log
    226 NOW2=$(date +%s)
    227 echo " OK (took $(( NOW2 - NOW )) secs )"
    228 
    229 
    230 echo -n "Checking money pot..."
    231 STATUS=$(curl 'http://localhost:9966/private/pots' \
    232     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    233 
    234 if [ "$STATUS" != "200" ]
    235 then
    236     cat "$LAST_RESPONSE" >&2
    237     exit_fail "Expected 200, pot status. got: $STATUS"
    238 fi
    239 MONEY_POT_BALANCE=$(jq -r .pots[0].pot_totals[0] < "$LAST_RESPONSE")
    240 
    241 if [ "$MONEY_POT_BALANCE" != "TESTKUDOS:7" ]
    242 then
    243     exit_fail "Expected 'TESTKUDOS:7' balance. Got instead $MONEY_POT_BALANCE"
    244 fi
    245 
    246 echo "OK"
    247 
    248 echo -n "Patching money pot ..."
    249 STATUS=$(curl "http://localhost:9966/private/pots/$MONEY_POT" -X PATCH \
    250     -d  '{"description":"Updated pot","expected_pot_totals":[],"new_pot_totals":["KUDOS:23"],"pot_name":"#1p"}' \
    251     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    252 
    253 if [ "$STATUS" != "204" ]
    254 then
    255     cat "$LAST_RESPONSE" >&2
    256     exit_fail "Expected 204, pot updated. got: $STATUS"
    257 fi
    258 echo "OK"
    259 
    260 echo -n "Schedule periodic report..."
    261 STATUS=$(curl 'http://localhost:9966/private/reports' \
    262     -d '{"description":"My report","program_section":"file","mime_type":"application/pdf","data_source":"/private/statistics-report/transactions","target_address":"'"${TMPDIR:-/tmp}/last-report"'","report_frequency":{"d_us" : 50000000}}' \
    263     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    264 
    265 if [ "$STATUS" != "200" ]
    266 then
    267     cat "$LAST_RESPONSE" >&2
    268     exit_fail "Expected 200, pot created. got: $STATUS"
    269 fi
    270 REPORT_SERIAL_ID=$(jq -r .report_serial_id < "$LAST_RESPONSE")
    271 echo "OK"
    272 
    273 echo -n "Generate periodic report..."
    274 taler-merchant-report-generator \
    275     -c "$CONF" \
    276     --test \
    277     --timetravel=100000000 \
    278     --log=INFO 2> report-generator.log
    279 echo " OK"
    280 
    281 
    282 
    283 exit 0