feat: auth, first-run setup, login

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
YugonitoandClaude Opus 5.5 committed 2026-10-27 18:12:41 +01:00
1 parent d722c0034c
commit 23f3192cd0
28 files changed
+648 -160

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
import { execSync } from "node:child_process";
import { loadEnv } from "vite";
/** Applique les migrations sur la base de test avant la suite. */
export default function setup() {
const url = loadEnv("test", process.cwd(), "").TEST_DATABASE_URL;
if (!url?.includes("_test")) throw new Error("TEST_DATABASE_URL doit pointer vers une base *_test");
execSync("npx prisma migrate deploy", { stdio: "ignore", env: { ...process.env, DATABASE_URL: url } });
}
+80
View File
@@ -0,0 +1,80 @@
import { beforeEach, describe, expect, it } from "vitest";
import { createHousehold, hashPassword, verifyPassword } from "@/server/auth";
import { prisma } from "@/server/db";
import { resetDb } from "./helpers";
const person = (firstName: string, login: string, color: string) => ({
firstName,
login,
password: "motdepasse",
passwordConfirm: "motdepasse",
color,
});
describe("passwords", () => {
it("verifies the right password and rejects a wrong one", async () => {
const hash = await hashPassword("secret-123");
expect(await verifyPassword("secret-123", hash)).toBe(true);
expect(await verifyPassword("nope", hash)).toBe(false);
});
});
describe("createHousehold", () => {
beforeEach(resetDb);
it("creates both people, the default split and an opening balance", async () => {
const { users } = await createHousehold({
people: [person("Hugues", "hugues", "#4f46e5"), person("Léa", "lea", "#db2777")],
defaultPercentFirst: "60",
openingAmount: "150",
openingDirection: "second-owes-first",
});
expect(await prisma.user.count()).toBe(2);
const setting = await prisma.setting.findUniqueOrThrow({ where: { id: 1 } });
expect(setting.defaultSplit).toEqual({ [users[0].id]: 6000, [users[1].id]: 4000 });
const s = await prisma.settlement.findFirstOrThrow();
expect(s.fromUserId).toBe(users[1].id);
expect(s.toUserId).toBe(users[0].id);
expect(s.amount.toString()).toBe("150");
});
it("skips the opening balance when empty", async () => {
await createHousehold({
people: [person("A", "a", "#4f46e5"), person("B", "b", "#db2777")],
defaultPercentFirst: "50",
openingAmount: "",
openingDirection: "second-owes-first",
});
expect(await prisma.settlement.count()).toBe(0);
});
it("refuses to run twice", async () => {
const input = {
people: [person("A", "a", "#4f46e5"), person("B", "b", "#db2777")] as [ReturnType<typeof person>, ReturnType<typeof person>],
defaultPercentFirst: "50",
openingAmount: "",
openingDirection: "second-owes-first" as const,
};
await createHousehold(input);
await expect(createHousehold(input)).rejects.toThrow("Installation déjà effectuée");
});
it("validates passwords and logins", async () => {
await expect(
createHousehold({
people: [{ ...person("A", "a", "#4f46e5"), password: "court", passwordConfirm: "court" }, person("B", "b", "#db2777")],
defaultPercentFirst: "50",
openingAmount: "",
openingDirection: "second-owes-first",
}),
).rejects.toThrow("Le mot de passe doit faire au moins 8 caractères.");
await expect(
createHousehold({
people: [person("A", "same", "#4f46e5"), person("B", "same", "#db2777")],
defaultPercentFirst: "50",
openingAmount: "",
openingDirection: "second-owes-first",
}),
).rejects.toThrow("Les deux identifiants doivent être différents.");
});
});
+27
View File
@@ -0,0 +1,27 @@
import { prisma } from "@/server/db";
import { seed } from "../../prisma/seed";
const TABLES = [
"OperationSplit", "Operation", "RecurringSplit", "Recurring", "LoanSplit", "Loan",
"Settlement", "Budget", "Category", "Setting", "User",
];
/** Vide la base de test et remet les catégories de départ. */
export async function resetDb() {
if (!process.env.DATABASE_URL?.includes("_test")) throw new Error("resetDb refuse de tourner hors base de test");
await prisma.$executeRawUnsafe("SET FOREIGN_KEY_CHECKS = 0");
for (const t of TABLES) await prisma.$executeRawUnsafe(`TRUNCATE TABLE \`${t}\``);
await prisma.$executeRawUnsafe("SET FOREIGN_KEY_CHECKS = 1");
await seed(prisma);
}
export async function makeUsers() {
const h = await prisma.user.create({ data: { firstName: "H", login: "h", passwordHash: "x", color: "#4f46e5" } });
const c = await prisma.user.create({ data: { firstName: "C", login: "c", passwordHash: "x", color: "#db2777" } });
await prisma.setting.update({ where: { id: 1 }, data: { defaultSplit: { [h.id]: 5000, [c.id]: 5000 } } });
return { h, c };
}
export async function categoryId(name: string) {
return (await prisma.category.findFirstOrThrow({ where: { name } })).id;
}