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,95 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const getOptInConfirmationStatus = vi.fn();
|
||||
const confirmOptIn = vi.fn();
|
||||
const validateCSRFToken = vi.fn();
|
||||
|
||||
vi.mock("~/modules/emailMarketing/services/ConsentService.server", () => ({
|
||||
getOptInConfirmationStatus,
|
||||
confirmOptIn,
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/session.server", () => ({
|
||||
validateCSRFToken,
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/logger.server", () => ({
|
||||
createScopedLogger: () => ({ info: () => undefined, warn: () => undefined, error: () => undefined }),
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
validateCSRFToken.mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
describe("optin.$token", () => {
|
||||
it("keeps GET read-only and returns a confirmation state", async () => {
|
||||
getOptInConfirmationStatus.mockResolvedValueOnce("confirm");
|
||||
const route = await import("./optin.$token.tsx");
|
||||
|
||||
const result = await route.loader({
|
||||
params: { token: "valid-token" },
|
||||
request: new Request("http://x/optin/valid-token"),
|
||||
context: {},
|
||||
} as unknown as Parameters<typeof route.loader>[0]);
|
||||
|
||||
expect(result).toEqual({ status: "confirm" });
|
||||
expect(confirmOptIn).not.toHaveBeenCalled();
|
||||
}, 60000);
|
||||
|
||||
it("requires CSRF before confirming the opt-in", async () => {
|
||||
confirmOptIn.mockResolvedValueOnce("confirmed");
|
||||
const route = await import("./optin.$token.tsx");
|
||||
const request = new Request("http://x/optin/valid-token", {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({ csrf: "csrf-token" }),
|
||||
});
|
||||
|
||||
await expect(route.action({ params: { token: "valid-token" }, request, context: {} } as unknown as Parameters<typeof route.action>[0])).resolves.toEqual({
|
||||
status: "confirmed",
|
||||
});
|
||||
expect(validateCSRFToken).toHaveBeenCalledWith(request);
|
||||
expect(confirmOptIn).toHaveBeenCalledWith("valid-token");
|
||||
});
|
||||
|
||||
it("does not consume the token when CSRF validation fails", async () => {
|
||||
validateCSRFToken.mockRejectedValueOnce(new Error("invalid csrf"));
|
||||
const route = await import("./optin.$token.tsx");
|
||||
const request = new Request("http://x/optin/valid-token", {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({ csrf: "bad" }),
|
||||
});
|
||||
|
||||
await expect(route.action({ params: { token: "valid-token" }, request, context: {} } as unknown as Parameters<typeof route.action>[0])).rejects.toThrow(
|
||||
"invalid csrf"
|
||||
);
|
||||
expect(confirmOptIn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("is idempotent on replay", async () => {
|
||||
confirmOptIn.mockResolvedValueOnce("confirmed").mockResolvedValueOnce("already_confirmed");
|
||||
const route = await import("./optin.$token.tsx");
|
||||
const makeRequest = () =>
|
||||
new Request("http://x/optin/valid-token", {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({ csrf: "csrf-token" }),
|
||||
});
|
||||
|
||||
await expect(
|
||||
route.action({ params: { token: "valid-token" }, request: makeRequest(), context: {} } as unknown as Parameters<typeof route.action>[0])
|
||||
).resolves.toEqual({
|
||||
status: "confirmed",
|
||||
});
|
||||
await expect(
|
||||
route.action({ params: { token: "valid-token" }, request: makeRequest(), context: {} } as unknown as Parameters<typeof route.action>[0])
|
||||
).resolves.toEqual({
|
||||
status: "already_confirmed",
|
||||
});
|
||||
expect(confirmOptIn).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("prevents caching and referrer leakage for capability URLs", async () => {
|
||||
const route = await import("./optin.$token.tsx");
|
||||
expect(route.headers()).toEqual({ "Cache-Control": "no-store", "Referrer-Policy": "no-referrer" });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user