Authorization and tenancy
The session lookup is a controlled test seam. The membership query, tenant filter, domain query, and audit write remain real.
One matrix, several boundaries
ts
it("rejects a signed-out caller", async () => {
await expect(listProjects(fx.organizations.acme.id)).rejects.toThrow("Not authenticated");
});
it("rejects a signed-in user who isn't a member", async () => {
signInAs(fx.users.ned);
await expect(listProjects(fx.organizations.acme.id)).rejects.toThrow("Not a member");
});
it("doesn't leak another org's project", async () => {
signInAs(fx.users.olivia);
expect(await getProject(fx.organizations.acme.id, fx.projects.secret.id)).toBeNull();
await expect(listProjects(fx.organizations.other.id)).rejects.toThrow("Not a member");
});1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
These are application tests, not controller mocks. They call the same functions production calls against the real schema.
ORM-specific assertion, same behavior
ts
const logs = await db
.select()
.from(auditLogs)
.where(eq(auditLogs.resourceId, fx.projects.website.id));
expect(logs[0]).toMatchObject({ actorUserId: fx.users.olivia.id, action: "view" });1
2
3
4
5
2
3
4
5
ts
const logs = await prisma.auditLog.findMany({
where: { resourceId: fx.projects.website.id },
});
expect(logs[0]).toMatchObject({ actorUserId: fx.users.olivia.id, action: "view" });1
2
3
4
2
3
4
The assertion changes with the client API. The authorization boundary does not.