57 lines
3.7 KiB
TypeScript
57 lines
3.7 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { getEnabledServerById } from "./wireguard-live.js"
|
|
import { MikrotikClient } from "./mikrotik.js"
|
|
import { pool, closePool } from "../db/index.js"
|
|
import { initDatabase } from "../db/bootstrap.js"
|
|
import { buildApp } from "../index.js"
|
|
|
|
try {
|
|
for (const id of ["srv1", "1abc", "", "0", "-1", "1.5", "9007199254740993", "2147483648"]) assert.equal(await getEnabledServerById(id), null)
|
|
const own = process.env.MMAPP_OWN_TEST_DB
|
|
if (own) {
|
|
assert(/^mm_accept_152_\d+$/.test(own))
|
|
assert.equal((await pool.query("SELECT current_database() AS name")).rows[0].name, own)
|
|
await initDatabase()
|
|
const id = Number((await pool.query("INSERT INTO servers (name,host,enabled) VALUES ('disabled-network-fixture','127.0.0.1',false) RETURNING id")).rows[0].id)
|
|
const app = await buildApp({ logger: false, startScheduler: false })
|
|
const original = MikrotikClient.fromServer
|
|
let contacts = 0, checks = 0
|
|
try {
|
|
MikrotikClient.fromServer = () => { contacts++; throw new Error("Disabled server was contacted") }
|
|
const key = Buffer.alloc(32, 7).toString("base64")
|
|
for (const type of ["home-router", "jump-host", "exit-node"]) {
|
|
await pool.query("UPDATE servers SET type=$1 WHERE id=$2", [type, id])
|
|
assert.equal(await getEnabledServerById(id), null)
|
|
for (const path of ["/wireguard", "/gre/tunnels"]) {
|
|
const r = await app.inject({ method: "GET", url: `/api${path}?serverId=${id}` })
|
|
assert.equal(r.statusCode, 200)
|
|
assert.deepEqual(r.json().failures, [])
|
|
assert.deepEqual(path === "/wireguard" ? r.json().interfaces : r.json().tunnels, [])
|
|
checks++
|
|
}
|
|
const cases = [
|
|
{ method: "POST", path: "/wireguard/interfaces", payload: { serverId: id, name: "own", address: "fd00::1/64" } },
|
|
{ method: "PATCH", path: `/wireguard/interfaces/${id}/*none`, payload: { address: "fd00::1/64" } },
|
|
{ method: "DELETE", path: `/wireguard/interfaces/${id}/*none`, payload: undefined },
|
|
{ method: "POST", path: "/wireguard/peers", payload: { serverId: id, interfaceName: "own", publicKey: key, allowedAddresses: ["fd00::2/128"] } },
|
|
{ method: "POST", path: "/wireguard/export", payload: { serverId: id, interfaceName: "own", format: "rsc" } },
|
|
{ method: "POST", path: "/wireguard/import", payload: { serverId: id, format: "conf", content: `[Interface]\nPrivateKey = ${key}\nAddress = fd00::1/64\n` } },
|
|
{ method: "POST", path: "/gre/tunnels", payload: { serverId: String(id), name: "own", remoteAddress: "192.0.2.1", localInnerIp: "fd00::1/64" } },
|
|
{ method: "PATCH", path: "/gre/tunnels", payload: { serverId: String(id), name: "own", localInnerIp: "fd00::1/64" } },
|
|
{ method: "DELETE", path: "/gre/tunnels", payload: { serverId: String(id), name: "own" } },
|
|
] as const
|
|
for (const item of cases) {
|
|
const r = await app.inject({ method: item.method, url: "/api" + item.path, payload: item.payload })
|
|
assert.equal(r.statusCode, 404, type + " " + item.path + " " + r.body)
|
|
checks++
|
|
}
|
|
}
|
|
assert.equal(checks, 33)
|
|
assert.equal(contacts, 0)
|
|
await pool.query("UPDATE servers SET enabled=true WHERE id=$1", [id])
|
|
assert.equal((await getEnabledServerById(id))?.id, id)
|
|
console.log("network-disabled-server.test.ts: 33 API/SQL checks for three types, zero RouterOS contacts, enabled positive control passed")
|
|
} finally { MikrotikClient.fromServer = original; await app.close() }
|
|
} else console.log("network-disabled-server.test.ts: invalid IDs passed; own SQL/disabled API checks skipped (MMAPP_OWN_TEST_DB required)")
|
|
} finally { await closePool() }
|