71 lines
4.0 KiB
TypeScript
71 lines
4.0 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import { createCertificateIssueJob, getCertificateIssueJob, getAcmeSettings, putAcmeSettings, testAcmeSettings, getCertificateRenewSettings, putCertificateRenewSettings } from "./certificates"
|
|
const base = "https://api.example.test"
|
|
const issue = { serverId: "1", certName: "cert", domainNames: ["test.example"] }
|
|
const acme = { directoryUrl: "https://acme.example.test/directory", tokenConfigured: false }
|
|
const renew = { enabled: false, intervalSec: 300, renewBeforeDays: 30 }
|
|
const job = { id: "job-1", serverId: "1", certName: "cert", domainNames: ["test.example"], status: "queued", step: "queued", requestedAt: "2026-10-01T00:00:00Z" }
|
|
|
|
test("settings readers reject malformed and empty responses instead of defaults", async () => {
|
|
const original = globalThis.fetch
|
|
try {
|
|
for (const payload of [{}, [], { ...acme, tokenConfigured: "false" }, { ...renew, enabled: "false" }]) {
|
|
globalThis.fetch = async () => new Response(JSON.stringify(payload), { status: 200 })
|
|
await assert.rejects(() => getAcmeSettings(base))
|
|
await assert.rejects(() => getCertificateRenewSettings(base))
|
|
}
|
|
globalThis.fetch = async () => new Response(null, { status: 204 })
|
|
await assert.rejects(() => putAcmeSettings(base, {}))
|
|
await assert.rejects(() => putCertificateRenewSettings(base, { enabled: true }))
|
|
} finally { globalThis.fetch = original }
|
|
})
|
|
|
|
test("strict settings preserve false, carry caller cancellation and PUT body", async () => {
|
|
const original = globalThis.fetch; let init: RequestInit | undefined
|
|
globalThis.fetch = async (url, options) => { init = options; return new Response(JSON.stringify(String(url).includes("renew") ? renew : acme), { status: 200 }) }
|
|
try {
|
|
const abort = new AbortController()
|
|
assert.equal((await getAcmeSettings(base, { signal: abort.signal })).tokenConfigured, false)
|
|
const signal = init?.signal; abort.abort(); assert.equal(signal?.aborted, true)
|
|
assert.equal((await putCertificateRenewSettings(base, { enabled: false })).enabled, false)
|
|
assert.equal(init?.method, "PUT"); assert.equal(init?.body, JSON.stringify({ enabled: false }))
|
|
} finally { globalThis.fetch = original }
|
|
})
|
|
|
|
test("invalid request fields fail before sending mutation", async () => {
|
|
const original = globalThis.fetch; let calls = 0
|
|
globalThis.fetch = async () => { calls++; return new Response("{}", { status: 200 }) }
|
|
try {
|
|
await assert.rejects(() => putAcmeSettings(base, { directoryUrl: "bad" }))
|
|
await assert.rejects(() => putCertificateRenewSettings(base, { intervalSec: 0 }))
|
|
await assert.rejects(() => createCertificateIssueJob(base, { ...issue, domainNames: [] }))
|
|
assert.equal(calls, 0)
|
|
} finally { globalThis.fetch = original }
|
|
})
|
|
|
|
test("issue acknowledgment and polling require matching job identity", async () => {
|
|
const original = globalThis.fetch
|
|
try {
|
|
for (const payload of [{}, { jobId: "" }, { jobId: " " }]) {
|
|
globalThis.fetch = async () => new Response(JSON.stringify(payload), { status: 200 })
|
|
await assert.rejects(() => createCertificateIssueJob(base, issue))
|
|
}
|
|
globalThis.fetch = async () => new Response(JSON.stringify(job), { status: 200 })
|
|
assert.equal((await getCertificateIssueJob(base, "job-1")).status, "queued")
|
|
await assert.rejects(() => getCertificateIssueJob(base, "job-other"), /другой задаче/)
|
|
} finally { globalThis.fetch = original }
|
|
})
|
|
|
|
test("Cloudflare failure preserves backend message and malformed success is rejected", async () => {
|
|
const original = globalThis.fetch
|
|
try {
|
|
globalThis.fetch = async () => new Response(JSON.stringify({ ok: false, message: "Cloudflare отказал" }), { status: 400 })
|
|
await assert.rejects(() => testAcmeSettings(base), /Cloudflare отказал/)
|
|
globalThis.fetch = async () => new Response(JSON.stringify({}), { status: 200 })
|
|
await assert.rejects(() => testAcmeSettings(base))
|
|
globalThis.fetch = async () => new Response(JSON.stringify({ ok: false, message: "denied" }), { status: 200 })
|
|
assert.equal((await testAcmeSettings(base)).ok, false)
|
|
} finally { globalThis.fetch = original }
|
|
})
|