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