test: add initial unit integration and e2e coverage
Adds the initial testing baseline for the project: Unit coverage: - Zod schemas for items, assignments, movements, categories, auth, recipients, users, and assets - password hashing and verification helpers - auth role helper functions Integration coverage with PostgreSQL Testcontainers: - item use-cases: create, duplicate names, delete constraints - assignment use-cases: create, insufficient stock, return, double return - asset use-cases: available/assigned creation and lifecycle transitions - user use-cases: create/update, uniqueness, admin safeguards, password reset - category use-cases: create/update/delete constraints - recipient use-cases: create/update and uniqueness constraints E2E smoke coverage with Playwright: - unauthenticated redirect to login - seeded admin login - dashboard load - admin users page - inventory items page - assignments page Also configures: - Vitest - Playwright - PostgreSQL Testcontainers helpers - deterministic E2E admin bootstrap - test artifact ignores Validation: - bun run test: 9 files / 37 tests passed - bun run test:e2e: 3 passed - bunx tsc --noEmit: passed - bunx prisma validate: passed
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { expect, type Page, test } from "@playwright/test"
|
||||
|
||||
async function signInAsAdmin(page: Page) {
|
||||
await page.goto("/login")
|
||||
await page.getByLabel("Username").fill("admin")
|
||||
await page.getByLabel("Password").fill("admin-password")
|
||||
await page.getByRole("button", { name: "Sign In" }).click()
|
||||
await expect(page).toHaveURL("/")
|
||||
}
|
||||
|
||||
test.describe("main app smoke", () => {
|
||||
test("redirects unauthenticated users to login", async ({ page }) => {
|
||||
await page.goto("/admin/users")
|
||||
|
||||
await expect(page).toHaveURL(/\/login/)
|
||||
await expect(page.getByLabel("Username")).toBeVisible()
|
||||
await expect(page.getByRole("button", { name: "Sign In" })).toBeVisible()
|
||||
})
|
||||
|
||||
test("signs in as seeded admin and opens the dashboard", async ({ page }) => {
|
||||
await signInAsAdmin(page)
|
||||
|
||||
await expect(page.getByRole("heading", { name: "Dashboard" })).toBeVisible()
|
||||
await expect(page.getByText("E2E Admin")).toBeVisible()
|
||||
await expect(
|
||||
page.getByRole("link", { name: /Stock Manager/i }),
|
||||
).toBeVisible()
|
||||
})
|
||||
|
||||
test("admin can open users and inventory pages", async ({ page }) => {
|
||||
await signInAsAdmin(page)
|
||||
|
||||
await page.getByRole("link", { name: "Users" }).click()
|
||||
await expect(page).toHaveURL(/\/admin\/users/)
|
||||
await expect(page.getByRole("heading", { name: "Users" })).toBeVisible()
|
||||
await expect(
|
||||
page.getByRole("cell", { name: "admin@example.test" }),
|
||||
).toBeVisible()
|
||||
|
||||
await page.goto("/inventory/items")
|
||||
await expect(page.getByRole("heading", { name: "Items" })).toBeVisible()
|
||||
await expect(page.getByText("No items found.")).toBeVisible()
|
||||
|
||||
await page.goto("/assignments")
|
||||
await expect(
|
||||
page.getByRole("heading", { name: "Assignments" }),
|
||||
).toBeVisible()
|
||||
await expect(page.getByText("No assignments found")).toBeVisible()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,110 @@
|
||||
import { execFileSync, spawn, type ChildProcess } from "node:child_process"
|
||||
import process from "node:process"
|
||||
import {
|
||||
PostgreSqlContainer,
|
||||
type StartedPostgreSqlContainer,
|
||||
} from "@testcontainers/postgresql"
|
||||
|
||||
const port = process.env.E2E_PORT ?? "3100"
|
||||
const host = "127.0.0.1"
|
||||
const baseUrl = `http://${host}:${port}`
|
||||
|
||||
let container: StartedPostgreSqlContainer | undefined
|
||||
let nextProcess: ChildProcess | undefined
|
||||
let shuttingDown = false
|
||||
|
||||
async function cleanup() {
|
||||
if (shuttingDown) return
|
||||
shuttingDown = true
|
||||
|
||||
if (nextProcess && !nextProcess.killed) {
|
||||
nextProcess.kill("SIGTERM")
|
||||
}
|
||||
|
||||
await container?.stop()
|
||||
container = undefined
|
||||
}
|
||||
|
||||
async function main() {
|
||||
container = await new PostgreSqlContainer("postgres:18-alpine")
|
||||
.withDatabase("stock_manager_e2e")
|
||||
.withUsername("e2e")
|
||||
.withPassword("e2e")
|
||||
.start()
|
||||
|
||||
const databaseUrl = container.getConnectionUri()
|
||||
const env = {
|
||||
...process.env,
|
||||
DATABASE_URL: databaseUrl,
|
||||
AUTH_SECRET: process.env.AUTH_SECRET ?? "e2e-auth-secret",
|
||||
AUTH_URL: baseUrl,
|
||||
NEXTAUTH_URL: baseUrl,
|
||||
ADMIN_BOOTSTRAP_ENABLED: "true",
|
||||
ADMIN_USERNAME: "admin",
|
||||
ADMIN_EMAIL: "admin@example.test",
|
||||
ADMIN_NAME: "E2E Admin",
|
||||
ADMIN_PASSWORD: "admin-password",
|
||||
}
|
||||
|
||||
process.env.DATABASE_URL = databaseUrl
|
||||
process.env.AUTH_SECRET = env.AUTH_SECRET
|
||||
process.env.AUTH_URL = baseUrl
|
||||
process.env.NEXTAUTH_URL = baseUrl
|
||||
process.env.ADMIN_BOOTSTRAP_ENABLED = "true"
|
||||
process.env.ADMIN_USERNAME = "admin"
|
||||
process.env.ADMIN_EMAIL = "admin@example.test"
|
||||
process.env.ADMIN_NAME = "E2E Admin"
|
||||
process.env.ADMIN_PASSWORD = "admin-password"
|
||||
|
||||
try {
|
||||
execFileSync("bunx", ["prisma", "migrate", "deploy"], {
|
||||
env,
|
||||
stdio: "inherit",
|
||||
})
|
||||
|
||||
const prismaModule = await import("@/lib/prisma")
|
||||
const bootstrapModule = await import("../../../prisma/bootstrap-admin")
|
||||
|
||||
await bootstrapModule.bootstrapAdmin(prismaModule.prisma)
|
||||
await prismaModule.prisma.$disconnect()
|
||||
} catch (error) {
|
||||
await cleanup()
|
||||
throw error
|
||||
}
|
||||
|
||||
const startedNextProcess = spawn(
|
||||
"bunx",
|
||||
["next", "dev", "--webpack", "--hostname", host, "--port", port],
|
||||
{
|
||||
env,
|
||||
stdio: "inherit",
|
||||
},
|
||||
)
|
||||
|
||||
nextProcess = startedNextProcess
|
||||
|
||||
startedNextProcess.on("exit", async (code, signal) => {
|
||||
const wasShuttingDown = shuttingDown
|
||||
await cleanup()
|
||||
if (!wasShuttingDown && code !== 0) {
|
||||
console.error(`Next dev server exited with code ${code} signal ${signal}`)
|
||||
process.exit(code ?? 1)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
process.on("SIGTERM", async () => {
|
||||
await cleanup()
|
||||
process.exit(0)
|
||||
})
|
||||
|
||||
process.on("SIGINT", async () => {
|
||||
await cleanup()
|
||||
process.exit(0)
|
||||
})
|
||||
|
||||
main().catch(async (error) => {
|
||||
console.error(error)
|
||||
await cleanup()
|
||||
process.exit(1)
|
||||
})
|
||||
@@ -0,0 +1,95 @@
|
||||
import type {
|
||||
PrismaClient,
|
||||
RecipientDepartment,
|
||||
UserRole,
|
||||
} from "@/generated/prisma/client"
|
||||
|
||||
let sequence = 0
|
||||
|
||||
function nextSuffix() {
|
||||
sequence += 1
|
||||
return `${Date.now()}-${sequence}`
|
||||
}
|
||||
|
||||
export async function createTestUser(
|
||||
prisma: PrismaClient,
|
||||
overrides: Partial<{
|
||||
username: string
|
||||
email: string
|
||||
name: string
|
||||
role: UserRole
|
||||
isActive: boolean
|
||||
}> = {},
|
||||
) {
|
||||
const suffix = nextSuffix()
|
||||
|
||||
return prisma.user.create({
|
||||
data: {
|
||||
username: overrides.username ?? `test-user-${suffix}`,
|
||||
email: overrides.email ?? `test-user-${suffix}@example.test`,
|
||||
name: overrides.name ?? "Test User",
|
||||
password: "hashed-password",
|
||||
role: overrides.role ?? "ADMIN",
|
||||
isActive: overrides.isActive ?? true,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function createTestCategory(
|
||||
prisma: PrismaClient,
|
||||
overrides: Partial<{ name: string }> = {},
|
||||
) {
|
||||
const suffix = nextSuffix()
|
||||
|
||||
return prisma.category.create({
|
||||
data: {
|
||||
name: overrides.name ?? `Test Category ${suffix}`,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function createTestRecipient(
|
||||
prisma: PrismaClient,
|
||||
overrides: Partial<{
|
||||
username: string
|
||||
firstName: string
|
||||
lastName: string
|
||||
department: RecipientDepartment
|
||||
email: string | null
|
||||
phone: string | null
|
||||
}> = {},
|
||||
) {
|
||||
const suffix = nextSuffix()
|
||||
|
||||
return prisma.recipient.create({
|
||||
data: {
|
||||
username: overrides.username ?? `test-recipient-${suffix}`,
|
||||
firstName: overrides.firstName ?? "Test",
|
||||
lastName: overrides.lastName ?? "Recipient",
|
||||
department: overrides.department ?? "OTHER",
|
||||
email: overrides.email ?? null,
|
||||
phone: overrides.phone ?? null,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function createTestItem(
|
||||
prisma: PrismaClient,
|
||||
overrides: Partial<{
|
||||
name: string
|
||||
stock: number
|
||||
categoryId: string
|
||||
}> = {},
|
||||
) {
|
||||
const categoryId =
|
||||
overrides.categoryId ?? (await createTestCategory(prisma)).id
|
||||
const suffix = nextSuffix()
|
||||
|
||||
return prisma.item.create({
|
||||
data: {
|
||||
name: overrides.name ?? `Test Item ${suffix}`,
|
||||
stock: overrides.stock ?? 0,
|
||||
category: { connect: { id: categoryId } },
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { execFileSync } from "node:child_process"
|
||||
import {
|
||||
PostgreSqlContainer,
|
||||
type StartedPostgreSqlContainer,
|
||||
} from "@testcontainers/postgresql"
|
||||
import type { PrismaClient } from "@/generated/prisma/client"
|
||||
|
||||
type TestDatabaseState = {
|
||||
container?: StartedPostgreSqlContainer
|
||||
databaseUrl?: string
|
||||
}
|
||||
|
||||
const state: TestDatabaseState = {}
|
||||
|
||||
const TABLES_TO_TRUNCATE = [
|
||||
"Movement",
|
||||
"Assignment",
|
||||
"Asset",
|
||||
"Item",
|
||||
"Category",
|
||||
"Recipient",
|
||||
"User",
|
||||
]
|
||||
|
||||
export async function startIntegrationTestDatabase() {
|
||||
if (state.container && state.databaseUrl) {
|
||||
return state.databaseUrl
|
||||
}
|
||||
|
||||
const container = await new PostgreSqlContainer("postgres:18-alpine")
|
||||
.withDatabase("stock_manager_test")
|
||||
.withUsername("test")
|
||||
.withPassword("test")
|
||||
.start()
|
||||
|
||||
const databaseUrl = container.getConnectionUri()
|
||||
|
||||
state.container = container
|
||||
state.databaseUrl = databaseUrl
|
||||
|
||||
process.env.DATABASE_URL = databaseUrl
|
||||
process.env.AUTH_SECRET = process.env.AUTH_SECRET || "test-auth-secret"
|
||||
|
||||
try {
|
||||
execFileSync("bunx", ["prisma", "migrate", "deploy"], {
|
||||
env: {
|
||||
...process.env,
|
||||
DATABASE_URL: databaseUrl,
|
||||
},
|
||||
stdio: "inherit",
|
||||
})
|
||||
} catch (error) {
|
||||
await stopIntegrationTestDatabase()
|
||||
throw error
|
||||
}
|
||||
|
||||
return databaseUrl
|
||||
}
|
||||
|
||||
export async function resetIntegrationTestDatabase(prisma: PrismaClient) {
|
||||
const quotedTables = TABLES_TO_TRUNCATE.map((table) => `"${table}"`).join(
|
||||
", ",
|
||||
)
|
||||
|
||||
await prisma.$executeRawUnsafe(`TRUNCATE TABLE ${quotedTables} CASCADE`)
|
||||
}
|
||||
|
||||
export async function stopIntegrationTestDatabase() {
|
||||
await state.container?.stop()
|
||||
state.container = undefined
|
||||
state.databaseUrl = undefined
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"
|
||||
import type { PrismaClient } from "@/generated/prisma/client"
|
||||
import {
|
||||
createTestItem,
|
||||
createTestRecipient,
|
||||
createTestUser,
|
||||
} from "../helpers/factories"
|
||||
import {
|
||||
resetIntegrationTestDatabase,
|
||||
startIntegrationTestDatabase,
|
||||
stopIntegrationTestDatabase,
|
||||
} from "../helpers/test-db"
|
||||
|
||||
let prisma: PrismaClient
|
||||
let createAssetUseCase: typeof import("@/use-cases/asset.use-cases").createAssetUseCase
|
||||
let updateAssetUseCase: typeof import("@/use-cases/asset.use-cases").updateAssetUseCase
|
||||
|
||||
beforeAll(async () => {
|
||||
await startIntegrationTestDatabase()
|
||||
|
||||
const prismaModule = await import("@/lib/prisma")
|
||||
const assetUseCases = await import("@/use-cases/asset.use-cases")
|
||||
|
||||
prisma = prismaModule.prisma
|
||||
createAssetUseCase = assetUseCases.createAssetUseCase
|
||||
updateAssetUseCase = assetUseCases.updateAssetUseCase
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetIntegrationTestDatabase(prisma)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await prisma?.$disconnect()
|
||||
await stopIntegrationTestDatabase()
|
||||
})
|
||||
|
||||
describe("asset use-cases", () => {
|
||||
it("creates an available asset, increments item stock, and records an IN movement", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const item = await createTestItem(prisma, { stock: 0 })
|
||||
|
||||
const result = await createAssetUseCase({
|
||||
actorId: actor.id,
|
||||
itemId: item.id,
|
||||
serialNumber: "ASSET-AVAILABLE-001",
|
||||
deliveryNote: "DN-001",
|
||||
status: "AVAILABLE",
|
||||
notes: "Ready to assign",
|
||||
})
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
if (!result.success) throw new Error("Expected asset creation success")
|
||||
|
||||
const [asset, updatedItem, movements] = await Promise.all([
|
||||
prisma.asset.findUniqueOrThrow({ where: { id: result.assetId } }),
|
||||
prisma.item.findUniqueOrThrow({ where: { id: item.id } }),
|
||||
prisma.movement.findMany({
|
||||
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
||||
}),
|
||||
])
|
||||
|
||||
expect(asset).toMatchObject({
|
||||
itemId: item.id,
|
||||
serialNumber: "ASSET-AVAILABLE-001",
|
||||
deliveryNote: "DN-001",
|
||||
status: "AVAILABLE",
|
||||
notes: "Ready to assign",
|
||||
})
|
||||
expect(updatedItem.stock).toBe(1)
|
||||
expect(movements).toHaveLength(1)
|
||||
expect(movements[0]).toMatchObject({
|
||||
type: "IN",
|
||||
itemId: item.id,
|
||||
assetId: result.assetId,
|
||||
quantity: 1,
|
||||
userId: actor.id,
|
||||
})
|
||||
})
|
||||
|
||||
it("creates an assigned asset with assignment and ASSIGNMENT movement", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const recipient = await createTestRecipient(prisma)
|
||||
const item = await createTestItem(prisma, { stock: 0 })
|
||||
|
||||
const result = await createAssetUseCase({
|
||||
actorId: actor.id,
|
||||
itemId: item.id,
|
||||
serialNumber: "ASSET-ASSIGNED-001",
|
||||
status: "ASSIGNED",
|
||||
recipientId: recipient.id,
|
||||
})
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
if (!result.success) throw new Error("Expected asset creation success")
|
||||
|
||||
const [asset, updatedItem, assignment, movements] = await Promise.all([
|
||||
prisma.asset.findUniqueOrThrow({ where: { id: result.assetId } }),
|
||||
prisma.item.findUniqueOrThrow({ where: { id: item.id } }),
|
||||
prisma.assignment.findFirstOrThrow({
|
||||
where: { assetId: result.assetId, returnDate: null },
|
||||
}),
|
||||
prisma.movement.findMany({
|
||||
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
||||
}),
|
||||
])
|
||||
|
||||
expect(asset).toMatchObject({
|
||||
itemId: item.id,
|
||||
serialNumber: "ASSET-ASSIGNED-001",
|
||||
status: "ASSIGNED",
|
||||
})
|
||||
expect(updatedItem.stock).toBe(0)
|
||||
expect(assignment).toMatchObject({
|
||||
itemId: item.id,
|
||||
assetId: result.assetId,
|
||||
recipientId: recipient.id,
|
||||
quantity: 1,
|
||||
createdBy: actor.id,
|
||||
returnDate: null,
|
||||
})
|
||||
expect(movements).toHaveLength(1)
|
||||
expect(movements[0]).toMatchObject({
|
||||
type: "ASSIGNMENT",
|
||||
itemId: item.id,
|
||||
assetId: result.assetId,
|
||||
recipientId: recipient.id,
|
||||
assignmentId: assignment.id,
|
||||
quantity: 1,
|
||||
userId: actor.id,
|
||||
})
|
||||
})
|
||||
|
||||
it("moves an available asset to assigned and back to available", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const recipient = await createTestRecipient(prisma)
|
||||
const item = await createTestItem(prisma, { stock: 0 })
|
||||
|
||||
const created = await createAssetUseCase({
|
||||
actorId: actor.id,
|
||||
itemId: item.id,
|
||||
serialNumber: "ASSET-LIFECYCLE-001",
|
||||
status: "AVAILABLE",
|
||||
})
|
||||
|
||||
expect(created.success).toBe(true)
|
||||
if (!created.success) throw new Error("Expected asset creation success")
|
||||
|
||||
await expect(
|
||||
updateAssetUseCase({
|
||||
actorId: actor.id,
|
||||
id: created.assetId,
|
||||
itemId: item.id,
|
||||
serialNumber: "ASSET-LIFECYCLE-001",
|
||||
status: "ASSIGNED",
|
||||
recipientId: recipient.id,
|
||||
}),
|
||||
).resolves.toEqual({ success: true })
|
||||
|
||||
const [assignedAsset, assignedItem, activeAssignment] = await Promise.all([
|
||||
prisma.asset.findUniqueOrThrow({ where: { id: created.assetId } }),
|
||||
prisma.item.findUniqueOrThrow({ where: { id: item.id } }),
|
||||
prisma.assignment.findFirstOrThrow({
|
||||
where: { assetId: created.assetId, returnDate: null },
|
||||
}),
|
||||
])
|
||||
|
||||
expect(assignedAsset.status).toBe("ASSIGNED")
|
||||
expect(assignedItem.stock).toBe(0)
|
||||
expect(activeAssignment).toMatchObject({
|
||||
itemId: item.id,
|
||||
assetId: created.assetId,
|
||||
recipientId: recipient.id,
|
||||
quantity: 1,
|
||||
})
|
||||
|
||||
await expect(
|
||||
updateAssetUseCase({
|
||||
actorId: actor.id,
|
||||
id: created.assetId,
|
||||
itemId: item.id,
|
||||
serialNumber: "ASSET-LIFECYCLE-001",
|
||||
status: "AVAILABLE",
|
||||
}),
|
||||
).resolves.toEqual({ success: true })
|
||||
|
||||
const [availableAsset, availableItem, returnedAssignment, movements] =
|
||||
await Promise.all([
|
||||
prisma.asset.findUniqueOrThrow({ where: { id: created.assetId } }),
|
||||
prisma.item.findUniqueOrThrow({ where: { id: item.id } }),
|
||||
prisma.assignment.findUniqueOrThrow({
|
||||
where: { id: activeAssignment.id },
|
||||
}),
|
||||
prisma.movement.findMany({
|
||||
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
||||
}),
|
||||
])
|
||||
|
||||
expect(availableAsset.status).toBe("AVAILABLE")
|
||||
expect(availableItem.stock).toBe(1)
|
||||
expect(returnedAssignment.returnDate).toBeInstanceOf(Date)
|
||||
expect(returnedAssignment).toMatchObject({
|
||||
itemId: null,
|
||||
assetId: null,
|
||||
recipientId: null,
|
||||
quantity: null,
|
||||
})
|
||||
expect(movements).toHaveLength(3)
|
||||
expect(movements.map((movement) => movement.type)).toEqual([
|
||||
"IN",
|
||||
"ASSIGNMENT",
|
||||
"RETURN",
|
||||
])
|
||||
expect(movements[1]).toMatchObject({
|
||||
itemId: item.id,
|
||||
assetId: created.assetId,
|
||||
recipientId: recipient.id,
|
||||
assignmentId: activeAssignment.id,
|
||||
quantity: 1,
|
||||
userId: actor.id,
|
||||
})
|
||||
expect(movements[2]).toMatchObject({
|
||||
itemId: item.id,
|
||||
assetId: created.assetId,
|
||||
assignmentId: activeAssignment.id,
|
||||
quantity: 1,
|
||||
userId: actor.id,
|
||||
})
|
||||
})
|
||||
|
||||
it("returns an active assignment without restoring stock when an assigned asset moves to a terminal status", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const recipient = await createTestRecipient(prisma)
|
||||
const item = await createTestItem(prisma, { stock: 0 })
|
||||
|
||||
const created = await createAssetUseCase({
|
||||
actorId: actor.id,
|
||||
itemId: item.id,
|
||||
serialNumber: "ASSET-BROKEN-001",
|
||||
status: "ASSIGNED",
|
||||
recipientId: recipient.id,
|
||||
})
|
||||
|
||||
expect(created.success).toBe(true)
|
||||
if (!created.success) throw new Error("Expected asset creation success")
|
||||
|
||||
const activeAssignment = await prisma.assignment.findFirstOrThrow({
|
||||
where: { assetId: created.assetId, returnDate: null },
|
||||
})
|
||||
|
||||
await expect(
|
||||
updateAssetUseCase({
|
||||
actorId: actor.id,
|
||||
id: created.assetId,
|
||||
itemId: item.id,
|
||||
serialNumber: "ASSET-BROKEN-001",
|
||||
status: "BROKEN",
|
||||
}),
|
||||
).resolves.toEqual({ success: true })
|
||||
|
||||
const [asset, itemAfterUpdate, returnedAssignment, movements] =
|
||||
await Promise.all([
|
||||
prisma.asset.findUniqueOrThrow({ where: { id: created.assetId } }),
|
||||
prisma.item.findUniqueOrThrow({ where: { id: item.id } }),
|
||||
prisma.assignment.findUniqueOrThrow({
|
||||
where: { id: activeAssignment.id },
|
||||
}),
|
||||
prisma.movement.findMany({
|
||||
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
||||
}),
|
||||
])
|
||||
|
||||
expect(asset.status).toBe("BROKEN")
|
||||
expect(itemAfterUpdate.stock).toBe(0)
|
||||
expect(returnedAssignment.returnDate).toBeInstanceOf(Date)
|
||||
expect(movements).toHaveLength(2)
|
||||
expect(movements.map((movement) => movement.type)).toEqual([
|
||||
"ASSIGNMENT",
|
||||
"RETURN",
|
||||
])
|
||||
expect(movements[1]).toMatchObject({
|
||||
type: "RETURN",
|
||||
itemId: item.id,
|
||||
assetId: created.assetId,
|
||||
recipientId: recipient.id,
|
||||
assignmentId: activeAssignment.id,
|
||||
quantity: 1,
|
||||
userId: actor.id,
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,198 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"
|
||||
import type { PrismaClient } from "@/generated/prisma/client"
|
||||
import {
|
||||
createTestItem,
|
||||
createTestRecipient,
|
||||
createTestUser,
|
||||
} from "../helpers/factories"
|
||||
import {
|
||||
resetIntegrationTestDatabase,
|
||||
startIntegrationTestDatabase,
|
||||
stopIntegrationTestDatabase,
|
||||
} from "../helpers/test-db"
|
||||
|
||||
let prisma: PrismaClient
|
||||
let createAssignmentUseCase: typeof import("@/use-cases/assignment.use-cases").createAssignmentUseCase
|
||||
let returnAssignmentUseCase: typeof import("@/use-cases/assignment.use-cases").returnAssignmentUseCase
|
||||
|
||||
beforeAll(async () => {
|
||||
await startIntegrationTestDatabase()
|
||||
|
||||
const prismaModule = await import("@/lib/prisma")
|
||||
const assignmentUseCases = await import("@/use-cases/assignment.use-cases")
|
||||
|
||||
prisma = prismaModule.prisma
|
||||
createAssignmentUseCase = assignmentUseCases.createAssignmentUseCase
|
||||
returnAssignmentUseCase = assignmentUseCases.returnAssignmentUseCase
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetIntegrationTestDatabase(prisma)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await prisma?.$disconnect()
|
||||
await stopIntegrationTestDatabase()
|
||||
})
|
||||
|
||||
describe("assignment use-cases", () => {
|
||||
it("creates an assignment, decrements stock, and records an ASSIGNMENT movement", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const recipient = await createTestRecipient(prisma)
|
||||
const item = await createTestItem(prisma, { stock: 5 })
|
||||
|
||||
const assignmentDate = new Date("2026-01-01T00:00:00.000Z")
|
||||
|
||||
const result = await createAssignmentUseCase({
|
||||
actorId: actor.id,
|
||||
itemId: item.id,
|
||||
recipientId: recipient.id,
|
||||
quantity: 2,
|
||||
assignmentDate,
|
||||
notes: "Initial assignment",
|
||||
})
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
if (!result.success) throw new Error("Expected assignment creation success")
|
||||
|
||||
const [updatedItem, assignment, movements] = await Promise.all([
|
||||
prisma.item.findUniqueOrThrow({ where: { id: item.id } }),
|
||||
prisma.assignment.findUniqueOrThrow({
|
||||
where: { id: result.assignmentId },
|
||||
}),
|
||||
prisma.movement.findMany({ orderBy: [{ createdAt: "asc" }, { id: "asc" }] }),
|
||||
])
|
||||
|
||||
expect(updatedItem.stock).toBe(3)
|
||||
expect(assignment).toMatchObject({
|
||||
itemId: item.id,
|
||||
recipientId: recipient.id,
|
||||
quantity: 2,
|
||||
notes: "Initial assignment",
|
||||
createdBy: actor.id,
|
||||
returnDate: null,
|
||||
})
|
||||
expect(assignment.assignmentDate).toEqual(assignmentDate)
|
||||
expect(movements).toHaveLength(1)
|
||||
expect(movements[0]).toMatchObject({
|
||||
type: "ASSIGNMENT",
|
||||
itemId: item.id,
|
||||
recipientId: recipient.id,
|
||||
assignmentId: result.assignmentId,
|
||||
quantity: 2,
|
||||
userId: actor.id,
|
||||
})
|
||||
})
|
||||
|
||||
it("rejects assignment creation when item stock is insufficient", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const recipient = await createTestRecipient(prisma)
|
||||
const item = await createTestItem(prisma, { stock: 1 })
|
||||
|
||||
const result = await createAssignmentUseCase({
|
||||
actorId: actor.id,
|
||||
itemId: item.id,
|
||||
recipientId: recipient.id,
|
||||
quantity: 2,
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
success: false,
|
||||
errors: {
|
||||
quantity: ["Item does not have enough stock"],
|
||||
},
|
||||
})
|
||||
|
||||
await expect(
|
||||
prisma.item.findUniqueOrThrow({ where: { id: item.id } }),
|
||||
).resolves.toMatchObject({ stock: 1 })
|
||||
await expect(prisma.assignment.count()).resolves.toBe(0)
|
||||
await expect(prisma.movement.count()).resolves.toBe(0)
|
||||
})
|
||||
|
||||
it("returns an assignment, restores stock, closes it, and records a RETURN movement", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const recipient = await createTestRecipient(prisma)
|
||||
const item = await createTestItem(prisma, { stock: 4 })
|
||||
|
||||
const created = await createAssignmentUseCase({
|
||||
actorId: actor.id,
|
||||
itemId: item.id,
|
||||
recipientId: recipient.id,
|
||||
quantity: 3,
|
||||
})
|
||||
|
||||
expect(created.success).toBe(true)
|
||||
if (!created.success)
|
||||
throw new Error("Expected assignment creation success")
|
||||
|
||||
const returned = await returnAssignmentUseCase({
|
||||
id: created.assignmentId,
|
||||
actorId: actor.id,
|
||||
})
|
||||
|
||||
expect(returned).toEqual({ success: true })
|
||||
|
||||
const [updatedItem, assignment, movements] = await Promise.all([
|
||||
prisma.item.findUniqueOrThrow({ where: { id: item.id } }),
|
||||
prisma.assignment.findUniqueOrThrow({
|
||||
where: { id: created.assignmentId },
|
||||
}),
|
||||
prisma.movement.findMany({ orderBy: [{ createdAt: "asc" }, { id: "asc" }] }),
|
||||
])
|
||||
|
||||
expect(updatedItem.stock).toBe(4)
|
||||
expect(assignment.returnDate).toBeInstanceOf(Date)
|
||||
expect(assignment).toMatchObject({
|
||||
itemId: null,
|
||||
assetId: null,
|
||||
recipientId: null,
|
||||
quantity: null,
|
||||
})
|
||||
expect(movements).toHaveLength(2)
|
||||
expect(movements[0]).toMatchObject({
|
||||
type: "ASSIGNMENT",
|
||||
itemId: item.id,
|
||||
assignmentId: created.assignmentId,
|
||||
quantity: 3,
|
||||
userId: actor.id,
|
||||
})
|
||||
expect(movements[1]).toMatchObject({
|
||||
type: "RETURN",
|
||||
itemId: item.id,
|
||||
assignmentId: created.assignmentId,
|
||||
quantity: 3,
|
||||
userId: actor.id,
|
||||
})
|
||||
})
|
||||
|
||||
it("rejects returning the same assignment twice", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const recipient = await createTestRecipient(prisma)
|
||||
const item = await createTestItem(prisma, { stock: 2 })
|
||||
|
||||
const created = await createAssignmentUseCase({
|
||||
actorId: actor.id,
|
||||
itemId: item.id,
|
||||
recipientId: recipient.id,
|
||||
quantity: 1,
|
||||
})
|
||||
|
||||
expect(created.success).toBe(true)
|
||||
if (!created.success)
|
||||
throw new Error("Expected assignment creation success")
|
||||
|
||||
await expect(
|
||||
returnAssignmentUseCase({ id: created.assignmentId, actorId: actor.id }),
|
||||
).resolves.toEqual({ success: true })
|
||||
|
||||
await expect(
|
||||
returnAssignmentUseCase({ id: created.assignmentId, actorId: actor.id }),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { id: ["Assignment already returned"] },
|
||||
})
|
||||
|
||||
await expect(prisma.movement.count()).resolves.toBe(2)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,111 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"
|
||||
import type { PrismaClient } from "@/generated/prisma/client"
|
||||
import { createTestCategory, createTestItem } from "../helpers/factories"
|
||||
import {
|
||||
resetIntegrationTestDatabase,
|
||||
startIntegrationTestDatabase,
|
||||
stopIntegrationTestDatabase,
|
||||
} from "../helpers/test-db"
|
||||
|
||||
let prisma: PrismaClient
|
||||
let createCategoryUseCase: typeof import("@/use-cases/category.use-cases").createCategoryUseCase
|
||||
let updateCategoryUseCase: typeof import("@/use-cases/category.use-cases").updateCategoryUseCase
|
||||
let deleteCategoryUseCase: typeof import("@/use-cases/category.use-cases").deleteCategoryUseCase
|
||||
|
||||
beforeAll(async () => {
|
||||
await startIntegrationTestDatabase()
|
||||
|
||||
const prismaModule = await import("@/lib/prisma")
|
||||
const categoryUseCases = await import("@/use-cases/category.use-cases")
|
||||
|
||||
prisma = prismaModule.prisma
|
||||
createCategoryUseCase = categoryUseCases.createCategoryUseCase
|
||||
updateCategoryUseCase = categoryUseCases.updateCategoryUseCase
|
||||
deleteCategoryUseCase = categoryUseCases.deleteCategoryUseCase
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetIntegrationTestDatabase(prisma)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await prisma?.$disconnect()
|
||||
await stopIntegrationTestDatabase()
|
||||
})
|
||||
|
||||
describe("category use-cases", () => {
|
||||
it("creates a category and rejects duplicate names", async () => {
|
||||
await expect(createCategoryUseCase({ name: "Hardware" })).resolves.toEqual({
|
||||
success: true,
|
||||
})
|
||||
|
||||
await expect(
|
||||
prisma.category.findUniqueOrThrow({ where: { name: "Hardware" } }),
|
||||
).resolves.toMatchObject({ name: "Hardware" })
|
||||
|
||||
await expect(createCategoryUseCase({ name: "Hardware" })).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { name: ["Category already exists"] },
|
||||
})
|
||||
|
||||
await expect(prisma.category.count()).resolves.toBe(1)
|
||||
})
|
||||
|
||||
it("updates a category and rejects unchanged or duplicate names", async () => {
|
||||
const category = await createTestCategory(prisma, { name: "Peripherals" })
|
||||
const other = await createTestCategory(prisma, { name: "Networking" })
|
||||
|
||||
await expect(
|
||||
updateCategoryUseCase({ id: category.id, name: "Accessories" }),
|
||||
).resolves.toEqual({ success: true })
|
||||
|
||||
await expect(
|
||||
prisma.category.findUniqueOrThrow({ where: { id: category.id } }),
|
||||
).resolves.toMatchObject({ name: "Accessories" })
|
||||
|
||||
await expect(
|
||||
updateCategoryUseCase({ id: category.id, name: "Accessories" }),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { name: ["Category name is the same as the old one"] },
|
||||
})
|
||||
|
||||
await expect(
|
||||
updateCategoryUseCase({ id: category.id, name: other.name }),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { name: ["Category already exists"] },
|
||||
})
|
||||
|
||||
await expect(
|
||||
prisma.category.findUniqueOrThrow({ where: { id: category.id } }),
|
||||
).resolves.toMatchObject({ name: "Accessories" })
|
||||
})
|
||||
|
||||
it("deletes empty categories and blocks deleting categories with items", async () => {
|
||||
const categoryWithItems = await createTestCategory(prisma, {
|
||||
name: "Computers",
|
||||
})
|
||||
await createTestItem(prisma, { categoryId: categoryWithItems.id })
|
||||
|
||||
await expect(deleteCategoryUseCase(categoryWithItems.id)).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { id: ["Category has items"] },
|
||||
})
|
||||
|
||||
await expect(
|
||||
prisma.category.findUnique({ where: { id: categoryWithItems.id } }),
|
||||
).resolves.not.toBeNull()
|
||||
await expect(prisma.item.count()).resolves.toBe(1)
|
||||
|
||||
const emptyCategory = await createTestCategory(prisma, { name: "Cables" })
|
||||
|
||||
await expect(deleteCategoryUseCase(emptyCategory.id)).resolves.toEqual({
|
||||
success: true,
|
||||
})
|
||||
|
||||
await expect(
|
||||
prisma.category.findUnique({ where: { id: emptyCategory.id } }),
|
||||
).resolves.toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,134 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"
|
||||
import type { PrismaClient } from "@/generated/prisma/client"
|
||||
import { createTestCategory, createTestUser } from "../helpers/factories"
|
||||
import {
|
||||
resetIntegrationTestDatabase,
|
||||
startIntegrationTestDatabase,
|
||||
stopIntegrationTestDatabase,
|
||||
} from "../helpers/test-db"
|
||||
|
||||
let prisma: PrismaClient
|
||||
let createItemUseCase: typeof import("@/use-cases/item.use-cases").createItemUseCase
|
||||
let deleteItemUseCase: typeof import("@/use-cases/item.use-cases").deleteItemUseCase
|
||||
|
||||
beforeAll(async () => {
|
||||
await startIntegrationTestDatabase()
|
||||
|
||||
const prismaModule = await import("@/lib/prisma")
|
||||
const itemUseCases = await import("@/use-cases/item.use-cases")
|
||||
|
||||
prisma = prismaModule.prisma
|
||||
createItemUseCase = itemUseCases.createItemUseCase
|
||||
deleteItemUseCase = itemUseCases.deleteItemUseCase
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetIntegrationTestDatabase(prisma)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await prisma?.$disconnect()
|
||||
await stopIntegrationTestDatabase()
|
||||
})
|
||||
|
||||
describe("item use-cases", () => {
|
||||
it("creates an item with initial stock and records an IN movement", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const category = await createTestCategory(prisma)
|
||||
|
||||
const result = await createItemUseCase({
|
||||
actorId: actor.id,
|
||||
name: "Laptop",
|
||||
categoryId: category.id,
|
||||
stock: 3,
|
||||
})
|
||||
|
||||
expect(result).toEqual({ success: true })
|
||||
|
||||
const item = await prisma.item.findUnique({
|
||||
where: { name: "Laptop" },
|
||||
include: { movements: true },
|
||||
})
|
||||
|
||||
expect(item).toMatchObject({
|
||||
name: "Laptop",
|
||||
categoryId: category.id,
|
||||
stock: 3,
|
||||
deletedAt: null,
|
||||
})
|
||||
expect(item?.movements).toHaveLength(1)
|
||||
expect(item?.movements[0]).toMatchObject({
|
||||
type: "IN",
|
||||
quantity: 3,
|
||||
userId: actor.id,
|
||||
})
|
||||
})
|
||||
|
||||
it("rejects duplicate item names", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const category = await createTestCategory(prisma)
|
||||
|
||||
await createItemUseCase({
|
||||
actorId: actor.id,
|
||||
name: "Monitor",
|
||||
categoryId: category.id,
|
||||
stock: 0,
|
||||
})
|
||||
|
||||
const duplicate = await createItemUseCase({
|
||||
actorId: actor.id,
|
||||
name: "Monitor",
|
||||
categoryId: category.id,
|
||||
stock: 0,
|
||||
})
|
||||
|
||||
expect(duplicate).toEqual({
|
||||
success: false,
|
||||
errors: {
|
||||
name: ["An item with this name already exists"],
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it("blocks deleting items with stock and soft deletes empty items", async () => {
|
||||
const actor = await createTestUser(prisma)
|
||||
const category = await createTestCategory(prisma)
|
||||
|
||||
await createItemUseCase({
|
||||
actorId: actor.id,
|
||||
name: "Keyboard",
|
||||
categoryId: category.id,
|
||||
stock: 2,
|
||||
})
|
||||
|
||||
const stockedItem = await prisma.item.findUniqueOrThrow({
|
||||
where: { name: "Keyboard" },
|
||||
})
|
||||
|
||||
await expect(deleteItemUseCase(stockedItem.id)).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { id: ["Item has stock, you cannot delete it"] },
|
||||
})
|
||||
|
||||
await createItemUseCase({
|
||||
actorId: actor.id,
|
||||
name: "Mouse",
|
||||
categoryId: category.id,
|
||||
stock: 0,
|
||||
})
|
||||
|
||||
const emptyItem = await prisma.item.findUniqueOrThrow({
|
||||
where: { name: "Mouse" },
|
||||
})
|
||||
|
||||
await expect(deleteItemUseCase(emptyItem.id)).resolves.toEqual({
|
||||
success: true,
|
||||
})
|
||||
|
||||
const deletedItem = await prisma.item.findUniqueOrThrow({
|
||||
where: { id: emptyItem.id },
|
||||
})
|
||||
|
||||
expect(deletedItem.deletedAt).toBeInstanceOf(Date)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,170 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"
|
||||
import type { PrismaClient } from "@/generated/prisma/client"
|
||||
import { createTestRecipient } from "../helpers/factories"
|
||||
import {
|
||||
resetIntegrationTestDatabase,
|
||||
startIntegrationTestDatabase,
|
||||
stopIntegrationTestDatabase,
|
||||
} from "../helpers/test-db"
|
||||
|
||||
let prisma: PrismaClient
|
||||
let createRecipientUseCase: typeof import("@/use-cases/recipient.use-cases").createRecipientUseCase
|
||||
let updateRecipientUseCase: typeof import("@/use-cases/recipient.use-cases").updateRecipientUseCase
|
||||
|
||||
beforeAll(async () => {
|
||||
await startIntegrationTestDatabase()
|
||||
|
||||
const prismaModule = await import("@/lib/prisma")
|
||||
const recipientUseCases = await import("@/use-cases/recipient.use-cases")
|
||||
|
||||
prisma = prismaModule.prisma
|
||||
createRecipientUseCase = recipientUseCases.createRecipientUseCase
|
||||
updateRecipientUseCase = recipientUseCases.updateRecipientUseCase
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetIntegrationTestDatabase(prisma)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await prisma?.$disconnect()
|
||||
await stopIntegrationTestDatabase()
|
||||
})
|
||||
|
||||
describe("recipient use-cases", () => {
|
||||
it("creates a recipient and normalizes empty optional contact fields to null", async () => {
|
||||
await expect(
|
||||
createRecipientUseCase({
|
||||
username: "recipient-one",
|
||||
firstName: "Recipient",
|
||||
lastName: "One",
|
||||
department: "IT",
|
||||
email: "",
|
||||
phone: "",
|
||||
}),
|
||||
).resolves.toEqual({ success: true })
|
||||
|
||||
await expect(
|
||||
prisma.recipient.findUniqueOrThrow({
|
||||
where: { username: "recipient-one" },
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
username: "recipient-one",
|
||||
firstName: "Recipient",
|
||||
lastName: "One",
|
||||
department: "IT",
|
||||
email: null,
|
||||
phone: null,
|
||||
})
|
||||
})
|
||||
|
||||
it("rejects duplicate usernames and duplicate emails on create", async () => {
|
||||
await createTestRecipient(prisma, {
|
||||
username: "existing-recipient",
|
||||
email: "existing-recipient@example.test",
|
||||
})
|
||||
|
||||
await expect(
|
||||
createRecipientUseCase({
|
||||
username: "existing-recipient",
|
||||
firstName: "Duplicate",
|
||||
lastName: "Username",
|
||||
department: "OTHER",
|
||||
email: "unique-recipient@example.test",
|
||||
phone: null,
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { username: ["Username already exists"] },
|
||||
})
|
||||
|
||||
await expect(
|
||||
createRecipientUseCase({
|
||||
username: "unique-recipient",
|
||||
firstName: "Duplicate",
|
||||
lastName: "Email",
|
||||
department: "OTHER",
|
||||
email: "existing-recipient@example.test",
|
||||
phone: null,
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { email: ["Email already exists"] },
|
||||
})
|
||||
|
||||
await expect(prisma.recipient.count()).resolves.toBe(1)
|
||||
})
|
||||
|
||||
it("updates a recipient and rejects duplicate usernames or emails", async () => {
|
||||
const recipient = await createTestRecipient(prisma, {
|
||||
username: "editable-recipient",
|
||||
email: "editable-recipient@example.test",
|
||||
phone: "111111111",
|
||||
})
|
||||
const other = await createTestRecipient(prisma, {
|
||||
username: "other-recipient",
|
||||
email: "other-recipient@example.test",
|
||||
})
|
||||
|
||||
await expect(
|
||||
updateRecipientUseCase({
|
||||
id: recipient.id,
|
||||
username: "edited-recipient",
|
||||
firstName: "Edited",
|
||||
lastName: "Recipient",
|
||||
department: "ENGINEERING",
|
||||
email: "edited-recipient@example.test",
|
||||
phone: "222222222",
|
||||
}),
|
||||
).resolves.toEqual({ success: true })
|
||||
|
||||
await expect(
|
||||
prisma.recipient.findUniqueOrThrow({ where: { id: recipient.id } }),
|
||||
).resolves.toMatchObject({
|
||||
username: "edited-recipient",
|
||||
firstName: "Edited",
|
||||
lastName: "Recipient",
|
||||
department: "ENGINEERING",
|
||||
email: "edited-recipient@example.test",
|
||||
phone: "222222222",
|
||||
})
|
||||
|
||||
await expect(
|
||||
updateRecipientUseCase({
|
||||
id: recipient.id,
|
||||
username: other.username,
|
||||
firstName: "Edited",
|
||||
lastName: "Recipient",
|
||||
department: "ENGINEERING",
|
||||
email: "new-recipient@example.test",
|
||||
phone: "222222222",
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { username: ["Username already exists"] },
|
||||
})
|
||||
|
||||
await expect(
|
||||
updateRecipientUseCase({
|
||||
id: recipient.id,
|
||||
username: "edited-recipient",
|
||||
firstName: "Edited",
|
||||
lastName: "Recipient",
|
||||
department: "ENGINEERING",
|
||||
email: other.email,
|
||||
phone: "222222222",
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { email: ["Email already exists"] },
|
||||
})
|
||||
|
||||
await expect(
|
||||
prisma.recipient.findUniqueOrThrow({ where: { id: recipient.id } }),
|
||||
).resolves.toMatchObject({
|
||||
username: "edited-recipient",
|
||||
email: "edited-recipient@example.test",
|
||||
})
|
||||
await expect(prisma.recipient.count()).resolves.toBe(2)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,289 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"
|
||||
import type { PrismaClient } from "@/generated/prisma/client"
|
||||
import { createTestUser } from "../helpers/factories"
|
||||
import {
|
||||
resetIntegrationTestDatabase,
|
||||
startIntegrationTestDatabase,
|
||||
stopIntegrationTestDatabase,
|
||||
} from "../helpers/test-db"
|
||||
|
||||
let prisma: PrismaClient
|
||||
let createUserUseCase: typeof import("@/use-cases/user.use-cases").createUserUseCase
|
||||
let updateUserUseCase: typeof import("@/use-cases/user.use-cases").updateUserUseCase
|
||||
let setUserActiveUseCase: typeof import("@/use-cases/user.use-cases").setUserActiveUseCase
|
||||
let resetUserPasswordUseCase: typeof import("@/use-cases/user.use-cases").resetUserPasswordUseCase
|
||||
let verifyPassword: typeof import("@/lib/security").verifyPassword
|
||||
|
||||
beforeAll(async () => {
|
||||
await startIntegrationTestDatabase()
|
||||
|
||||
const prismaModule = await import("@/lib/prisma")
|
||||
const userUseCases = await import("@/use-cases/user.use-cases")
|
||||
const security = await import("@/lib/security")
|
||||
|
||||
prisma = prismaModule.prisma
|
||||
createUserUseCase = userUseCases.createUserUseCase
|
||||
updateUserUseCase = userUseCases.updateUserUseCase
|
||||
setUserActiveUseCase = userUseCases.setUserActiveUseCase
|
||||
resetUserPasswordUseCase = userUseCases.resetUserPasswordUseCase
|
||||
verifyPassword = security.verifyPassword
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetIntegrationTestDatabase(prisma)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await prisma?.$disconnect()
|
||||
await stopIntegrationTestDatabase()
|
||||
})
|
||||
|
||||
describe("user use-cases", () => {
|
||||
it("creates a user with a hashed password", async () => {
|
||||
const result = await createUserUseCase({
|
||||
username: "new-user",
|
||||
name: "New User",
|
||||
email: "new-user@example.test",
|
||||
password: "secure-password",
|
||||
role: "STAFF",
|
||||
isActive: true,
|
||||
})
|
||||
|
||||
expect(result).toEqual({ success: true })
|
||||
|
||||
const user = await prisma.user.findUniqueOrThrow({
|
||||
where: { username: "new-user" },
|
||||
})
|
||||
|
||||
expect(user).toMatchObject({
|
||||
username: "new-user",
|
||||
name: "New User",
|
||||
email: "new-user@example.test",
|
||||
role: "STAFF",
|
||||
isActive: true,
|
||||
})
|
||||
expect(user.password).not.toBe("secure-password")
|
||||
await expect(
|
||||
verifyPassword("secure-password", user.password),
|
||||
).resolves.toBe(true)
|
||||
})
|
||||
|
||||
it("rejects duplicate usernames and duplicate emails", async () => {
|
||||
await createTestUser(prisma, {
|
||||
username: "existing-user",
|
||||
email: "existing@example.test",
|
||||
})
|
||||
|
||||
await expect(
|
||||
createUserUseCase({
|
||||
username: "existing-user",
|
||||
name: "Duplicate Username",
|
||||
email: "unique@example.test",
|
||||
password: "secure-password",
|
||||
role: "STAFF",
|
||||
isActive: true,
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { username: ["Username already exists"] },
|
||||
})
|
||||
|
||||
await expect(
|
||||
createUserUseCase({
|
||||
username: "unique-user",
|
||||
name: "Duplicate Email",
|
||||
email: "existing@example.test",
|
||||
password: "secure-password",
|
||||
role: "STAFF",
|
||||
isActive: true,
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { email: ["Email already exists"] },
|
||||
})
|
||||
|
||||
await expect(prisma.user.count()).resolves.toBe(1)
|
||||
await expect(
|
||||
prisma.user.findUniqueOrThrow({ where: { username: "existing-user" } }),
|
||||
).resolves.toMatchObject({ email: "existing@example.test" })
|
||||
})
|
||||
|
||||
it("updates a user while preserving uniqueness constraints", async () => {
|
||||
const actor = await createTestUser(prisma, { role: "ADMIN" })
|
||||
const user = await createTestUser(prisma, {
|
||||
username: "editable-user",
|
||||
email: "editable@example.test",
|
||||
role: "STAFF",
|
||||
})
|
||||
const other = await createTestUser(prisma, {
|
||||
username: "other-user",
|
||||
email: "other@example.test",
|
||||
role: "STAFF",
|
||||
})
|
||||
|
||||
await expect(
|
||||
updateUserUseCase({
|
||||
actorId: actor.id,
|
||||
id: user.id,
|
||||
username: "edited-user",
|
||||
name: "Edited User",
|
||||
email: "edited@example.test",
|
||||
role: "MANAGER",
|
||||
isActive: true,
|
||||
}),
|
||||
).resolves.toEqual({ success: true })
|
||||
|
||||
await expect(
|
||||
prisma.user.findUniqueOrThrow({ where: { id: user.id } }),
|
||||
).resolves.toMatchObject({
|
||||
username: "edited-user",
|
||||
name: "Edited User",
|
||||
email: "edited@example.test",
|
||||
role: "MANAGER",
|
||||
isActive: true,
|
||||
})
|
||||
|
||||
await expect(
|
||||
updateUserUseCase({
|
||||
actorId: actor.id,
|
||||
id: user.id,
|
||||
username: other.username,
|
||||
name: "Edited User",
|
||||
email: "another-email@example.test",
|
||||
role: "MANAGER",
|
||||
isActive: true,
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { username: ["Username already exists"] },
|
||||
})
|
||||
|
||||
await expect(
|
||||
prisma.user.findUniqueOrThrow({ where: { id: user.id } }),
|
||||
).resolves.toMatchObject({
|
||||
username: "edited-user",
|
||||
email: "edited@example.test",
|
||||
})
|
||||
})
|
||||
|
||||
it("prevents an administrator from removing their own administrator access", async () => {
|
||||
const admin = await createTestUser(prisma, { role: "ADMIN" })
|
||||
|
||||
await expect(
|
||||
updateUserUseCase({
|
||||
actorId: admin.id,
|
||||
id: admin.id,
|
||||
username: admin.username,
|
||||
name: admin.name,
|
||||
email: admin.email,
|
||||
role: "STAFF",
|
||||
isActive: true,
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { id: ["You cannot remove your own administrator access"] },
|
||||
})
|
||||
|
||||
await expect(
|
||||
prisma.user.findUniqueOrThrow({ where: { id: admin.id } }),
|
||||
).resolves.toMatchObject({ role: "ADMIN", isActive: true })
|
||||
})
|
||||
|
||||
it("protects the last active administrator but allows deactivation when another active admin exists", async () => {
|
||||
const firstAdmin = await createTestUser(prisma, {
|
||||
username: "first-admin",
|
||||
email: "first-admin@example.test",
|
||||
role: "ADMIN",
|
||||
})
|
||||
const staffActor = await createTestUser(prisma, {
|
||||
username: "staff-actor",
|
||||
email: "staff-actor@example.test",
|
||||
role: "STAFF",
|
||||
})
|
||||
|
||||
await expect(
|
||||
setUserActiveUseCase({
|
||||
actorId: staffActor.id,
|
||||
id: firstAdmin.id,
|
||||
isActive: false,
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: {
|
||||
id: ["Cannot remove access from the last active administrator"],
|
||||
},
|
||||
})
|
||||
|
||||
const secondAdmin = await createTestUser(prisma, {
|
||||
username: "second-admin",
|
||||
email: "second-admin@example.test",
|
||||
role: "ADMIN",
|
||||
})
|
||||
|
||||
await expect(
|
||||
setUserActiveUseCase({
|
||||
actorId: secondAdmin.id,
|
||||
id: firstAdmin.id,
|
||||
isActive: false,
|
||||
}),
|
||||
).resolves.toEqual({ success: true })
|
||||
|
||||
await expect(
|
||||
prisma.user.findUniqueOrThrow({ where: { id: firstAdmin.id } }),
|
||||
).resolves.toMatchObject({ isActive: false })
|
||||
})
|
||||
|
||||
it("prevents self-deactivation", async () => {
|
||||
const admin = await createTestUser(prisma, { role: "ADMIN" })
|
||||
await createTestUser(prisma, { role: "ADMIN" })
|
||||
|
||||
await expect(
|
||||
setUserActiveUseCase({
|
||||
actorId: admin.id,
|
||||
id: admin.id,
|
||||
isActive: false,
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { id: ["You cannot deactivate your own user"] },
|
||||
})
|
||||
|
||||
await expect(
|
||||
prisma.user.findUniqueOrThrow({ where: { id: admin.id } }),
|
||||
).resolves.toMatchObject({ isActive: true })
|
||||
})
|
||||
|
||||
it("resets a user password and rejects missing users", async () => {
|
||||
const user = await createTestUser(prisma, {
|
||||
username: "password-user",
|
||||
email: "password-user@example.test",
|
||||
role: "STAFF",
|
||||
})
|
||||
|
||||
await expect(
|
||||
resetUserPasswordUseCase({
|
||||
id: user.id,
|
||||
password: "new-secure-password",
|
||||
}),
|
||||
).resolves.toEqual({ success: true })
|
||||
|
||||
const updatedUser = await prisma.user.findUniqueOrThrow({
|
||||
where: { id: user.id },
|
||||
})
|
||||
|
||||
expect(updatedUser.password).not.toBe(user.password)
|
||||
await expect(
|
||||
verifyPassword("new-secure-password", updatedUser.password),
|
||||
).resolves.toBe(true)
|
||||
|
||||
await expect(
|
||||
resetUserPasswordUseCase({
|
||||
id: "00000000-0000-0000-0000-000000000000",
|
||||
password: "new-secure-password",
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
success: false,
|
||||
errors: { id: ["User not found"] },
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,16 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
import { getPasswordHash, verifyPassword } from "@/lib/security"
|
||||
|
||||
describe("security helpers", () => {
|
||||
it("hashes passwords and verifies the original plain password", async () => {
|
||||
const hash = await getPasswordHash("secure-password")
|
||||
|
||||
expect(hash).not.toBe("secure-password")
|
||||
await expect(verifyPassword("secure-password", hash)).resolves.toBe(true)
|
||||
await expect(verifyPassword("wrong-password", hash)).resolves.toBe(false)
|
||||
})
|
||||
|
||||
it("rejects empty passwords when hashing", async () => {
|
||||
await expect(getPasswordHash("")).rejects.toThrow("Password is required")
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,227 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
import { createAssetSchema, updateAssetSchema } from "@/schemas/asset.schema"
|
||||
import { createAssignmentSchema } from "@/schemas/assignment.schema"
|
||||
import { signInSchema } from "@/schemas/auth.schema"
|
||||
import { createCategorySchema } from "@/schemas/category.schema"
|
||||
import { createItemSchema, updateItemSchema } from "@/schemas/item.schema"
|
||||
import { createMovementSchema } from "@/schemas/movement.schema"
|
||||
import { createRecipientSchema } from "@/schemas/recipient.schema"
|
||||
import { createUserSchema, updateUserSchema } from "@/schemas/user.schema"
|
||||
|
||||
describe("core schemas", () => {
|
||||
it("coerces valid numeric fields", () => {
|
||||
expect(
|
||||
createItemSchema.safeParse({
|
||||
name: "Laptop",
|
||||
categoryId: "category-id",
|
||||
stock: "3",
|
||||
}),
|
||||
).toMatchObject({ success: true, data: { stock: 3 } })
|
||||
|
||||
expect(
|
||||
createAssignmentSchema.safeParse({
|
||||
itemId: "item-id",
|
||||
recipientId: "recipient-id",
|
||||
quantity: "2",
|
||||
}),
|
||||
).toMatchObject({ success: true, data: { quantity: 2 } })
|
||||
|
||||
expect(
|
||||
createMovementSchema.safeParse({
|
||||
type: "IN",
|
||||
quantity: "1",
|
||||
itemId: "item-id",
|
||||
}),
|
||||
).toMatchObject({ success: true, data: { quantity: 1 } })
|
||||
})
|
||||
|
||||
it("rejects invalid numeric fields", () => {
|
||||
expect(
|
||||
createItemSchema.safeParse({
|
||||
name: "Laptop",
|
||||
categoryId: "category-id",
|
||||
stock: -1,
|
||||
}).success,
|
||||
).toBe(false)
|
||||
|
||||
expect(
|
||||
createAssignmentSchema.safeParse({
|
||||
itemId: "item-id",
|
||||
recipientId: "recipient-id",
|
||||
quantity: 0,
|
||||
}).success,
|
||||
).toBe(false)
|
||||
|
||||
expect(
|
||||
createMovementSchema.safeParse({
|
||||
type: "IN",
|
||||
quantity: 0,
|
||||
itemId: "item-id",
|
||||
}).success,
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
it("validates required identifiers for update schemas", () => {
|
||||
expect(
|
||||
updateItemSchema.safeParse({
|
||||
id: "item-id",
|
||||
name: "Laptop",
|
||||
categoryId: "category-id",
|
||||
stock: 1,
|
||||
}).success,
|
||||
).toBe(true)
|
||||
|
||||
expect(
|
||||
updateItemSchema.safeParse({
|
||||
id: "",
|
||||
name: "Laptop",
|
||||
categoryId: "category-id",
|
||||
stock: 1,
|
||||
}).success,
|
||||
).toBe(false)
|
||||
|
||||
expect(
|
||||
updateUserSchema.safeParse({
|
||||
id: "user-id",
|
||||
username: "user",
|
||||
name: "User",
|
||||
email: "user@example.test",
|
||||
role: "ADMIN",
|
||||
isActive: true,
|
||||
}).success,
|
||||
).toBe(true)
|
||||
|
||||
expect(
|
||||
updateUserSchema.safeParse({
|
||||
id: "",
|
||||
username: "user",
|
||||
name: "User",
|
||||
email: "user@example.test",
|
||||
role: "ADMIN",
|
||||
isActive: true,
|
||||
}).success,
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
it("validates category and auth requirements", () => {
|
||||
expect(createCategorySchema.safeParse({ name: "IT" }).success).toBe(false)
|
||||
expect(createCategorySchema.safeParse({ name: "Hardware" }).success).toBe(
|
||||
true,
|
||||
)
|
||||
|
||||
expect(
|
||||
signInSchema.safeParse({ username: "admin", password: "abc" }).success,
|
||||
).toBe(true)
|
||||
expect(
|
||||
signInSchema.safeParse({ username: "", password: "abc" }).success,
|
||||
).toBe(false)
|
||||
expect(
|
||||
signInSchema.safeParse({ username: "admin", password: "ab" }).success,
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
it("validates recipient email only when present", () => {
|
||||
expect(
|
||||
createRecipientSchema.safeParse({
|
||||
username: "recipient",
|
||||
firstName: "Rec",
|
||||
lastName: "Ipient",
|
||||
department: "IT",
|
||||
email: "recipient@example.test",
|
||||
}).success,
|
||||
).toBe(true)
|
||||
|
||||
expect(
|
||||
createRecipientSchema.safeParse({
|
||||
username: "recipient",
|
||||
firstName: "Rec",
|
||||
lastName: "Ipient",
|
||||
department: "IT",
|
||||
email: "not-an-email",
|
||||
}).success,
|
||||
).toBe(false)
|
||||
|
||||
expect(
|
||||
createRecipientSchema.safeParse({
|
||||
username: "recipient",
|
||||
firstName: "Rec",
|
||||
lastName: "Ipient",
|
||||
department: "IT",
|
||||
email: "",
|
||||
}).success,
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
it("validates user password, email, and role", () => {
|
||||
expect(
|
||||
createUserSchema.safeParse({
|
||||
username: "user",
|
||||
name: "User",
|
||||
email: "user@example.test",
|
||||
password: "password1",
|
||||
role: "STAFF",
|
||||
isActive: true,
|
||||
}).success,
|
||||
).toBe(true)
|
||||
|
||||
expect(
|
||||
createUserSchema.safeParse({
|
||||
username: "user",
|
||||
name: "User",
|
||||
email: "bad-email",
|
||||
password: "password1",
|
||||
role: "STAFF",
|
||||
isActive: true,
|
||||
}).success,
|
||||
).toBe(false)
|
||||
|
||||
expect(
|
||||
createUserSchema.safeParse({
|
||||
username: "user",
|
||||
name: "User",
|
||||
email: "user@example.test",
|
||||
password: "short",
|
||||
role: "STAFF",
|
||||
isActive: true,
|
||||
}).success,
|
||||
).toBe(false)
|
||||
|
||||
expect(
|
||||
createUserSchema.safeParse({
|
||||
username: "user",
|
||||
name: "User",
|
||||
email: "user@example.test",
|
||||
password: "password1",
|
||||
role: "OWNER",
|
||||
isActive: true,
|
||||
}).success,
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
it("validates asset status options", () => {
|
||||
expect(
|
||||
createAssetSchema.safeParse({
|
||||
itemId: "item-id",
|
||||
serialNumber: "SERIAL-001",
|
||||
status: "AVAILABLE",
|
||||
}).success,
|
||||
).toBe(true)
|
||||
|
||||
expect(
|
||||
createAssetSchema.safeParse({
|
||||
itemId: "item-id",
|
||||
serialNumber: "SERIAL-001",
|
||||
status: "BROKEN",
|
||||
}).success,
|
||||
).toBe(false)
|
||||
|
||||
expect(
|
||||
updateAssetSchema.safeParse({
|
||||
id: "asset-id",
|
||||
itemId: "item-id",
|
||||
serialNumber: "SERIAL-001",
|
||||
status: "BROKEN",
|
||||
}).success,
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,64 @@
|
||||
import { describe, expect, it, vi } from "vitest"
|
||||
|
||||
vi.mock("next/navigation", () => ({
|
||||
redirect: vi.fn((path: string) => {
|
||||
throw new Error(`redirect:${path}`)
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock("@/lib/auth", () => ({
|
||||
auth: vi.fn(),
|
||||
}))
|
||||
|
||||
import type { Session } from "next-auth"
|
||||
import {
|
||||
hasAnyRole,
|
||||
hasMinimumRole,
|
||||
hasRole,
|
||||
isAdmin,
|
||||
} from "@/services/auth.service"
|
||||
|
||||
function sessionWithRole(role: Session["user"]["role"]): Session {
|
||||
return {
|
||||
expires: new Date(Date.now() + 60_000).toISOString(),
|
||||
user: {
|
||||
id: "user-id",
|
||||
name: "Test User",
|
||||
email: "test@example.test",
|
||||
role,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
describe("auth service role helpers", () => {
|
||||
it("checks exact roles", () => {
|
||||
const admin = sessionWithRole("ADMIN")
|
||||
const staff = sessionWithRole("STAFF")
|
||||
|
||||
expect(hasRole(admin, "ADMIN")).toBe(true)
|
||||
expect(hasRole(staff, "ADMIN")).toBe(false)
|
||||
expect(hasRole(null, "ADMIN")).toBe(false)
|
||||
})
|
||||
|
||||
it("checks any allowed role", () => {
|
||||
const manager = sessionWithRole("MANAGER")
|
||||
|
||||
expect(hasAnyRole(manager, ["ADMIN", "MANAGER"])).toBe(true)
|
||||
expect(hasAnyRole(manager, ["ADMIN", "STAFF"])).toBe(false)
|
||||
expect(hasAnyRole(null, ["ADMIN", "MANAGER"])).toBe(false)
|
||||
})
|
||||
|
||||
it("checks minimum role hierarchy", () => {
|
||||
expect(hasMinimumRole(sessionWithRole("ADMIN"), "MANAGER")).toBe(true)
|
||||
expect(hasMinimumRole(sessionWithRole("MANAGER"), "STAFF")).toBe(true)
|
||||
expect(hasMinimumRole(sessionWithRole("STAFF"), "MANAGER")).toBe(false)
|
||||
expect(hasMinimumRole(sessionWithRole("VIEWER"), "STAFF")).toBe(false)
|
||||
expect(hasMinimumRole(null, "VIEWER")).toBe(false)
|
||||
})
|
||||
|
||||
it("identifies admins", () => {
|
||||
expect(isAdmin(sessionWithRole("ADMIN"))).toBe(true)
|
||||
expect(isAdmin(sessionWithRole("MANAGER"))).toBe(false)
|
||||
expect(isAdmin(null)).toBe(false)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user