Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Keep the API key test from pushing the next sign-in over the limit

Claudef560f4d2280e
diff --git a/e2e/signing-flow.spec.ts b/e2e/signing-flow.spec.ts
index 6c92e83..08faa19 100644
--- a/e2e/signing-flow.spec.ts
+++ b/e2e/signing-flow.spec.ts
@@ -5,6 +5,7 @@ import { e2eDatabaseUrl, e2eSetupToken } from "../playwright.config";
import { extractLink, extractLinks, waitForEmail } from "./support/outbox";
import { totpCode } from "./support/totp";
import { writeLargePdf, writeSamplePdf } from "./support/sample-pdf";
+import { startSignInWindowAfresh } from "./support/sign-in-limit";
const PASSWORD = "correct horse battery staple 42";
// The ampersand is on purpose: the name goes into HTML emails and page titles.
@@ -1701,6 +1702,9 @@ test("an administrator makes an API key, the API accepts it, and revoking it sto
`select email from public."user" where role = 'admin' order by "createdAt" limit 1`,
)
).rows[0]?.email ?? "";
+ // This test's sign-in comes between others, so the two-step test after
+ // it would be the fourth sign-in in ten seconds.
+ await startSignInWindowAfresh(database);
} finally {
await database.end();
}
diff --git a/e2e/support/sign-in-limit.ts b/e2e/support/sign-in-limit.ts
new file mode 100644
index 0000000..046516c
--- /dev/null
+++ b/e2e/support/sign-in-limit.ts
@@ -0,0 +1,31 @@
+import { createHash } from "node:crypto";
+
+import type pg from "pg";
+
+// Better Auth lets one address sign in three times in ten seconds, and every
+// test signs in from this machine. A test that adds a sign-in between others
+// would push the next one over that limit, and which test is refused would
+// depend on how fast the ones before it ran. Such a test starts the window
+// afresh before signing in.
+
+// The addresses the app may see a local request come from, and the key the
+// app's rate limiter stores (hashed) for Better Auth's sign-in bucket.
+const LOCAL_ADDRESSES = [
+ "127.0.0.1",
+ "::1",
+ "::ffff:127.0.0.1",
+ "no-trusted-ip",
+];
+
+export async function startSignInWindowAfresh(
+ database: pg.Client,
+): Promise<void> {
+ const keys = LOCAL_ADDRESSES.map((address) =>
+ createHash("sha256").update(`auth:${address}|/sign-in/email`).digest("hex"),
+ );
+
+ await database.query(
+ "delete from public.rate_limits where key = any($1::text[])",
+ [keys],
+ );
+}