fix: opening balance direction, safe return paths, backup failure detection, review fixes
- opening balance: "person 2 owes person 1" was stored the wrong way round - retour param: reject backslash/control chars (open redirect) - backup: pipefail + check dump completeness, failed dumps no longer logged OK - deleting a recurring occurrence marks it skipped so sync does not recreate it - editing a charge regenerates future occurrences of incomes allocated to it - settlements (incl. opening balance) listed in settings and deletable Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a0bcea4eb1
commit
5b402691f0
15 files changed
+222
-15
No files matched your search
@@ -0,0 +1,15 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { safeReturn } from "@/lib/safe-return";
|
||||
|
||||
describe("safeReturn", () => {
|
||||
it("keeps internal paths with their query string", () => {
|
||||
expect(safeReturn("/operations?m=2026-10&q=a", "/")).toBe("/operations?m=2026-10&q=a");
|
||||
expect(safeReturn("/", "/x")).toBe("/");
|
||||
});
|
||||
|
||||
it("falls back for anything that could leave the site", () => {
|
||||
for (const bad of [undefined, "", "https://evil.com", "//evil.com", "/\\evil.com", "/%09/evil.com", "\t//evil.com", "javascript:alert(1)", "/\t/evil.com"]) {
|
||||
expect(safeReturn(bad, "/fallback")).toBe("/fallback");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,7 @@
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { computeBalances, describeBalance } from "@/domain/balance";
|
||||
import { createHousehold, hashPassword, verifyPassword } from "@/server/auth";
|
||||
import { toSettlement } from "@/server/mappers";
|
||||
import { prisma } from "@/server/db";
|
||||
import { resetDb } from "./helpers";
|
||||
|
||||
@@ -33,9 +35,14 @@ describe("createHousehold", () => {
|
||||
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");
|
||||
// « La personne 2 doit à la personne 1 » : le tableau de bord doit l'afficher ainsi.
|
||||
const ids = users.map((u) => u.id);
|
||||
expect(describeBalance(computeBalances([], [toSettlement(s)], ids))).toEqual({
|
||||
debtor: users[1].id,
|
||||
creditor: users[0].id,
|
||||
amount: 15000,
|
||||
});
|
||||
});
|
||||
|
||||
it("skips the opening balance when empty", async () => {
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
confirmOperation,
|
||||
deleteOperation,
|
||||
saveOperation,
|
||||
deleteSettlement,
|
||||
saveSettlement,
|
||||
skipOperation,
|
||||
type OperationInput,
|
||||
@@ -108,6 +109,19 @@ describe("saveOperation", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteOperation on a recurring occurrence", () => {
|
||||
it("does not come back at the next sync", async () => {
|
||||
const r = await saveOperation(expense({ makeRecurring: { frequency: "MONTHLY", endDate: null } }), H);
|
||||
if (!r.ok) throw new Error(r.error);
|
||||
await deleteOperation(r.id);
|
||||
expect(await syncRecurrings("2026-10-27")).toBe(11);
|
||||
const october = await prisma.operation.findMany({
|
||||
where: { date: new Date("2026-10-27T00:00:00Z"), status: { not: "SKIPPED" } },
|
||||
});
|
||||
expect(october).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("confirm / skip", () => {
|
||||
it("confirms a planned operation with an adjusted amount and date", async () => {
|
||||
const r = await saveOperation(expense({ status: "PLANNED" }), H);
|
||||
@@ -130,6 +144,15 @@ describe("confirm / skip", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteSettlement", () => {
|
||||
it("removes a repayment entered by mistake", async () => {
|
||||
await saveSettlement({ fromUserId: C, toUserId: H, amount: "450", date: "2026-10-27" });
|
||||
const s = await prisma.settlement.findFirstOrThrow();
|
||||
expect(await deleteSettlement(s.id)).toEqual({ ok: true });
|
||||
expect(await prisma.settlement.count()).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("saveSettlement", () => {
|
||||
it("records a repayment and refuses one to oneself", async () => {
|
||||
expect(await saveSettlement({ fromUserId: C, toUserId: H, amount: "450", date: "2026-10-27" })).toEqual({ ok: true });
|
||||
|
||||
@@ -50,6 +50,34 @@ describe("saveRecurring", () => {
|
||||
expect(future.every((o) => o.amount.toString() === "1050")).toBe(true);
|
||||
});
|
||||
|
||||
it("re-splits future allocated incomes when the charge's split changes", async () => {
|
||||
const rent = await saveRecurring(rentInput, TODAY);
|
||||
if (!rent.ok) throw new Error(rent.error);
|
||||
const employer = await saveRecurring(
|
||||
{
|
||||
...rentInput,
|
||||
kind: "INCOME",
|
||||
amount: "200",
|
||||
label: "Part loyer employeur",
|
||||
categoryId: await categoryId("Remboursement employeur"),
|
||||
split: { [H]: 10000, [C]: 0 },
|
||||
allocatedToRecurringId: rent.id,
|
||||
allocationMode: "REDUCE_SHARED",
|
||||
},
|
||||
TODAY,
|
||||
);
|
||||
if (!employer.ok) throw new Error(employer.error);
|
||||
await saveRecurring({ ...rentInput, id: rent.id, split: { [H]: 6000, [C]: 4000 } }, TODAY);
|
||||
const future = await prisma.operation.findMany({
|
||||
where: { recurringId: employer.id, status: "PLANNED", date: { gte: fromIsoDate(TODAY) } },
|
||||
include: { splits: true },
|
||||
});
|
||||
expect(future.length).toBeGreaterThan(0);
|
||||
for (const o of future) {
|
||||
expect(Object.fromEntries(o.splits.map((s) => [s.userId, s.basisPoints]))).toEqual({ [H]: 6000, [C]: 4000 });
|
||||
}
|
||||
});
|
||||
|
||||
it("validates the input", async () => {
|
||||
expect(await saveRecurring({ ...rentInput, amount: "0" }, TODAY)).toEqual({ ok: false, error: "Montant invalide." });
|
||||
expect(await saveRecurring({ ...rentInput, dayOfMonth: 32 }, TODAY)).toEqual({ ok: false, error: "Jour du mois invalide (1 à 31)." });
|
||||
|
||||
Reference in new issue
Block a user