import { describe, expect, it, vi } from "vitest"; import { findLegacyAuthUserById, findLegacyLoginUserByEmail, findLegacyUserWithoutPasswordById, recordLegacyLogin, type LegacyAuthUserReaderClient, type LegacyLoginLogWriterClient, type LegacyLoginUserReaderClient, type LegacyUserWithoutPasswordReaderClient, } from "./users.server.js"; import { getRlsExecutionContext } from "./rls-context.server.js"; describe("findLegacyAuthUserById", () => { it("fetches only the legacy auth user fields by id", async () => { const findUnique = vi.fn().mockImplementation(async () => { expect(getRlsExecutionContext()?.userId).toBe("user_123"); return { id: "user_123", email: "user@example.com", totpEnabledAt: null, pendingDeletion: false, pseudonymized: false, active: true, passwordHash: "should-not-leak", }; }); const client = { user: { findUnique }, } as unknown as LegacyAuthUserReaderClient; await expect(findLegacyAuthUserById("user_123", client)).resolves.toEqual({ id: "user_123", email: "user@example.com", totpEnabledAt: null, pendingDeletion: false, pseudonymized: false, active: true, }); expect(findUnique).toHaveBeenCalledWith({ where: { id: "user_123" }, select: { id: true, email: true, totpEnabledAt: true, pendingDeletion: true, pseudonymized: true, active: true, }, }); }); it("returns null when the user does not exist", async () => { const client: LegacyAuthUserReaderClient = { user: { findUnique: vi.fn().mockResolvedValue(null), }, }; await expect(findLegacyAuthUserById("missing", client)).resolves.toBeNull(); }); it("fetches only the legacy login user fields by email", async () => { const findUnique = vi.fn().mockImplementation(async () => { expect(getRlsExecutionContext()?.userId).toBe("user_123"); return { id: "user_123", email: "user@example.com", passwordHash: "hash", totpEnabledAt: new Date("2026-07-12T00:00:00.000Z"), pendingDeletion: false, pseudonymized: false, active: true, firstName: "should-not-leak", }; }); const queryRaw = vi.fn().mockResolvedValue([{ tenant_id: null, resource_id: "user_123", user_id: "user_123" }]); const client = { $queryRaw: queryRaw, user: { findUnique }, } as unknown as LegacyLoginUserReaderClient; await expect(findLegacyLoginUserByEmail("user@example.com", client)).resolves.toEqual({ id: "user_123", email: "user@example.com", passwordHash: "hash", totpEnabledAt: new Date("2026-07-12T00:00:00.000Z"), pendingDeletion: false, pseudonymized: false, active: true, }); expect(findUnique).toHaveBeenCalledWith({ where: { id: "user_123" }, select: { id: true, email: true, passwordHash: true, totpEnabledAt: true, pendingDeletion: true, pseudonymized: true, active: true, }, }); expect(queryRaw).toHaveBeenCalledOnce(); }); it("returns null when the login user does not exist", async () => { const client = { $queryRaw: vi.fn().mockResolvedValue([]), user: { findUnique: vi.fn().mockResolvedValue(null), }, } as unknown as LegacyLoginUserReaderClient; await expect(findLegacyLoginUserByEmail("missing@example.com", client)).resolves.toBeNull(); expect(client.user.findUnique).not.toHaveBeenCalled(); }); it("fetches the legacy user fields required by API bearer auth", async () => { const user = { id: "user_123", email: "user@example.com", firstName: "Ada", lastName: "Lovelace", avatar: null, phone: null, admin: null, defaultTenantId: null, createdAt: new Date("2026-05-01T00:00:00.000Z"), locale: "de", }; const findUnique = vi.fn().mockResolvedValue(user); const client = { adminUser: { findUnique: vi.fn().mockResolvedValue(null) }, user: { findUnique }, } as unknown as LegacyUserWithoutPasswordReaderClient; await expect(findLegacyUserWithoutPasswordById("user_123", client)).resolves.toEqual(user); expect(findUnique).toHaveBeenCalledWith({ where: { id: "user_123" }, select: { id: true, email: true, firstName: true, lastName: true, avatar: true, phone: true, defaultTenantId: true, createdAt: true, locale: true, lightOrDarkMode: true, }, }); }); it("returns null when no legacy user id is provided", async () => { const client: LegacyUserWithoutPasswordReaderClient = { adminUser: { findUnique: vi.fn(), }, user: { findUnique: vi.fn(), }, }; await expect(findLegacyUserWithoutPasswordById(undefined, client)).resolves.toBeNull(); expect(client.user.findUnique).not.toHaveBeenCalled(); }); it("records legacy login logs", async () => { const create = vi.fn().mockImplementation(async () => { expect(getRlsExecutionContext()?.userId).toBe("user_123"); return {}; }); const client: LegacyLoginLogWriterClient = { log: { create }, }; await recordLegacyLogin("user_123", client); expect(create).toHaveBeenCalledWith({ data: { userId: "user_123", url: "", action: "Login", details: "", }, }); }); });