Skip to content
File

Blob: tests/worker/db/tessera-identities.workers.test.ts

typescript54 lines
1import { beforeEach, describe, expect, it } from "vitest";
2import { eq } from "drizzle-orm";
3 
4import { tesseraIdentities, users } from "@/worker/db/d1/schema";
5import { getDb, resetD1Tables } from "@tests/worker/helpers/db";
6import { seedTesseraIdentity, seedUser } from "@tests/worker/helpers/seeds";
7 
8describe("tessera_identities schema", () => {
9 beforeEach(async () => {
10 await resetD1Tables();
11 });
12 
13 it("rejects two rows with the same sub", async () => {
14 const a = await seedUser({ email: "a@example.com" });
15 const b = await seedUser({ email: "b@example.com" });
16 await seedTesseraIdentity({ sub: "shared-sub", user_id: a.id });
17 
18 await expect(seedTesseraIdentity({ sub: "shared-sub", user_id: b.id })).rejects.toThrow();
19 });
20 
21 it("rejects two identity rows for the same user_id", async () => {
22 const user = await seedUser({ email: "single@example.com" });
23 await seedTesseraIdentity({ sub: "sub-1", user_id: user.id });
24 
25 await expect(seedTesseraIdentity({ sub: "sub-2", user_id: user.id })).rejects.toThrow();
26 });
27 
28 it("loads legacy users without an identity row", async () => {
29 const legacy = await seedUser({ email: "legacy@example.com" });
30 const identity = await getDb()
31 .select()
32 .from(tesseraIdentities)
33 .where(eq(tesseraIdentities.user_id, legacy.id))
34 .get();
35 expect(identity).toBeUndefined();
36 const stored = await getDb().select().from(users).where(eq(users.id, legacy.id)).get();
37 expect(stored?.email).toBe("legacy@example.com");
38 });
39 
40 it("cascades identity rows when the user is deleted", async () => {
41 const user = await seedUser({ email: "cascade@example.com" });
42 await seedTesseraIdentity({ sub: "cascade-sub", user_id: user.id });
43 
44 await getDb().delete(users).where(eq(users.id, user.id));
45 
46 const remaining = await getDb()
47 .select()
48 .from(tesseraIdentities)
49 .where(eq(tesseraIdentities.sub, "cascade-sub"))
50 .get();
51 expect(remaining).toBeUndefined();
52 });
53});