Business logic and race condition testing
Brief
A workflow is only as strict as the server's own record of where each request sits in it, and scanners cannot see a step that was skipped or a coupon that worked twice. The review walks each rule the business relies on with test accounts and the client's test data, and checks single-use actions with exactly two simultaneous requests.
On this page
- Situation
- Two test accounts, the client's test catalogue, a test coupon and a test gift card issued for us, and a short written list of the rules the workflow should enforce (order of steps, quantity limits, single-use items, per-account limits). Every order, coupon and balance on this page belongs to the test data the client issued, and only the applications the scope letter names are tested. Concurrency never goes above two requests at once and nothing here is a load test.
- Goal
- A rule table for each workflow: the rule, the request that tested it, the result, and the server-side check that enforces or should enforce it.
- Outputs
- Captured on 4 Nov 2026 against the Atarus Bench Web and API lab fixture, edition
web-api-0.2, running on loopback, with one origin playing every host, renamed to the example domains and printed ashttps(the lab ran plainhttp). The race output is one run: the two status lines can come back in either order, and the balance line is the result.
Check first
- The rules, written down by the client: the order of steps, quantity and value limits, which items are single use, and per-account limits.
- Test data issued for us: a test coupon, a test gift card with a known balance, and test products at test prices.
- Who voids or resets test orders, and how, before the first request.
- Whether the payment provider is in test mode for the environment we use.
- Whether the application logs order state changes with the account and the request id.
Steps
Can a workflow step be skipped?
curl -s -X POST https://shop.example.com/api/orders/ord_test_a_0107/confirm \
-H "Authorization: Bearer $TESTER_A_TOKEN" -H 'Content-Type: application/json' -d '{}'
Expected output
{"id":"ord_test_a_0107","status":"confirmed","payment_status":"unpaid"} # <- confirmed without the payment step
ord_test_a_0107 is tester-a's own test order, stopped after the shipping step. We send the request the last step makes, without the one before it. The same check applies to sign-up flows that verify email after the account is usable and approval flows where the approve call can be sent before review.
Negative result. A 409 or 422 naming the missing step, and the order still at its earlier state when read back.
Fix. Keep the workflow state on the server and allow each transition only from the state before it. The confirm handler reads the order's state and payment record itself; it never trusts that the client walked the steps in order.
Does the server accept negative, zero or boundary quantities?
for q in -1 0 1000000; do
curl -s -X PATCH https://shop.example.com/api/cart/items/item_test_01 \
-H "Authorization: Bearer $TESTER_A_TOKEN" -H 'Content-Type: application/json' \
-d "{\"quantity\":$q}" | jq -c '{quantity, line_total, cart_total}'
done
Expected output
{"quantity":-1,"line_total":"-24.00","cart_total":"-24.00"} # <- a negative line reduces the total
{"quantity":0,"line_total":"0.00","cart_total":"0.00"}
{"quantity":1000000,"line_total":"24000000.00","cart_total":"24000000.00"}
Three values per numeric field: below the minimum, zero, and above any sensible maximum. A negative line on its own is a display bug; beside a positive line it lowers what the other item costs. We stop at the cart and never pay for a cart with a negative line.
Negative result. Each value rejected with a validation error naming the field and its allowed range.
Fix. Validate quantity, amount and count fields on the server with an explicit range per field, and recompute totals from catalogue prices rather than from anything the client sends.
Can a coupon or balance be used twice?
for o in ord_test_a_0108 ord_test_a_0109; do
curl -s -X POST "https://shop.example.com/api/orders/$o/coupon" \
-H "Authorization: Bearer $TESTER_A_TOKEN" -H 'Content-Type: application/json' \
-d '{"code":"WELCOME-TEST"}' | jq -c '{id, discount}'
done
Expected output
{"id":"ord_test_a_0108","discount":"10.00"}
{"id":"ord_test_a_0109","discount":"10.00"} # <- a once per customer coupon applied to a second order
WELCOME-TEST is the client's test copy of a once per customer coupon. We also try removing it from a cancelled order and applying it again, and a second test account that shares tester-a's address, since many rules are meant per person, not per account.
Negative result. The second application refused, and the coupon's redemption count reads one.
Fix. Record redemptions in a table with a unique constraint on the coupon and the customer, and apply the discount in the same transaction as the insert.
Do two simultaneous requests double-apply a limited action?
for i in 1 2; do
curl -s -o "redeem-$i.json" -w "$i %{http_code}\n" -X POST https://shop.example.com/api/giftcards/redeem \
-H "Authorization: Bearer $TESTER_A_TOKEN" -H 'Content-Type: application/json' \
-d '{"code":"GC-TEST-0001","order":"ord_test_a_0110"}' &
done; wait
curl -s https://shop.example.com/api/giftcards/GC-TEST-0001 -H "Authorization: Bearer $TESTER_A_TOKEN" | jq -c '{balance, redemptions}'
Expected output
2 200
1 200
{"balance":"-50.00","redemptions":2} # <- a 50.00 card spent twice by two requests in the same moment
Exactly two requests, sent together, on a test gift card with a 50.00 balance. The block above is a single run: which request finishes first changes from run to run, so the two status lines can swap, and the balance line is what we read. A check that reads the balance and a write that spends it are two operations, and both requests can pass the check before either writes. Timing varies, so a pair that does not collide proves little; we repeat the pair up to five times, each time on a fresh test card the client issues, and never raise the count above two.
Negative result. One 200 and one refusal in every pair, and the balance never below zero.
Why this works
The window between reading a value and writing it back is short but real, and two requests that arrive inside it both see the old value.
Fix. Make the check and the write one operation: a conditional update (UPDATE ... SET balance = balance - 50 WHERE balance >= 50) whose row count decides success, a row lock, or an idempotency key on the redemption. Add a database constraint that the balance cannot go below zero.
The defender sees
Each request here is an ordinary customer action from a test account, so the signal is in business records rather than in access logs: an order confirmed with no payment record, a negative line total, a coupon redeemed twice by one customer, two redemptions of one card in the same second, and a balance below zero. A reconciliation job that compares orders, payments and redemptions each day finds all four. If nothing flagged them, the missing reconciliation is a finding, and the business impact page shows how to state what it would have cost.
Done when
Each listed rule has been tested with a test account, each single-use item has had one pair of simultaneous requests, and every test order, coupon and balance has been read back and returned to its starting state or voided by the client.
Not covered
Object ownership (see IDOR testing), proving money impact (see business impact), and load or denial of service.