Files
MikrotikManager/backend/src/services/ipsec-user-delete.test.ts
T

36 lines
3.6 KiB
TypeScript

import assert from "node:assert/strict"
import { planIpsecUserDelete, projectIpsecUserDelete } from "./ipsec-user-delete.js"
import { ipsecPeerDeletePaths, type IpsecPeerDeleteState } from "./ipsec-peer-delete.js"
import type { ServerRow } from "../modules/servers/repository/servers-repository.js"
const server = { id: 1, enabled: true, host: "fixture", name: "fixture" } as ServerRow
const base = (): IpsecPeerDeleteState => ({ source: "private", proof: "private", protections: [], catalogs: { ...Object.fromEntries(ipsecPeerDeletePaths.map(path => [path, []])),
"/ip/ipsec/peer": [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", disabled: "true" }],
"/ip/ipsec/identity": [{ ".id": "*i", peer: "ike", "auth-method": "digital-signature", "remote-certificate": "ipsec-user-Alice", "mode-config": "mc-ipsec-alice", comment: "MikrotikManager:ipsec user=Alice", disabled: "true" }],
"/ip/ipsec/mode-config": [{ ".id": "*m", name: "mc-ipsec-alice", address: "198.18.0.1" }],
"/certificate": [{ ".id": "*c", name: "ipsec-user-Alice", "common-name": "Alice", "key-usage": "tls-client", ca: "ca", opaque: "secret" }, { ".id": "*a", name: "ca", "key-usage": "key-cert-sign", authority: "true" }]
} })
const state = base(), commands = planIpsecUserDelete(state, server, "identity:*i")
assert.deepEqual(commands, [{ path: "/ip/ipsec/identity", id: "*i" }, { path: "/ip/ipsec/mode-config", id: "*m" }, { path: "/certificate", id: "*c" }])
assert.equal(planIpsecUserDelete(state, server, "cert:*c").length, 3)
assert.equal(planIpsecUserDelete(state, server, "identity:*i", false).length, 2)
let projected = state; for (const command of commands) projected = projectIpsecUserDelete(projected, command)
assert.deepEqual(projected.catalogs["/certificate"], [state.catalogs["/certificate"][1]])
assert.equal(state.catalogs["/ip/ipsec/identity"].length, 1)
const shared = base(); shared.catalogs["/ip/ipsec/identity"].push({ ".id": "*j", "mode-config": "mc-ipsec-alice", "remote-certificate": "ipsec-user-Alice" })
assert.throws(() => planIpsecUserDelete(shared, server, "identity:*i"), /другой identity/)
assert.deepEqual(planIpsecUserDelete(shared, server, "identity:*i", false), [{ path: "/ip/ipsec/identity", id: "*i" }])
const crl = base(); crl.catalogs["/certificate"][0].issued = "true"; crl.catalogs["/certificate"][1].crl = "true"
assert.throws(() => planIpsecUserDelete(crl, server, "identity:*i"), /CRL/)
crl.catalogs["/certificate"][1].crl="yes";crl.catalogs["/certificate"][0].issued="yes"
assert.throws(()=>planIpsecUserDelete(crl,server,"identity:*i"),/CRL/)
const children = base(); children.catalogs["/certificate"].push({ ".id": "*d", name: "descendant", ca: "ipsec-user-Alice" })
assert.throws(() => planIpsecUserDelete(children, server, "identity:*i"), /цепочку/)
const ambiguous = base(); ambiguous.catalogs["/certificate"][0][".id"] = "*i"
assert.throws(() => planIpsecUserDelete(ambiguous, server, "*i"), /Неоднозначный/)
assert.equal(planIpsecUserDelete(ambiguous, server, "identity:*i").length, 3)
const certOnly = base(); certOnly.catalogs["/ip/ipsec/identity"] = []
assert.deepEqual(planIpsecUserDelete(certOnly, server, "cert:*c"), [{ path: "/certificate", id: "*c" }])
assert.deepEqual(planIpsecUserDelete(certOnly, server, "cert:*c", false), [])
const system = base(); system.catalogs["/ip/ipsec/identity"][0].dynamic = "true"; assert.throws(() => planIpsecUserDelete(system, server, "identity:*i"), /Системный/)
console.log("✓ IPsec user delete planner: full optional certificate cascade, exact namespace ambiguity, shared refs/mode-config, CA descendants/CRL, cert-only no-op, immutable projection, native/system guards")