test_webhook_trigger.sh (12717B)
1 #!/bin/bash 2 # This file is in the public domain. 3 4 set -eu 5 6 echo -n "Test if OpenSSL is available..." 7 openssl &>/dev/null || (echo " No!" ; exit 77 ) 8 echo " OK" 9 10 taler-merchant-dbinit -r -c test-merchantdb-postgres.conf 11 12 # Generate random BYTEA values for testing 13 WTID=$(openssl rand -hex 32) 14 EXCHANGE_PUB=$(openssl rand -hex 32) 15 EXCHANGE_SIG=$(openssl rand -hex 64) 16 COIN_PUB=$(openssl rand -hex 32) 17 18 # Generate valid BYTEA values 19 MERCHANT_PUB=$(openssl rand -hex 32) 20 AUTH_HASH=$(openssl rand -hex 64) 21 AUTH_SALT=$(openssl rand -hex 32) 22 23 # Define other parameters 24 INSTANCE_ID="test-merchant" 25 EXCHANGE_URL="http://example.com/exchange" 26 PAYTO_URI="payto://example.com/account" 27 EXECUTION_TIME=$(date +%s) 28 29 # Generate taler_amount_currency values 30 TOTAL_AMOUNT="ROW(100, 0, 'USD')::taler_amount_currency" 31 WIRE_FEE="ROW(1, 0, 'USD')::taler_amount_currency" 32 33 # Generate arrays for taler_amount_currency and BYTEA 34 # Generate arrays for taler_amount_currency and BYTEA 35 COIN_VALUES="ARRAY[ROW(10, 0, 'USD')::taler_amount_currency, ROW(20, 0, 'USD')::taler_amount_currency, ROW(30, 0, 'USD')::taler_amount_currency]::taler_amount_currency[]" 36 DEPOSIT_FEES="ARRAY[ROW(1, 0, 'USD')::taler_amount_currency, ROW(2, 50000000, 'USD')::taler_amount_currency, ROW(3, 25000000, 'USD')::taler_amount_currency]::taler_amount_currency[]" 37 COIN_PUBS="ARRAY[decode('$COIN_PUB', 'hex'), decode('$(openssl rand -hex 32)', 'hex'), decode('$(openssl rand -hex 32)', 'hex')]::BYTEA[]" 38 CONTRACT_TERMS="ARRAY[decode('$(openssl rand -hex 32)', 'hex'), decode('$(openssl rand -hex 32)', 'hex'), decode('$(openssl rand -hex 32)', 'hex')]::BYTEA[]" 39 40 # Create the merchant database 41 echo "SET search_path TO merchant; 42 INSERT INTO merchant_instances ( 43 merchant_serial, 44 merchant_name, 45 merchant_id, 46 address, 47 jurisdiction, 48 default_wire_transfer_delay, 49 default_pay_delay, 50 merchant_pub, 51 auth_hash, 52 auth_salt 53 ) VALUES ( 54 1, 55 'Test Merchant', 56 'test-merchant', 57 '{"city":"TestCity"}', 58 '{"country":"TestCountry"}', 59 3600, 60 3600, 61 decode('$MERCHANT_PUB', 'hex'), 62 decode('$AUTH_HASH', 'hex'), 63 decode('$AUTH_SALT', 'hex') 64 );" | psql talercheck 65 66 # Create the webhooks 67 echo "SET search_path TO merchant; 68 INSERT INTO merchant_webhook (merchant_serial, webhook_id, event_type, url, http_method, body_template) 69 VALUES 70 (1, 'category_added_hook', 'category_added', 'http://example.com/category_added', 'POST', '{"event":"{{ webhook_type }}"}'), 71 (1, 'category_updated_hook', 'category_updated', 'http://example.com/category_updated', 'POST', '{"event":"{{ webhook_type }}"}'), 72 (1, 'category_deleted_hook', 'category_deleted', 'http://example.com/category_deleted', 'POST', '{"event":"{{ webhook_type }}"}'), 73 (1, 'inventory_added_hook', 'inventory_added', 'http://example.com/inventory_added', 'POST', '{"event":"{{ webhook_type }}"}'), 74 (1, 'inventory_updated_hook', 'inventory_updated', 'http://example.com/inventory_updated', 'POST', '{"event":"{{ webhook_type }}"}'), 75 (1, 'inventory_deleted_hook', 'inventory_deleted', 'http://example.com/inventory_deleted', 'POST', '{"event":"{{ webhook_type }}"}'), 76 (1, 'order_settled_hook', 'order_settled', 'http://example.com/order_settled', 'POST', '{"event":"{{ webhook_type }}"}');" | psql talercheck 77 78 # Create the category 79 echo "SET search_path TO merchant; 80 INSERT INTO merchant_categories (merchant_serial, category_name, category_name_i18n) 81 VALUES (1, 'Test Category', '{"en":"Test Category"}');" | psql talercheck 82 83 # Update the category 84 echo "SET search_path TO merchant; 85 UPDATE merchant_categories 86 SET category_name = 'Updated Category' 87 WHERE category_name = 'Test Category';" | psql talercheck 88 89 # Delete the category 90 echo "SET search_path TO merchant; 91 DELETE FROM merchant_categories 92 WHERE category_name = 'Updated Category';" | psql talercheck 93 94 # Add product to inventory 95 echo "SET search_path TO merchant; 96 INSERT INTO merchant_inventory (merchant_serial, product_id, description, description_i18n, unit, image, taxes, price, total_stock, address, next_restock) 97 VALUES 98 (1, 'test-product', 'Test Product', '{"en":"Test Product"}', 'unit', 'image-data-url', '[]', ROW(10, 0, 'USD'), 100, '{"city":"TestCity"}', 0);" | psql talercheck 99 100 # Update product in inventory 101 echo "SET search_path TO merchant; 102 UPDATE merchant_inventory 103 SET description = 'Updated Test Product' 104 WHERE product_id = 'test-product';" | psql talercheck 105 106 # Delete product from inventory 107 echo "SET search_path TO merchant; 108 DELETE FROM merchant_inventory 109 WHERE product_id = 'test-product';" | psql talercheck 110 111 # Create the merchant account 112 echo "SET search_path TO merchant; 113 INSERT INTO merchant_accounts ( 114 merchant_serial, 115 h_wire, 116 salt, 117 credit_facade_url, 118 credit_facade_credentials, 119 last_bank_serial, 120 payto_uri, 121 active 122 ) VALUES ( 123 1, 124 decode('$(openssl rand -hex 64)', 'hex'), 125 decode('$(openssl rand -hex 16)', 'hex'), 126 'http://example.com/credit', 127 '{"key":"value"}', 128 0, 129 '$PAYTO_URI', 130 TRUE 131 );" | psql talercheck 132 133 # Create the merchant transfer 134 echo "SET search_path TO merchant; 135 INSERT INTO merchant_transfers ( 136 credit_serial, 137 exchange_url, 138 wtid, 139 credit_amount, 140 account_serial 141 ) VALUES ( 142 1, 143 'http://example.com/exchange', 144 decode('$WTID', 'hex'), 145 ROW(100, 0, 'USD')::taler_amount_currency, 146 1 147 );" | psql talercheck 148 149 # Create the exchange signing key 150 echo "SET search_path TO merchant; 151 INSERT INTO merchant_exchange_signing_keys ( 152 master_pub, 153 exchange_pub, 154 start_date, 155 expire_date, 156 end_date, 157 master_sig 158 ) VALUES ( 159 decode('$(openssl rand -hex 32)', 'hex'), 160 decode('$EXCHANGE_PUB', 'hex'), 161 EXTRACT(EPOCH FROM NOW()), 162 EXTRACT(EPOCH FROM NOW()) + 3600, 163 EXTRACT(EPOCH FROM NOW()) + 7200, 164 decode('$(openssl rand -hex 64)', 'hex') 165 );" | psql talercheck 166 167 # Create the merchant_contract_terms 168 echo "SET search_path TO merchant; 169 INSERT INTO merchant_contract_terms ( 170 order_serial, 171 merchant_serial, 172 order_id, 173 contract_terms, 174 wallet_data, 175 h_contract_terms, 176 creation_time, 177 pay_deadline, 178 refund_deadline, 179 paid, 180 wired, 181 fulfillment_url, 182 session_id, 183 pos_key, 184 pos_algorithm, 185 claim_token 186 ) VALUES ( 187 1001, 188 1, 189 'order-1234', 190 decode('$(openssl rand -hex 64)', 'hex'), 191 '{"wallet":"test"}', 192 decode('$(openssl rand -hex 64)', 'hex'), 193 EXTRACT(EPOCH FROM NOW()), 194 EXTRACT(EPOCH FROM NOW()) + 3600, 195 EXTRACT(EPOCH FROM NOW()) + 7200, 196 FALSE, 197 FALSE, 198 'http://example.com/fulfillment', 199 '', 200 NULL, 201 0, 202 decode('$(openssl rand -hex 16)', 'hex') 203 );" | psql talercheck 204 205 echo "SET search_path TO merchant; 206 INSERT INTO merchant_deposit_confirmations ( 207 order_serial, 208 deposit_timestamp, 209 exchange_url, 210 total_without_fee, 211 wire_fee, 212 signkey_serial, 213 exchange_sig, 214 account_serial, 215 wire_pending 216 ) VALUES ( 217 1001, 218 EXTRACT(EPOCH FROM NOW()), 219 '$EXCHANGE_URL', 220 ROW(90, 0, 'USD')::taler_amount_currency, 221 ROW(10, 0, 'USD')::taler_amount_currency, 222 1, 223 decode('$EXCHANGE_SIG', 'hex'), 224 1, 225 TRUE 226 );" | psql talercheck 227 228 echo "SET search_path TO merchant; 229 UPDATE merchant_contract_terms 230 SET wired = TRUE 231 WHERE order_serial = 1001 232 AND NOT EXISTS ( 233 SELECT 1 234 FROM merchant_deposit_confirmations 235 WHERE wire_pending 236 AND order_serial = 1001 237 );" | psql talercheck 238 239 # Handle deposit confirmation 240 echo "SET search_path TO merchant; 241 INSERT INTO merchant_deposit_confirmations ( 242 order_serial, 243 deposit_timestamp, 244 exchange_url, 245 total_without_fee, 246 wire_fee, 247 signkey_serial, 248 exchange_sig, 249 account_serial, 250 wire_pending 251 ) VALUES ( 252 1001, 253 EXTRACT(EPOCH FROM NOW()), 254 'http://example.com/exchange', 255 ROW(90, 0, 'USD')::taler_amount_currency, 256 ROW(10, 0, 'USD')::taler_amount_currency, 257 1, 258 decode('$(openssl rand -hex 64)', 'hex'), 259 1, 260 FALSE 261 );" | psql talercheck 262 263 echo "SET search_path TO merchant; 264 UPDATE merchant_contract_terms 265 SET wired = TRUE 266 WHERE order_serial = 1001 267 AND NOT EXISTS ( 268 SELECT 1 269 FROM merchant_deposit_confirmations 270 WHERE wire_pending 271 AND order_serial = 1001 272 );" | psql talercheck 273 274 # Add entries to merchant_deposits 275 echo "SET search_path TO merchant; 276 INSERT INTO merchant_deposits ( 277 deposit_serial, 278 coin_offset, 279 deposit_confirmation_serial, 280 coin_pub, 281 coin_sig, 282 amount_with_fee, 283 deposit_fee, 284 refund_fee 285 ) VALUES ( 286 (SELECT COALESCE(MAX(deposit_serial), 0) + 1 FROM merchant_deposits), -- deposit_serial 287 0, -- coin_offset 288 1, -- deposit_confirmation_serial (link to confirmation) 289 decode('$COIN_PUB', 'hex'), -- coin_pub 290 decode('$(openssl rand -hex 64)', 'hex'), -- coin_sig 291 ROW(30, 0, 'USD')::taler_amount_currency, -- amount_with_fee 292 ROW(1, 0, 'USD')::taler_amount_currency, -- deposit_fee 293 ROW(0, 0, 'USD')::taler_amount_currency -- refund_fee 294 );" | psql talercheck 295 296 # Link deposits to wire transfers 297 echo "SET search_path TO merchant; 298 INSERT INTO merchant_deposit_to_transfer ( 299 deposit_serial, 300 coin_contribution_value, 301 execution_time, 302 signkey_serial, 303 exchange_sig, 304 wtid 305 ) VALUES ( 306 (SELECT MAX(deposit_serial) FROM merchant_deposits), -- deposit_serial 307 ROW(10, 0, 'USD')::taler_amount_currency, -- coin_contribution_value 308 $EXECUTION_TIME, -- execution_time 309 1, -- signkey_serial 310 decode('$(openssl rand -hex 64)', 'hex'), -- exchange_sig 311 decode('$WTID', 'hex') -- wtid 312 );" | psql talercheck 313 314 315 # Debug the UPDATE condition 316 echo "SET search_path TO merchant; 317 SELECT md.deposit_confirmation_serial, mdtt.wtid 318 FROM merchant_deposits md 319 LEFT JOIN merchant_deposit_to_transfer mdtt 320 USING (deposit_serial) 321 WHERE md.deposit_confirmation_serial=1 322 AND mdtt.wtid IS NULL;" | psql talercheck 323 324 # Re-run the update logic 325 echo "SET search_path TO merchant; 326 UPDATE merchant_deposit_confirmations 327 SET wire_pending=FALSE 328 WHERE deposit_confirmation_serial=1 329 AND NOT EXISTS 330 (SELECT 1 331 FROM merchant_deposits md 332 LEFT JOIN merchant_deposit_to_transfer mdtt 333 USING (deposit_serial) 334 WHERE md.deposit_confirmation_serial=1 335 AND mdtt.wtid IS NULL);" | psql talercheck 336 337 338 echo "SET search_path TO merchant; 339 SELECT merchant_do_insert_transfer_details( 340 '$INSTANCE_ID', 341 '$EXCHANGE_URL', 342 '$PAYTO_URI', 343 decode('$WTID', 'hex')::BYTEA, 344 $EXECUTION_TIME::BIGINT, 345 decode('$EXCHANGE_PUB', 'hex')::BYTEA, 346 decode('$EXCHANGE_SIG', 'hex')::BYTEA, 347 $TOTAL_AMOUNT, 348 $WIRE_FEE, 349 $COIN_VALUES, 350 $DEPOSIT_FEES, 351 $COIN_PUBS, 352 $CONTRACT_TERMS 353 );" | psql talercheck 354 355 # Echo all webhooks 356 echo "SET search_path TO merchant; 357 SELECT * FROM merchant_pending_webhooks;" | psql talercheck 358 359 # Fetch and validate the number of pending webhooks 360 WEBHOOK_COUNT=$(echo "SET search_path TO merchant; 361 SELECT COUNT(*) 362 FROM merchant_pending_webhooks;" | psql talercheck -t -A | tr -d '[:space:]') 363 364 if [ "$WEBHOOK_COUNT" -ne 7 ]; then 365 echo "ERROR: Expected 7 webhook entries, but received $WEBHOOK_COUNT." 366 exit 1 367 else 368 echo "SUCCESS: Received 7 webhook entries as expected." 369 fi