fix: correct handler bugs exposed by test suite
- ajustements [numEtud]/[idUE]: fix .where() missing and() — PUT/DELETE were applying only numEtud condition, modifying all rows for a student - modules/users/enseignements POST: add try/catch, return 500 on invalid JSON - modules/[idModule] PUT: add try/catch + type check on nom (string required) - modules POST: add .trim() check to reject whitespace-only id/nom - users POST: add .trim() check to reject whitespace-only id/nom/prenom - ues POST: add .trim() check to reject whitespace-only nom - notes POST: add type check (typeof number) and bounds check (0 ≤ note ≤ 20) - ue-modules POST: add coeff >= 0 validation Update robustness tests to reflect fixed behavior (remove [BUG] labels, replace assertRejects with status code assertions).
This commit is contained in:
@@ -2,7 +2,7 @@ import { FreshContext, Handlers } from "$fresh/server.ts";
|
||||
import { db } from "$root/databases/db.ts";
|
||||
import { ajustements } from "$root/databases/schema.ts";
|
||||
import { AuthenticatedState } from "$root/defaults/interfaces.ts";
|
||||
import { eq } from "npm:drizzle-orm@0.45.2";
|
||||
import { and, eq } from "npm:drizzle-orm@0.45.2";
|
||||
|
||||
const NOT_FOUND = new Response(
|
||||
JSON.stringify({ error: "Ajustement introuvable" }),
|
||||
@@ -31,7 +31,7 @@ export const handler: Handlers<null, AuthenticatedState> = {
|
||||
const ajustement = await db
|
||||
.select()
|
||||
.from(ajustements)
|
||||
.where(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE))
|
||||
.where(and(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE)))
|
||||
.then((rows) => rows[0] ?? null);
|
||||
|
||||
if (!ajustement) return NOT_FOUND;
|
||||
@@ -69,7 +69,7 @@ export const handler: Handlers<null, AuthenticatedState> = {
|
||||
const [updated] = await db
|
||||
.update(ajustements)
|
||||
.set({ valeur: body.valeur })
|
||||
.where(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE))
|
||||
.where(and(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE)))
|
||||
.returning();
|
||||
|
||||
if (!updated) return NOT_FOUND;
|
||||
@@ -97,7 +97,7 @@ export const handler: Handlers<null, AuthenticatedState> = {
|
||||
|
||||
const [deleted] = await db
|
||||
.delete(ajustements)
|
||||
.where(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE))
|
||||
.where(and(eq(ajustements.numEtud, numEtud), eq(ajustements.idUE, idUE)))
|
||||
.returning();
|
||||
|
||||
if (!deleted) return NOT_FOUND;
|
||||
|
||||
@@ -49,6 +49,12 @@ export const handler: Handlers = {
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof note !== "number" || note < 0 || note > 20) {
|
||||
return new Response("Champ 'note' doit être un nombre entre 0 et 20", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const result = await db.insert(notes).values({ note, numEtud, idModule })
|
||||
.returning();
|
||||
|
||||
|
||||
@@ -47,6 +47,12 @@ export const handler: Handlers = {
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof coeff !== "number" || coeff < 0) {
|
||||
return new Response("Champ 'coeff' doit être un nombre >= 0", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const result = await db.insert(ueModules).values({
|
||||
idModule,
|
||||
idUE,
|
||||
|
||||
@@ -24,7 +24,7 @@ export const handler: Handlers = {
|
||||
const body = await request.json();
|
||||
const { nom } = body;
|
||||
|
||||
if (!nom) {
|
||||
if (!nom || !nom.trim()) {
|
||||
return new Response("Champ 'nom' manquant", { status: 400 });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user