91 lines
4.7 KiB
TypeScript
91 lines
4.7 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
verifyCalendarOAuthState: vi.fn(),
|
|
verifyEmailOAuthState: vi.fn(),
|
|
runAuthenticatedProviderOAuthCallback: vi.fn(),
|
|
completeCalendarOAuthCallback: vi.fn(),
|
|
completeMailboxOAuthCallback: vi.fn(),
|
|
syncConnection: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("~/modules/calendar/services/oauthState.server", () => ({ verifyCalendarOAuthState: mocks.verifyCalendarOAuthState }));
|
|
vi.mock("~/modules/emailInbox/services/oauthState.server", () => ({ verifyEmailOAuthState: mocks.verifyEmailOAuthState }));
|
|
vi.mock("~/utils/integrations/providerOAuthCallbackAccess.server", () => ({
|
|
runAuthenticatedProviderOAuthCallback: mocks.runAuthenticatedProviderOAuthCallback,
|
|
}));
|
|
vi.mock("~/modules/calendar/services/CalendarConnectionService.server", () => ({ completeOAuthCallback: mocks.completeCalendarOAuthCallback }));
|
|
vi.mock("~/modules/calendar/services/CalendarSyncService.server", () => ({ syncConnection: mocks.syncConnection }));
|
|
vi.mock("~/modules/emailInbox/services/MailboxConnectionService.server", () => ({ completeOAuthCallback: mocks.completeMailboxOAuthCallback }));
|
|
|
|
import { loader as calendarLoader } from "./integrations.google.oauth.callback";
|
|
import { loader as gmailLoader } from "./integrations.email.google.oauth.callback";
|
|
import { loader as microsoftLoader } from "./integrations.email.microsoft.oauth.callback";
|
|
|
|
const calendarPayload = { tenantId: "tenant_1", userId: "user_1", tenantSlug: "acme", createdAt: Date.now() };
|
|
const gmailPayload = { ...calendarPayload, provider: "gmail" };
|
|
const microsoftPayload = { ...calendarPayload, provider: "microsoft" };
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.verifyCalendarOAuthState.mockReturnValue({ ok: true, payload: calendarPayload });
|
|
mocks.verifyEmailOAuthState.mockImplementation((_state: string, provider: "gmail" | "microsoft") => ({
|
|
ok: true,
|
|
payload: provider === "gmail" ? gmailPayload : microsoftPayload,
|
|
}));
|
|
mocks.runAuthenticatedProviderOAuthCallback.mockImplementation(async ({ callback }: { callback: () => Promise<unknown> }) => callback());
|
|
mocks.completeCalendarOAuthCallback.mockResolvedValue({ ...calendarPayload, connectionId: "connection_1" });
|
|
mocks.completeMailboxOAuthCallback.mockResolvedValue({ ...calendarPayload, mailboxId: "mailbox_1" });
|
|
mocks.syncConnection.mockResolvedValue(undefined);
|
|
});
|
|
|
|
describe("provider OAuth callback security", () => {
|
|
it("rejects an invalid signed Calendar state before session access or exchange", async () => {
|
|
mocks.verifyCalendarOAuthState.mockReturnValue({ ok: false, reason: "bad-signature" });
|
|
|
|
const response = await calendarLoader({ request: new Request("https://app.example.test/integrations/google/oauth/callback?code=code&state=bad") } as never);
|
|
|
|
expect(response.status).toBe(302);
|
|
expect(mocks.runAuthenticatedProviderOAuthCallback).not.toHaveBeenCalled();
|
|
expect(mocks.completeCalendarOAuthCallback).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("keeps Calendar exchange and initial sync inside the authenticated state context", async () => {
|
|
const request = new Request("https://app.example.test/integrations/google/oauth/callback?code=code&state=signed");
|
|
const response = await calendarLoader({ request } as never);
|
|
|
|
expect(response.status).toBe(302);
|
|
expect(mocks.runAuthenticatedProviderOAuthCallback).toHaveBeenCalledWith({
|
|
request,
|
|
tenantId: "tenant_1",
|
|
userId: "user_1",
|
|
callback: expect.any(Function),
|
|
});
|
|
expect(mocks.completeCalendarOAuthCallback).toHaveBeenCalledWith({ code: "code", state: "signed" });
|
|
expect(mocks.syncConnection).toHaveBeenCalledWith("tenant_1", "connection_1");
|
|
});
|
|
|
|
it("does not exchange Gmail tokens when the current session or membership gate rejects", async () => {
|
|
mocks.runAuthenticatedProviderOAuthCallback.mockRejectedValue(new Error("stale session"));
|
|
|
|
const response = await gmailLoader({
|
|
request: new Request("https://app.example.test/integrations/email/google/oauth/callback?code=code&state=signed"),
|
|
} as never);
|
|
|
|
expect(response.status).toBe(302);
|
|
expect(mocks.completeMailboxOAuthCallback).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("pins Microsoft state to the Microsoft callback before exchange", async () => {
|
|
mocks.verifyEmailOAuthState.mockReturnValue({ ok: false, reason: "wrong-provider" });
|
|
|
|
const response = await microsoftLoader({
|
|
request: new Request("https://app.example.test/integrations/email/microsoft/oauth/callback?code=code&state=gmail-state"),
|
|
} as never);
|
|
|
|
expect(response.status).toBe(302);
|
|
expect(mocks.runAuthenticatedProviderOAuthCallback).not.toHaveBeenCalled();
|
|
expect(mocks.completeMailboxOAuthCallback).not.toHaveBeenCalled();
|
|
});
|
|
});
|