fix: harden production flows and user messaging
This commit is contained in:
1 parent
52dce168dd
commit
86dfe8053d
43 files changed
+3520
-358
No files matched your search
@@ -6,14 +6,21 @@ beforeEach(() => {
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
approveEditorial: vi.fn(),
|
||||
dbSet: vi.fn(),
|
||||
dbWhere: vi.fn(),
|
||||
headers: vi.fn(),
|
||||
requireAdmin: vi.fn(),
|
||||
revalidatePath: vi.fn(),
|
||||
}));
|
||||
|
||||
mocks.dbSet.mockReturnValue({ where: mocks.dbWhere });
|
||||
|
||||
vi.mock("next/headers", () => ({ headers: mocks.headers }));
|
||||
vi.mock("next/cache", () => ({ revalidatePath: mocks.revalidatePath }));
|
||||
vi.mock("@/modules/auth/principal", () => ({ requireAdmin: mocks.requireAdmin }));
|
||||
vi.mock("@/db/client", () => ({
|
||||
db: { update: vi.fn(() => ({ set: mocks.dbSet })) },
|
||||
}));
|
||||
vi.mock("@/modules/editorials/service", () => ({
|
||||
editorialService: { approveEditorial: mocks.approveEditorial },
|
||||
EditorialServiceError: class EditorialServiceError extends Error {
|
||||
@@ -23,7 +30,7 @@ vi.mock("@/modules/editorials/service", () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
import { approveEditorialAction } from "./actions";
|
||||
import { approveEditorialAction, updateFlatrateAction } from "./actions";
|
||||
|
||||
const initial = { ok: true, message: "" };
|
||||
|
||||
@@ -97,4 +104,34 @@ test("the approve action translates a service error into a German message", asyn
|
||||
ok: false,
|
||||
message: "Bitte weisen Sie zuerst mindestens ein Album zu.",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test("an active flatrate cannot be persisted without a positive price", async () => {
|
||||
mocks.headers.mockResolvedValue(new Headers());
|
||||
mocks.requireAdmin.mockResolvedValue({ userId: "admin-1" });
|
||||
const formData = new FormData();
|
||||
formData.set("flatrateActive", "on");
|
||||
|
||||
await expect(
|
||||
updateFlatrateAction("00000000-0000-4000-8000-000000000001", formData),
|
||||
).rejects.toThrow("INVALID_FLATRATE_PRICE");
|
||||
expect(mocks.dbSet).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("an active flatrate persists a positive cents price and optional limit", async () => {
|
||||
mocks.headers.mockResolvedValue(new Headers());
|
||||
mocks.requireAdmin.mockResolvedValue({ userId: "admin-1" });
|
||||
mocks.dbWhere.mockResolvedValue(undefined);
|
||||
const formData = new FormData();
|
||||
formData.set("flatrateActive", "on");
|
||||
formData.set("flatratePriceEuros", "149,50");
|
||||
formData.set("flatrateAssetLimit", "20");
|
||||
|
||||
await updateFlatrateAction("00000000-0000-4000-8000-000000000001", formData);
|
||||
|
||||
expect(mocks.dbSet).toHaveBeenCalledWith({
|
||||
flatrateActive: true,
|
||||
flatratePriceCents: 14_950,
|
||||
flatrateAssetLimit: 20,
|
||||
});
|
||||
});
|
||||
@@ -98,6 +98,9 @@ export async function updateFlatrateAction(editorialId: string, formData: FormDa
|
||||
flatratePriceCents = price.cents;
|
||||
}
|
||||
}
|
||||
if (flatrateActive && flatratePriceCents === null) {
|
||||
throw new Error("INVALID_FLATRATE_PRICE");
|
||||
}
|
||||
|
||||
let flatrateAssetLimit: number | null = null;
|
||||
const rawLimit = formData.get("flatrateAssetLimit");
|
||||
|
||||
Reference in new issue
Block a user