Skip to content

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

TypeScript

Keep the end-to-end tests under the sign-in and send limits

Claude7534a00afda6
diff --git a/e2e/support/admin-session.ts b/e2e/support/admin-session.ts
index 91d743d..7c00c5f 100644
--- a/e2e/support/admin-session.ts
+++ b/e2e/support/admin-session.ts
@@ -2,6 +2,8 @@ import { expect, type Page } from "@playwright/test";
import pg from "pg";
import { e2eDatabaseUrl } from "../../playwright.config";
+import { startSendLimitAfresh } from "./send-limit";
+import { startSignInWindowAfresh } from "./sign-in-limit";
// The password every account in signing-flow.spec.ts is made with.
export const E2E_PASSWORD = "correct horse battery staple 42";
@@ -35,6 +37,10 @@ export async function signInAsAdministrator(page: Page): Promise<string> {
await database.query(
`update public.app_settings set require_two_factor = false`,
);
+ // These tests each sign in, one after another, and quick ones can fit
+ // four sign-ins into Better Auth's ten-second window.
+ await startSignInWindowAfresh(database);
+ await startSendLimitAfresh(database, admin.rows[0]?.id ?? "");
} finally {
await database.end();
}
diff --git a/e2e/support/send-limit.ts b/e2e/support/send-limit.ts
new file mode 100644
index 0000000..eec1954
--- /dev/null
+++ b/e2e/support/send-limit.ts
@@ -0,0 +1,17 @@
+import { createHash } from "node:crypto";
+
+import type pg from "pg";
+
+// One account may send ten envelopes an hour (the limit in
+// src/app/api/envelopes/[id]/send/route.ts), and together the tests send
+// more than that as the administrator. A test that sends starts that count
+// afresh, so whether it is refused does not depend on how many ran before.
+
+export async function startSendLimitAfresh(
+ database: pg.Client,
+ userId: string,
+): Promise<void> {
+ await database.query("delete from public.rate_limits where key = $1", [
+ createHash("sha256").update(`envelope-send:${userId}`).digest("hex"),
+ ]);
+}
diff --git a/e2e/support/sign-in-limit.ts b/e2e/support/sign-in-limit.ts
new file mode 100644
index 0000000..36e4123
--- /dev/null
+++ b/e2e/support/sign-in-limit.ts
@@ -0,0 +1,35 @@
+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, as Better Auth
+// keys them. It keeps an IPv6 address to its /64, so ::1, which a runner
+// that resolves localhost to IPv6 connects from, is all zeros.
+const LOCAL_ADDRESSES = [
+ "127.0.0.1",
+ "::1",
+ "0000:0000:0000:0000:0000:0000:0000:0000",
+ "::ffff:127.0.0.1",
+ "no-trusted-ip",
+];
+
+export async function startSignInWindowAfresh(
+ database: pg.Client,
+): Promise<void> {
+ // The key the app's rate limiter stores (hashed) for Better Auth's
+ // sign-in bucket.
+ 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],
+ );
+}