feat(hub): add idempotent service registration
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import { mkdtemp, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { createControlPlaneStore } from "./control-plane-store.mjs";
|
||||
|
||||
const temporaryRoots = [];
|
||||
const actor = { sub: "phase1-contract", name: "Phase 1 contract test" };
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(temporaryRoots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
|
||||
});
|
||||
|
||||
async function createStore() {
|
||||
const projectRoot = await mkdtemp(join(tmpdir(), "nodedc-launcher-service-registration-"));
|
||||
temporaryRoots.push(projectRoot);
|
||||
return createControlPlaneStore({ projectRoot });
|
||||
}
|
||||
|
||||
function registration(overrides = {}) {
|
||||
return {
|
||||
slug: "device-core",
|
||||
title: "DEVICE CORE",
|
||||
subtitle: "Управление устройствами",
|
||||
description: "Универсальный Device Manager NODE.DC.",
|
||||
url: "https://device.nodedc.ru",
|
||||
launchUrl: "https://device.nodedc.ru",
|
||||
logoutUrl: "https://device.nodedc.ru/auth/logout",
|
||||
loginSyncUrl: "https://device.nodedc.ru/auth/login-sync",
|
||||
status: "active",
|
||||
authentikApplicationSlug: "device-core",
|
||||
authentikGroupName: "nodedc:device-core:access",
|
||||
authHandoffPath: "/auth/nodedc/handoff",
|
||||
isAvailableForAllNewClients: false,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("generic Hub service registration", () => {
|
||||
it("creates and then updates one service by slug without an implicit grant", async () => {
|
||||
const store = await createStore();
|
||||
const first = await store.ensureService("device-core", registration(), actor);
|
||||
|
||||
expect(first.created).toBe(true);
|
||||
expect(first.service).toMatchObject({
|
||||
slug: "device-core",
|
||||
authentikApplicationSlug: "device-core",
|
||||
authentikGroupName: "nodedc:device-core:access",
|
||||
authHandoffPath: "/auth/nodedc/handoff",
|
||||
isAvailableForAllNewClients: false,
|
||||
});
|
||||
expect(first.data.grants.some((grant) => grant.serviceId === first.service.id)).toBe(false);
|
||||
|
||||
const second = await store.ensureService("device-core", registration({ title: "DEVICE CORE 2" }), actor);
|
||||
const registered = second.data.services.filter((service) => service.slug === "device-core");
|
||||
|
||||
expect(second.created).toBe(false);
|
||||
expect(second.service.id).toBe(first.service.id);
|
||||
expect(second.service.title).toBe("DEVICE CORE 2");
|
||||
expect(registered).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("rejects slug mismatch, duplicate identities and unsafe handoff destinations", async () => {
|
||||
const store = await createStore();
|
||||
await store.ensureService("device-core", registration(), actor);
|
||||
|
||||
await expect(store.ensureService("device-core", registration({ slug: "another-service" }), actor)).rejects.toThrow(
|
||||
"service slug path and payload mismatch"
|
||||
);
|
||||
await expect(
|
||||
store.createService(registration({ slug: "device-core-copy", authentikApplicationSlug: "device-core" }), actor)
|
||||
).rejects.toThrow("Authentik application slug already exists");
|
||||
await expect(
|
||||
store.createService(registration({
|
||||
slug: "device-core-group-copy",
|
||||
authentikApplicationSlug: "device-core-group-copy",
|
||||
}), actor)
|
||||
).rejects.toThrow("Authentik group already exists");
|
||||
await expect(
|
||||
store.ensureService("unsafe-handoff", registration({
|
||||
slug: "unsafe-handoff",
|
||||
authentikApplicationSlug: "unsafe-handoff",
|
||||
authHandoffPath: "https://attacker.example/handoff",
|
||||
}), actor)
|
||||
).rejects.toThrow("authHandoffPath must be a relative absolute-path reference");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user