merchant

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

test_merchant_report_deliveries.sh (8729B)


      1 #!/usr/bin/env bash
      2 # This file is part of TALER
      3 # Copyright (C) 2026 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 # Exercise report tests and history with the local file delivery helper.
     20 set -eu -o pipefail
     21 . setup.sh
     22 setup -c test_template.conf -m
     23 CONF=test_template.conf.edited
     24 TMP=$(mktemp -d)
     25 trap 'rm -rf "$TMP"; exit_cleanup' EXIT
     26 BASE=http://localhost:9966
     27 JSON='Content-Type: application/json'
     28 instance=$(jq -nc '{auth:{method:"token",password:"test-pass"},id:"admin",
     29   name:"Delivery test",address:{},jurisdiction:{},use_stefan:true,
     30   default_wire_transfer_delay:{d_us:3600000000},default_pay_delay:{d_us:3600000000}}')
     31 curl -fsS -H "$JSON" -d "$instance" "$BASE/management/instances"
     32 TOKEN=$(merchant_login admin test-pass)
     33 api() {
     34   curl -fsS -H "$JSON" -H "Authorization: Bearer $TOKEN" "$@"
     35 }
     36 generator() {
     37   taler-merchant-report-generator -c "$CONF" --test
     38 }
     39 reject_test() {
     40   local status
     41   status=$(curl -sS -o /dev/null -w '%{http_code}' -H "$JSON" \
     42     -H "Authorization: Bearer $TOKEN" -d "$1" "$BASE/private/report-tests")
     43   if [[ "$status" != 400 ]]; then
     44     echo "Expected HTTP 400, got $status for $1" >&2
     45     return 1
     46   fi
     47 }
     48 
     49 draft=$(jq -nc --arg target "$TMP/report" '{type:"draft",description:"Sales",program_section:"file",
     50   data_source:"/private/statistics-report/transactions?count=1",target_address:$target,
     51   mime_type:"application/json"}')
     52 # Schedule a daily report (24 hours in microseconds).
     53 scheduled=$(api -d "$(jq 'del(.type) + {report_frequency:{d_us:86400000000}}' <<< "$draft")" \
     54   "$BASE/private/reports" | jq -er .report_serial_id)
     55 api "$BASE/private/reports/$scheduled" > "$TMP/before"
     56 saved_request=$(jq -nc --argjson id "$scheduled" --arg target "$TMP/override" \
     57   '{type:"scheduled",report_serial:$id,target_address:$target}')
     58 for filter in 'del(.type)' '.type="other"' '.type=null' '.type=1' \
     59   '.report_serial=$id' '.report_serial=null'; do
     60   reject_test "$(jq --argjson id "$scheduled" "$filter" <<< "$draft")"
     61 done
     62 for filter in 'del(.type)' 'del(.report_serial)' '.report_serial=0' '.report_serial="1"'; do
     63   reject_test "$(jq "$filter" <<< "$saved_request")"
     64 done
     65 for field in description program_section mime_type data_source; do
     66   reject_test "$(jq --arg field "$field" --argjson draft "$draft" \
     67     '.[$field]=$draft[$field]' <<< "$saved_request")"
     68 done
     69 api "$BASE/private/report-deliveries" | jq -e '.deliveries == []'
     70 id=$(api -D "$TMP/headers" -d "$draft" "$BASE/private/report-tests" | jq -er .delivery_id)
     71 grep -qi "^location: report-deliveries/$id" "$TMP/headers"
     72 saved=$(api -d "$saved_request" "$BASE/private/report-tests" | jq -er .delivery_id)
     73 api "$BASE/private/report-deliveries/$id" | jq -e '.status=="queued" and .origin=="test"'
     74 api "$BASE/private/reports" | jq -e '.reports | length==1'
     75 generator &
     76 first_worker=$!
     77 generator
     78 wait "$first_worker"
     79 jq -e '.business_name=="Delivery test"' "$TMP/report.json" "$TMP/override.json"
     80 api "$BASE/private/report-deliveries/$saved" | jq -e --argjson serial "$scheduled" \
     81   '.status=="succeeded" and .report_serial==$serial and .description=="[Test] Sales"'
     82 api "$BASE/private/reports/$scheduled" | diff "$TMP/before" -
     83 
     84 DB=$(taler-merchant-config -c "$CONF" -s merchantdb-postgres -o CONFIG)
     85 schema=$(psql "$DB" -XAtc "SELECT 'merchant_instance_' || merchant_serial FROM merchant.merchant_instances WHERE merchant_id='admin'")
     86 sql() {
     87   PGOPTIONS="-c search_path=$schema" psql "$DB" -XqAt -v ON_ERROR_STOP=1 -c "$1"
     88 }
     89 # Keep the first insertion uncommitted. A second writer must wait until
     90 # the lower delivery ID is visible before it can allocate the next ID.
     91 queue="SELECT merchant.queue_report_delivery(NULL,NULL,'cursor test','file',
     92   '/private/statistics-report/transactions','unused','application/json',0)"
     93 coproc WRITER { PGOPTIONS="-c search_path=$schema" psql "$DB" -XqAt -v ON_ERROR_STOP=1; }
     94 writer_pid=$WRITER_PID
     95 printf 'BEGIN;\n%s;\n' "$queue" >&"${WRITER[1]}"
     96 read -r pending_id <&"${WRITER[0]}"
     97 [[ "$pending_id" =~ ^[0-9]+$ ]]
     98 sql "SET LOCAL lock_timeout='500ms'; DO \$\$ BEGIN
     99   PERFORM * FROM ($queue) AS inserted;
    100   RAISE EXCEPTION 'second delivery passed an uncommitted delivery';
    101 EXCEPTION WHEN lock_not_available THEN NULL; END \$\$"
    102 printf 'ROLLBACK;\n\\q\n' >&"${WRITER[1]}"
    103 wait "$writer_pid"
    104 
    105 # Simulate a worker disappearing after the database claim. Closing the
    106 # claiming connection leaves the outcome unknown and prevents another send.
    107 lost=$(api -d "$draft" "$BASE/private/report-tests" | jq -er .delivery_id)
    108 test "$(sql "SELECT merchant.claim_report_delivery(job_id,created_at,9223372036854775807,
    109   created_at,11) FROM merchant_report_deliveries WHERE delivery_id=$lost")" = "$lost"
    110 api "$BASE/private/report-deliveries/$lost" | jq -e '.status=="unknown"'
    111 # An overdue schedule takes precedence over a later test at another instance.
    112 api -d "$(jq '.id="other"' <<< "$instance")" "$BASE/management/instances"
    113 OTHER=$(merchant_login other test-pass)
    114 sql "UPDATE merchant_reports SET next_transmission=1 WHERE report_serial=$scheduled"
    115 curl -fsS -H "$JSON" -H "Authorization: Bearer $OTHER" -d "$draft" \
    116   "$BASE/instances/other/private/report-tests" > "$TMP/other-test"
    117 test "$(sql "SELECT out_merchant_id, out_report_serial FROM merchant.lookup_reports_pending()")" = "admin|$scheduled"
    118 generator
    119 api "$BASE/private/report-deliveries?report_serial=$scheduled" | jq -e \
    120   '.deliveries | map(.origin)==["scheduled","test"] and all(.[]; .status=="succeeded")'
    121 # The product lookup echoes the missing ID in its error detail. Byte 1024
    122 # falls inside the final UTF-8 character.
    123 missing_product=$(jq -nr '("a" * 1023) + "é"')
    124 bad=$(api -d "$(jq --arg id "$missing_product" '.data_source="/private/products/" + $id' <<< "$draft")" \
    125   "$BASE/private/report-tests" | jq -er .delivery_id)
    126 expired=$(api -d "$draft" "$BASE/private/report-tests" | jq -er .delivery_id)
    127 # Move creation back by 24 hours in microseconds so the queued test expires.
    128 sql "UPDATE merchant_report_deliveries SET created_at=created_at-86400000000 WHERE delivery_id=$expired"
    129 generator
    130 api "$BASE/private/report-deliveries/$bad" | jq -e \
    131   '.status=="failed" and .error_code>0 and (.error_detail | endswith("a" * 1023))'
    132 api "$BASE/private/report-deliveries/$expired" | jq -e '.status=="failed" and (.started_at==null)'
    133 api -X DELETE "$BASE/private/reports/$scheduled"
    134 test "$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: Bearer $TOKEN" \
    135   "$BASE/private/report-deliveries?limit=0")" = 400
    136 test "$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: Bearer $TOKEN" \
    137   "$BASE/private/report-deliveries?report_serial=0")" = 400
    138 api "$BASE/private/report-deliveries?limit=-1&offset=$expired" | jq -e --argjson id "$bad" '.deliveries | length==1 and .[0].delivery_id==$id'
    139 api "$BASE/private/report-deliveries?report_serial=$scheduled" | jq -e '.deliveries | length==2'
    140 sql "UPDATE merchant_report_deliveries SET created_at=0 WHERE delivery_id=$expired"
    141 test "$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: Bearer $TOKEN" \
    142   "$BASE/private/report-deliveries/$expired")" = 404
    143 taler-merchant-dbinit -c "$CONF" -g
    144 test "$(sql "SELECT count(*) FROM merchant_report_deliveries WHERE delivery_id=$expired")" = 0
    145 
    146 
    147 # A failing transmission helper (the target directory does not exist).
    148 broken=$(api -d "$(jq --arg target "$TMP/missing/output" '.target_address=$target' <<< "$draft")" \
    149   "$BASE/private/report-tests" | jq -er .delivery_id)
    150 generator
    151 api "$BASE/private/report-deliveries/$broken" | jq -e '.status=="failed" and .error_code>0'
    152 api "$BASE/private/report-deliveries/$lost" | jq -e '.status=="unknown"'
    153 
    154 # Delivery IDs and cursors belong to the authenticated instance.
    155 test "$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: Bearer $OTHER" \
    156   "$BASE/instances/other/private/report-deliveries/$saved")" = 404
    157 curl -fsS -H "Authorization: Bearer $OTHER" "$BASE/instances/other/private/report-deliveries" | jq -e '.deliveries | length==1 and .[0].origin=="test"'
    158 READONLY=$(merchant_login admin test-pass readonly)
    159 test "$(curl -s -o /dev/null -w '%{http_code}' -H "$JSON" -H "Authorization: Bearer $READONLY" \
    160   -d "$draft" "$BASE/private/report-tests")" = 401