merchant

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

test_merchant_statistics.sh (8328B)


      1 #!/usr/bin/env 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     http://localhost:9966/management/instances \
     99     -d '{"auth":{"method":"token","password":"super_secret"},"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}}' \
    100     -w "%{http_code}" -s -o /dev/null)
    101 
    102 if [ "$STATUS" != "204" ]
    103 then
    104     exit_fail "Expected '204 No content' response. Got instead $STATUS"
    105 fi
    106 ADMIN_TOKEN=$(merchant_login admin super_secret)
    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 $ADMIN_TOKEN" \
    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 $ADMIN_TOKEN" \
    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 $ADMIN_TOKEN" \
    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     -H "Authorization: Bearer $ADMIN_TOKEN" \
    162     -d '{"create_token":false,"refund_delay":{"d_us":0},"order":{"amount":"TESTKUDOS:7","summary":"3","order_default_money_pot":1}}' \
    163     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    164 
    165 if [ "$STATUS" != "404" ]
    166 then
    167     cat "$LAST_RESPONSE" >&2
    168     exit_fail "Expected 404, money pot unknown. got: $STATUS"
    169 fi
    170 echo "OK"
    171 
    172 
    173 echo -n "Creating money pot..."
    174 STATUS=$(curl 'http://localhost:9966/private/pots' \
    175     -H "Authorization: Bearer $ADMIN_TOKEN" \
    176     -d '{"description":"First pot","pot_name":"pot1"}' \
    177     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    178 
    179 if [ "$STATUS" != "200" ]
    180 then
    181     cat "$LAST_RESPONSE" >&2
    182     exit_fail "Expected 200, pot created. got: $STATUS"
    183 fi
    184 MONEY_POT=$(jq -r .pot_serial_id < "$LAST_RESPONSE")
    185 echo "OK"
    186 
    187 
    188 echo -n "Creating order with money pot..."
    189 STATUS=$(curl 'http://localhost:9966/private/orders' \
    190     -H "Authorization: Bearer $ADMIN_TOKEN" \
    191     -d '{"create_token":false,"refund_delay":{"d_us":0},"order":{"amount":"TESTKUDOS:7","summary":"3","order_default_money_pot":'"$MONEY_POT"'}}' \
    192     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    193 
    194 if [ "$STATUS" != "200" ]
    195 then
    196     cat "$LAST_RESPONSE" >&2
    197     exit_fail "Expected 200, order created. got: $STATUS"
    198 fi
    199 
    200 ORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    201 
    202 echo "OK"
    203 
    204 echo -n "Checking created order without TOKEN..."
    205 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID" \
    206               -w "%{http_code}" -s -o "$LAST_RESPONSE")
    207 PAY_URI=$(jq -r .taler_pay_uri < "$LAST_RESPONSE")
    208 if [ "$PAY_URI" == "null" ]
    209 then
    210     cat "$LAST_RESPONSE" >&2
    211     exit_fail "Expected non-NULL payuri. got $PAY_URI"
    212 fi
    213 echo "OK"
    214 
    215 NOW=$(date +%s)
    216 
    217 echo -n "Pay first order ${PAY_URI} ..."
    218 taler-wallet-cli \
    219     --no-throttle \
    220     --wallet-db="$WALLET_DB" \
    221     handle-uri "${PAY_URI}" \
    222     -y 2> wallet-pay1.err > wallet-pay1.log
    223 timeout 60 taler-wallet-cli \
    224     --no-throttle \
    225     --wallet-db="$WALLET_DB" \
    226     run-until-done \
    227     2> wallet-finish-pay1.err \
    228     > wallet-finish-pay1.log
    229 NOW2=$(date +%s)
    230 echo " OK (took $(( NOW2 - NOW )) secs )"
    231 
    232 
    233 echo -n "Checking money pot..."
    234 STATUS=$(curl 'http://localhost:9966/private/pots' \
    235     -H "Authorization: Bearer $ADMIN_TOKEN" \
    236     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    237 
    238 if [ "$STATUS" != "200" ]
    239 then
    240     cat "$LAST_RESPONSE" >&2
    241     exit_fail "Expected 200, pot status. got: $STATUS"
    242 fi
    243 MONEY_POT_BALANCE=$(jq -r .pots[0].pot_totals[0] < "$LAST_RESPONSE")
    244 
    245 if [ "$MONEY_POT_BALANCE" != "TESTKUDOS:7" ]
    246 then
    247     exit_fail "Expected 'TESTKUDOS:7' balance. Got instead $MONEY_POT_BALANCE"
    248 fi
    249 
    250 echo "OK"
    251 
    252 echo -n "Patching money pot ..."
    253 STATUS=$(curl "http://localhost:9966/private/pots/$MONEY_POT" -X PATCH \
    254     -H "Authorization: Bearer $ADMIN_TOKEN" \
    255     -d  '{"description":"Updated pot","expected_pot_totals":[],"new_pot_totals":["KUDOS:23"],"pot_name":"pot1p"}' \
    256     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    257 
    258 if [ "$STATUS" != "204" ]
    259 then
    260     cat "$LAST_RESPONSE" >&2
    261     exit_fail "Expected 204, pot updated. got: $STATUS"
    262 fi
    263 echo "OK"
    264 
    265 echo -n "Schedule periodic report..."
    266 STATUS=$(curl 'http://localhost:9966/private/reports' \
    267     -H "Authorization: Bearer $ADMIN_TOKEN" \
    268     -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}}' \
    269     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    270 
    271 if [ "$STATUS" != "200" ]
    272 then
    273     cat "$LAST_RESPONSE" >&2
    274     exit_fail "Expected 200, pot created. got: $STATUS"
    275 fi
    276 REPORT_SERIAL_ID=$(jq -r .report_serial_id < "$LAST_RESPONSE")
    277 echo "OK"
    278 
    279 echo -n "Generate periodic report..."
    280 taler-merchant-report-generator \
    281     -c "$CONF" \
    282     --test \
    283     --timetravel=100000000 \
    284     --log=INFO 2> report-generator.log
    285 echo " OK"
    286 
    287 echo "Test PASSED"
    288 
    289 exit 0