Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,141 @@
|
||||
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn(async () => ({ t: (key: string) => `translated:${key}` })) }));
|
||||
vi.mock("~/modules/onboarding/db/onboardingSessions.db.server", () => ({ getOnboardingSession: vi.fn() }));
|
||||
vi.mock("~/modules/onboarding/services/OnboardingSessionService", () => ({
|
||||
default: {
|
||||
addActions: vi.fn(),
|
||||
complete: vi.fn(),
|
||||
dismissed: vi.fn(),
|
||||
setStep: vi.fn(),
|
||||
started: vi.fn(),
|
||||
},
|
||||
}));
|
||||
vi.mock("~/utils/session.server", () => ({
|
||||
getUserInfo: vi.fn(async () => ({ userId: "user_1" })),
|
||||
validateCSRFToken: vi.fn(),
|
||||
}));
|
||||
|
||||
const onboardingDb = await import("~/modules/onboarding/db/onboardingSessions.db.server");
|
||||
const serviceModule = await import("~/modules/onboarding/services/OnboardingSessionService");
|
||||
const sessionServer = await import("~/utils/session.server");
|
||||
const { action, loader } = await import("./onboarding.$id");
|
||||
|
||||
const ownedSession = {
|
||||
id: "session_1",
|
||||
userId: "user_1",
|
||||
sessionSteps: [],
|
||||
} as any;
|
||||
|
||||
describe("onboarding session route security", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(onboardingDb.getOnboardingSession).mockResolvedValue(ownedSession);
|
||||
vi.mocked(sessionServer.getUserInfo).mockResolvedValue({ userId: "user_1" } as any);
|
||||
vi.mocked(sessionServer.validateCSRFToken).mockResolvedValue(undefined as never);
|
||||
});
|
||||
|
||||
it("returns only the current user's onboarding session", async () => {
|
||||
const response = await loader({
|
||||
request: new Request("https://app.example.com/onboarding/session_1"),
|
||||
params: { id: "session_1" },
|
||||
context: {},
|
||||
} as unknown as LoaderFunctionArgs);
|
||||
|
||||
expect(response).toMatchObject({ item: ownedSession });
|
||||
});
|
||||
|
||||
it("rejects onboarding sessions owned by another user", async () => {
|
||||
vi.mocked(onboardingDb.getOnboardingSession).mockResolvedValueOnce({ ...ownedSession, userId: "user_2" });
|
||||
|
||||
const response = (await action({
|
||||
request: new Request("https://app.example.com/onboarding/session_1", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams({ action: "started", csrf: "csrf-token" }),
|
||||
}),
|
||||
params: { id: "session_1" },
|
||||
context: {},
|
||||
} as unknown as ActionFunctionArgs)) as Response;
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
await expect(response.json()).resolves.toEqual({ error: "translated:onboarding.session.errors.unauthorized" });
|
||||
expect(serviceModule.default.started).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects loader access to onboarding sessions owned by another user", async () => {
|
||||
vi.mocked(onboardingDb.getOnboardingSession).mockResolvedValueOnce({ ...ownedSession, userId: "user_2" });
|
||||
|
||||
const response = await loader({
|
||||
request: new Request("https://app.example.com/onboarding/session_1"),
|
||||
params: { id: "session_1" },
|
||||
context: {},
|
||||
} as unknown as LoaderFunctionArgs).catch((error: Response) => error);
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
await expect(response.json()).resolves.toEqual({ error: "translated:onboarding.session.errors.unauthorized" });
|
||||
});
|
||||
|
||||
it("returns localized not-found errors for missing onboarding sessions", async () => {
|
||||
vi.mocked(onboardingDb.getOnboardingSession).mockResolvedValueOnce(null);
|
||||
|
||||
const response = (await action({
|
||||
request: new Request("https://app.example.com/onboarding/session_1", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams({ action: "started", csrf: "csrf-token" }),
|
||||
}),
|
||||
params: { id: "session_1" },
|
||||
context: {},
|
||||
} as unknown as ActionFunctionArgs)) as Response;
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
await expect(response.json()).resolves.toEqual({ error: "translated:onboarding.session.errors.notFound" });
|
||||
expect(serviceModule.default.started).not.toHaveBeenCalled();
|
||||
|
||||
vi.mocked(onboardingDb.getOnboardingSession).mockResolvedValueOnce(null);
|
||||
const loaderResponse = await loader({
|
||||
request: new Request("https://app.example.com/onboarding/session_1"),
|
||||
params: { id: "session_1" },
|
||||
context: {},
|
||||
} as unknown as LoaderFunctionArgs).catch((error: Response) => error);
|
||||
|
||||
expect(loaderResponse.status).toBe(404);
|
||||
await expect(loaderResponse.json()).resolves.toEqual({ error: "translated:onboarding.session.errors.notFound" });
|
||||
});
|
||||
|
||||
it("validates CSRF before mutating onboarding session state", async () => {
|
||||
const response = (await action({
|
||||
request: new Request("https://app.example.com/onboarding/session_1", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams({ action: "started", csrf: "csrf-token" }),
|
||||
}),
|
||||
params: { id: "session_1" },
|
||||
context: {},
|
||||
} as unknown as ActionFunctionArgs)) as Response;
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(sessionServer.validateCSRFToken).toHaveBeenCalledWith(expect.any(Request));
|
||||
expect(serviceModule.default.started).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not mutate when CSRF validation fails", async () => {
|
||||
vi.mocked(sessionServer.validateCSRFToken).mockRejectedValueOnce(new Error("Invalid CSRF token"));
|
||||
|
||||
const response = (await action({
|
||||
request: new Request("https://app.example.com/onboarding/session_1", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams({ action: "started" }),
|
||||
}),
|
||||
params: { id: "session_1" },
|
||||
context: {},
|
||||
} as unknown as ActionFunctionArgs)) as Response;
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
await expect(response.json()).resolves.toEqual({ error: "Invalid CSRF token" });
|
||||
expect(serviceModule.default.started).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user