|
| 1 | +import { postgresTest } from "@internal/testcontainers"; |
| 2 | +import { describe, expect, vi } from "vitest"; |
| 3 | +import { findOrCreateDirectoryUser } from "~/models/directoryUser.server"; |
| 4 | + |
| 5 | +vi.setConfig({ testTimeout: 60_000 }); |
| 6 | + |
| 7 | +describe("findOrCreateDirectoryUser", () => { |
| 8 | + postgresTest("reuses the user with the lowercased email", async ({ prisma }) => { |
| 9 | + const existing = await prisma.user.create({ |
| 10 | + data: { email: "kai@example.com", authenticationMethod: "GITHUB" }, |
| 11 | + }); |
| 12 | + |
| 13 | + const result = await findOrCreateDirectoryUser(prisma, { |
| 14 | + email: "Kai@Example.com", |
| 15 | + firstName: "Kai", |
| 16 | + lastName: null, |
| 17 | + }); |
| 18 | + |
| 19 | + expect(result.userId).toBe(existing.id); |
| 20 | + expect(await prisma.user.count()).toBe(1); |
| 21 | + }); |
| 22 | + |
| 23 | + postgresTest("reuses a user whose stored email differs only in case", async ({ prisma }) => { |
| 24 | + const existing = await prisma.user.create({ |
| 25 | + data: { email: "Kai.Smith@Example.com", authenticationMethod: "GITHUB" }, |
| 26 | + }); |
| 27 | + |
| 28 | + const result = await findOrCreateDirectoryUser(prisma, { |
| 29 | + email: "kai.smith@example.com", |
| 30 | + firstName: "Kai", |
| 31 | + lastName: null, |
| 32 | + }); |
| 33 | + |
| 34 | + expect(result.userId).toBe(existing.id); |
| 35 | + expect(await prisma.user.count()).toBe(1); |
| 36 | + }); |
| 37 | + |
| 38 | + postgresTest("creates a separate user when only ambiguous casings exist", async ({ prisma }) => { |
| 39 | + const upper = await prisma.user.create({ |
| 40 | + data: { email: "Amb@example.com", authenticationMethod: "GITHUB" }, |
| 41 | + }); |
| 42 | + const shout = await prisma.user.create({ |
| 43 | + data: { email: "AMB@example.com", authenticationMethod: "GOOGLE" }, |
| 44 | + }); |
| 45 | + |
| 46 | + const result = await findOrCreateDirectoryUser(prisma, { |
| 47 | + email: "amb@example.com", |
| 48 | + firstName: null, |
| 49 | + lastName: null, |
| 50 | + }); |
| 51 | + |
| 52 | + expect([upper.id, shout.id]).not.toContain(result.userId); |
| 53 | + const created = await prisma.user.findFirstOrThrow({ where: { id: result.userId } }); |
| 54 | + expect(created.email).toBe("amb@example.com"); |
| 55 | + }); |
| 56 | + |
| 57 | + postgresTest("creates a lowercased SSO user when none matches", async ({ prisma }) => { |
| 58 | + const result = await findOrCreateDirectoryUser(prisma, { |
| 59 | + email: " New.User@Example.com ", |
| 60 | + firstName: "New", |
| 61 | + lastName: "User", |
| 62 | + }); |
| 63 | + |
| 64 | + const created = await prisma.user.findFirstOrThrow({ where: { id: result.userId } }); |
| 65 | + expect(created).toMatchObject({ |
| 66 | + email: "new.user@example.com", |
| 67 | + authenticationMethod: "SSO", |
| 68 | + name: "New User", |
| 69 | + }); |
| 70 | + }); |
| 71 | +}); |
0 commit comments