2f4d8db1bf
- Unit tests (mock DB + API) for all missing endpoints - Integration tests (Drizzle direct) for all missing entities - E2E tests (handler + real DB) for all missing endpoints - Robustness tests: invalid inputs, SQL injection, type errors, business rule violations - Seed helpers: seedNotes, seedUeModules, seedEnseignements, seedAjustements - Add test:coverage and test:coverage:html tasks to deno.json Tests expose known handler bugs (marked [BUG] in test names): - ajustements PUT/DELETE: .where() without and() modifies all rows for student - Missing try/catch in modules, users, enseignements handlers - Whitespace accepted as valid string values - No type or business rule validation (note bounds, coeff >= 0)
190 lines
6.5 KiB
TypeScript
190 lines
6.5 KiB
TypeScript
// Unit tests for /ajustements endpoints — fixtures, mock API, mock DB
|
|
|
|
import { assertEquals, assertExists } from "@std/assert";
|
|
import { mockFetch, restoreFetch } from "../helpers/api_mock.ts";
|
|
import { createMockDb } from "../helpers/db_mock.ts";
|
|
import { type Ajustement, ajustements } from "../helpers/fixtures.ts";
|
|
|
|
// --- Fixtures ---
|
|
|
|
Deno.test("ajustements: fixtures have correct shape", () => {
|
|
assertEquals(ajustements.length, 2);
|
|
assertEquals(typeof ajustements[0].numEtud, "number");
|
|
assertEquals(typeof ajustements[0].idUE, "number");
|
|
assertEquals(typeof ajustements[0].valeur, "number");
|
|
});
|
|
|
|
// --- Mock API ---
|
|
|
|
Deno.test("mock API: GET /ajustements returns list", async () => {
|
|
mockFetch({ "/ajustements": ajustements });
|
|
try {
|
|
const res = await fetch("http://localhost/api/ajustements");
|
|
assertEquals(res.status, 200);
|
|
const data: Ajustement[] = await res.json();
|
|
assertEquals(data.length, 2);
|
|
} finally {
|
|
restoreFetch();
|
|
}
|
|
});
|
|
|
|
Deno.test("mock API: GET /ajustements?numEtud filters by student", async () => {
|
|
const filtered = ajustements.filter((a) => a.numEtud === 21212006);
|
|
mockFetch({ "/ajustements": filtered });
|
|
try {
|
|
const res = await fetch("http://localhost/api/ajustements?numEtud=21212006");
|
|
const data: Ajustement[] = await res.json();
|
|
assertEquals(data.length, 1);
|
|
assertEquals(data[0].numEtud, 21212006);
|
|
} finally {
|
|
restoreFetch();
|
|
}
|
|
});
|
|
|
|
Deno.test("mock API: GET /ajustements?numEtud=NaN returns 400", async () => {
|
|
mockFetch({ "/ajustements": { status: 400 } });
|
|
try {
|
|
const res = await fetch("http://localhost/api/ajustements?numEtud=abc");
|
|
assertEquals(res.status, 400);
|
|
} finally {
|
|
restoreFetch();
|
|
}
|
|
});
|
|
|
|
Deno.test("mock API: POST /ajustements creates ajustement (201) as employee", async () => {
|
|
const newAjust: Ajustement = { numEtud: 21212007, idUE: 2, valeur: 14.0 };
|
|
mockFetch({ "/ajustements": { method: "POST", status: 201, body: newAjust } });
|
|
try {
|
|
const res = await fetch("http://localhost/api/ajustements", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify(newAjust),
|
|
});
|
|
assertEquals(res.status, 201);
|
|
const data: Ajustement = await res.json();
|
|
assertEquals(data.numEtud, 21212007);
|
|
assertEquals(data.valeur, 14.0);
|
|
} finally {
|
|
restoreFetch();
|
|
}
|
|
});
|
|
|
|
Deno.test("mock API: POST /ajustements 403 for non-employee", async () => {
|
|
mockFetch({ "/ajustements": { method: "POST", status: 403 } });
|
|
try {
|
|
const res = await fetch("http://localhost/api/ajustements", { method: "POST" });
|
|
assertEquals(res.status, 403);
|
|
} finally {
|
|
restoreFetch();
|
|
}
|
|
});
|
|
|
|
Deno.test("mock API: POST /ajustements 400 on missing fields", async () => {
|
|
mockFetch({ "/ajustements": { method: "POST", status: 400 } });
|
|
try {
|
|
const res = await fetch("http://localhost/api/ajustements", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ numEtud: 21212006 }),
|
|
});
|
|
assertEquals(res.status, 400);
|
|
} finally {
|
|
restoreFetch();
|
|
}
|
|
});
|
|
|
|
Deno.test("mock API: GET /ajustements/:numEtud/:idUE returns ajustement (employee)", async () => {
|
|
mockFetch({ "/ajustements/21212006/1": ajustements[0] });
|
|
try {
|
|
const res = await fetch("http://localhost/api/ajustements/21212006/1");
|
|
assertEquals(res.status, 200);
|
|
const data: Ajustement = await res.json();
|
|
assertEquals(data.valeur, 13.25);
|
|
} finally {
|
|
restoreFetch();
|
|
}
|
|
});
|
|
|
|
Deno.test("mock API: GET /ajustements/:numEtud/:idUE 403 for non-employee", async () => {
|
|
mockFetch({ "/ajustements/21212006/1": { status: 403 } });
|
|
try {
|
|
const res = await fetch("http://localhost/api/ajustements/21212006/1");
|
|
assertEquals(res.status, 403);
|
|
} finally {
|
|
restoreFetch();
|
|
}
|
|
});
|
|
|
|
Deno.test("mock API: GET /ajustements/:numEtud/:idUE 404 when not found", async () => {
|
|
mockFetch({ "/ajustements/99999/9": { status: 404, body: { error: "Ajustement introuvable" } } });
|
|
try {
|
|
const res = await fetch("http://localhost/api/ajustements/99999/9");
|
|
assertEquals(res.status, 404);
|
|
} finally {
|
|
restoreFetch();
|
|
}
|
|
});
|
|
|
|
Deno.test("mock API: PUT /ajustements/:numEtud/:idUE updates valeur", async () => {
|
|
const updated: Ajustement = { ...ajustements[0], valeur: 18.0 };
|
|
mockFetch({ "/ajustements/21212006/1": { method: "PUT", status: 200, body: updated } });
|
|
try {
|
|
const res = await fetch("http://localhost/api/ajustements/21212006/1", {
|
|
method: "PUT",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ valeur: 18.0 }),
|
|
});
|
|
assertEquals(res.status, 200);
|
|
const data: Ajustement = await res.json();
|
|
assertEquals(data.valeur, 18.0);
|
|
} finally {
|
|
restoreFetch();
|
|
}
|
|
});
|
|
|
|
Deno.test("mock API: DELETE /ajustements/:numEtud/:idUE returns 204", async () => {
|
|
mockFetch({ "/ajustements/21212006/1": { method: "DELETE", status: 204 } });
|
|
try {
|
|
const res = await fetch("http://localhost/api/ajustements/21212006/1", { method: "DELETE" });
|
|
assertEquals(res.status, 204);
|
|
} finally {
|
|
restoreFetch();
|
|
}
|
|
});
|
|
|
|
// --- Mock DB ---
|
|
|
|
Deno.test("mock DB: find ajustement by composite key", () => {
|
|
const db = createMockDb({ tables: { ajustements: [...ajustements] } });
|
|
const a = db.findOne<Ajustement>("ajustements", (a) => a.numEtud === 21212006 && a.idUE === 1);
|
|
assertExists(a);
|
|
assertEquals(a.valeur, 13.25);
|
|
});
|
|
|
|
Deno.test("mock DB: filter ajustements by numEtud", () => {
|
|
const db = createMockDb({ tables: { ajustements: [...ajustements] } });
|
|
const rows = db.findMany<Ajustement>("ajustements", (a) => a.numEtud === 21212006);
|
|
assertEquals(rows.length, 1);
|
|
});
|
|
|
|
Deno.test("mock DB: insert ajustement", () => {
|
|
const db = createMockDb({ tables: { ajustements: [...ajustements] } });
|
|
db.insert<Ajustement>("ajustements", { numEtud: 21212007, idUE: 2, valeur: 14.0 });
|
|
assertEquals(db.getTable("ajustements").length, 3);
|
|
});
|
|
|
|
Deno.test("mock DB: update ajustement valeur", () => {
|
|
const db = createMockDb({ tables: { ajustements: [...ajustements] } });
|
|
db.updateWhere<Ajustement>("ajustements", (a) => a.numEtud === 21212006 && a.idUE === 1, { valeur: 20.0 });
|
|
assertEquals(
|
|
db.findOne<Ajustement>("ajustements", (a) => a.numEtud === 21212006 && a.idUE === 1)?.valeur,
|
|
20.0,
|
|
);
|
|
});
|
|
|
|
Deno.test("mock DB: delete ajustement", () => {
|
|
const db = createMockDb({ tables: { ajustements: [...ajustements] } });
|
|
db.deleteWhere<Ajustement>("ajustements", (a) => a.numEtud === 21212006 && a.idUE === 1);
|
|
assertEquals(db.getTable("ajustements").length, 1);
|
|
});
|