#!/bin/bash set -e echo "Logging in..." TOKEN=$(curl -s -X POST -H "Content-Type: application/json" -d '{"email":"test@example.com", "password":"password123"}' http://localhost:8000/auth/login | jq -r .access_token) echo "Token obtained." echo "Getting Chest ID..." CHEST_ID=$(curl -s http://localhost:8000/catalog/chests | jq -r '.[0].id') echo "Chest ID: $CHEST_ID" KEY="idempotency-test-$(date +%s)" echo "--- Request 1 (Key: $KEY) ---" RESP1=$(curl -s -X POST -H "Authorization: Bearer $TOKEN" -H "Idempotency-Key: $KEY" http://localhost:8000/chests/$CHEST_ID/open) echo "Response 1: $RESP1" BALANCE1=$(curl -s -H "Authorization: Bearer $TOKEN" http://localhost:8000/me/wallet | jq -r .balance) echo "Balance after 1st request: $BALANCE1" echo "--- Request 2 (Key: $KEY - Retry) ---" RESP2=$(curl -s -X POST -H "Authorization: Bearer $TOKEN" -H "Idempotency-Key: $KEY" http://localhost:8000/chests/$CHEST_ID/open) echo "Response 2: $RESP2" BALANCE2=$(curl -s -H "Authorization: Bearer $TOKEN" http://localhost:8000/me/wallet | jq -r .balance) echo "Balance after 2nd request: $BALANCE2" if [ "$RESP1" == "$RESP2" ]; then echo "SUCCESS: Responses are identical." else echo "FAILURE: Responses differ." exit 1 fi if [ "$BALANCE1" == "$BALANCE2" ]; then echo "SUCCESS: Balance did not change on retry." else echo "FAILURE: Balance changed on retry!" exit 1 fi KEY_NEW="idempotency-test-new-$(date +%s)" echo "--- Request 3 (New Key: $KEY_NEW) ---" RESP3=$(curl -s -X POST -H "Authorization: Bearer $TOKEN" -H "Idempotency-Key: $KEY_NEW" http://localhost:8000/chests/$CHEST_ID/open) echo "Response 3: $RESP3" BALANCE3=$(curl -s -H "Authorization: Bearer $TOKEN" http://localhost:8000/me/wallet | jq -r .balance) echo "Balance after 3rd request: $BALANCE3" if [ "$BALANCE3" -lt "$BALANCE2" ]; then echo "SUCCESS: Balance decreased on new request." else echo "FAILURE: Balance did not decrease on new request." exit 1 fi echo "ALL TESTS PASSED"