75 lines
2.9 KiB
TypeScript
75 lines
2.9 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
findLegacyUserWithoutPasswordById,
|
|
upgradeLegacyUserPasswordHash,
|
|
updateLegacyUserDefaultTenantId,
|
|
type LegacyUserDefaultTenantWriterClient,
|
|
type LegacyUserPasswordHashWriterClient,
|
|
type LegacyUserWithoutPasswordReaderClient,
|
|
} from "./users.server.js";
|
|
import { getRlsExecutionContext } from "./rls-context.server.js";
|
|
|
|
describe("findLegacyUserWithoutPasswordById error handling", () => {
|
|
it("returns null instead of propagating a read failure", async () => {
|
|
const client = {
|
|
user: {
|
|
findUnique: vi.fn().mockRejectedValue(new Error("connection lost")),
|
|
},
|
|
} as unknown as LegacyUserWithoutPasswordReaderClient;
|
|
|
|
await expect(findLegacyUserWithoutPasswordById("user_1", client)).resolves.toBeNull();
|
|
expect(client.user.findUnique).toHaveBeenCalledOnce();
|
|
});
|
|
});
|
|
|
|
describe("updateLegacyUserDefaultTenantId", () => {
|
|
it("updates the default tenant for a valid user id", async () => {
|
|
const update = vi.fn().mockImplementation(async () => {
|
|
expect(getRlsExecutionContext()?.userId).toBe("user_1");
|
|
return { id: "user_1", defaultTenantId: "tenant_2" };
|
|
});
|
|
const client = { user: { update } } as unknown as LegacyUserDefaultTenantWriterClient;
|
|
|
|
const result = await updateLegacyUserDefaultTenantId({ defaultTenantId: "tenant_2" }, "user_1", client);
|
|
|
|
expect(result).toEqual({ id: "user_1", defaultTenantId: "tenant_2" });
|
|
expect(update).toHaveBeenCalledWith({
|
|
where: { id: "user_1" },
|
|
data: { defaultTenantId: "tenant_2" },
|
|
});
|
|
});
|
|
|
|
it("short-circuits to null when the user id is empty", async () => {
|
|
const update = vi.fn();
|
|
const client = { user: { update } } as unknown as LegacyUserDefaultTenantWriterClient;
|
|
|
|
await expect(updateLegacyUserDefaultTenantId({ defaultTenantId: "tenant_2" }, "", client)).resolves.toBeNull();
|
|
expect(update).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("upgradeLegacyUserPasswordHash", () => {
|
|
it("performs the compare-and-swap update in the user's RLS context", async () => {
|
|
const updateMany = vi.fn().mockImplementation(async () => {
|
|
expect(getRlsExecutionContext()?.userId).toBe("user_1");
|
|
return { count: 1 };
|
|
});
|
|
const client = { user: { updateMany } } as unknown as LegacyUserPasswordHashWriterClient;
|
|
|
|
await expect(upgradeLegacyUserPasswordHash("user_1", "old_hash", "new_hash", client)).resolves.toBe(true);
|
|
expect(updateMany).toHaveBeenCalledWith({
|
|
where: { id: "user_1", passwordHash: "old_hash" },
|
|
data: { passwordHash: "new_hash" },
|
|
});
|
|
});
|
|
|
|
it("returns false when the password hash changed concurrently", async () => {
|
|
const client = {
|
|
user: { updateMany: vi.fn().mockResolvedValue({ count: 0 }) },
|
|
} as unknown as LegacyUserPasswordHashWriterClient;
|
|
|
|
await expect(upgradeLegacyUserPasswordHash("user_1", "stale_hash", "new_hash", client)).resolves.toBe(false);
|
|
});
|
|
});
|