Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(auth): cover OTP concurrent-guess atomicity against real MySQL

addyCooks6473cc94ccdd
diff --git a/apps/web/__tests__/integration/sso-database.test.ts b/apps/web/__tests__/integration/sso-database.test.ts
index 5d4c6ce48..3f9d61988 100644
--- a/apps/web/__tests__/integration/sso-database.test.ts
+++ b/apps/web/__tests__/integration/sso-database.test.ts
@@ -6,6 +6,7 @@ import {
organizationSso,
organizations,
users,
+ verificationTokens,
} from "@cap/database/schema";
import { Organisation, User } from "@cap/web-domain";
import { and, eq } from "drizzle-orm";
@@ -344,5 +345,31 @@ describe.runIf(Boolean(databaseUrl))(
.where(eq(organizationMembers.userId, userId)),
).toHaveLength(0);
});
+
+ it("burns an email OTP exactly once under concurrent guesses", async () => {
+ const identifier = `${id()}@example.com`;
+ const token = "482913";
+ const adapter = DrizzleAdapter(database());
+ if (!adapter.useVerificationToken) {
+ throw new Error("Missing useVerificationToken adapter.");
+ }
+ await database()
+ .insert(verificationTokens)
+ .values({ identifier, token, expires: new Date(Date.now() + 600_000) });
+
+ const attempts = await Promise.all(
+ Array.from({ length: 8 }, () =>
+ adapter.useVerificationToken({ identifier, token }),
+ ),
+ );
+
+ expect(attempts.filter((result) => result !== null)).toHaveLength(1);
+ expect(
+ await database()
+ .select()
+ .from(verificationTokens)
+ .where(eq(verificationTokens.identifier, identifier)),
+ ).toHaveLength(0);
+ });
},
);