Test an API at Its Boundaries
Two notebooks cost AED 27, yet the receipt is blank and the order says AED 24. Trace the fault with pricing, PostgreSQL and HTTP tests.
Intermediate · Lesson 5 of 12
Software Architecture
Follow two customers on a shared tablet. Scope receipt reads and pickup-branch changes to the signed-in customer, then test both the response and stored orders.

Maryam buys two notebooks and saves the receipt link on a family tablet. Later she signs out. Her brother Sami signs in on the same tablet and opens the bookmark, thinking it belongs to his purchase. What should he see? The shop recognizes Sami as a customer. The link identifies a real order. Yet the receipt belongs to Maryam. The correct screen offers a way back to Sami’s own orders and reveals nothing about hers.
Now give the bookmark a second purpose. Maryam opens it on her phone to change the pickup branch before the shop packs the notebooks. She should be able to save the change. Sami should be able to change the branch for his own eligible purchase, too. We will follow these two cases from the session to the database and design tests that check the actual receipt and saved branch. A test that merely counts successful sign-ins cannot establish either result.
In this shop, signing in creates a server-side session. The browser holds an opaque session identifier in a cookie. On a later request the framework uses the cookie to find the session, checks its validity and supplies the authenticated account to the application. Our shop associates each customer account with one customer record. Maryam resolves to customer 37 and Sami to customer 52. Those numbers are ordinary fixture values for the lesson. They come from the server’s account mapping; the checkout form has no reason to ask either shopper to type one.
Think about the bookmark after the account switch. Its order reference is exactly the same. The cookie now represents a different session, so the account resolved by the server is different. If the session expires or the person signs out, the shop requires a fresh sign-in. It never guesses the customer from the last receipt displayed by that device. Configure the framework to renew the session identifier after sign-in, invalidate the session on logout and protect the cookie in transit and from ordinary script access with Secure and HttpOnly. The cookie is a credential and its value belongs outside application logs. OWASP Session Management Cheat Sheet
Three values enter the change-branch operation. The authenticated account establishes who is acting. The order reference identifies which purchase they want to change. The new branch code describes the requested edit. Only the first comes from the session. A value supplied by the customer can select a purchase from the set available to them, but cannot expand that set. We will assume this storefront serves individual customers. Shared company purchases and staff acting on behalf of a customer would need explicit relationships and policies of their own.
| Fact | Source in this shop | Decision it supports |
|---|---|---|
| Acting customer is 37 | Valid session linked to an active customer account | Which customer’s orders are visible |
| Selected order is M410 | Receipt reference in the route | Which member of that set to read |
| Requested branch is MAJAZ | Validated form field | What change the customer wants |
| Order state is received | Current persisted row | Whether branch changes are still allowed |
Write an ownership rule that fits on a single line: a customer may read an order when its recorded customer matches their authenticated customer. To change its pickup branch, require that same relationship and require the order to be in the received state. These are separate questions. Maryam still owns the notebooks after packing begins and can read the receipt then. Her ownership does not extend the window for changing the branch. Keeping the two decisions explicit also helps the frontend show the right explanation to the right person. OWASP Authorization Cheat Sheet
Set up Maryam’s order M410 with branch NAHDA, state received and total AED 50. Set up Sami’s order S218 with branch KHAN, state received and total AED 35. Use distinct totals and branches so the test will notice if the screen shows a plausible receipt for the wrong person. A receipt query for Maryam starts from orders belonging to customer 37, then selects M410. It returns one row and renders the two-notebook receipt. The corresponding query for Sami starts from orders belonging to customer 52. M410 is absent from this set. Under our shop’s policy it yields the same public missing-order response as a reference unavailable to the customer.
In a Django repository the following small function can give list, detail and update operations the same starting point. The caller supplies a customer object already resolved by authentication. For a detail view, select the reference from the returned queryset and handle a missing row using the storefront’s ordinary missing-order response. For a list, apply search, ordering and pagination to the same queryset. Its total count must describe the customer’s own results, too. Django documentation on filtering and updating querysets
def customer_orders(customer):
return Order.objects.filter(customer_id=customer.pk)
visible = customer_orders(customer)
receipt = visible.filter(reference=order_reference).first()
This small choice changes what the view can accidentally disclose. It never needs to load the other shopper’s delivery address or total just to decide whether to build a response. If the receipt has a downloadable copy, give the download action the same ownership policy before releasing the file. If a summary is cached, keep private summaries isolated by customer and account context. A correct database query cannot protect a response served from a cache entry shared by everyone. For a small shop, rendering customer receipts on demand can be a sensible starting point.
Maryam selects MAJAZ as her new branch. The handler verifies the session, applies the framework’s CSRF protection for cookie-authenticated changes, and accepts only a supported branch code. It passes the authenticated customer, order reference and new branch to the application operation. Checking CSRF protects a different boundary from checking ownership. Both belong in the flow. Customer identity, payment total and order state remain server-controlled fields even when the browser submits a valid change.
Here is the core database operation in Django, assuming the current branch has already been validated as a permitted destination. The status and owner are conditions on the write itself. In this shop an order reference is unique, so at most one row can match. Treat a match as an accepted request to set the branch. The matched count alone does not prove that the previous branch differed. Django’s queryset update is a direct database operation; any required audit record belongs explicitly in the encompassing transaction.
matched = (
customer_orders(customer)
.filter(reference=order_reference, state="received")
.update(pickup_branch=new_branch)
)
For customer 37, M410 and MAJAZ, one row matches. After commit, a new read shows M410 at MAJAZ and S218 still at KHAN. From customer 52’s context the same proposed edit to M410 matches zero rows. A zero result is deliberately modest evidence: the order may be unavailable to this customer or may have progressed beyond received. Perform any follow-up lookup inside the same customer scope. An unavailable order gets the missing-order response. If Maryam can see her order and it has already been packed, she can receive a state-conflict explanation. Sami learns no details about that parcel.
Build an isolated test fixture with both accounts, both customer records and both purchases. Represent each signed-in customer with an independent test client. Start by visiting the order list for each. Assert the exact references and count rather than simply checking for a successful page load. For Maryam expect M410 only, with a count of one. For Sami expect S218 only. Then assert the total and original branch on each owner’s detail screen. These positive cases catch an over-restrictive implementation that denies every request and superficially passes all denial tests.
Next exercise the policy as a matrix using the fixture’s known objects. The two diagonal cells pair each customer with their own order and should allow reading and an eligible branch change. The other two cells must deny both operations. Use fresh rows for each change case or reset them between cases so that test order cannot determine the result. For a denied edit, compare the persisted branch, customer, state and audit count before and after. A denial message is a poor substitute for those assertions: the server might have written a change before formatting the response.
Give the same tablet its own test case. Sign in as Maryam, read the receipt, sign out, and sign in as Sami through the normal application flow. Reopen the bookmarked route. Check the newly rendered page for the standard missing-order response and the absence of Maryam’s receipt fields. Check that Sami’s own order is still accessible. A stale in-memory receipt in a frontend component can spoil the experience even when the server correctly denies the new read. Clearing account-specific state at the account switch and testing the rendered screen catches that separate defect.
Finally change one fact at a time. Pack M410 while Maryam’s edit form is open, then submit a valid branch. She still gets her receipt but the branch stays unchanged and the app offers a refresh. Expire her session in a separate case and repeat. Now the correct next step is sign-in, and no order edit should begin. These tests let support distinguish three customer experiences that might otherwise share a vague “something went wrong” message: a missing session, an unavailable receipt and a visible purchase whose fulfillment state prevents an edit.
A new account dashboard shows “one order ready for pickup.” Maryam has one ready order; Sami has one received order that is still being prepared. Specify the number each dashboard should show, the evidence needed for an allowed branch change by Sami, and one check after he opens Maryam’s old bookmark.
Answer. Maryam should see one and Sami zero. Filter by customer before counting ready orders. For Sami’s eligible change, verify his receipt reflects the new branch after a fresh read and that Maryam’s order is untouched. His visit to her bookmark should produce the storefront’s missing-order result with none of her pickup details. Together those observations check the summary, the individual resource and the durable write under the identity of the person actually signed in.
YOUR TAKE
Choose one reaction for this article.
A cookie helps limit repeat responses. Your feedback is private.