feat(people): adapt person user flows to status model
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"
|
||||
import type { PrismaClient } from "@/generated/prisma/client"
|
||||
import { normalizeEmail } from "@/lib/email"
|
||||
import { createTestPerson, createTestUser } from "../helpers/factories"
|
||||
import {
|
||||
resetIntegrationTestDatabase,
|
||||
@@ -58,7 +59,9 @@ describe("createPersonUserUseCase", () => {
|
||||
|
||||
// No User record created
|
||||
await expect(
|
||||
prisma.user.findUnique({ where: { email: "john@example.test" } }),
|
||||
prisma.user.findUnique({
|
||||
where: { emailNormalized: normalizeEmail("john@example.test") },
|
||||
}),
|
||||
).resolves.toBeNull()
|
||||
})
|
||||
|
||||
@@ -109,16 +112,19 @@ describe("createPersonUserUseCase", () => {
|
||||
|
||||
// User record should exist with derived name
|
||||
expect(person.userId).not.toBeNull()
|
||||
if (!person.userId) throw new Error("Expected linked user")
|
||||
|
||||
const user = await prisma.user.findUniqueOrThrow({
|
||||
where: { id: person.userId! },
|
||||
where: { id: person.userId },
|
||||
})
|
||||
expect(user).toMatchObject({
|
||||
name: "Admin User",
|
||||
email: "admin@example.test",
|
||||
role: "ADMIN",
|
||||
isActive: true,
|
||||
status: "ACTIVE",
|
||||
})
|
||||
expect(user.activatedAt).toBeInstanceOf(Date)
|
||||
expect(user.passwordChangedAt).toBeInstanceOf(Date)
|
||||
})
|
||||
|
||||
it("creates Person and User for all real roles (MANAGER, STAFF, VIEWER)", async () => {
|
||||
@@ -143,9 +149,10 @@ describe("createPersonUserUseCase", () => {
|
||||
where: { lastName: suffix },
|
||||
})
|
||||
expect(person.userId).not.toBeNull()
|
||||
if (!person.userId) throw new Error("Expected linked user")
|
||||
|
||||
const user = await prisma.user.findUniqueOrThrow({
|
||||
where: { id: person.userId! },
|
||||
where: { id: person.userId },
|
||||
})
|
||||
expect(user.role).toBe(role)
|
||||
expect(user.name).toBe(`Person ${suffix}`)
|
||||
@@ -165,7 +172,7 @@ describe("createPersonUserUseCase", () => {
|
||||
})
|
||||
|
||||
const user = await prisma.user.findUniqueOrThrow({
|
||||
where: { email: "maria@example.test" },
|
||||
where: { emailNormalized: normalizeEmail("maria@example.test") },
|
||||
})
|
||||
expect(user.name).toBe("Maria Garcia")
|
||||
})
|
||||
@@ -183,13 +190,14 @@ describe("createPersonUserUseCase", () => {
|
||||
})
|
||||
|
||||
const user = await prisma.user.findUniqueOrThrow({
|
||||
where: { email: "hash-test@example.test" },
|
||||
where: { emailNormalized: normalizeEmail("hash-test@example.test") },
|
||||
})
|
||||
expect(user.password).not.toBe("plaintext-password")
|
||||
expect(user.passwordHash).not.toBe("plaintext-password")
|
||||
if (!user.passwordHash) throw new Error("Expected password hash")
|
||||
|
||||
const { verifyPassword } = await import("@/lib/security")
|
||||
await expect(
|
||||
verifyPassword("plaintext-password", user.password),
|
||||
verifyPassword("plaintext-password", user.passwordHash),
|
||||
).resolves.toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"
|
||||
import type { PrismaClient } from "@/generated/prisma/client"
|
||||
import { type PrismaClient, UserStatus } from "@/generated/prisma/client"
|
||||
import { normalizeEmail } from "@/lib/email"
|
||||
import { getPasswordHash } from "@/lib/security"
|
||||
import { createTestPerson, createTestUser } from "../helpers/factories"
|
||||
import {
|
||||
@@ -123,7 +124,7 @@ describe("updatePersonUserUseCase", () => {
|
||||
expect(updatedPerson.user).toMatchObject({
|
||||
id: user.id,
|
||||
role: "ADMIN",
|
||||
isActive: false,
|
||||
status: "DISABLED",
|
||||
})
|
||||
})
|
||||
|
||||
@@ -157,8 +158,9 @@ describe("updatePersonUserUseCase", () => {
|
||||
where: { id: user.id },
|
||||
})
|
||||
const { verifyPassword } = await import("@/lib/security")
|
||||
if (!updated.passwordHash) throw new Error("Expected password hash")
|
||||
await expect(
|
||||
verifyPassword("new-password-1", updated.password),
|
||||
verifyPassword("new-password-1", updated.passwordHash),
|
||||
).resolves.toBe(true)
|
||||
})
|
||||
|
||||
@@ -167,10 +169,13 @@ describe("updatePersonUserUseCase", () => {
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
email: "no-pw@example.test",
|
||||
emailNormalized: normalizeEmail("no-pw@example.test"),
|
||||
name: "No PW",
|
||||
password: originalHash,
|
||||
passwordHash: originalHash,
|
||||
role: "STAFF",
|
||||
isActive: true,
|
||||
status: UserStatus.ACTIVE,
|
||||
activatedAt: new Date(),
|
||||
passwordChangedAt: new Date(),
|
||||
},
|
||||
})
|
||||
const person = await createTestPerson(prisma, {
|
||||
@@ -198,8 +203,9 @@ describe("updatePersonUserUseCase", () => {
|
||||
where: { id: user.id },
|
||||
})
|
||||
const { verifyPassword: verify } = await import("@/lib/security")
|
||||
if (!updated.passwordHash) throw new Error("Expected password hash")
|
||||
await expect(
|
||||
verify("original-password-1", updated.password),
|
||||
verify("original-password-1", updated.passwordHash),
|
||||
).resolves.toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user