|
| 1 | +import assert from "node:assert" |
| 2 | +import fs from "node:fs" |
| 3 | +import net from "node:net" |
| 4 | +import { afterEach, describe, it } from "node:test" |
| 5 | +import tls from "node:tls" |
| 6 | + |
| 7 | +import { generate, getCerts, remove } from "../src/certs.ts" |
| 8 | +import { getEnv } from "../src/env.ts" |
| 9 | + |
| 10 | +describe("certs", { timeout: 300000 }, () => { |
| 11 | + afterEach(() => { |
| 12 | + remove("test/custom-folder") |
| 13 | + remove("test/custom folder") |
| 14 | + delete process.env["CERT_PATH"] |
| 15 | + delete process.env["HOST"] |
| 16 | + }) |
| 17 | + |
| 18 | + it("can be uninstalled", () => { |
| 19 | + remove() |
| 20 | + }) |
| 21 | + |
| 22 | + it("uninstall is idempotent (doesn't fail if called twice)", () => { |
| 23 | + remove() |
| 24 | + }) |
| 25 | + |
| 26 | + it("can be installed", async () => { |
| 27 | + await generate() |
| 28 | + }) |
| 29 | + |
| 30 | + it("provides the certificate", async () => { |
| 31 | + const env = getEnv() |
| 32 | + const appCerts = await getCerts({ |
| 33 | + domain: env.HOST, |
| 34 | + certPath: env.CERT_PATH, |
| 35 | + reinstall: env.REINSTALL, |
| 36 | + }) |
| 37 | + const realCerts = await getCerts({ |
| 38 | + domain: env.HOST, |
| 39 | + certPath: env.CERT_PATH, |
| 40 | + reinstall: env.REINSTALL, |
| 41 | + }) |
| 42 | + assert.deepStrictEqual(appCerts, realCerts) |
| 43 | + }) |
| 44 | + |
| 45 | + it("works with environment domain", async () => { |
| 46 | + const appCerts = await getCerts({ domain: "192.168.0.1" }) |
| 47 | + const secureContext = tls.createSecureContext({ |
| 48 | + cert: appCerts.cert, |
| 49 | + }) |
| 50 | + const secureSocket = new tls.TLSSocket(new net.Socket(), { |
| 51 | + secureContext, |
| 52 | + }) |
| 53 | + const cert = secureSocket.getCertificate() |
| 54 | + assert(cert && "subjectaltname" in cert) |
| 55 | + const certDomain = cert.subjectaltname?.split(":")[1] |
| 56 | + |
| 57 | + assert.strictEqual(certDomain, "192.168.0.1") |
| 58 | + }) |
| 59 | + |
| 60 | + it("crashes if certs do not exist in custom folder", async () => { |
| 61 | + const customCertPath = "test/custom-folder" |
| 62 | + await generate({ appDataPath: customCertPath }) |
| 63 | + fs.unlinkSync("test/custom-folder/localhost.crt") |
| 64 | + fs.unlinkSync("test/custom-folder/localhost.key") |
| 65 | + |
| 66 | + await assert.rejects(getCerts({ certPath: customCertPath }), /Certificates are missing/) |
| 67 | + remove(customCertPath) |
| 68 | + }) |
| 69 | +}) |
0 commit comments