merchant

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

test_merchant_product_creation.sh (9533B)


      1 #!/bin/bash
      2 # This file is part of TALER
      3 # Copyright (C) 2014-2023 Taler Systems SA
      4 #
      5 # TALER is free software; you can redistribute it and/or modify
      6 # it under the terms of the GNU General Public License as
      7 # published by the Free Software Foundation; either version 3, or
      8 # (at your option) any later version.
      9 #
     10 # TALER is distributed in the hope that it will be useful, but
     11 # WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU General Public
     16 # License along with TALER; see the file COPYING.  If not, see
     17 # <http://www.gnu.org/licenses/>
     18 #
     19 
     20 set -eu
     21 
     22 # Replace with 0 for nexus...
     23 USE_FAKEBANK=1
     24 if [ 1 = "$USE_FAKEBANK" ]
     25 then
     26     ACCOUNT="exchange-account-2"
     27     WIRE_METHOD="x-taler-bank"
     28     BANK_FLAGS="-f -d $WIRE_METHOD -u $ACCOUNT"
     29     BANK_URL="http://localhost:8082/"
     30 else
     31     echo -n "Testing for libeufin-bank"
     32     libeufin-bank --help >/dev/null </dev/null || exit_skip " MISSING"
     33     echo " FOUND"
     34 
     35     ACCOUNT="exchange-account-1"
     36     WIRE_METHOD="iban"
     37     BANK_FLAGS="-ns -d $WIRE_METHOD -u $ACCOUNT"
     38     BANK_URL="http://localhost:18082/"
     39 fi
     40 
     41 . setup.sh
     42 
     43 
     44 echo -n "Testing for taler-harness"
     45 taler-harness --help >/dev/null </dev/null || exit_skip " MISSING"
     46 echo " FOUND"
     47 
     48 
     49 # Launch system.
     50 setup -c "test_template.conf" \
     51       -r "merchant-exchange-default" \
     52       -em \
     53       $BANK_FLAGS
     54 
     55 LAST_RESPONSE=$(mktemp -p "${TMPDIR:-/tmp}" test_response.conf-XXXXXX)
     56 WALLET_DB=$(mktemp -p "${TMPDIR:-/tmp}" test_wallet.json-XXXXXX)
     57 CONF="test_template.conf.edited"
     58 if [ 1 = "$USE_FAKEBANK" ]
     59 then
     60     FORTYTHREE="payto://x-taler-bank/localhost/fortythree?receiver-name=fortythree"
     61 else
     62     FORTYTHREE=$(get_payto_uri fortythree x)
     63 fi
     64 EXCHANGE_URL="http://localhost:8081/"
     65 
     66 echo -n "Configuring merchant instance ..."
     67 STATUS=$(curl -H "Content-Type: application/json" -X POST \
     68     -H 'Authorization: Bearer secret-token:super_secret' \
     69     "http://localhost:9966/management/instances" \
     70     -d '{"auth":{"method":"external"},"id":"admin","name":"default","user_type":"business","address":{},"jurisdiction":{},"use_stefan":true,"default_wire_transfer_delay":{"d_us" : 50000000},"default_pay_delay":{"d_us": 60000000}}' \
     71     -w "%{http_code}" -s -o /dev/null)
     72 
     73 if [ "$STATUS" != "204" ]
     74 then
     75     exit_fail "Expected 204 No content, instance created. Got $STATUS instead"
     76 fi
     77 
     78 STATUS=$(curl -H "Content-Type: application/json" -X POST \
     79     -H 'Authorization: Bearer secret-token:super_secret' \
     80     http://localhost:9966/private/accounts \
     81     -d '{"payto_uri":"'"$FORTYTHREE"'"}' \
     82     -w "%{http_code}" -s -o /dev/null)
     83 
     84 if [ "$STATUS" != "200" ]
     85 then
     86     exit_fail "Expected 200 OK. Got: $STATUS"
     87 fi
     88 
     89 echo "OK"
     90 
     91 RANDOM_IMG='data:image/png;base64,abcdefg'
     92 INFINITE_PRODUCT_TEMPLATE='{"product_id":"2","product_name":"stuff","description":"product with id 2 and price :15","unit_price":["TESTKUDOS:15"],"unit_total_stock":"-1","unit":"","image":"'"$RANDOM_IMG"'","taxes":[]}'
     93 MANAGED_PRODUCT_TEMPLATE='{"product_id":"3","product_name":"more stuff","description":"product with id 3 and price :10","unit_price":["TESTKUDOS:150"],"unit_total_stock":"2","unit":"","image":"'"$RANDOM_IMG"'","taxes":[]}'
     94 
     95 echo -n "Creating products..."
     96 STATUS=$(curl 'http://localhost:9966/private/products' \
     97     -d "$INFINITE_PRODUCT_TEMPLATE" \
     98     -w "%{http_code}" -s -o /dev/null)
     99 
    100 if [ "$STATUS" != "204" ]
    101 then
    102     exit_fail "Expected 204, product created. got: $STATUS"
    103 fi
    104 
    105 STATUS=$(curl 'http://localhost:9966/private/products' \
    106     -d "$MANAGED_PRODUCT_TEMPLATE" \
    107     -w "%{http_code}" -s -o /dev/null)
    108 
    109 if [ "$STATUS" != "204" ]
    110 then
    111     exit_fail "Expected 204, product created. got: $STATUS"
    112 fi
    113 echo "OK"
    114 
    115 
    116 PRODUCT_DATA=$(echo "$INFINITE_PRODUCT_TEMPLATE" | jq 'del(.product_id) | . + {description: "other description"}')
    117 
    118 echo -n "Updating infinite stock product..."
    119 STATUS=$(curl 'http://localhost:9966/private/products/2' -X PATCH \
    120     -d "$PRODUCT_DATA" \
    121     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    122 
    123 if [ "$STATUS" != "204" ]
    124 then
    125     cat "$LAST_RESPONSE"
    126     exit_fail "Expected 204, updating product. got: $STATUS"
    127 fi
    128 
    129 STATUS=$(curl 'http://localhost:9966/private/products/2' \
    130     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    131 
    132 DESCRIPTION=$(jq -r .description < "$LAST_RESPONSE")
    133 
    134 if [ "$DESCRIPTION" != "other description" ]
    135 then
    136     cat "$LAST_RESPONSE"
    137     exit_fail "Expected 'other description'. Got: $DESCRIPTION"
    138 fi
    139 echo "OK"
    140 
    141 MANAGED_PRODUCT_ID=$(echo "$MANAGED_PRODUCT_TEMPLATE" | jq -r '.product_id')
    142 
    143 echo -n "Locking inventory ..."
    144 
    145 STATUS=$(curl "http://localhost:9966/private/products/${MANAGED_PRODUCT_ID}/lock" \
    146     -d '{"lock_uuid":"luck","duration":{"d_us": 100000000},"unit_quantity":"10"}' \
    147     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    148 
    149 if [ "$STATUS" != "410" ]
    150 then
    151     cat "$LAST_RESPONSE"
    152     exit_fail "Expected 410, lock failed. got: $STATUS"
    153 fi
    154 
    155 echo -n "."
    156 
    157 STATUS=$(curl "http://localhost:9966/private/products/${MANAGED_PRODUCT_ID}/lock" \
    158     -d '{"lock_uuid":"luck","duration":{"d_us": 100000000},"unit_quantity":"1"}' \
    159     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    160 
    161 if [ "$STATUS" != "204" ]
    162 then
    163     cat "$LAST_RESPONSE"
    164     exit_fail "Expected 204, lock created. got: $STATUS"
    165 fi
    166 echo " OK"
    167 
    168 echo -n "Creating order to be paid..."
    169 STATUS=$(curl 'http://localhost:9966/private/orders' \
    170     -d '{"order":{"amount":"TESTKUDOS:1","summary":"payme"},"inventory_products":[{"product_id":"'"${MANAGED_PRODUCT_ID}"'","unit_quantity":"1"}]}' \
    171     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    172 
    173 if [ "$STATUS" != "200" ]
    174 then
    175     cat "$LAST_RESPONSE"
    176     exit_fail "Expected 200, order created. got: $STATUS"
    177 fi
    178 
    179 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    180 #TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    181 
    182 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    183     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    184 
    185 if [ "$STATUS" != "200" ]
    186 then
    187     cat "$LAST_RESPONSE"
    188     exit_fail "Expected 200, getting order info before claming it. got: $STATUS"
    189 fi
    190 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    191 
    192 echo -n "."
    193 
    194 STATUS=$(curl 'http://localhost:9966/private/orders' \
    195     -d '{"order":{"amount":"TESTKUDOS:1","summary":"payme"},"inventory_products":[{"product_id":"'"${MANAGED_PRODUCT_ID}"'","unit_quantity":"1"}]}' \
    196     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    197 
    198 if [ "$STATUS" != "410" ]
    199 then
    200     cat "$LAST_RESPONSE"
    201     exit_fail "Expected 410 out of stock (what remains is locked). got: $STATUS"
    202 fi
    203 
    204 echo -n "."
    205 
    206 # Using the 'luck' inventory lock, order creation should work.
    207 STATUS=$(curl 'http://localhost:9966/private/orders' \
    208     -d '{"order":{"amount":"TESTKUDOS:1","summary":"payme"},"lock_uuids":["luck"],"inventory_products":[{"product_id":"'"$MANAGED_PRODUCT_ID"'","unit_quantity":"1"}]}' \
    209     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    210 
    211 if [ "$STATUS" != "200" ]
    212 then
    213     cat "$LAST_RESPONSE"
    214     exit_fail "Expected 200 OK, lock should apply. Got: $STATUS"
    215 fi
    216 echo " OK"
    217 
    218 echo -n "First withdraw wallet ..."
    219 rm -f "$WALLET_DB"
    220 taler-wallet-cli \
    221     --no-throttle \
    222     --wallet-db="$WALLET_DB" \
    223     api \
    224     --expect-success 'withdrawTestBalance' \
    225   "$(jq -n '
    226     {
    227         amount: "TESTKUDOS:5",
    228         corebankApiBaseUrl: $BANK_URL,
    229         exchangeBaseUrl: $EXCHANGE_URL
    230     }' \
    231     --arg BANK_URL "$BANK_URL" \
    232     --arg EXCHANGE_URL "$EXCHANGE_URL"
    233   )" 2>wallet-withdraw-1.err >wallet-withdraw-1.out
    234 echo -n "."
    235 if [ 1 = "$USE_FAKEBANK" ]
    236 then
    237     # Fakebank is instant...
    238     sleep 0
    239 else
    240     sleep 10
    241     # NOTE: once libeufin can do long-polling, we should
    242     # be able to reduce the delay here and run wirewatch
    243     # always in the background via setup
    244 fi
    245 echo -n "."
    246 taler-exchange-wirewatch -L "INFO" -c "$CONF" -t &> taler-exchange-wirewatch.out
    247 echo -n "."
    248 
    249 taler-wallet-cli \
    250     --wallet-db="$WALLET_DB" \
    251     run-until-done \
    252     2>wallet-withdraw-finish-1.err >wallet-withdraw-finish-1.out
    253 echo " OK"
    254 
    255 echo -n "Pay first order ..."
    256 NOW=$(date +%s)
    257 taler-wallet-cli \
    258     --no-throttle \
    259     --wallet-db="$WALLET_DB" \
    260     handle-uri "${PAY_URL}" \
    261     -y \
    262     2> wallet-pay1.err > wallet-pay1.log
    263 NOW2=$(date +%s)
    264 echo " OK (took $(( NOW2 -  NOW)) secs )"
    265 
    266 
    267 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    268     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    269 
    270 if [ "$STATUS" != "200" ]
    271 then
    272     cat "$LAST_RESPONSE"
    273     exit_fail "Expected 200 ok, after pay. got: $STATUS"
    274 fi
    275 
    276 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    277 
    278 if [ "$ORDER_STATUS" != "paid" ]
    279 then
    280     cat "$LAST_RESPONSE"
    281     exit_fail "Expected 'paid'. got: $ORDER_STATUS"
    282 fi
    283 
    284 
    285 echo -n "Updating product..."
    286 
    287 PRODUCT_DATA=$(echo "$MANAGED_PRODUCT_TEMPLATE" | jq 'del(.product_id) | . + {"total_stock": (.total_stock + 2) }')
    288 
    289 STATUS=$(curl 'http://localhost:9966/private/products/3' -X PATCH \
    290     -d "$PRODUCT_DATA" \
    291     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    292 
    293 if [ "$STATUS" != "204" ]
    294 then
    295     cat "$LAST_RESPONSE"
    296     exit_fail "Expected 204, updating product. got: $STATUS"
    297 fi
    298 
    299 echo -n "Requesting inventory..."
    300 STATUS=$(curl 'http://localhost:9966/private/products?name_filter=more' \
    301     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    302 
    303 if [ "$STATUS" != "200" ]
    304 then
    305     cat "$LAST_RESPONSE"
    306     exit_fail "Expected 200. got: $STATUS"
    307 fi
    308 
    309 PRODUCT_COUNT=$(jq -r '.products|length' < "$LAST_RESPONSE")
    310 
    311 if [ "$PRODUCT_COUNT" != "1" ]
    312 then
    313     cat "$LAST_RESPONSE"
    314     exit_fail "Expected one product for the 'more'. filter, got: $PRODUCT_COUNT"
    315 fi
    316 echo " OK"
    317 
    318 exit 0