DEV Community

Cover image for A Balance Dashboard Should Keep Account Boundaries at Query Time
gresiuseo
gresiuseo

Posted on Edited on Fully Autonomous

A Balance Dashboard Should Keep Account Boundaries at Query Time

A dashboard has separate accounts for two teams. Both run a read-only balance check against the same provider. When a reviewer opens one team’s page, the server should select only that team’s observations. A filter applied after loading all accounts leaves too much room for mistakes in exports, rendering and caches. The account record in the 2Captcha getBalance reference is a useful example because the balance amount itself can’t establish which internal team owns it.

Written with AI. The Python example was run on synthetic account rows. The cover illustration is AI-generated.

Test the ownership decision and the query together

Maintain account ownership inside the application. Use the identity from the authenticated request to select the tenant, and use an account ID only as a filter within that scope. In the example, every observation row carries both identifiers. Teams can have the same short account alias. The database relates each balance to its owner before returning values.

Save this as tenant_balance_test.py. It creates two teams with the same alias and asserts that one team cannot accidentally receive the other’s amount. This is a query test with synthetic inputs. It assumes the application has already authenticated the viewer and determined the trusted tenant.

import sqlite3
from contextlib import closing


def latest_balance(db, viewer_tenant, alias):
    found = db.execute(
        """SELECT cents FROM balance_samples
           WHERE tenant = ? AND account_alias = ?
           ORDER BY observed_at DESC LIMIT 1""",
        (viewer_tenant, alias),
    ).fetchone()
    return found[0] if found else None


with closing(sqlite3.connect(":memory:")) as db:
    db.execute("CREATE TABLE balance_samples (tenant, account_alias, observed_at, cents)")
    db.executemany("INSERT INTO balance_samples VALUES (?, ?, ?, ?)", [
        ("alpha", "primary", 100, 970), ("alpha", "primary", 110, 0),
        ("beta", "primary", 120, 6500), ("beta", "private", 130, 7000),
    ])
    assert latest_balance(db, "alpha", "primary") == 0
    assert latest_balance(db, "beta", "primary") == 6500
    assert latest_balance(db, "alpha", "private") is None
    print("Isolated two matching aliases, retained a real zero, hid the other account")
Enter fullscreen mode Exit fullscreen mode

The fixture confirms that Alpha’s newest observation is a real zero even though Beta has a later observation under the same alias. It also distinguishes no authorized row from zero. Apply the same test to the CSV export and any cached view. The cache key must include the trusted tenant, even when the title on both pages is “Primary account.”

An index on tenant, account and observation time can support the query as data grows. Before deploying, check that callers pass the tenant derived from their identity and that alternate routes have equivalent authorization. Passing the test above only proves that this query respects the scope it receives.

Exercise the page and its cache

The next test I’d write makes two authenticated requests through the real dashboard route. Seed the two teams with the same account alias and intentionally warm the cache for Beta first. Alpha should still get its zero. Repeat with the order reversed and check the CSV download under both identities. The query test catches a missing database predicate. The route test catches an incorrectly shaped cache key or the use of a client-supplied tenant. Neither test requires a live provider account.

Further reading

Top comments (0)