From d71885da48065da64cadb08e31ef39ba7b30bb31 Mon Sep 17 00:00:00 2001 From: Jason Yu Date: Mon, 14 Oct 2024 23:49:33 +0100 Subject: [PATCH] build: make npm build to run integration tests too --- e2e/create-certificate.test.ts | 2 +- e2e/workflows.test.ts | 2 +- ...{workflows.test.ts => 01workflows.test.ts} | 16 +++++++--------- integration/account.test.ts | 5 ++++- integration/order.test.ts | 5 ++++- integration/utils/PebbleChallTestSrv.ts | 2 +- integration/utils/resolveDns.ts | 19 +++++++++++++++++++ integration/utils/setupNode.ts | 6 ++++++ scripts/build-npm.ts | 10 +++++++--- src/DnsUtils/_helpers.ts | 3 ++- src/DnsUtils/resolveDns.node.ts | 11 ++++++++++- src/DnsUtils/resolveDns.ts | 1 + 12 files changed, 63 insertions(+), 19 deletions(-) rename integration/{workflows.test.ts => 01workflows.test.ts} (81%) create mode 100644 integration/utils/resolveDns.ts create mode 100644 integration/utils/setupNode.ts diff --git a/e2e/create-certificate.test.ts b/e2e/create-certificate.test.ts index c530e64..4971314 100644 --- a/e2e/create-certificate.test.ts +++ b/e2e/create-certificate.test.ts @@ -6,7 +6,7 @@ import { AcmeOrder, Dns01Challenge, DnsUtils, -} from "@fishballpkg/acme"; +} from "../src/mod.ts"; import { expect, it } from "../test_deps.ts"; import { CloudflareZone } from "./utils/cloudflare.ts"; import { expectToBeDefined } from "./utils/expectToBeDefined.ts"; diff --git a/e2e/workflows.test.ts b/e2e/workflows.test.ts index c6c07ef..f169511 100644 --- a/e2e/workflows.test.ts +++ b/e2e/workflows.test.ts @@ -3,7 +3,7 @@ import { AcmeClient, AcmeOrder, AcmeWorkflows, -} from "@fishballpkg/acme"; +} from "../src/mod.ts"; import { describe, expect, it } from "../test_deps.ts"; import { CloudflareZone } from "./utils/cloudflare.ts"; import { randomFishballTestingSubdomain } from "./utils/randomFishballTestingSubdomain.ts"; diff --git a/integration/workflows.test.ts b/integration/01workflows.test.ts similarity index 81% rename from integration/workflows.test.ts rename to integration/01workflows.test.ts index 969dbb4..28c5faa 100644 --- a/integration/workflows.test.ts +++ b/integration/01workflows.test.ts @@ -1,8 +1,13 @@ -import { AcmeClient, AcmeOrder, AcmeWorkflows } from "@fishballpkg/acme"; +// 01 prefix to this file because of https://github.com/denoland/dnt/issues/432 +import { AcmeClient, AcmeOrder, AcmeWorkflows } from "../src/mod.ts"; import { describe, expect, it } from "../test_deps.ts"; import { EMAIL, PEBBLE_DIRECTORY_URL } from "./CONSTANTS.ts"; import { generateRandomDomain } from "./utils/generateRandomDomain.ts"; import { PebbleChallTestSrv } from "./utils/PebbleChallTestSrv.ts"; +import { resolveDns } from "./utils/resolveDns.ts"; +import { setupNode } from "./utils/setupNode.ts"; + +setupNode(); const DOMAINS = [ generateRandomDomain(), @@ -27,14 +32,7 @@ describe("requestCertificates", () => { updateDnsRecords: async (dnsRecords) => { await pebbleChallTestSrv.createDnsRecords(dnsRecords); }, - resolveDns: async (query, recordType) => { - return await Deno.resolveDns(query, recordType, { - nameServer: { - ipAddr: "127.0.0.1", - port: 8053, - }, - }); - }, + resolveDns, }); expect(certKeyPair.privateKey).toBeInstanceOf(CryptoKey); diff --git a/integration/account.test.ts b/integration/account.test.ts index 8712e59..bd6daa1 100644 --- a/integration/account.test.ts +++ b/integration/account.test.ts @@ -1,6 +1,9 @@ -import { AcmeClient } from "@fishballpkg/acme"; +import { AcmeClient } from "../src/mod.ts"; import { expect, it } from "../test_deps.ts"; import { EMAIL, PEBBLE_DIRECTORY_URL } from "./CONSTANTS.ts"; +import { setupNode } from "./utils/setupNode.ts"; + +setupNode(); it("should create the account successfully and the key pair should allow login", async () => { const client = await AcmeClient.init(PEBBLE_DIRECTORY_URL); diff --git a/integration/order.test.ts b/integration/order.test.ts index 2711ff5..b9f74a7 100644 --- a/integration/order.test.ts +++ b/integration/order.test.ts @@ -1,7 +1,10 @@ -import { AcmeClient } from "@fishballpkg/acme"; +import { AcmeClient } from "../src/mod.ts"; import { expect, it } from "../test_deps.ts"; import { EMAIL, PEBBLE_DIRECTORY_URL } from "./CONSTANTS.ts"; import { generateRandomDomain } from "./utils/generateRandomDomain.ts"; +import { setupNode } from "./utils/setupNode.ts"; + +setupNode(); it("should place an order correctly and get the corresponding authorizations", async () => { const client = await AcmeClient.init(PEBBLE_DIRECTORY_URL); diff --git a/integration/utils/PebbleChallTestSrv.ts b/integration/utils/PebbleChallTestSrv.ts index b20eba7..fb8e3b9 100644 --- a/integration/utils/PebbleChallTestSrv.ts +++ b/integration/utils/PebbleChallTestSrv.ts @@ -1,4 +1,4 @@ -import type { DnsTxtRecord } from "@fishballpkg/acme"; +import type { DnsTxtRecord } from "../../src/mod.ts"; import { afterEach } from "../../test_deps.ts"; const PEBBLE_CHALLTESTSRV_URL = "http://localhost:8055"; diff --git a/integration/utils/resolveDns.ts b/integration/utils/resolveDns.ts new file mode 100644 index 0000000..b289d48 --- /dev/null +++ b/integration/utils/resolveDns.ts @@ -0,0 +1,19 @@ +import { + defaultResolveDns, + type ResolveDnsFunction, +} from "../../src/DnsUtils/resolveDns.ts"; + +/** + * A resolveDns function specifically for integration tests to allow TXT lookups to be done via pebble-testchallsrv + */ +export const resolveDns: ResolveDnsFunction = async (query, recordType) => { + return (await defaultResolveDns(query, recordType, { + nameServer: recordType === "TXT" + ? { + ipAddr: "127.0.0.1", + port: 8053, + } // only lookup via pebble-challtestsrv for txt records + : undefined, + // deno-lint-ignore no-explicit-any -- typescript is hard + })) as any; +}; diff --git a/integration/utils/setupNode.ts b/integration/utils/setupNode.ts new file mode 100644 index 0000000..17150fb --- /dev/null +++ b/integration/utils/setupNode.ts @@ -0,0 +1,6 @@ +export const setupNode = () => { + if ("process" in globalThis) { + // @ts-ignore: node specific + globalThis["process"]["env"]["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; + } +}; diff --git a/scripts/build-npm.ts b/scripts/build-npm.ts index 7f1cab4..cf0b398 100644 --- a/scripts/build-npm.ts +++ b/scripts/build-npm.ts @@ -19,16 +19,20 @@ await emptyDir(OUT_DIR); const GITHUB_REPO = "https://github.com/fishballapp/acme"; await build({ + testPattern: "integration/**/*.test.ts", entryPoints: Object.entries(DENO_JSON.exports).map(([name, path]) => ({ kind: "export", name, path: join(PROJECT_ROOT, path), })), outDir: OUT_DIR, - test: false, - shims: {}, + test: true, + shims: { + deno: "dev", + undici: "dev", + }, compilerOptions: { - lib: ["ES2023", "DOM"], + lib: ["ESNext", "DOM"], target: "ES2022", }, package: { diff --git a/src/DnsUtils/_helpers.ts b/src/DnsUtils/_helpers.ts index a41c829..c2ec73f 100644 --- a/src/DnsUtils/_helpers.ts +++ b/src/DnsUtils/_helpers.ts @@ -28,7 +28,8 @@ const isIpVersionSupported = async ( }, }); return true; - } catch { + } catch (e) { + console.error(e); return false; } })(); diff --git a/src/DnsUtils/resolveDns.node.ts b/src/DnsUtils/resolveDns.node.ts index 3c52785..a51e2bb 100644 --- a/src/DnsUtils/resolveDns.node.ts +++ b/src/DnsUtils/resolveDns.node.ts @@ -5,6 +5,7 @@ */ import { Resolver } from "node:dns/promises"; +import { isIPv4, isIPv6 } from "node:net"; import type { ResolveDnsFunction } from "./resolveDns.ts"; export const resolveDns: ResolveDnsFunction = async ( @@ -14,9 +15,17 @@ export const resolveDns: ResolveDnsFunction = async ( ) => { const resolver = new Resolver(); if (options?.nameServer?.ipAddr !== undefined) { - resolver.setServers([options.nameServer.ipAddr]); + resolver.setServers([ + ipPort(options.nameServer.ipAddr, options.nameServer.port ?? 53), + ]); } // deno-lint-ignore no-explicit-any -- typescript is hard return (await resolver.resolve(domain, recordType)) as any; }; + +function ipPort(ip: string, port: number): string { + if (isIPv4(ip)) return `${ip}:${port}`; + if (isIPv6(ip)) return `[${ip}]:${port}`; + throw new Error("Invalid IP address"); +} diff --git a/src/DnsUtils/resolveDns.ts b/src/DnsUtils/resolveDns.ts index 19d06fc..83107ac 100644 --- a/src/DnsUtils/resolveDns.ts +++ b/src/DnsUtils/resolveDns.ts @@ -7,6 +7,7 @@ export type ResolveDnsFunction = ( options?: { nameServer?: { ipAddr: string; + port?: number; }; }, ) => Promise<"TXT" extends R ? string[][] : string[]>;