Files
nginx-proxy-manager/frontend/src/api/backend/testHttpCertificate.test.ts
T
Hafiz Ahmed Zia 9972e78dc8 fix: preserve hostname keys in HTTP certificate test results
Preserve raw response keys for the HTTP certificate test endpoint so
hostname punctuation and distinct result entries survive normalization.
Add API and mounted-modal regression coverage for the exact candidate
reviewed against develop 56ab784117.

Related: NginxProxyManager/nginx-proxy-manager#5849

AI assistance: Codex assisted with the implementation, tests, independent
review and contribution text.
2026-10-12 08:36:32 +05:00

93 lines
3.4 KiB
TypeScript

import AuthStore from "src/modules/AuthStore";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import * as api from "./base";
import { testHttpCertificate } from "./testHttpCertificate";
const fetchMock = vi.fn<typeof fetch>();
const reply = (payload: unknown, status = 200) => {
fetchMock.mockResolvedValueOnce(new Response(JSON.stringify(payload), { status }));
};
beforeEach(() => {
localStorage.clear();
vi.stubGlobal("fetch", fetchMock);
});
afterEach(() => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
fetchMock.mockReset();
});
describe("certificate HTTP result keys", () => {
it.each([
"test-domain.example",
"test--domain.example",
"xn--bcher-kva.example",
"one.two-three.example",
"_acme-challenge.example",
"Test-Domain.example",
"plain.example",
])("preserves the exact response key %s", async (domain) => {
const result = { [domain]: "wrong-data" };
reply(result);
expect(await testHttpCertificate([domain])).toEqual(result);
});
it("keeps colliding spellings and their distinct statuses", async () => {
const result = { "test-domain.example": "ok", "test--domain.example": "no-host" };
reply(result);
expect(await testHttpCertificate(Object.keys(result))).toEqual(result);
});
it("sends unchanged domains with the normal request headers", async () => {
const domains = ["test-domain.example", "test--domain.example"];
reply({});
await testHttpCertificate(domains);
expect(fetchMock).toHaveBeenCalledWith("/api/nginx/certificates/test-http", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ domains }),
signal: undefined,
});
});
it("preserves an empty result", async () => {
reply({});
expect(await testHttpCertificate([])).toEqual({});
});
it.each([
[400, { message: "invalid domain" }, "invalid domain"],
[500, { message: "fallback", messageI18n: "certificate-error" }, "certificate-error"],
])("preserves error handling for status %s", async (status, error, message) => {
reply({ error }, status);
await expect(testHttpCertificate(["test-domain.example"])).rejects.toThrow(message);
});
it("still clears authentication on 401", async () => {
const clear = vi.spyOn(AuthStore, "clear");
const reload = vi.spyOn(window.location, "reload").mockImplementation(() => {});
reply({ error: { message: "unauthorized" } }, 401);
await expect(testHttpCertificate(["test-domain.example"])).rejects.toThrow("unauthorized");
expect(clear).toHaveBeenCalledOnce();
expect(reload).toHaveBeenCalledOnce();
});
it.each([
["get", api.get],
["post", api.post],
["put", api.put],
["del", api.del],
] as const)("keeps default %s response normalization", async (_name, method) => {
reply({ domain_names: ["test-domain.example"], nested_value: { created_on: "today" } });
expect(await method({ url: "/control" })).toEqual({
domainNames: ["test-domain.example"],
nestedValue: { createdOn: "today" },
});
});
it("preserves POST abort forwarding and request normalization", async () => {
const controller = new AbortController();
reply({ domain_names: [] });
await api.post({ url: "/control", data: { domainNames: ["a-b.example"] } }, controller);
expect(fetchMock).toHaveBeenCalledWith(
"/api/control",
expect.objectContaining({
body: JSON.stringify({ domain_names: ["a-b.example"] }),
signal: controller.signal,
}),
);
});
});