Skip to content

Commit

Permalink
build: make npm build to run integration tests too
Browse files Browse the repository at this point in the history
  • Loading branch information
ycmjason committed Oct 14, 2024
1 parent 4537787 commit d71885d
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 19 deletions.
2 changes: 1 addition & 1 deletion e2e/create-certificate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion e2e/workflows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion integration/account.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
5 changes: 4 additions & 1 deletion integration/order.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion integration/utils/PebbleChallTestSrv.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
19 changes: 19 additions & 0 deletions integration/utils/resolveDns.ts
Original file line number Diff line number Diff line change
@@ -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;
};
6 changes: 6 additions & 0 deletions integration/utils/setupNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const setupNode = () => {
if ("process" in globalThis) {
// @ts-ignore: node specific
globalThis["process"]["env"]["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
}
};
10 changes: 7 additions & 3 deletions scripts/build-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 2 additions & 1 deletion src/DnsUtils/_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const isIpVersionSupported = async (
},
});
return true;
} catch {
} catch (e) {
console.error(e);
return false;
}
})();
Expand Down
11 changes: 10 additions & 1 deletion src/DnsUtils/resolveDns.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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");
}
1 change: 1 addition & 0 deletions src/DnsUtils/resolveDns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type ResolveDnsFunction = <R extends "A" | "AAAA" | "NS" | "TXT">(
options?: {
nameServer?: {
ipAddr: string;
port?: number;
};
},
) => Promise<"TXT" extends R ? string[][] : string[]>;
Expand Down

0 comments on commit d71885d

Please sign in to comment.