140 lines
4.2 KiB
TypeScript
140 lines
4.2 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { runAsAdminIdentity } from "~/services/admin/admin-identity.server";
|
|
import {
|
|
applyAdminFeatureFlagBulkOverride,
|
|
getAdminFeatureFlagRolloutView,
|
|
} from "~/services/admin/feature-flag-rollout.server";
|
|
import { validateCSRFToken } from "~/utils/session.server";
|
|
|
|
import { action, loader } from "./$key.bulk";
|
|
|
|
vi.mock("~/locale/i18next.server", () => ({
|
|
getTranslations: vi.fn(async () => ({ t: (key: string) => key })),
|
|
}));
|
|
|
|
vi.mock("~/services/admin/admin-identity.server", () => ({
|
|
runAsAdminIdentity: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("~/services/admin/feature-flag-rollout.server", async () => {
|
|
const actual = await vi.importActual<typeof import("~/services/admin/feature-flag-rollout.server")>(
|
|
"~/services/admin/feature-flag-rollout.server"
|
|
);
|
|
return {
|
|
...actual,
|
|
applyAdminFeatureFlagBulkOverride: vi.fn(),
|
|
getAdminFeatureFlagRolloutView: vi.fn(),
|
|
};
|
|
});
|
|
|
|
vi.mock("~/utils/session.server", () => ({
|
|
validateCSRFToken: vi.fn(),
|
|
}));
|
|
|
|
const runAsAdminIdentityMock = vi.mocked(runAsAdminIdentity);
|
|
const getRolloutViewMock = vi.mocked(getAdminFeatureFlagRolloutView);
|
|
const applyBulkOverrideMock = vi.mocked(applyAdminFeatureFlagBulkOverride);
|
|
|
|
describe("admin feature flag bulk route", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
runAsAdminIdentityMock.mockImplementation(async (_request, _permission, operation) =>
|
|
operation({
|
|
adminUserId: "admin-raffael",
|
|
sessionId: "session-1",
|
|
assurance: "mfa",
|
|
} as never)
|
|
);
|
|
});
|
|
|
|
it("builds the effective rollout VM behind the read permission", async () => {
|
|
getRolloutViewMock.mockResolvedValue({
|
|
flag: {
|
|
id: "flag-1",
|
|
key: "sales_workspace",
|
|
label: "Sales Workspace",
|
|
defaultValue: false,
|
|
lifecycleStage: "rollout",
|
|
},
|
|
tenants: [
|
|
{
|
|
id: "tenant-1",
|
|
name: "Muster SHK",
|
|
slug: "muster",
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
size: "11-25",
|
|
industry: "shk",
|
|
status: "active",
|
|
subscriptionProductIds: [],
|
|
},
|
|
],
|
|
preview: {
|
|
enabled: 1,
|
|
disabled: 0,
|
|
overrides: 1,
|
|
filtered: 0,
|
|
defaulted: 0,
|
|
rows: [{ tenantId: "tenant-1", decision: { value: true, source: "override", filterId: null } }],
|
|
},
|
|
});
|
|
|
|
const response = await loader({
|
|
request: new Request("https://admin.hivegrid.de/feature-flags/sales_workspace/bulk"),
|
|
params: { key: "sales_workspace" },
|
|
context: {},
|
|
} as never);
|
|
const body = await response.json();
|
|
|
|
expect(runAsAdminIdentityMock).toHaveBeenCalledWith(
|
|
expect.any(Request),
|
|
"admin.featureFlags.view",
|
|
expect.any(Function)
|
|
);
|
|
expect(body.vm.tenants[0]).toMatchObject({
|
|
id: "tenant-1",
|
|
effectiveValue: true,
|
|
source: "override",
|
|
});
|
|
});
|
|
|
|
it("validates CSRF and uses the smallest manage permission for a bulk override", async () => {
|
|
applyBulkOverrideMock.mockResolvedValue({ updated: 2 });
|
|
const form = new FormData();
|
|
form.set("action", "bulk-override");
|
|
form.set("value", "true");
|
|
form.set("reason", "Pilotgruppe freigegeben");
|
|
form.append("tenantId", "tenant-1");
|
|
form.append("tenantId", "tenant-2");
|
|
|
|
const body = new URLSearchParams();
|
|
for (const [key, value] of form.entries()) {
|
|
if (typeof value === "string") body.append(key, value);
|
|
}
|
|
const response = await action({
|
|
request: new Request("https://admin.hivegrid.de/feature-flags/sales_workspace/bulk", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
body,
|
|
}),
|
|
params: { key: "sales_workspace" },
|
|
context: {},
|
|
} as never);
|
|
|
|
expect(validateCSRFToken).toHaveBeenCalledOnce();
|
|
expect(runAsAdminIdentityMock).toHaveBeenCalledWith(
|
|
expect.any(Request),
|
|
"admin.featureFlags.manage",
|
|
expect.any(Function)
|
|
);
|
|
expect(applyBulkOverrideMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
flagKey: "sales_workspace",
|
|
tenantIds: ["tenant-1", "tenant-2"],
|
|
actorAdminUserId: "admin-raffael",
|
|
})
|
|
);
|
|
expect(response.status).toBe(303);
|
|
});
|
|
});
|