137 lines
5.6 KiB
TypeScript
137 lines
5.6 KiB
TypeScript
// @vitest-environment jsdom
|
|
import "@testing-library/jest-dom/vitest";
|
|
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
import { createRoutesStub } from "react-router";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import type { KwpSetupData, KwpSetupVM } from "@hivegrid/view-models";
|
|
|
|
import { KwpSetupPage } from "./KwpSetupPage";
|
|
|
|
vi.mock("react-i18next", () => ({
|
|
useTranslation: () => ({
|
|
t: (key: string, values?: Record<string, unknown>) => (values ? `${key}:${Object.values(values).join("/")}` : key),
|
|
}),
|
|
}));
|
|
|
|
// Radix radio inputs measure themselves; jsdom has no ResizeObserver.
|
|
class ResizeObserverStub {
|
|
observe(): void {}
|
|
unobserve(): void {}
|
|
disconnect(): void {}
|
|
}
|
|
globalThis.ResizeObserver = globalThis.ResizeObserver ?? (ResizeObserverStub as unknown as typeof ResizeObserver);
|
|
|
|
const CREDENTIAL_SENTINELS = ["CLIENT_SECRET_MUST_NOT_RENDER", "ACCESS_TOKEN_MUST_NOT_RENDER", "REFRESH_TOKEN_MUST_NOT_RENDER"];
|
|
|
|
function setupVM(data: KwpSetupData): KwpSetupVM {
|
|
return {
|
|
status: "ready",
|
|
data: {
|
|
...data,
|
|
clientSecret: CREDENTIAL_SENTINELS[0],
|
|
accessToken: CREDENTIAL_SENTINELS[1],
|
|
refreshToken: CREDENTIAL_SENTINELS[2],
|
|
},
|
|
} as unknown as KwpSetupVM;
|
|
}
|
|
|
|
function renderPage(vm: KwpSetupVM) {
|
|
const Stub = createRoutesStub([
|
|
{
|
|
path: "/",
|
|
Component: () => <KwpSetupPage vm={vm} csrf="csrf-visual-baseline" submitting={false} />,
|
|
action: async () => null,
|
|
},
|
|
]);
|
|
|
|
return render(<Stub initialEntries={["/"]} />);
|
|
}
|
|
|
|
function stableMarkup(container: HTMLElement): string {
|
|
return container.innerHTML.replace(/<svg\b[^>]*>[\s\S]*?<\/svg>/gu, '<svg data-visual-icon="true"></svg>');
|
|
}
|
|
|
|
function expectNoCredentialLeak(container: HTMLElement): void {
|
|
for (const sentinel of CREDENTIAL_SENTINELS) expect(container).not.toHaveTextContent(sentinel);
|
|
expect(container.innerHTML).not.toMatch(/CLIENT_SECRET_MUST_NOT_RENDER|ACCESS_TOKEN_MUST_NOT_RENDER|REFRESH_TOKEN_MUST_NOT_RENDER/u);
|
|
for (const input of container.querySelectorAll<HTMLInputElement>('input[type="hidden"]')) {
|
|
expect(input.name).not.toMatch(/client.?secret|access.?token|refresh.?token|credential/iu);
|
|
}
|
|
}
|
|
|
|
afterEach(() => cleanup());
|
|
|
|
describe("VR-P32a KWP setup interim visual baseline", () => {
|
|
it("submits the customer code with an explicit tenant-bound action and fallback field", () => {
|
|
const { container } = renderPage(setupVM({
|
|
mode: "oauth",
|
|
tenantSlug: "acme",
|
|
settingsHref: "/acme/settings/integrations/kwp",
|
|
feedback: null,
|
|
}));
|
|
|
|
const form = container.querySelector("form");
|
|
expect(form).toHaveAttribute("action", "/integrations/kwp/setup?tenant=acme");
|
|
expect(container.querySelector<HTMLInputElement>('input[name="tenant"]')).toHaveValue("acme");
|
|
expect(container.querySelector<HTMLInputElement>('input[name="intent"]')).toHaveValue("connect");
|
|
expect(container.querySelector<HTMLInputElement>('input[name="customerCode"]')).toBeRequired();
|
|
expectNoCredentialLeak(container);
|
|
});
|
|
|
|
it("renders an explicit choice when KWP discovers multiple companies", () => {
|
|
const { container } = renderPage(setupVM({
|
|
mode: "mandant",
|
|
tenantSlug: "acme",
|
|
settingsHref: "/app/acme/integrations/kwp",
|
|
connectionId: "connection-discovered-multi",
|
|
connectionVersion: "2026-07-21T09:30:00.000Z",
|
|
customerCode: "acme-kwp",
|
|
discoveryMode: "available",
|
|
mandants: [
|
|
{ id: 1, code: "BERLIN", label: "Acme Berlin GmbH" },
|
|
{ id: 2, code: "HAMBURG", label: "Acme Hamburg GmbH" },
|
|
],
|
|
feedback: null,
|
|
}));
|
|
|
|
expect(screen.getByRole("heading", { name: "integrations.kwp.oauth.mandantTitle" })).toBeInTheDocument();
|
|
expect(screen.getAllByRole("radio")).toHaveLength(2);
|
|
expect(screen.getByRole("button", { name: "integrations.kwp.oauth.confirmMandant" })).toBeDisabled();
|
|
expect(container.querySelector("form")).toHaveAttribute(
|
|
"action",
|
|
"/integrations/kwp/setup?tenant=acme&connection=connection-discovered-multi",
|
|
);
|
|
expect(container.querySelector<HTMLInputElement>('input[name="tenant"]')).toHaveValue("acme");
|
|
expect(container.querySelector("[data-ui-id='ui.integrations.kwp.oauth.mandants']")).toBeInTheDocument();
|
|
expectNoCredentialLeak(container);
|
|
expect(stableMarkup(container)).toMatchSnapshot();
|
|
});
|
|
|
|
it("renders the manual fallback and binds only the explicitly entered company code", () => {
|
|
const { container } = renderPage(setupVM({
|
|
mode: "mandant",
|
|
tenantSlug: "acme",
|
|
settingsHref: "/app/acme/integrations/kwp",
|
|
connectionId: "connection-manual-fallback",
|
|
connectionVersion: "2026-07-21T09:35:00.000Z",
|
|
customerCode: "acme-kwp",
|
|
discoveryMode: "manual",
|
|
mandants: [],
|
|
feedback: "selection-error",
|
|
}));
|
|
|
|
fireEvent.click(screen.getByRole("radio", { name: "integrations.kwp.oauth.manualMandant" }));
|
|
const manualCode = container.querySelector<HTMLInputElement>("[data-ui-id='ui.integrations.kwp.oauth.mandantCode']");
|
|
expect(manualCode).toBeInTheDocument();
|
|
fireEvent.change(manualCode as HTMLInputElement, { target: { value: "FIRMA-02" } });
|
|
|
|
expect(screen.getByRole("button", { name: "integrations.kwp.oauth.confirmMandant" })).toBeEnabled();
|
|
expect(container.querySelector<HTMLInputElement>('input[name="selectionKind"]')).toHaveValue("selected");
|
|
expect(container.querySelector<HTMLInputElement>('input[name="mandantCode"]')).toHaveValue("FIRMA-02");
|
|
expect(container.querySelector("[data-ui-id='ui.integrations.kwp.oauth.feedback']")).toBeInTheDocument();
|
|
expectNoCredentialLeak(container);
|
|
expect(stableMarkup(container)).toMatchSnapshot();
|
|
});
|
|
});
|