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,95 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { action } from "./settings";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { updateAppConfiguration } from "~/utils/db/appConfiguration.db.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
vi.mock("~/utils/db.server", () => ({
|
||||
db: {
|
||||
metricLog: {
|
||||
count: vi.fn(),
|
||||
deleteMany: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/db/appConfiguration.db.server", () => ({
|
||||
getAppConfiguration: vi.fn(),
|
||||
getOrCreateAppConfiguration: vi.fn(),
|
||||
updateAppConfiguration: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/helpers/.server/PermissionsService", () => ({
|
||||
verifyUserHasPermission: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/session.server", () => ({
|
||||
validateCSRFToken: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/locale/i18next.server", () => ({
|
||||
getTranslations: vi.fn(async () => ({
|
||||
t: (key: string) =>
|
||||
({
|
||||
"admin.metrics.logsDeleted": "Metrics logs deleted.",
|
||||
"shared.invalidForm": "Invalid form.",
|
||||
})[key] ?? key,
|
||||
})),
|
||||
}));
|
||||
|
||||
function postRequest(form: FormData) {
|
||||
return new Request("https://admin.example.test/metrics/settings", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams(form as unknown as Record<string, string>),
|
||||
});
|
||||
}
|
||||
|
||||
function formWithAction(value: string) {
|
||||
const form = new FormData();
|
||||
form.set("action", value);
|
||||
return form;
|
||||
}
|
||||
|
||||
describe("admin metrics settings action", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(verifyUserHasPermission).mockResolvedValue(undefined as never);
|
||||
vi.mocked(validateCSRFToken).mockResolvedValue(undefined as never);
|
||||
vi.mocked(db.metricLog.deleteMany).mockResolvedValue({ count: 2 } as never);
|
||||
vi.mocked(updateAppConfiguration).mockResolvedValue(undefined as never);
|
||||
});
|
||||
|
||||
it("returns the localized invalid form message without mutating settings or logs", async () => {
|
||||
const response = (await action({
|
||||
request: postRequest(formWithAction("rotate")),
|
||||
params: {},
|
||||
context: {},
|
||||
unstable_pattern: "admin/metrics/settings",
|
||||
})) as Response;
|
||||
|
||||
await expect(response.json()).resolves.toEqual({ error: "Invalid form." });
|
||||
expect(response.status).toBe(400);
|
||||
expect(verifyUserHasPermission).toHaveBeenCalledWith(expect.any(Request), "admin.metrics.update");
|
||||
expect(validateCSRFToken).toHaveBeenCalledOnce();
|
||||
expect(updateAppConfiguration).not.toHaveBeenCalled();
|
||||
expect(db.metricLog.deleteMany).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns the localized delete success message after the delete permission check", async () => {
|
||||
const response = (await action({
|
||||
request: postRequest(formWithAction("delete")),
|
||||
params: {},
|
||||
context: {},
|
||||
unstable_pattern: "admin/metrics/settings",
|
||||
})) as Response;
|
||||
|
||||
await expect(response.json()).resolves.toEqual({ success: "Metrics logs deleted." });
|
||||
expect(response.status).toBe(200);
|
||||
expect(verifyUserHasPermission).toHaveBeenNthCalledWith(1, expect.any(Request), "admin.metrics.update");
|
||||
expect(verifyUserHasPermission).toHaveBeenNthCalledWith(2, expect.any(Request), "admin.metrics.delete");
|
||||
expect(db.metricLog.deleteMany).toHaveBeenCalledWith({ where: {} });
|
||||
expect(updateAppConfiguration).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user