Define isolation as a testable invariant
Authentication on every endpoint does not guarantee tenant isolation. Multi-tenant authorization has at least two separate questions: may this identity perform the requested action, and does the target resource belong to the tenant the identity currently represents? Many bypasses occur after the first check succeeds. The application accepts an order ID, file key, or customer ID from the request and loads it directly without applying the tenant boundary again.
Before writing test cases, define an executable invariant: an identity acting for tenant A must not read, change, delete, search, export, or indirectly infer tenant B’s data unless it holds an explicit, narrowly scoped platform permission. Apply this rule to APIs, administrative tools, jobs, caches, search indexes, object storage, and event messages. The primary database is only one part of the isolation surface.
Build a tenant and identity matrix that exposes mistakes
A useful fixture needs more than one tenant and one administrator. We typically create at least two structurally similar tenants and deliberately give them confusing data: matching display names, nearby identifiers, or separate records from the same external system. Each tenant should have a regular member, tenant administrator, disabled account, and service identity. Support and platform roles need their own explicit scope rather than an assumed universal exception.
Describe each test as an actor, represented tenant, resource type, resource owner, and action. Include allowed paths, but treat cross-tenant denials, role downgrades, tenant switching, expired tokens, and membership removal as first-class cases. If support staff receive temporary access, test approval, expiration, auditability, and revocation. A platform role that silently becomes a permanent master key is an operational risk even when it is technically intentional.
- Direct object substitution: Replace a resource ID in a URL, GraphQL variable, or request body with a valid ID owned by another tenant.
- Relationship mismatch: Combine tenant A’s project with tenant B’s file, user, billing record, or external connection.
- Scope manipulation: Change tenantId, ownerId, filters, sort fields, pagination cursors, and lists of IDs in bulk requests.
- Identity transitions: Replay an old token, reuse cached state after switching tenants, lower a role, or retry after membership removal.
Test the complete data path, not only the status code
A 403 response is not sufficient evidence. A handler may update a row before rejecting the response, or leak data into an audit entry, notification, queue, or cache. Every negative test should inspect the response, persistent state, emitted events, generated files, cache entries, and scheduled jobs. Deletions and bulk updates also need assertions that the entire transaction rolled back.
Frequently missed surfaces include search, autocomplete, counts, error messages, exports, pre-signed URLs, webhook replays, and asynchronous reports. These features often bypass the main repository or service layer. Maintain a data-flow inventory from each HTTP or message entry point through databases, search engines, object storage, and external SaaS systems. At every boundary, identify where tenant context is created, propagated, and independently checked.
- Lists, details, searches, and aggregates must not expose another tenant’s records or inferable counts.
- Writes, deletes, relationships, and bulk operations must constrain targets by both resource ID and tenant ID.
- Jobs and queue messages must carry trusted tenant context, and consumers must validate it again.
- Cache keys, indexes, storage prefixes, and download tokens must contain an unambiguous tenant boundary.
- Error responses should not reveal whether a foreign resource exists, what it is called, or who owns it.
Place enforcement where developers cannot easily forget it
If every engineer must remember to append tenant_id to every query, the test suite will eventually find an omission. A stronger design centralizes scoping in the data-access interface, an ORM scope, or database Row-Level Security, and fails closed when tenant context is absent. Application-level filtering over shared tables is straightforward, but it relies heavily on consistent coding. Database policies add an independent barrier, with additional complexity around connection pooling, migrations, debugging, and platform administration.
Separate schemas or databases can reduce the impact of some mistakes, but they do not prevent selecting the wrong database, using an unsafe cache key, or running a cross-tenant integration job incorrectly. Choose the model according to isolation requirements, tenant count, operational cost, and migration needs. Whatever the architecture, add mutation tests that deliberately remove a tenant predicate or disable a policy. The suite should fail clearly; otherwise, a green result may only mean that the real isolation control was never exercised.
Make isolation testing a release condition
Run the core authorization matrix as integration tests on every merge. Exercise exports, queues, search, and object storage in an environment that includes those real components. Randomized resource identifiers, property-based tests, and API schema inspection can reveal new endpoints or parameters that escaped established scoping rules. Keep the environment representative: an in-memory queue or local filesystem can hide tenant-context failures that appear only in production infrastructure.
Production still needs detection. Record the actor, effective tenant, target tenant, resource type, and authorization outcome without placing sensitive content in logs. Sudden cross-tenant denials, unusual platform-role access, or jobs missing tenant context should become investigable signals. The goal is not to claim that a system can never contain an authorization flaw. It is to require every new data path to satisfy the same isolation contract—and to make violations fail early, visibly, and safely.