Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -0,0 +1,167 @@
|
||||
// @vitest-environment node
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { getRlsExecutionContext } from "@hivegrid/db/rls-context.server";
|
||||
|
||||
const getUserInfoMock = vi.fn();
|
||||
const createMediaDownloadUrlMock = vi.fn();
|
||||
const userFindUniqueMock = vi.fn();
|
||||
const tenantUserFindManyMock = vi.fn();
|
||||
|
||||
vi.mock("~/utils/session.server", () => ({
|
||||
getUserInfo: getUserInfoMock,
|
||||
}));
|
||||
vi.mock("~/utils/integrations/mediaSigning.server", () => ({
|
||||
createMediaDownloadUrl: createMediaDownloadUrlMock,
|
||||
}));
|
||||
vi.mock("~/utils/db.server", () => ({
|
||||
db: {
|
||||
user: { findUnique: userFindUniqueMock },
|
||||
tenantUser: { findMany: tenantUserFindManyMock },
|
||||
},
|
||||
}));
|
||||
|
||||
type UserFixture = { avatar: string | null; tenants: string[]; operator?: boolean };
|
||||
|
||||
// The mocked client stands in for the database *including* its policies. Under
|
||||
// RLS the route sees nothing it has not bound an identity for, so a mock that
|
||||
// answers every query would hide exactly the production bug this route guards
|
||||
// against (a context-free read returns zero rows once the app connects as
|
||||
// hivegrid_app). Visibility here mirrors hg_User_select / hg_TenantUser_select.
|
||||
let users: Record<string, UserFixture>;
|
||||
const boundContexts: Array<{ userId?: string; tenantId?: string }> = [];
|
||||
|
||||
function isVisibleUser(targetId: string, target: UserFixture): boolean {
|
||||
const context = getRlsExecutionContext();
|
||||
if (!context?.userId) {
|
||||
return false;
|
||||
}
|
||||
if (targetId === context.userId) {
|
||||
return true;
|
||||
}
|
||||
if (users[context.userId]?.operator) {
|
||||
return true;
|
||||
}
|
||||
return context.tenantId ? target.tenants.includes(context.tenantId) : false;
|
||||
}
|
||||
|
||||
async function importLoader() {
|
||||
const mod = await import("./avatars.$userId");
|
||||
return mod.loader;
|
||||
}
|
||||
|
||||
function call(userId: string | undefined) {
|
||||
return importLoader().then((loader) =>
|
||||
loader({
|
||||
request: new Request("http://app.localhost/avatars/target"),
|
||||
params: { userId },
|
||||
context: {},
|
||||
} as unknown as Parameters<Awaited<ReturnType<typeof importLoader>>>[0])
|
||||
);
|
||||
}
|
||||
|
||||
const SIGNED_URL = "https://fsn1.example.test/bucket/users/target/avatar?X-Amz-Signature=abc";
|
||||
|
||||
describe("avatar auth-proxy", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
boundContexts.length = 0;
|
||||
getUserInfoMock.mockResolvedValue({ userId: "requester" });
|
||||
createMediaDownloadUrlMock.mockResolvedValue(SIGNED_URL);
|
||||
// Default: requester and target share workspace "t1"; neither is an operator.
|
||||
users = {
|
||||
requester: { avatar: null, tenants: ["t1"] },
|
||||
target: { avatar: "/avatars/target?v=1", tenants: ["t1"] },
|
||||
};
|
||||
|
||||
userFindUniqueMock.mockImplementation(async ({ where, select }: { where: { id: string }; select: Record<string, boolean> }) => {
|
||||
boundContexts.push({ userId: getRlsExecutionContext()?.userId, tenantId: getRlsExecutionContext()?.tenantId });
|
||||
const target = users[where.id];
|
||||
if (!target || !isVisibleUser(where.id, target)) {
|
||||
return null;
|
||||
}
|
||||
return select.avatar ? { avatar: target.avatar } : target;
|
||||
});
|
||||
|
||||
// Own memberships are the one projection visible without a tenant bound.
|
||||
tenantUserFindManyMock.mockImplementation(async ({ where }: { where: { userId: string } }) => {
|
||||
const context = getRlsExecutionContext();
|
||||
if (context?.userId !== where.userId) {
|
||||
return [];
|
||||
}
|
||||
return (users[where.userId]?.tenants ?? []).map((tenantId) => ({ tenantId }));
|
||||
});
|
||||
});
|
||||
|
||||
it("redirects a co-worker to a short-lived signed URL", async () => {
|
||||
const response = (await call("target")) as Response;
|
||||
|
||||
expect(response.status).toBe(302);
|
||||
expect(response.headers.get("location")).toBe(SIGNED_URL);
|
||||
// The private bucket is only ever reached through the deterministic key.
|
||||
expect(createMediaDownloadUrlMock).toHaveBeenCalledWith({ key: "users/target/avatar" });
|
||||
});
|
||||
|
||||
it("binds the shared tenant before reading a co-worker's row, since RLS hides it otherwise", async () => {
|
||||
await call("target");
|
||||
|
||||
// First read runs on the user identity alone (owner/operator); only the
|
||||
// retry carries the tenant that proves the shared membership.
|
||||
expect(boundContexts).toEqual([
|
||||
{ userId: "requester", tenantId: undefined },
|
||||
{ userId: "requester", tenantId: "t1" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("serves the owner their own picture without a membership lookup", async () => {
|
||||
getUserInfoMock.mockResolvedValue({ userId: "target" });
|
||||
|
||||
const response = (await call("target")) as Response;
|
||||
|
||||
expect(response.status).toBe(302);
|
||||
expect(tenantUserFindManyMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("answers 404 — not 403 — for a user who shares no workspace", async () => {
|
||||
users.target.tenants = ["t2"];
|
||||
|
||||
const response = (await call("target")) as Response;
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(createMediaDownloadUrlMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("never signs a URL for an unauthenticated request", async () => {
|
||||
getUserInfoMock.mockResolvedValue({ userId: undefined });
|
||||
|
||||
const response = (await call("target")) as Response;
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(createMediaDownloadUrlMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("answers 404 when the stored value is a legacy data URI, since no object exists", async () => {
|
||||
users.target.avatar = "data:image/png;base64,AAAA";
|
||||
|
||||
const response = (await call("target")) as Response;
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(createMediaDownloadUrlMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("answers 404 when the user has no picture at all", async () => {
|
||||
users.target.avatar = null;
|
||||
|
||||
const response = (await call("target")) as Response;
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
|
||||
it("lets a super admin see any user (admin console renders avatars too)", async () => {
|
||||
users.requester.operator = true;
|
||||
users.target.tenants = ["t2"];
|
||||
|
||||
const response = (await call("target")) as Response;
|
||||
|
||||
expect(response.status).toBe(302);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user