Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Name every setup field at once, and check only the repository's files
Clauded477d6eb1761
diff --git a/src/lib/project-contact.test.ts b/src/lib/project-contact.test.tsindex d4025d1..7358b22 100644--- a/src/lib/project-contact.test.ts+++ b/src/lib/project-contact.test.ts@@ -1,5 +1,6 @@import assert from "node:assert/strict";-import { readdirSync, readFileSync } from "node:fs";+import { execFileSync } from "node:child_process";+import { existsSync, readdirSync, readFileSync } from "node:fs";import path from "node:path";import { test } from "node:test";@@ -26,22 +27,49 @@ const SKIPPED_DIRECTORIES = new Set([const TEXT_FILE =/\.(?:css|example|html|js|json|md|mjs|sql|ts|tsx|txt|yaml|yml)$/;-function listTextFiles(directory: string): string[] {+function walkTextFiles(directory: string): string[] {return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {const entryPath = path.join(directory, entry.name);if (entry.isDirectory()) {return SKIPPED_DIRECTORIES.has(entry.name)? []- : listTextFiles(entryPath);+ : walkTextFiles(entryPath);}return entry.isFile() && TEXT_FILE.test(entry.name) ? [entryPath] : [];});}+/**+ * The text files that are part of the repository. Git is asked for them,+ * so a developer's own notes, editor folders and build output, which the+ * project does not ship, cannot fail the test. A copy without Git (a+ * downloaded archive) holds only the repository's files, and is walked.+ */+function listRepositoryTextFiles(): string[] {+ let tracked: string[];++ try {+ tracked = execFileSync("git", ["ls-files", "-z"], {+ cwd: repoRoot,+ encoding: "utf8",+ stdio: ["ignore", "pipe", "ignore"],+ })+ .split("\0")+ .filter((file) => file.length > 0);+ } catch {+ return walkTextFiles(repoRoot);+ }++ return tracked+ .filter((file) => TEXT_FILE.test(file))+ .map((file) => path.join(repoRoot, file))+ .filter((file) => existsSync(file));+}+test("nothing in the repository points at a site or a mailbox run by the project", () => {- const files = listTextFiles(repoRoot);+ const files = listRepositoryTextFiles();const offenders = files.filter((file) =>readFileSync(file, "utf8").toLowerCase().includes(formerHostedDomain),diff --git a/src/lib/setup.test.ts b/src/lib/setup.test.tsindex 31f6acb..46a2a1f 100644--- a/src/lib/setup.test.ts+++ b/src/lib/setup.test.ts@@ -129,3 +129,45 @@ test("setup refuses input it cannot use before touching the database", async ()},);});++test("a new install's form is told about every field at once, the account's included", async () => {+ const database = {} as DbPool;+ const createAdminAccount = async () => {+ throw new Error("must not be reached");+ };+ const refusedFields = async (input: Record<string, string>) => {+ try {+ await completeSetup(input, { createAdminAccount, database, env: {} });+ } catch (error) {+ assert.ok(error instanceof SetupError);+ assert.equal(error.statusCode, 400);++ return Object.keys(error.fieldErrors).sort();+ }++ return assert.fail("setup accepted input it should have refused");+ };++ // A support address that is not one, next to a password that is too+ // short: both are named in the same answer.+ assert.deepEqual(+ await refusedFields({+ email: "[email protected]",+ fullName: "Olive Owner",+ organizationName: "Acme Corp",+ password: "too short",+ supportAddress: "not an address",+ }),+ ["password", "supportAddress"],+ );++ // Claiming an older install sends no account fields, so none are judged.+ assert.deepEqual(+ await refusedFields({+ organizationName: "Acme Corp",+ setupToken: "a-secret-of-the-owner",+ supportAddress: "not an address",+ }),+ ["supportAddress"],+ );+});diff --git a/src/lib/setup.ts b/src/lib/setup.tsindex 95586a1..3e76053 100644--- a/src/lib/setup.ts+++ b/src/lib/setup.ts@@ -157,6 +157,32 @@ function toFieldErrors(error: z.ZodError) {return fieldErrors;}+const ACCOUNT_FIELDS = ["email", "fullName", "password"] as const;++/**+ * What is wrong with the account fields, when the request carries any of+ * them. The form of a new install sends them, and the form that claims an+ * older install does not, so this tells the two apart before the database+ * has been read.+ */+function accountFieldErrors(+ rawInput: unknown,+): Partial<Record<SetupFieldName, string>> {+ if (typeof rawInput !== "object" || rawInput === null) {+ return {};+ }++ const input = rawInput as Record<string, unknown>;++ if (ACCOUNT_FIELDS.every((field) => input[field] === undefined)) {+ return {};+ }++ const parsed = createAccountSchema.safeParse(rawInput);++ return parsed.success ? {} : toFieldErrors(parsed.error);+}+function tokensMatch(provided: string | undefined, expected: string): boolean {if (!provided) {return false;@@ -268,11 +294,12 @@ export async function completeSetup(const parsedBase = baseSetupSchema.safeParse(rawInput);if (!parsedBase.success) {- throw new SetupError(- "Check the highlighted fields.",- 400,- toFieldErrors(parsedBase.error),- );+ // One answer names every field that needs attention, the account's+ // among them, so nobody fixes one field only to be told about the next.+ throw new SetupError("Check the highlighted fields.", 400, {+ ...accountFieldErrors(rawInput),+ ...toFieldErrors(parsedBase.error),+ });}const base = parsedBase.data;