merchant

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

test_merchant_product_creation.sh (10178B)


      1 #!/usr/bin/env 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     "http://localhost:9966/management/instances" \
     69     -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" : 50000000},"default_pay_delay":{"d_us": 60000000}}' \
     70     -w "%{http_code}" -s -o /dev/null)
     71 
     72 if [ "$STATUS" != "204" ]
     73 then
     74     exit_fail "Expected 204 No content, instance created. Got $STATUS instead"
     75 fi
     76 ADMIN_TOKEN=$(merchant_login admin super_secret)
     77 
     78 STATUS=$(curl -H "Content-Type: application/json" -X POST \
     79     -H "Authorization: Bearer $ADMIN_TOKEN" \
     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":"u","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":"u","image":"'"$RANDOM_IMG"'","taxes":[]}'
     94 
     95 echo -n "Creating products..."
     96 STATUS=$(curl 'http://localhost:9966/private/products' \
     97     -H "Authorization: Bearer $ADMIN_TOKEN" \
     98     -d "$INFINITE_PRODUCT_TEMPLATE" \
     99     -w "%{http_code}" -s -o /dev/null)
    100 
    101 if [ "$STATUS" != "204" ]
    102 then
    103     exit_fail "Expected 204, product created. got: $STATUS"
    104 fi
    105 
    106 STATUS=$(curl 'http://localhost:9966/private/products' \
    107     -H "Authorization: Bearer $ADMIN_TOKEN" \
    108     -d "$MANAGED_PRODUCT_TEMPLATE" \
    109     -w "%{http_code}" -s -o /dev/null)
    110 
    111 if [ "$STATUS" != "204" ]
    112 then
    113     exit_fail "Expected 204, product created. got: $STATUS"
    114 fi
    115 echo "OK"
    116 
    117 
    118 PRODUCT_DATA=$(echo "$INFINITE_PRODUCT_TEMPLATE" | jq 'del(.product_id) | . + {description: "other description"}')
    119 
    120 echo -n "Updating infinite stock product..."
    121 STATUS=$(curl 'http://localhost:9966/private/products/2' -X PATCH \
    122     -H "Authorization: Bearer $ADMIN_TOKEN" \
    123     -d "$PRODUCT_DATA" \
    124     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    125 
    126 if [ "$STATUS" != "204" ]
    127 then
    128     cat "$LAST_RESPONSE"
    129     exit_fail "Expected 204, updating product. got: $STATUS"
    130 fi
    131 
    132 STATUS=$(curl 'http://localhost:9966/private/products/2' \
    133     -H "Authorization: Bearer $ADMIN_TOKEN" \
    134     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    135 
    136 DESCRIPTION=$(jq -r .description < "$LAST_RESPONSE")
    137 
    138 if [ "$DESCRIPTION" != "other description" ]
    139 then
    140     cat "$LAST_RESPONSE"
    141     exit_fail "Expected 'other description'. Got: $DESCRIPTION"
    142 fi
    143 echo "OK"
    144 
    145 MANAGED_PRODUCT_ID=$(echo "$MANAGED_PRODUCT_TEMPLATE" | jq -r '.product_id')
    146 
    147 echo -n "Locking inventory ..."
    148 
    149 STATUS=$(curl "http://localhost:9966/private/products/${MANAGED_PRODUCT_ID}/lock" \
    150     -H "Authorization: Bearer $ADMIN_TOKEN" \
    151     -d '{"lock_uuid":"luck","duration":{"d_us": 100000000},"unit_quantity":"10"}' \
    152     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    153 
    154 if [ "$STATUS" != "410" ]
    155 then
    156     cat "$LAST_RESPONSE"
    157     exit_fail "Expected 410, lock failed. got: $STATUS"
    158 fi
    159 
    160 echo -n "."
    161 
    162 STATUS=$(curl "http://localhost:9966/private/products/${MANAGED_PRODUCT_ID}/lock" \
    163     -H "Authorization: Bearer $ADMIN_TOKEN" \
    164     -d '{"lock_uuid":"luck","duration":{"d_us": 100000000},"unit_quantity":"1"}' \
    165     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    166 
    167 if [ "$STATUS" != "204" ]
    168 then
    169     cat "$LAST_RESPONSE"
    170     exit_fail "Expected 204, lock created. got: $STATUS"
    171 fi
    172 echo " OK"
    173 
    174 echo -n "Creating order to be paid..."
    175 STATUS=$(curl 'http://localhost:9966/private/orders' \
    176     -H "Authorization: Bearer $ADMIN_TOKEN" \
    177     -d '{"order":{"amount":"TESTKUDOS:1","summary":"payme"},"inventory_products":[{"product_id":"'"${MANAGED_PRODUCT_ID}"'","unit_quantity":"1"}]}' \
    178     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    179 
    180 if [ "$STATUS" != "200" ]
    181 then
    182     cat "$LAST_RESPONSE"
    183     exit_fail "Expected 200, order created. got: $STATUS"
    184 fi
    185 
    186 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    187 #TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    188 
    189 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    190     -H "Authorization: Bearer $ADMIN_TOKEN" \
    191     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    192 
    193 if [ "$STATUS" != "200" ]
    194 then
    195     cat "$LAST_RESPONSE"
    196     exit_fail "Expected 200, getting order info before claming it. got: $STATUS"
    197 fi
    198 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    199 
    200 echo -n "."
    201 
    202 STATUS=$(curl 'http://localhost:9966/private/orders' \
    203     -H "Authorization: Bearer $ADMIN_TOKEN" \
    204     -d '{"order":{"amount":"TESTKUDOS:1","summary":"payme"},"inventory_products":[{"product_id":"'"${MANAGED_PRODUCT_ID}"'","unit_quantity":"1"}]}' \
    205     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    206 
    207 if [ "$STATUS" != "410" ]
    208 then
    209     cat "$LAST_RESPONSE"
    210     exit_fail "Expected 410 out of stock (what remains is locked). got: $STATUS"
    211 fi
    212 
    213 echo -n "."
    214 
    215 # Using the 'luck' inventory lock, order creation should work.
    216 STATUS=$(curl 'http://localhost:9966/private/orders' \
    217     -H "Authorization: Bearer $ADMIN_TOKEN" \
    218     -d '{"order":{"amount":"TESTKUDOS:1","summary":"payme"},"lock_uuids":["luck"],"inventory_products":[{"product_id":"'"$MANAGED_PRODUCT_ID"'","unit_quantity":"1"}]}' \
    219     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    220 
    221 if [ "$STATUS" != "200" ]
    222 then
    223     cat "$LAST_RESPONSE"
    224     exit_fail "Expected 200 OK, lock should apply. Got: $STATUS"
    225 fi
    226 echo " OK"
    227 
    228 echo -n "First withdraw wallet ..."
    229 rm -f "$WALLET_DB"
    230 taler-wallet-cli \
    231     --no-throttle \
    232     --wallet-db="$WALLET_DB" \
    233     api \
    234     --expect-success 'withdrawTestBalance' \
    235   "$(jq -n '
    236     {
    237         amount: "TESTKUDOS:5",
    238         corebankApiBaseUrl: $BANK_URL,
    239         exchangeBaseUrl: $EXCHANGE_URL
    240     }' \
    241     --arg BANK_URL "$BANK_URL" \
    242     --arg EXCHANGE_URL "$EXCHANGE_URL"
    243   )" 2>wallet-withdraw-1.err >wallet-withdraw-1.out
    244 echo -n "."
    245 if [ 1 = "$USE_FAKEBANK" ]
    246 then
    247     # Fakebank is instant...
    248     sleep 0
    249 else
    250     sleep 10
    251     # NOTE: once libeufin can do long-polling, we should
    252     # be able to reduce the delay here and run wirewatch
    253     # always in the background via setup
    254 fi
    255 echo -n "."
    256 taler-exchange-wirewatch -L "INFO" -c "$CONF" -t &> taler-exchange-wirewatch.out
    257 echo -n "."
    258 
    259 timeout 60 taler-wallet-cli \
    260     --wallet-db="$WALLET_DB" \
    261     run-until-done \
    262     2>wallet-withdraw-finish-1.err \
    263     >wallet-withdraw-finish-1.out
    264 echo " OK"
    265 
    266 echo -n "Pay first order ..."
    267 NOW=$(date +%s)
    268 taler-wallet-cli \
    269     --no-throttle \
    270     --wallet-db="$WALLET_DB" \
    271     handle-uri "${PAY_URL}" \
    272     -y \
    273     2> wallet-pay1.err > wallet-pay1.log
    274 NOW2=$(date +%s)
    275 echo " OK (took $(( NOW2 -  NOW)) secs )"
    276 
    277 
    278 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    279     -H "Authorization: Bearer $ADMIN_TOKEN" \
    280     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    281 
    282 if [ "$STATUS" != "200" ]
    283 then
    284     cat "$LAST_RESPONSE"
    285     exit_fail "Expected 200 ok, after pay. got: $STATUS"
    286 fi
    287 
    288 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    289 
    290 if [ "$ORDER_STATUS" != "paid" ]
    291 then
    292     cat "$LAST_RESPONSE"
    293     exit_fail "Expected 'paid'. got: $ORDER_STATUS"
    294 fi
    295 
    296 
    297 echo -n "Updating product..."
    298 
    299 PRODUCT_DATA=$(echo "$MANAGED_PRODUCT_TEMPLATE" | jq 'del(.product_id) | . + {"total_stock": (.total_stock + 2) }')
    300 
    301 STATUS=$(curl 'http://localhost:9966/private/products/3' -X PATCH \
    302     -H "Authorization: Bearer $ADMIN_TOKEN" \
    303     -d "$PRODUCT_DATA" \
    304     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    305 
    306 if [ "$STATUS" != "204" ]
    307 then
    308     cat "$LAST_RESPONSE"
    309     exit_fail "Expected 204, updating product. got: $STATUS"
    310 fi
    311 
    312 echo -n "Requesting inventory..."
    313 STATUS=$(curl 'http://localhost:9966/private/products?name_filter=more' \
    314     -H "Authorization: Bearer $ADMIN_TOKEN" \
    315     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    316 
    317 if [ "$STATUS" != "200" ]
    318 then
    319     cat "$LAST_RESPONSE"
    320     exit_fail "Expected 200. got: $STATUS"
    321 fi
    322 
    323 PRODUCT_COUNT=$(jq -r '.products|length' < "$LAST_RESPONSE")
    324 
    325 if [ "$PRODUCT_COUNT" != "1" ]
    326 then
    327     cat "$LAST_RESPONSE"
    328     exit_fail "Expected one product for the 'more'. filter, got: $PRODUCT_COUNT"
    329 fi
    330 echo " OK"
    331 
    332 echo "Test PASSED"
    333 
    334 exit 0