mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-10-29 06:35:24 +00:00
fix: skip existing hosts on JSON and SSH config import (#1607)
* fix: skip existing hosts on JSON and SSH config import Import (skip existing) only loaded existing hosts when overwriting, so every host was created again. Always load them, and skip a match unless overwrite is set. A skipped host still resolves as a jump host for the hosts imported after it. Fixes #1606 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: use .js extension for test import Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2fe7530cd4
commit
8d9c83b0f6
2 files changed
+100
-34
No files matched your search
@@ -36,12 +36,14 @@ vi.mock("../../../database/repositories/factory.js", () => ({
|
||||
}),
|
||||
}));
|
||||
import { registerHostBulkRoutes } from "../../../database/routes/host-bulk-routes.js";
|
||||
import { buildExportPayload } from "../../../../ui/sidebar/host-export-payload";
|
||||
import { buildExportPayload } from "../../../../ui/sidebar/host-export-payload.js";
|
||||
|
||||
let handler: RequestHandler;
|
||||
let sshConfigHandler: RequestHandler;
|
||||
const router = {
|
||||
post: (path: string, ...handlers: RequestHandler[]) => {
|
||||
if (path === "/bulk-import") handler = handlers.at(-1)!;
|
||||
if (path === "/ssh-config-import") sshConfigHandler = handlers.at(-1)!;
|
||||
},
|
||||
put: () => {},
|
||||
patch: () => {},
|
||||
@@ -66,12 +68,13 @@ beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.create.mockResolvedValue({ id: 71 });
|
||||
mocks.update.mockResolvedValue({ id: 19 });
|
||||
mocks.list.mockResolvedValue([{ ...host, id: 19 }]);
|
||||
mocks.list.mockResolvedValue([]);
|
||||
});
|
||||
|
||||
it.each([false, true])(
|
||||
"carries plugin host settings through export selection and import (overwrite=%s)",
|
||||
async (overwrite) => {
|
||||
if (overwrite) mocks.list.mockResolvedValue([{ ...host, id: 19 }]);
|
||||
const payload = buildExportPayload(
|
||||
{ hosts: [host] },
|
||||
null,
|
||||
@@ -168,6 +171,60 @@ it.each([20, "20"])(
|
||||
},
|
||||
);
|
||||
|
||||
it("skips hosts that already exist when not overwriting", async () => {
|
||||
mocks.list.mockResolvedValue([{ ...host, id: 19 }]);
|
||||
const res = await importHosts([host]);
|
||||
expect(res.json).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ success: 0, updated: 0, skipped: 1, failed: 0 }),
|
||||
);
|
||||
expect(mocks.create).not.toHaveBeenCalled();
|
||||
expect(mocks.update).not.toHaveBeenCalled();
|
||||
expect(mocks.applyPluginSettings).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("maps a skipped jump host reference to its existing destination ID", async () => {
|
||||
mocks.list.mockResolvedValue([{ ...host, ip: "192.0.2.2", id: 801 }]);
|
||||
const res = await importHosts([
|
||||
{ ...host, exportId: 10, jumpHosts: [{ hostId: 20 }] },
|
||||
{ ...host, ip: "192.0.2.2", exportId: 20 },
|
||||
]);
|
||||
expect(res.json).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ success: 1, skipped: 1, failed: 0 }),
|
||||
);
|
||||
expect(mocks.create).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.update).not.toHaveBeenCalled();
|
||||
expect(JSON.parse(mocks.create.mock.calls[0][1].jumpHosts)).toEqual([
|
||||
{ hostId: 801 },
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
[false, { success: 0, updated: 0, skipped: 1 }],
|
||||
[true, { success: 0, updated: 1, skipped: 0 }],
|
||||
])(
|
||||
"SSH config import with an existing host (overwrite=%s)",
|
||||
async (overwrite, counts) => {
|
||||
mocks.list.mockResolvedValue([{ ...host, id: 19 }]);
|
||||
const res = { json: vi.fn(), status: vi.fn().mockReturnThis() };
|
||||
await sshConfigHandler(
|
||||
{
|
||||
userId: "user-1",
|
||||
body: {
|
||||
content: "Host web\n HostName 192.0.2.1\n User alice\n Port 22\n",
|
||||
overwrite,
|
||||
},
|
||||
} as unknown as Request,
|
||||
res as unknown as Response,
|
||||
vi.fn(),
|
||||
);
|
||||
expect(res.json).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ ...counts, failed: 0 }),
|
||||
);
|
||||
expect(mocks.create).not.toHaveBeenCalled();
|
||||
expect(mocks.update).toHaveBeenCalledTimes(overwrite ? 1 : 0);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
[{ ...host, jumpHosts: [{ hostId: 20 }] }],
|
||||
[{ ...host, jumpHosts: [{ hostId: "20" }] }],
|
||||
|
||||
Reference in new issue
Block a user