merchant

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

test_merchant_templates.sh (14930B)


      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 echo -n "Configuring merchant instance ..."
     95 
     96 STATUS=$(curl -H "Content-Type: application/json" -X POST \
     97     http://localhost:9966/management/instances \
     98     -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}}' \
     99     -w "%{http_code}" -s -o /dev/null)
    100 
    101 if [ "$STATUS" != "204" ]
    102 then
    103     exit_fail "Expected '204 No content' response. Got instead $STATUS"
    104 fi
    105 ADMIN_TOKEN=$(merchant_login admin super_secret)
    106 echo "Ok"
    107 
    108 echo -n "Configuring merchant bank account ..."
    109 
    110 if [ 1 = "$USE_FAKEBANK" ]
    111 then
    112     FORTYTHREE="payto://x-taler-bank/localhost/fortythree?receiver-name=fortythree"
    113 else
    114     FORTYTHREE=$(get_payto_uri fortythree x)
    115 fi
    116 # add bank account address
    117 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    118     -H "Authorization: Bearer $ADMIN_TOKEN" \
    119     http://localhost:9966/private/accounts \
    120     -d '{"payto_uri":"'"$FORTYTHREE"'"}' \
    121     -w "%{http_code}" -s -o /dev/null)
    122 
    123 if [ "$STATUS" != "200" ]
    124 then
    125     exit_fail "Expected '200 OK' response. Got instead $STATUS"
    126 fi
    127 echo "Ok"
    128 
    129 
    130 echo -n "Creating Paivana template..."
    131 TID="paivana"
    132 STATUS=$(curl 'http://localhost:9966/private/templates' \
    133     -H "Authorization: Bearer $ADMIN_TOKEN" \
    134     -d '{"template_id":"paivana","template_description":"A Paivana template","template_contract":{"template_type":"paivana","summary":"The summary","choices":[{"amount":"TESTKUDOS:1"}]}}' \
    135     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    136 
    137 if [ "$STATUS" != "204" ]
    138 then
    139     cat "$LAST_RESPONSE" >&2
    140     exit_fail "Expected 204, template created. got: $STATUS"
    141 fi
    142 
    143 echo "Checking template data ..."
    144 STATUS=$(curl http://localhost:9966/templates/"$TID" \
    145     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    146 
    147 AMOUNT=$(jq -r .template_contract.choices[0].amount < "$LAST_RESPONSE")
    148 if [ "$AMOUNT" != "TESTKUDOS:1" ]
    149 then
    150     cat "$LAST_RESPONSE" >&2
    151     exit_fail "Expected TESTKUDOS:1. Got: $AMOUNT"
    152 fi
    153 echo " OK"
    154 
    155 set -x
    156 
    157 echo -n "Creating order using template..."
    158 PAIVANA_ID="4321-6hvIP7UnmNDVjp5Intuf9jFK7MzW0ycEyxf5Mszl3xs="
    159 STATUS=$(curl 'http://localhost:9966/templates/'"$TID" \
    160               -d '{"template_type":"paivana","tip":"TESTKUDOS:0.1","website":"https://example.com/","paivana_id":"'"${PAIVANA_ID}"'"}' \
    161     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    162 
    163 if [ "$STATUS" != "200" ]
    164 then
    165     cat "$LAST_RESPONSE" >&2
    166     exit_fail "Expected 200, order created. got: $STATUS"
    167 fi
    168 
    169 ORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    170 TOKEN=$(jq -r .token < "$LAST_RESPONSE")
    171 
    172 if [ "$TOKEN" == "null" ]
    173 then
    174     cat "$LAST_RESPONSE" >&2
    175     exit_fail "token should not be null, got: $TOKEN"
    176 fi
    177 
    178 echo "OK"
    179 
    180 echo -n "Fetching order details ..."
    181 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}?session_id=${PAIVANA_ID}" \
    182     -H "Authorization: Bearer $ADMIN_TOKEN" \
    183     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    184 
    185 if [ "$STATUS" != "200" ]
    186 then
    187     cat "$LAST_RESPONSE" >&2
    188     exit_fail "Expected 200, getting order info. got: $STATUS"
    189 fi
    190 
    191 PRICE=$(jq -r .total_amount < "$LAST_RESPONSE")
    192 if [ "$PRICE" != "TESTKUDOS:1.1" ]
    193 then
    194     cat "$LAST_RESPONSE" >&2
    195     exit_fail "Expected TESTKUDOS:1.1 (with tip) but got: $PRICE"
    196 fi
    197 
    198 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    199 echo "OK: $PAY_URL"
    200 
    201 
    202 NOW=$(date +%s)
    203 
    204 echo -n "Pay first order ${PAY_URL} ..."
    205 # echo "0" to tell wallet to use choice #0
    206 echo "0" | \
    207   taler-wallet-cli \
    208       --no-throttle \
    209       --wallet-db="$WALLET_DB" \
    210       handle-uri "${PAY_URL}" \
    211       -y 2> wallet-pay1.err > wallet-pay1.log
    212 
    213 timeout 60 taler-wallet-cli \
    214     --no-throttle \
    215     --wallet-db="$WALLET_DB" \
    216     run-until-done \
    217     2> wallet-finish-pay1.err \
    218     > wallet-finish-pay1.log
    219 NOW2=$(date +%s)
    220 echo " OK (took $(( NOW2 - NOW )) secs )"
    221 
    222 # Check payment status AND binding to session ID.
    223 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}?session_id=${PAIVANA_ID}" \
    224     -H "Authorization: Bearer $ADMIN_TOKEN" \
    225     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    226 
    227 if [ "$STATUS" != "200" ]
    228 then
    229     cat "$LAST_RESPONSE" >&2
    230     exit_fail "Expected 200, after pay. got: $STATUS"
    231 fi
    232 
    233 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    234 
    235 if [ "$ORDER_STATUS" != "paid" ]
    236 then
    237     cat "$LAST_RESPONSE" >&2
    238     exit_fail "Order status should be 'paid'. got: $ORDER_STATUS"
    239 fi
    240 
    241 #
    242 # PAIVANA TEMPLATES WITH A CLIENT-EDITABLE AMOUNT
    243 #
    244 
    245 echo -n "Rejecting amount limits without a currency ..."
    246 STATUS=$(curl 'http://localhost:9966/private/templates' \
    247     -H "Authorization: Bearer $ADMIN_TOKEN" \
    248     -d '{"template_id":"paivana-bad","template_description":"Limits but no currency","template_contract":{"template_type":"paivana","summary":"The summary","min_amount":"TESTKUDOS:0.5","choices":[{"amount":"TESTKUDOS:1"}]}}' \
    249     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    250 
    251 if [ "$STATUS" != "400" ]
    252 then
    253     cat "$LAST_RESPONSE" >&2
    254     exit_fail "Expected 400 for min_amount without currency. got: $STATUS"
    255 fi
    256 echo " OK"
    257 
    258 echo -n "Rejecting amount limits in different currencies ..."
    259 STATUS=$(curl 'http://localhost:9966/private/templates' \
    260     -H "Authorization: Bearer $ADMIN_TOKEN" \
    261     -d '{"template_id":"paivana-bad","template_description":"Limits in different currencies","template_contract":{"template_type":"paivana","summary":"The summary","min_amount":"TESTKUDOS:1","max_amount":"EUR:2","choices":[{"amount":"TESTKUDOS:1"}]}}' \
    262     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    263 
    264 if [ "$STATUS" != "400" ]
    265 then
    266     cat "$LAST_RESPONSE" >&2
    267     exit_fail "Expected 400 for min_amount and max_amount in different currencies. got: $STATUS"
    268 fi
    269 echo " OK"
    270 
    271 echo -n "Rejecting min_amount above max_amount ..."
    272 STATUS=$(curl 'http://localhost:9966/private/templates' \
    273     -H "Authorization: Bearer $ADMIN_TOKEN" \
    274     -d '{"template_id":"paivana-bad","template_description":"Limits inverted","template_contract":{"template_type":"paivana","summary":"The summary","currency":"TESTKUDOS","min_amount":"TESTKUDOS:5","max_amount":"TESTKUDOS:1","choices":[{"amount":"TESTKUDOS:1"}]}}' \
    275     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    276 
    277 if [ "$STATUS" != "400" ]
    278 then
    279     cat "$LAST_RESPONSE" >&2
    280     exit_fail "Expected 400 for min_amount > max_amount. got: $STATUS"
    281 fi
    282 echo " OK"
    283 
    284 echo -n "Rejecting an amount for a template with a fixed amount ..."
    285 STATUS=$(curl 'http://localhost:9966/templates/'"$TID" \
    286               -d '{"template_type":"paivana","amount":"TESTKUDOS:2","website":"https://example.com/","paivana_id":"4322-6hvIP7UnmNDVjp5Intuf9jFK7MzW0ycEyxf5Mszl3xs="}' \
    287     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    288 
    289 if [ "$STATUS" != "409" ]
    290 then
    291     cat "$LAST_RESPONSE" >&2
    292     exit_fail "Expected 409, amount is not editable. got: $STATUS"
    293 fi
    294 echo " OK"
    295 
    296 echo -n "Creating Paivana template with an editable amount..."
    297 ETID="paivana-editable"
    298 STATUS=$(curl 'http://localhost:9966/private/templates' \
    299     -H "Authorization: Bearer $ADMIN_TOKEN" \
    300     -d '{"template_id":"paivana-editable","template_description":"A Paivana template with an editable amount","template_contract":{"template_type":"paivana","summary":"The summary","currency":"TESTKUDOS","min_amount":"TESTKUDOS:0.5","max_amount":"TESTKUDOS:10","choices":[{"amount":"TESTKUDOS:2","editable_amount":true}]},"editable_defaults":{"amount":"TESTKUDOS:2"}}' \
    301     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    302 
    303 if [ "$STATUS" != "204" ]
    304 then
    305     cat "$LAST_RESPONSE" >&2
    306     exit_fail "Expected 204, template created. got: $STATUS"
    307 fi
    308 echo " OK"
    309 
    310 echo -n "Checking editable template data ..."
    311 STATUS=$(curl http://localhost:9966/templates/"$ETID" \
    312     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    313 
    314 EDITABLE=$(jq -r .template_contract.choices[0].editable_amount < "$LAST_RESPONSE")
    315 if [ "$EDITABLE" != "true" ]
    316 then
    317     cat "$LAST_RESPONSE" >&2
    318     exit_fail "Expected editable_amount true. Got: $EDITABLE"
    319 fi
    320 
    321 REQUIRED_CURRENCY=$(jq -r .required_currency < "$LAST_RESPONSE")
    322 if [ "$REQUIRED_CURRENCY" != "TESTKUDOS" ]
    323 then
    324     cat "$LAST_RESPONSE" >&2
    325     exit_fail "Expected required_currency TESTKUDOS. Got: $REQUIRED_CURRENCY"
    326 fi
    327 
    328 DEFAULT_AMOUNT=$(jq -r .editable_defaults.amount < "$LAST_RESPONSE")
    329 if [ "$DEFAULT_AMOUNT" != "TESTKUDOS:2" ]
    330 then
    331     cat "$LAST_RESPONSE" >&2
    332     exit_fail "Expected editable_defaults.amount TESTKUDOS:2. Got: $DEFAULT_AMOUNT"
    333 fi
    334 echo " OK"
    335 
    336 echo -n "Rejecting an amount below min_amount ..."
    337 STATUS=$(curl 'http://localhost:9966/templates/'"$ETID" \
    338               -d '{"template_type":"paivana","amount":"TESTKUDOS:0.25","website":"https://example.com/","paivana_id":"4323-6hvIP7UnmNDVjp5Intuf9jFK7MzW0ycEyxf5Mszl3xs="}' \
    339     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    340 
    341 if [ "$STATUS" != "409" ]
    342 then
    343     cat "$LAST_RESPONSE" >&2
    344     exit_fail "Expected 409, amount below min_amount. got: $STATUS"
    345 fi
    346 echo " OK"
    347 
    348 echo -n "Rejecting an amount above max_amount ..."
    349 STATUS=$(curl 'http://localhost:9966/templates/'"$ETID" \
    350               -d '{"template_type":"paivana","amount":"TESTKUDOS:50","website":"https://example.com/","paivana_id":"4324-6hvIP7UnmNDVjp5Intuf9jFK7MzW0ycEyxf5Mszl3xs="}' \
    351     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    352 
    353 if [ "$STATUS" != "409" ]
    354 then
    355     cat "$LAST_RESPONSE" >&2
    356     exit_fail "Expected 409, amount above max_amount. got: $STATUS"
    357 fi
    358 echo " OK"
    359 
    360 echo -n "Rejecting an out-of-range choice_index ..."
    361 STATUS=$(curl 'http://localhost:9966/templates/'"$ETID" \
    362               -d '{"template_type":"paivana","choice_amounts":[{"choice_index":7,"amount":"TESTKUDOS:2"}],"website":"https://example.com/","paivana_id":"4325-6hvIP7UnmNDVjp5Intuf9jFK7MzW0ycEyxf5Mszl3xs="}' \
    363     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    364 
    365 if [ "$STATUS" != "400" ]
    366 then
    367     cat "$LAST_RESPONSE" >&2
    368     exit_fail "Expected 400, choice_index out of range. got: $STATUS"
    369 fi
    370 echo " OK"
    371 
    372 echo -n "Rejecting amount together with choice_amounts ..."
    373 STATUS=$(curl 'http://localhost:9966/templates/'"$ETID" \
    374               -d '{"template_type":"paivana","amount":"TESTKUDOS:2","choice_amounts":[{"choice_index":0,"amount":"TESTKUDOS:2"}],"website":"https://example.com/","paivana_id":"4326-6hvIP7UnmNDVjp5Intuf9jFK7MzW0ycEyxf5Mszl3xs="}' \
    375     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    376 
    377 if [ "$STATUS" != "409" ]
    378 then
    379     cat "$LAST_RESPONSE" >&2
    380     exit_fail "Expected 409, amount and choice_amounts are exclusive. got: $STATUS"
    381 fi
    382 echo " OK"
    383 
    384 echo -n "Creating order with a client-specified amount ..."
    385 EPAIVANA_ID="4327-6hvIP7UnmNDVjp5Intuf9jFK7MzW0ycEyxf5Mszl3xs="
    386 STATUS=$(curl 'http://localhost:9966/templates/'"$ETID" \
    387               -d '{"template_type":"paivana","amount":"TESTKUDOS:5","tip":"TESTKUDOS:0.5","website":"https://example.com/","paivana_id":"'"${EPAIVANA_ID}"'"}' \
    388     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    389 
    390 if [ "$STATUS" != "200" ]
    391 then
    392     cat "$LAST_RESPONSE" >&2
    393     exit_fail "Expected 200, order created. got: $STATUS"
    394 fi
    395 
    396 EORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    397 STATUS=$(curl "http://localhost:9966/private/orders/${EORDER_ID}?session_id=${EPAIVANA_ID}" \
    398     -H "Authorization: Bearer $ADMIN_TOKEN" \
    399     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    400 
    401 if [ "$STATUS" != "200" ]
    402 then
    403     cat "$LAST_RESPONSE" >&2
    404     exit_fail "Expected 200, getting order info. got: $STATUS"
    405 fi
    406 
    407 PRICE=$(jq -r .total_amount < "$LAST_RESPONSE")
    408 if [ "$PRICE" != "TESTKUDOS:5.5" ]
    409 then
    410     cat "$LAST_RESPONSE" >&2
    411     exit_fail "Expected TESTKUDOS:5.5 (amount plus tip) but got: $PRICE"
    412 fi
    413 echo " OK"
    414 
    415 echo -n "Creating order with an explicit choice_amounts entry ..."
    416 CPAIVANA_ID="4328-6hvIP7UnmNDVjp5Intuf9jFK7MzW0ycEyxf5Mszl3xs="
    417 STATUS=$(curl 'http://localhost:9966/templates/'"$ETID" \
    418               -d '{"template_type":"paivana","choice_amounts":[{"choice_index":0,"amount":"TESTKUDOS:3"}],"website":"https://example.com/","paivana_id":"'"${CPAIVANA_ID}"'"}' \
    419     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    420 
    421 if [ "$STATUS" != "200" ]
    422 then
    423     cat "$LAST_RESPONSE" >&2
    424     exit_fail "Expected 200, order created. got: $STATUS"
    425 fi
    426 
    427 CORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    428 STATUS=$(curl "http://localhost:9966/private/orders/${CORDER_ID}?session_id=${CPAIVANA_ID}" \
    429     -H "Authorization: Bearer $ADMIN_TOKEN" \
    430     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    431 
    432 if [ "$STATUS" != "200" ]
    433 then
    434     cat "$LAST_RESPONSE" >&2
    435     exit_fail "Expected 200, getting order info. got: $STATUS"
    436 fi
    437 
    438 PRICE=$(jq -r .total_amount < "$LAST_RESPONSE")
    439 if [ "$PRICE" != "TESTKUDOS:3" ]
    440 then
    441     cat "$LAST_RESPONSE" >&2
    442     exit_fail "Expected TESTKUDOS:3 but got: $PRICE"
    443 fi
    444 echo " OK"
    445 
    446 echo "TEST PASSED"
    447 exit 0