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
+128
@@ -0,0 +1,128 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
loadChatContext: vi.fn(),
|
||||
readSession: vi.fn(),
|
||||
loadAttachment: vi.fn(),
|
||||
assertTenantKey: vi.fn(),
|
||||
createDownloadUrl: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/modules/chat/routes/chatAccess.server", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("~/modules/chat/routes/chatAccess.server")>();
|
||||
return { ...actual, loadChatContext: mocks.loadChatContext };
|
||||
});
|
||||
vi.mock("~/modules/chat/services/chatAuditSession.server", () => ({ readChatAuditSession: mocks.readSession }));
|
||||
vi.mock("~/modules/chat/services/ChatAuditService.server", () => ({ loadChatAuditAttachment: mocks.loadAttachment }));
|
||||
vi.mock("@hivegrid/storage", () => ({ assertFileKeyBelongsToTenant: mocks.assertTenantKey }));
|
||||
vi.mock("~/utils/integrations/mediaSigning.server", () => ({ createMediaDownloadUrl: mocks.createDownloadUrl }));
|
||||
|
||||
import { loader } from "./audit.$id.attachments.$fileId.$variant";
|
||||
|
||||
const context = {
|
||||
tenantId: "tenant-1",
|
||||
userId: "admin-1",
|
||||
tenantUserId: "tenant-user-1",
|
||||
permissions: ["chat.read_all_channels"],
|
||||
hasModule: true as const,
|
||||
};
|
||||
const session = {
|
||||
id: "session-1",
|
||||
tenantId: "tenant-1",
|
||||
userId: "admin-1",
|
||||
reason: "Supportfall",
|
||||
issuedAt: new Date("2026-07-16T10:00:00.000Z").getTime(),
|
||||
expiresAt: new Date("2026-07-16T10:02:00.000Z").getTime(),
|
||||
};
|
||||
|
||||
describe("chat audit attachment media route", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-07-16T10:00:00.000Z"));
|
||||
mocks.loadChatContext.mockResolvedValue(context);
|
||||
mocks.readSession.mockResolvedValue(session);
|
||||
mocks.loadAttachment.mockResolvedValue({
|
||||
fileId: "file-1",
|
||||
messageId: "message-1",
|
||||
displayName: "beleg.pdf",
|
||||
storageKey: "tenants/tenant-1/files/file-1",
|
||||
});
|
||||
mocks.createDownloadUrl.mockResolvedValue("https://storage.test/signed");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("returns 404 without a reason-bound audit session", async () => {
|
||||
mocks.readSession.mockResolvedValue(null);
|
||||
|
||||
const response = await loader(args());
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(mocks.loadAttachment).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("logs the audit-bound read before signing and redirecting", async () => {
|
||||
const response = await loader(args());
|
||||
|
||||
expect(mocks.loadAttachment).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
context,
|
||||
chatId: "chat-1",
|
||||
fileId: "file-1",
|
||||
variant: "original",
|
||||
session,
|
||||
requestId: expect.any(String),
|
||||
})
|
||||
);
|
||||
expect(mocks.assertTenantKey).toHaveBeenCalledWith("tenant-1", "tenants/tenant-1/files/file-1", "download");
|
||||
expect(mocks.createDownloadUrl).toHaveBeenCalledWith({
|
||||
key: "tenants/tenant-1/files/file-1",
|
||||
filename: "beleg.pdf",
|
||||
ttlSeconds: 60,
|
||||
});
|
||||
expect(response.status).toBe(302);
|
||||
expect(response.headers.get("location")).toBe("https://storage.test/signed");
|
||||
});
|
||||
|
||||
it("caps the signed URL to the remaining whole audit-session lifetime", async () => {
|
||||
mocks.readSession.mockResolvedValue({ ...session, expiresAt: Date.now() + 20_900 });
|
||||
|
||||
const response = await loader(args());
|
||||
|
||||
expect(response.status).toBe(302);
|
||||
expect(mocks.createDownloadUrl).toHaveBeenCalledWith(expect.objectContaining({ ttlSeconds: 20 }));
|
||||
});
|
||||
|
||||
it("does not audit or sign when less than one whole session second remains", async () => {
|
||||
mocks.readSession.mockResolvedValue({ ...session, expiresAt: Date.now() + 999 });
|
||||
|
||||
const response = await loader(args());
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(mocks.loadAttachment).not.toHaveBeenCalled();
|
||||
expect(mocks.createDownloadUrl).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("fails closed for missing variants and tenant-key mismatches", async () => {
|
||||
const invalidVariant = await loader(args({ variant: "raw" }));
|
||||
expect(invalidVariant.status).toBe(404);
|
||||
|
||||
mocks.assertTenantKey.mockImplementationOnce(() => {
|
||||
throw new Error("foreign key");
|
||||
});
|
||||
const foreignKey = await loader(args());
|
||||
expect(foreignKey.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
function args(overrides: Partial<Record<"tenant" | "id" | "fileId" | "variant", string>> = {}) {
|
||||
return {
|
||||
request: new Request("http://app.localhost/demo/settings/chat/audit/chat-1/attachments/file-1/original"),
|
||||
params: { tenant: "demo", id: "chat-1", fileId: "file-1", variant: "original", ...overrides },
|
||||
context: {},
|
||||
unstable_pattern: "app.$tenant/settings/chat/audit.$id.attachments.$fileId.$variant",
|
||||
};
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
import { CHAT_PERMISSIONS } from "@hivegrid/chat";
|
||||
import type { ChatAuditAttachmentVariant } from "@hivegrid/chat/audit.server";
|
||||
import { assertFileKeyBelongsToTenant } from "@hivegrid/storage";
|
||||
import type { LoaderFunctionArgs } from "react-router";
|
||||
import { redirect } from "react-router";
|
||||
|
||||
import { loadChatContext, requireChatPermission } from "~/modules/chat/routes/chatAccess.server";
|
||||
import { loadChatAuditAttachment } from "~/modules/chat/services/ChatAuditService.server";
|
||||
import { readChatAuditSession } from "~/modules/chat/services/chatAuditSession.server";
|
||||
import { createMediaDownloadUrl } from "~/utils/integrations/mediaSigning.server";
|
||||
|
||||
const CHAT_AUDIT_SIGNED_URL_MAX_TTL_SECONDS = 60;
|
||||
|
||||
function notFound(): Response {
|
||||
return new Response("Not Found", { status: 404 });
|
||||
}
|
||||
|
||||
export async function loader({ request, params }: LoaderFunctionArgs): Promise<Response> {
|
||||
const chatId = params.id?.trim() ?? "";
|
||||
const fileId = params.fileId?.trim() ?? "";
|
||||
const variant = parseVariant(params.variant);
|
||||
if (!chatId || !fileId || !variant) return notFound();
|
||||
|
||||
try {
|
||||
const loadedContext = await loadChatContext({ request, params });
|
||||
const context = requireChatPermission(loadedContext, CHAT_PERMISSIONS.readAllChannels);
|
||||
const session = await readChatAuditSession({ request, tenantId: context.tenantId, userId: context.userId });
|
||||
if (!session) return notFound();
|
||||
const ttlSeconds = auditDownloadTtlSeconds(session.expiresAt);
|
||||
if (ttlSeconds === null) return notFound();
|
||||
|
||||
const file = await loadChatAuditAttachment({
|
||||
context,
|
||||
chatId,
|
||||
fileId,
|
||||
variant,
|
||||
session,
|
||||
requestId: crypto.randomUUID(),
|
||||
});
|
||||
|
||||
assertFileKeyBelongsToTenant(context.tenantId, file.storageKey, "download");
|
||||
const url = await createMediaDownloadUrl({
|
||||
key: file.storageKey,
|
||||
filename: variant === "original" ? file.displayName : undefined,
|
||||
ttlSeconds,
|
||||
});
|
||||
return redirect(url, 302);
|
||||
} catch {
|
||||
return notFound();
|
||||
}
|
||||
}
|
||||
|
||||
function auditDownloadTtlSeconds(expiresAt: number): number | null {
|
||||
const remainingWholeSeconds = Math.floor((expiresAt - Date.now()) / 1_000);
|
||||
if (remainingWholeSeconds < 1) return null;
|
||||
return Math.min(CHAT_AUDIT_SIGNED_URL_MAX_TTL_SECONDS, remainingWholeSeconds);
|
||||
}
|
||||
|
||||
function parseVariant(value: string | undefined): ChatAuditAttachmentVariant | null {
|
||||
return value === "original" || value === "preview" || value === "thumb" ? value : null;
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// AK-SET-01 — legacy chat-audit detail path. The attachment route below it keeps
|
||||
// its own published URL and is unaffected by this redirect (flat sibling).
|
||||
import type { LoaderFunctionArgs } from "react-router";
|
||||
import { legacySettingsRedirect } from "../legacy-redirects.server";
|
||||
|
||||
export const loader = ({ request, params }: LoaderFunctionArgs) =>
|
||||
legacySettingsRedirect({ request, params }, `settings/compliance/chat/${encodeURIComponent(params.id ?? "")}`);
|
||||
|
||||
export default function LegacyChatAuditDetailRedirect() {
|
||||
return null;
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// AK-SET-01 — legacy `settings/chat/audit` is now a tab of the Compliance-Hub.
|
||||
import { legacySettingsRedirectLoader } from "../legacy-redirects.server";
|
||||
|
||||
export const loader = legacySettingsRedirectLoader("settings/compliance/chat");
|
||||
|
||||
export default function LegacyChatAuditRedirect() {
|
||||
return null;
|
||||
}
|
||||
Reference in new issue
Block a user