Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -6,13 +6,24 @@ import type { ActionFunctionArgs } from "react-router";
// endpoints ALWAYS ack (2xx) so providers never retry-storm; only verified
// notifications are persisted.
const findMailboxByWebhookSubscription = vi.fn();
const findMailboxByEmailAddress = vi.fn();
const resolveGmailMailboxRlsIdentities = vi.fn();
const resolveMicrosoftMailboxRlsIdentity = vi.fn();
const getGmailWebhookMailbox = vi.fn();
const getMicrosoftWebhookMailbox = vi.fn();
const recordEmailWebhookEvent = vi.fn();
vi.mock("@hivegrid/db/rls-resolvers.server", () => ({
resolveGmailMailboxRlsIdentities: (...args: unknown[]) => resolveGmailMailboxRlsIdentities(...args),
resolveMicrosoftMailboxRlsIdentity: (...args: unknown[]) => resolveMicrosoftMailboxRlsIdentity(...args),
}));
vi.mock("@hivegrid/db/rls-context.server", () => ({
runWithRlsContext: (_context: unknown, callback: () => unknown) => callback(),
}));
vi.mock("~/modules/emailInbox/db/emailInbox.db.server", () => ({
findMailboxByWebhookSubscription: (...args: unknown[]) => findMailboxByWebhookSubscription(...args),
findMailboxByEmailAddress: (...args: unknown[]) => findMailboxByEmailAddress(...args),
getGmailWebhookMailbox: (...args: unknown[]) => getGmailWebhookMailbox(...args),
getMicrosoftWebhookMailbox: (...args: unknown[]) => getMicrosoftWebhookMailbox(...args),
recordEmailWebhookEvent: (...args: unknown[]) => recordEmailWebhookEvent(...args),
}));
@@ -26,6 +37,8 @@ function call(action: typeof googleAction, request: Request) {
beforeEach(() => {
vi.clearAllMocks();
process.env.GOOGLE_GMAIL_PUBSUB_VERIFICATION_TOKEN = "shared-token";
resolveGmailMailboxRlsIdentities.mockResolvedValue([]);
resolveMicrosoftMailboxRlsIdentity.mockResolvedValue(null);
});
afterEach(() => {
@@ -47,20 +60,21 @@ describe("Gmail Pub/Sub webhook", () => {
});
it("persists a pending event for a known mailbox address", async () => {
findMailboxByEmailAddress.mockResolvedValue({ id: "mb1", tenantId: "t1", ownerUserId: "u1" });
resolveGmailMailboxRlsIdentities.mockResolvedValue([{ tenantId: "t1", resourceId: "mb1", userId: "u1" }]);
getGmailWebhookMailbox.mockResolvedValue({ id: "mb1", tenantId: "t1", ownerUserId: "u1" });
const response = await call(
googleAction,
new Request("https://app.example.test/webhooks/email/google?token=shared-token", { method: "POST", body: pubsubBody({ emailAddress: "A@B.de", historyId: 5 }) }),
);
expect(response.status).toBe(200);
expect(findMailboxByEmailAddress).toHaveBeenCalledWith("gmail", "A@B.de");
expect(resolveGmailMailboxRlsIdentities).toHaveBeenCalledWith("gmail", "a@b.de");
expect(getGmailWebhookMailbox).toHaveBeenCalledWith({ tenantId: "t1", ownerUserId: "u1", mailboxId: "mb1", emailAddress: "a@b.de" });
expect(recordEmailWebhookEvent).toHaveBeenCalledWith(
expect.objectContaining({ tenantId: "t1", mailboxId: "mb1", provider: "gmail", notificationPayload: expect.objectContaining({ historyId: "5" }) }),
);
});
it("acks unknown addresses without persisting", async () => {
findMailboxByEmailAddress.mockResolvedValue(null);
const response = await call(
googleAction,
new Request("https://app.example.test/webhooks/email/google?token=shared-token", { method: "POST", body: pubsubBody({ emailAddress: "ghost@b.de" }) }),
@@ -68,6 +82,44 @@ describe("Gmail Pub/Sub webhook", () => {
expect(response.status).toBe(200);
expect(recordEmailWebhookEvent).not.toHaveBeenCalled();
});
it("persists every exact mailbox when the same Gmail address belongs to multiple tenants", async () => {
resolveGmailMailboxRlsIdentities.mockResolvedValue([
{ tenantId: "t1", resourceId: "mb1", userId: "u1" },
{ tenantId: "t2", resourceId: "mb2", userId: "u2" },
]);
getGmailWebhookMailbox
.mockResolvedValueOnce({ id: "mb1", tenantId: "t1", ownerUserId: "u1" })
.mockResolvedValueOnce({ id: "mb2", tenantId: "t2", ownerUserId: "u2" });
const response = await call(
googleAction,
new Request("https://app.example.test/webhooks/email/google?token=shared-token", {
method: "POST",
body: pubsubBody({ emailAddress: "shared@example.test", historyId: 9 }),
}),
);
expect(response.status).toBe(200);
expect(recordEmailWebhookEvent).toHaveBeenCalledTimes(2);
expect(recordEmailWebhookEvent).toHaveBeenNthCalledWith(2, expect.objectContaining({ tenantId: "t2", mailboxId: "mb2" }));
});
it("acks a stale Gmail identity when its exact scoped re-read fails", async () => {
resolveGmailMailboxRlsIdentities.mockResolvedValue([{ tenantId: "t1", resourceId: "mb1", userId: "u1" }]);
getGmailWebhookMailbox.mockResolvedValue(null);
const response = await call(
googleAction,
new Request("https://app.example.test/webhooks/email/google?token=shared-token", {
method: "POST",
body: pubsubBody({ emailAddress: "shared@example.test" }),
}),
);
expect(response.status).toBe(200);
expect(recordEmailWebhookEvent).not.toHaveBeenCalled();
});
});
describe("Microsoft Graph webhook", () => {
@@ -83,7 +135,10 @@ describe("Microsoft Graph webhook", () => {
});
it("persists only notifications whose clientState matches the stored secret", async () => {
findMailboxByWebhookSubscription.mockResolvedValue({ id: "mb1", tenantId: "t1", ownerUserId: "u1", webhookClientState: "secret-1" });
resolveMicrosoftMailboxRlsIdentity.mockImplementation(async (_subscriptionId: string, clientState: string) =>
clientState === "secret-1" ? { tenantId: "t1", resourceId: "mb1", userId: "u1" } : null,
);
getMicrosoftWebhookMailbox.mockResolvedValue({ id: "mb1", tenantId: "t1", ownerUserId: "u1" });
const body = JSON.stringify({
value: [
{ subscriptionId: "sub1", clientState: "secret-1", changeType: "created", resource: "/me/messages/x", resourceData: { id: "x" } },
@@ -96,6 +151,13 @@ describe("Microsoft Graph webhook", () => {
expect(recordEmailWebhookEvent).toHaveBeenCalledWith(
expect.objectContaining({ tenantId: "t1", mailboxId: "mb1", provider: "microsoft", notificationPayload: expect.objectContaining({ resourceId: "x" }) }),
);
expect(getMicrosoftWebhookMailbox).toHaveBeenCalledWith({
tenantId: "t1",
ownerUserId: "u1",
mailboxId: "mb1",
subscriptionId: "sub1",
clientState: "secret-1",
});
});
it("acks malformed bodies without persisting", async () => {
@@ -103,4 +165,23 @@ describe("Microsoft Graph webhook", () => {
expect(response.status).toBe(202);
expect(recordEmailWebhookEvent).not.toHaveBeenCalled();
});
it("acks malformed notification elements without querying a mailbox", async () => {
const body = JSON.stringify({ value: [null, 42, [], { subscriptionId: ["sub1"], clientState: "secret-1" }] });
const response = await call(microsoftAction, new Request("https://app.example.test/webhooks/email/microsoft", { method: "POST", body }));
expect(response.status).toBe(202);
expect(resolveMicrosoftMailboxRlsIdentity).not.toHaveBeenCalled();
expect(recordEmailWebhookEvent).not.toHaveBeenCalled();
});
it("rejects an oversized notification batch before any database lookup", async () => {
const notification = { subscriptionId: "sub1", clientState: "secret-1" };
const body = JSON.stringify({ value: Array.from({ length: 101 }, () => notification) });
const response = await call(microsoftAction, new Request("https://app.example.test/webhooks/email/microsoft", { method: "POST", body }));
expect(response.status).toBe(202);
expect(resolveMicrosoftMailboxRlsIdentity).not.toHaveBeenCalled();
expect(recordEmailWebhookEvent).not.toHaveBeenCalled();
});
});