Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e3a7e20993 | |||
| c5d02a2890 | |||
| c86d20ca81 | |||
| f038e4020b | |||
| e75098083a | |||
| e3eefd945c | |||
| d25c353018 | |||
| b3eb1b60a5 | |||
| 222c3237f0 |
@@ -0,0 +1,210 @@
|
||||
// #113 - E2E tests for /modules endpoints
|
||||
|
||||
import { assertEquals } from "@std/assert";
|
||||
import {
|
||||
makeContextWithAffiliation,
|
||||
makeEmployeeContext,
|
||||
makeGetRequest,
|
||||
makeJsonRequest,
|
||||
} from "../helpers/handler.ts";
|
||||
import { seedModules, truncateAll } from "../helpers/db_integration.ts";
|
||||
import { handler as modulesHandler } from "$apps/admin/api/modules.ts";
|
||||
import { handler as moduleHandler } from "$apps/admin/api/modules/[idModule].ts";
|
||||
|
||||
// --- GET /modules ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e modules: GET /modules returns all as employee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedModules([{ id: "MATH101", nom: "Mathématiques" }, {
|
||||
id: "INFO101",
|
||||
nom: "Informatique",
|
||||
}]);
|
||||
const res = await modulesHandler.GET!(
|
||||
makeGetRequest("/modules"),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.length, 2);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e modules: GET /modules returns empty for non-employee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedModules([{ id: "MATH101", nom: "Mathématiques" }]);
|
||||
const res = await modulesHandler.GET!(
|
||||
makeGetRequest("/modules"),
|
||||
makeContextWithAffiliation("student"),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.length, 0);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- POST /modules ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e modules: POST /modules creates module (201)",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await modulesHandler.POST!(
|
||||
makeJsonRequest("/modules", "POST", { id: "PHYS101", nom: "Physique" }),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 201);
|
||||
const body = await res.json();
|
||||
assertEquals(body.id, "PHYS101");
|
||||
assertEquals(body.nom, "Physique");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e modules: POST /modules 409 on duplicate id",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedModules([{ id: "MATH101", nom: "Mathématiques" }]);
|
||||
const res = await modulesHandler.POST!(
|
||||
makeJsonRequest("/modules", "POST", { id: "MATH101", nom: "Doublon" }),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 409);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e modules: POST /modules 400 on missing fields",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await modulesHandler.POST!(
|
||||
makeJsonRequest("/modules", "POST", { id: "X" }),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 400);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e modules: POST /modules 403 for non-employee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await modulesHandler.POST!(
|
||||
makeJsonRequest("/modules", "POST", { id: "X", nom: "Y" }),
|
||||
makeContextWithAffiliation("student"),
|
||||
);
|
||||
assertEquals(res.status, 403);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- GET /modules/:id ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e modules: GET /modules/:id returns module",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedModules([{ id: "ELEC201", nom: "Électronique" }]);
|
||||
const res = await moduleHandler.GET!(
|
||||
makeGetRequest("/modules/ELEC201"),
|
||||
makeEmployeeContext({ idModule: "ELEC201" }),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.nom, "Électronique");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e modules: GET /modules/:id 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await moduleHandler.GET!(
|
||||
makeGetRequest("/modules/GHOST"),
|
||||
makeEmployeeContext({ idModule: "GHOST" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- PUT /modules/:id ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e modules: PUT /modules/:id updates nom",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedModules([{ id: "CHIM101", nom: "Chimie" }]);
|
||||
const res = await moduleHandler.PUT!(
|
||||
makeJsonRequest("/modules/CHIM101", "PUT", { nom: "Chimie organique" }),
|
||||
makeEmployeeContext({ idModule: "CHIM101" }),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.nom, "Chimie organique");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e modules: PUT /modules/:id 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await moduleHandler.PUT!(
|
||||
makeJsonRequest("/modules/GHOST", "PUT", { nom: "X" }),
|
||||
makeEmployeeContext({ idModule: "GHOST" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- DELETE /modules/:id ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e modules: DELETE /modules/:id returns 204",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedModules([{ id: "BIO101", nom: "Biologie" }]);
|
||||
const res = await moduleHandler.DELETE!(
|
||||
makeGetRequest("/modules/BIO101"),
|
||||
makeEmployeeContext({ idModule: "BIO101" }),
|
||||
);
|
||||
assertEquals(res.status, 204);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e modules: DELETE /modules/:id 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await moduleHandler.DELETE!(
|
||||
makeGetRequest("/modules/GHOST"),
|
||||
makeEmployeeContext({ idModule: "GHOST" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
@@ -1,42 +0,0 @@
|
||||
// #115 - E2E tests for GET /permissions
|
||||
// Handler statique (pas de DB), test direct du handler
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import { makeEmployeeContext, makeGetRequest } from "../helpers/handler.ts";
|
||||
import { handler as permissionsHandler } from "$apps/admin/api/permissions.ts";
|
||||
|
||||
Deno.test({
|
||||
name: "e2e permissions: GET /permissions returns all 9 permissions",
|
||||
fn() {
|
||||
const res = permissionsHandler.GET!(
|
||||
makeGetRequest("/permissions"),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
return res.text().then((text) => {
|
||||
const data = JSON.parse(text);
|
||||
assertEquals(data.length, 9);
|
||||
assertExists(data.find((p: { id: string }) => p.id === "student_read"));
|
||||
assertExists(data.find((p: { id: string }) => p.id === "role_write"));
|
||||
});
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e permissions: GET /permissions - all entries have id and nom",
|
||||
async fn() {
|
||||
const res = permissionsHandler.GET!(
|
||||
makeGetRequest("/permissions"),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
const data: { id: string; nom: string }[] = await res.json();
|
||||
for (const p of data) {
|
||||
assertEquals(typeof p.id, "string");
|
||||
assertEquals(typeof p.nom, "string");
|
||||
}
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
@@ -0,0 +1,212 @@
|
||||
// #110 - E2E tests for /promotions endpoints
|
||||
|
||||
import { assertEquals } from "@std/assert";
|
||||
import {
|
||||
makeContextWithAffiliation,
|
||||
makeEmployeeContext,
|
||||
makeGetRequest,
|
||||
makeJsonRequest,
|
||||
} from "../helpers/handler.ts";
|
||||
import { seedPromotions, truncateAll } from "../helpers/db_integration.ts";
|
||||
import { handler as promotionsHandler } from "$apps/students/api/promotions.ts";
|
||||
import { handler as promotionHandler } from "$apps/students/api/promotions/[idPromo].ts";
|
||||
|
||||
// --- GET /promotions ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e promotions: GET /promotions returns all as employee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([
|
||||
{ id: "PEIP1-2024", annee: "2024" },
|
||||
{ id: "PEIP2-2024", annee: "2024" },
|
||||
]);
|
||||
const res = await promotionsHandler.GET!(
|
||||
makeGetRequest("/promotions"),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.length, 2);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e promotions: GET /promotions returns empty for non-employee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "PEIP1-2024", annee: "2024" }]);
|
||||
const res = await promotionsHandler.GET!(
|
||||
makeGetRequest("/promotions"),
|
||||
makeContextWithAffiliation("student"),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.length, 0);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- POST /promotions ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e promotions: POST /promotions creates promotion (201)",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await promotionsHandler.POST!(
|
||||
makeJsonRequest("/promotions", "POST", {
|
||||
idPromo: "INFO3-2025",
|
||||
annee: "2025",
|
||||
}),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 201);
|
||||
const body = await res.json();
|
||||
assertEquals(body.id, "INFO3-2025");
|
||||
assertEquals(body.annee, "2025");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e promotions: POST /promotions 403 for non-employee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await promotionsHandler.POST!(
|
||||
makeJsonRequest("/promotions", "POST", { idPromo: "X", annee: "2025" }),
|
||||
makeContextWithAffiliation("student"),
|
||||
);
|
||||
assertEquals(res.status, 403);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e promotions: POST /promotions 400 on missing fields",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await promotionsHandler.POST!(
|
||||
makeJsonRequest("/promotions", "POST", { idPromo: "X" }),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 400);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- GET /promotions/:idPromo ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e promotions: GET /promotions/:id returns promotion",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "INFO3-2024", annee: "2024" }]);
|
||||
const res = await promotionHandler.GET!(
|
||||
makeGetRequest("/promotions/INFO3-2024"),
|
||||
makeEmployeeContext({ idPromo: "INFO3-2024" }),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.id, "INFO3-2024");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e promotions: GET /promotions/:id 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await promotionHandler.GET!(
|
||||
makeGetRequest("/promotions/GHOST"),
|
||||
makeEmployeeContext({ idPromo: "GHOST" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e promotions: GET /promotions/:id 403 for non-employee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await promotionHandler.GET!(
|
||||
makeGetRequest("/promotions/INFO3-2024"),
|
||||
makeContextWithAffiliation("student", { idPromo: "INFO3-2024" }),
|
||||
);
|
||||
assertEquals(res.status, 403);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- PUT /promotions/:idPromo ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e promotions: PUT /promotions/:id updates annee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "INFO3-2023", annee: "2023" }]);
|
||||
const res = await promotionHandler.PUT!(
|
||||
makeJsonRequest("/promotions/INFO3-2023", "PUT", { annee: "2024" }),
|
||||
makeEmployeeContext({ idPromo: "INFO3-2023" }),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.annee, "2024");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e promotions: PUT /promotions/:id 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await promotionHandler.PUT!(
|
||||
makeJsonRequest("/promotions/GHOST", "PUT", { annee: "2025" }),
|
||||
makeEmployeeContext({ idPromo: "GHOST" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- DELETE /promotions/:idPromo ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e promotions: DELETE /promotions/:id returns 204",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "INFO3-2022", annee: "2022" }]);
|
||||
const res = await promotionHandler.DELETE!(
|
||||
makeGetRequest("/promotions/INFO3-2022"),
|
||||
makeEmployeeContext({ idPromo: "INFO3-2022" }),
|
||||
);
|
||||
assertEquals(res.status, 204);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e promotions: DELETE /promotions/:id 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await promotionHandler.DELETE!(
|
||||
makeGetRequest("/promotions/GHOST"),
|
||||
makeEmployeeContext({ idPromo: "GHOST" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
@@ -0,0 +1,175 @@
|
||||
// #112 - E2E tests for /roles endpoints
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import {
|
||||
makeEmployeeContext,
|
||||
makeGetRequest,
|
||||
makeJsonRequest,
|
||||
} from "../helpers/handler.ts";
|
||||
import { seedRoles, testDb, truncateAll } from "../helpers/db_integration.ts";
|
||||
import { permissions } from "$root/databases/schema.ts";
|
||||
import { handler as rolesHandler } from "$apps/admin/api/roles.ts";
|
||||
import { handler as roleHandler } from "$apps/admin/api/roles/[idRole].ts";
|
||||
|
||||
// --- GET /roles ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: GET /roles returns all with permissions",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedRoles([{ nom: "admin" }, { nom: "employee" }]);
|
||||
const res = await rolesHandler.GET!(
|
||||
makeGetRequest("/roles"),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.length, 2);
|
||||
assertExists(body[0].permissions);
|
||||
assertEquals(Array.isArray(body[0].permissions), true);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- POST /roles ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: POST /roles creates role (201)",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await rolesHandler.POST!(
|
||||
makeJsonRequest("/roles", "POST", { nom: "viewer" }),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 201);
|
||||
const body = await res.json();
|
||||
assertExists(body.id);
|
||||
assertEquals(body.nom, "viewer");
|
||||
assertEquals(body.permissions, []);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: POST /roles 400 on missing nom",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await rolesHandler.POST!(
|
||||
makeJsonRequest("/roles", "POST", {}),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 400);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- GET /roles/:id ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: GET /roles/:id returns role with permissions",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [role] = await seedRoles([{ nom: "admin" }]);
|
||||
await testDb.insert(permissions).values([
|
||||
{ id: "student_read", nom: "Consulter les élèves" },
|
||||
]);
|
||||
const res = await roleHandler.GET!(
|
||||
makeGetRequest(`/roles/${role.id}`),
|
||||
makeEmployeeContext({ idRole: String(role.id) }),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.nom, "admin");
|
||||
assertEquals(Array.isArray(body.permissions), true);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: GET /roles/:id 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await roleHandler.GET!(
|
||||
makeGetRequest("/roles/9999"),
|
||||
makeEmployeeContext({ idRole: "9999" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- PUT /roles/:id ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: PUT /roles/:id updates nom and permissions",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [role] = await seedRoles([{ nom: "employee" }]);
|
||||
await testDb.insert(permissions).values([
|
||||
{ id: "note_read", nom: "Consulter les notes" },
|
||||
]);
|
||||
const res = await roleHandler.PUT!(
|
||||
makeJsonRequest(`/roles/${role.id}`, "PUT", {
|
||||
nom: "teacher",
|
||||
permissions: ["note_read"],
|
||||
}),
|
||||
makeEmployeeContext({ idRole: String(role.id) }),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.nom, "teacher");
|
||||
assertEquals(body.permissions, ["note_read"]);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: PUT /roles/:id 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await roleHandler.PUT!(
|
||||
makeJsonRequest("/roles/9999", "PUT", { nom: "ghost", permissions: [] }),
|
||||
makeEmployeeContext({ idRole: "9999" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- DELETE /roles/:id ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: DELETE /roles/:id returns 204",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [role] = await seedRoles([{ nom: "moderator" }]);
|
||||
const res = await roleHandler.DELETE!(
|
||||
makeGetRequest(`/roles/${role.id}`),
|
||||
makeEmployeeContext({ idRole: String(role.id) }),
|
||||
);
|
||||
assertEquals(res.status, 204);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: DELETE /roles/:id 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await roleHandler.DELETE!(
|
||||
makeGetRequest("/roles/9999"),
|
||||
makeEmployeeContext({ idRole: "9999" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
@@ -0,0 +1,104 @@
|
||||
// #113 - Integration tests for /modules endpoints
|
||||
|
||||
import { assertEquals, assertExists, assertRejects } from "@std/assert";
|
||||
import { seedModules, testDb, truncateAll } from "../helpers/db_integration.ts";
|
||||
import { modules } from "$root/databases/schema.ts";
|
||||
import { eq } from "npm:drizzle-orm@0.45.2";
|
||||
|
||||
Deno.test({
|
||||
name: "integration modules: list all modules",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedModules([{ id: "MATH101", nom: "Mathématiques" }, {
|
||||
id: "INFO101",
|
||||
nom: "Informatique",
|
||||
}]);
|
||||
const rows = await testDb.select().from(modules);
|
||||
assertEquals(rows.length, 2);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration modules: create and retrieve by id",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [created] = await testDb.insert(modules).values({
|
||||
id: "PHYS101",
|
||||
nom: "Physique",
|
||||
}).returning();
|
||||
assertExists(created);
|
||||
assertEquals(created.id, "PHYS101");
|
||||
|
||||
const row = await testDb
|
||||
.select()
|
||||
.from(modules)
|
||||
.where(eq(modules.id, "PHYS101"))
|
||||
.then((r) => r[0] ?? null);
|
||||
assertExists(row);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration modules: get by id returns null when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const row = await testDb
|
||||
.select()
|
||||
.from(modules)
|
||||
.where(eq(modules.id, "NONEXISTENT"))
|
||||
.then((r) => r[0] ?? null);
|
||||
assertEquals(row, null);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration modules: duplicate id insert fails",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedModules([{ id: "MATH101", nom: "Mathématiques" }]);
|
||||
await assertRejects(() =>
|
||||
testDb.insert(modules).values({ id: "MATH101", nom: "Doublon" })
|
||||
);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration modules: update nom",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedModules([{ id: "ELEC201", nom: "Électronique" }]);
|
||||
const [updated] = await testDb
|
||||
.update(modules)
|
||||
.set({ nom: "Électronique numérique" })
|
||||
.where(eq(modules.id, "ELEC201"))
|
||||
.returning();
|
||||
assertEquals(updated.nom, "Électronique numérique");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration modules: delete removes the module",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedModules([{ id: "BIO101", nom: "Biologie" }]);
|
||||
await testDb.delete(modules).where(eq(modules.id, "BIO101"));
|
||||
const row = await testDb
|
||||
.select()
|
||||
.from(modules)
|
||||
.where(eq(modules.id, "BIO101"))
|
||||
.then((r) => r[0] ?? null);
|
||||
assertEquals(row, null);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
@@ -0,0 +1,112 @@
|
||||
// #110 - Integration tests for /promotions endpoints
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import {
|
||||
seedPromotions,
|
||||
testDb,
|
||||
truncateAll,
|
||||
} from "../helpers/db_integration.ts";
|
||||
import { promotions } from "$root/databases/schema.ts";
|
||||
import { eq } from "npm:drizzle-orm@0.45.2";
|
||||
|
||||
Deno.test({
|
||||
name: "integration promotions: list all",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([
|
||||
{ id: "PEIP1-2024", annee: "2024" },
|
||||
{ id: "PEIP2-2024", annee: "2024" },
|
||||
]);
|
||||
const rows = await testDb.select().from(promotions);
|
||||
assertEquals(rows.length, 2);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration promotions: create and retrieve by id",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [created] = await testDb
|
||||
.insert(promotions)
|
||||
.values({ id: "INFO3-2025", annee: "2025" })
|
||||
.returning();
|
||||
assertExists(created);
|
||||
assertEquals(created.id, "INFO3-2025");
|
||||
assertEquals(created.annee, "2025");
|
||||
|
||||
const row = await testDb
|
||||
.select()
|
||||
.from(promotions)
|
||||
.where(eq(promotions.id, "INFO3-2025"))
|
||||
.then((r) => r[0] ?? null);
|
||||
assertExists(row);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration promotions: get by id returns null when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const row = await testDb
|
||||
.select()
|
||||
.from(promotions)
|
||||
.where(eq(promotions.id, "NONEXISTENT"))
|
||||
.then((r) => r[0] ?? null);
|
||||
assertEquals(row, null);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration promotions: update annee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "INFO3-2023", annee: "2023" }]);
|
||||
const [updated] = await testDb
|
||||
.update(promotions)
|
||||
.set({ annee: "2024" })
|
||||
.where(eq(promotions.id, "INFO3-2023"))
|
||||
.returning();
|
||||
assertExists(updated);
|
||||
assertEquals(updated.annee, "2024");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration promotions: delete removes the row",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "INFO3-2022", annee: "2022" }]);
|
||||
await testDb.delete(promotions).where(eq(promotions.id, "INFO3-2022"));
|
||||
const row = await testDb
|
||||
.select()
|
||||
.from(promotions)
|
||||
.where(eq(promotions.id, "INFO3-2022"))
|
||||
.then((r) => r[0] ?? null);
|
||||
assertEquals(row, null);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration promotions: update non-existent returns empty",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const result = await testDb
|
||||
.update(promotions)
|
||||
.set({ annee: "2099" })
|
||||
.where(eq(promotions.id, "GHOST"))
|
||||
.returning();
|
||||
assertEquals(result.length, 0);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
@@ -0,0 +1,123 @@
|
||||
// #112 - Integration tests for /roles endpoints
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import { seedRoles, testDb, truncateAll } from "../helpers/db_integration.ts";
|
||||
import { permissions, rolePermissions, roles } from "$root/databases/schema.ts";
|
||||
import { eq } from "npm:drizzle-orm@0.45.2";
|
||||
|
||||
Deno.test({
|
||||
name: "integration roles: list all roles",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedRoles([{ nom: "admin" }, { nom: "employee" }]);
|
||||
const rows = await testDb.select().from(roles);
|
||||
assertEquals(rows.length, 2);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration roles: create and retrieve by id",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [created] = await testDb.insert(roles).values({ nom: "viewer" })
|
||||
.returning();
|
||||
assertExists(created.id);
|
||||
assertEquals(created.nom, "viewer");
|
||||
const row = await testDb
|
||||
.select()
|
||||
.from(roles)
|
||||
.where(eq(roles.id, created.id))
|
||||
.then((r) => r[0] ?? null);
|
||||
assertExists(row);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration roles: assign and retrieve permissions",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [role] = await seedRoles([{ nom: "admin" }]);
|
||||
await testDb.insert(permissions).values([
|
||||
{ id: "student_read", nom: "Consulter les élèves" },
|
||||
{ id: "student_write", nom: "Gérer les élèves" },
|
||||
]);
|
||||
await testDb.insert(rolePermissions).values([
|
||||
{ idRole: role.id, idPermission: "student_read" },
|
||||
{ idRole: role.id, idPermission: "student_write" },
|
||||
]);
|
||||
const perms = await testDb
|
||||
.select()
|
||||
.from(rolePermissions)
|
||||
.where(eq(rolePermissions.idRole, role.id));
|
||||
assertEquals(perms.length, 2);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration roles: update role nom",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [role] = await seedRoles([{ nom: "employee" }]);
|
||||
const [updated] = await testDb
|
||||
.update(roles)
|
||||
.set({ nom: "teacher" })
|
||||
.where(eq(roles.id, role.id))
|
||||
.returning();
|
||||
assertEquals(updated.nom, "teacher");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration roles: reset permissions on update",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [role] = await seedRoles([{ nom: "admin" }]);
|
||||
await testDb.insert(permissions).values([
|
||||
{ id: "note_read", nom: "Consulter les notes" },
|
||||
{ id: "note_write", nom: "Gérer les notes" },
|
||||
]);
|
||||
await testDb.insert(rolePermissions).values([
|
||||
{ idRole: role.id, idPermission: "note_read" },
|
||||
]);
|
||||
// reset
|
||||
await testDb.delete(rolePermissions).where(
|
||||
eq(rolePermissions.idRole, role.id),
|
||||
);
|
||||
await testDb.insert(rolePermissions).values([
|
||||
{ idRole: role.id, idPermission: "note_write" },
|
||||
]);
|
||||
const perms = await testDb
|
||||
.select()
|
||||
.from(rolePermissions)
|
||||
.where(eq(rolePermissions.idRole, role.id));
|
||||
assertEquals(perms.length, 1);
|
||||
assertEquals(perms[0].idPermission, "note_write");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration roles: delete role removes it",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [role] = await seedRoles([{ nom: "moderator" }]);
|
||||
await testDb.delete(roles).where(eq(roles.id, role.id));
|
||||
const row = await testDb
|
||||
.select()
|
||||
.from(roles)
|
||||
.where(eq(roles.id, role.id))
|
||||
.then((r) => r[0] ?? null);
|
||||
assertEquals(row, null);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
@@ -0,0 +1,171 @@
|
||||
// #113 - Unit tests for /modules endpoints
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import { mockFetch, restoreFetch } from "../helpers/api_mock.ts";
|
||||
import { createMockDb } from "../helpers/db_mock.ts";
|
||||
import { type Module, modules } from "../helpers/fixtures.ts";
|
||||
|
||||
// --- Fixtures ---
|
||||
|
||||
Deno.test("modules: fixtures have correct shape", () => {
|
||||
assertEquals(modules.length, 3);
|
||||
assertEquals(typeof modules[0].id, "string");
|
||||
assertEquals(typeof modules[0].nom, "string");
|
||||
});
|
||||
|
||||
// --- Mock API ---
|
||||
|
||||
Deno.test("mock API: GET /modules returns list", async () => {
|
||||
mockFetch({ "/modules": modules });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/modules");
|
||||
assertEquals(res.status, 200);
|
||||
const data: Module[] = await res.json();
|
||||
assertEquals(data.length, 3);
|
||||
assertExists(data.find((m) => m.id === "JIN702C"));
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: GET /modules/:id returns one module", async () => {
|
||||
mockFetch({ "/modules/JIN702C": modules[0] });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/modules/JIN702C");
|
||||
assertEquals(res.status, 200);
|
||||
const data: Module = await res.json();
|
||||
assertEquals(data.id, "JIN702C");
|
||||
assertEquals(data.nom, "Optimisation");
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: GET /modules/:id 404 when not found", async () => {
|
||||
mockFetch({
|
||||
"/modules/UNKNOWN": {
|
||||
status: 404,
|
||||
body: { error: "Ressource introuvable" },
|
||||
},
|
||||
});
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/modules/UNKNOWN");
|
||||
assertEquals(res.status, 404);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: POST /modules creates module (201)", async () => {
|
||||
const newModule: Module = { id: "NEW101", nom: "Nouveau Module" };
|
||||
mockFetch({ "/modules": { method: "POST", status: 201, body: newModule } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/modules", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(newModule),
|
||||
});
|
||||
assertEquals(res.status, 201);
|
||||
const data: Module = await res.json();
|
||||
assertEquals(data.id, "NEW101");
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: POST /modules 409 on duplicate id", async () => {
|
||||
mockFetch({
|
||||
"/modules": {
|
||||
method: "POST",
|
||||
status: 409,
|
||||
body: { error: "Un module avec cet identifiant existe déjà" },
|
||||
},
|
||||
});
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/modules", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(modules[0]),
|
||||
});
|
||||
assertEquals(res.status, 409);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: POST /modules 400 on missing fields", async () => {
|
||||
mockFetch({ "/modules": { method: "POST", status: 400 } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/modules", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ id: "X" }),
|
||||
});
|
||||
assertEquals(res.status, 400);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: PUT /modules/:id updates nom", async () => {
|
||||
const updated: Module = { id: "JIN702C", nom: "Optimisation avancée" };
|
||||
mockFetch({
|
||||
"/modules/JIN702C": { method: "PUT", status: 200, body: updated },
|
||||
});
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/modules/JIN702C", {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ nom: "Optimisation avancée" }),
|
||||
});
|
||||
assertEquals(res.status, 200);
|
||||
const data: Module = await res.json();
|
||||
assertEquals(data.nom, "Optimisation avancée");
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: DELETE /modules/:id returns 204", async () => {
|
||||
mockFetch({ "/modules/JIN702C": { method: "DELETE", status: 204 } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/modules/JIN702C", {
|
||||
method: "DELETE",
|
||||
});
|
||||
assertEquals(res.status, 204);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
// --- Mock DB ---
|
||||
|
||||
Deno.test("mock DB: find module by id", () => {
|
||||
const db = createMockDb({ tables: { modules: [...modules] } });
|
||||
const m = db.findOne<Module>("modules", (m) => m.id === "JIN702C");
|
||||
assertExists(m);
|
||||
assertEquals(m.nom, "Optimisation");
|
||||
});
|
||||
|
||||
Deno.test("mock DB: insert module", () => {
|
||||
const db = createMockDb({ tables: { modules: [...modules] } });
|
||||
db.insert<Module>("modules", { id: "NEW101", nom: "Nouveau" });
|
||||
assertEquals(db.getTable("modules").length, 4);
|
||||
});
|
||||
|
||||
Deno.test("mock DB: update module nom", () => {
|
||||
const db = createMockDb({ tables: { modules: [...modules] } });
|
||||
db.updateWhere<Module>("modules", (m) => m.id === "JIN702C", {
|
||||
nom: "Updated",
|
||||
});
|
||||
assertEquals(
|
||||
db.findOne<Module>("modules", (m) => m.id === "JIN702C")?.nom,
|
||||
"Updated",
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("mock DB: delete module", () => {
|
||||
const db = createMockDb({ tables: { modules: [...modules] } });
|
||||
db.deleteWhere<Module>("modules", (m) => m.id === "JIN702C");
|
||||
assertEquals(db.getTable("modules").length, 2);
|
||||
});
|
||||
@@ -1,65 +0,0 @@
|
||||
// #115 - Unit tests for GET /permissions
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import { mockFetch, restoreFetch } from "../helpers/api_mock.ts";
|
||||
|
||||
interface Permission {
|
||||
id: string;
|
||||
nom: string;
|
||||
}
|
||||
|
||||
const EXPECTED_PERMISSIONS: Permission[] = [
|
||||
{ id: "student_read", nom: "Consulter les élèves" },
|
||||
{ id: "student_write", nom: "Gérer les élèves" },
|
||||
{ id: "note_read", nom: "Consulter les notes" },
|
||||
{ id: "note_write", nom: "Gérer les notes" },
|
||||
{ id: "module_read", nom: "Consulter les modules" },
|
||||
{ id: "module_write", nom: "Gérer les modules" },
|
||||
{ id: "user_read", nom: "Consulter les utilisateurs" },
|
||||
{ id: "user_write", nom: "Gérer les utilisateurs" },
|
||||
{ id: "role_write", nom: "Gérer les rôles" },
|
||||
];
|
||||
|
||||
Deno.test("permissions: known permission ids", () => {
|
||||
const ids = EXPECTED_PERMISSIONS.map((p) => p.id);
|
||||
assertEquals(ids.includes("student_read"), true);
|
||||
assertEquals(ids.includes("student_write"), true);
|
||||
assertEquals(ids.includes("note_read"), true);
|
||||
assertEquals(ids.includes("role_write"), true);
|
||||
assertEquals(ids.length, 9);
|
||||
});
|
||||
|
||||
Deno.test("permissions: all permissions have string id and nom", () => {
|
||||
for (const p of EXPECTED_PERMISSIONS) {
|
||||
assertEquals(typeof p.id, "string");
|
||||
assertEquals(typeof p.nom, "string");
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: GET /permissions returns all permissions", async () => {
|
||||
mockFetch({ "/permissions": EXPECTED_PERMISSIONS });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/permissions");
|
||||
assertEquals(res.status, 200);
|
||||
const data: Permission[] = await res.json();
|
||||
assertEquals(data.length, 9);
|
||||
assertExists(data.find((p) => p.id === "student_read"));
|
||||
assertExists(data.find((p) => p.id === "role_write"));
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: GET /permissions - each permission has id and nom", async () => {
|
||||
mockFetch({ "/permissions": EXPECTED_PERMISSIONS });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/permissions");
|
||||
const data: Permission[] = await res.json();
|
||||
for (const p of data) {
|
||||
assertExists(p.id);
|
||||
assertExists(p.nom);
|
||||
}
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,160 @@
|
||||
// #110 - Unit tests for /promotions endpoints
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import { mockFetch, restoreFetch } from "../helpers/api_mock.ts";
|
||||
import { createMockDb } from "../helpers/db_mock.ts";
|
||||
import { type Promotion, promotions } from "../helpers/fixtures.ts";
|
||||
|
||||
// --- Fixtures ---
|
||||
|
||||
Deno.test("promotions: fixtures have correct shape", () => {
|
||||
assertEquals(promotions.length, 3);
|
||||
assertEquals(typeof promotions[0].idPromo, "string");
|
||||
assertEquals(typeof promotions[0].annee, "string");
|
||||
});
|
||||
|
||||
// --- Mock API ---
|
||||
|
||||
Deno.test("mock API: GET /promotions returns list", async () => {
|
||||
mockFetch({ "/promotions": promotions });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/promotions");
|
||||
assertEquals(res.status, 200);
|
||||
const data: Promotion[] = await res.json();
|
||||
assertEquals(data.length, 3);
|
||||
assertExists(data.find((p) => p.idPromo === "4AFISE25/26"));
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: GET /promotions/:id returns one", async () => {
|
||||
mockFetch({ "/promotions/4AFISE25%2F26": promotions[0] });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/promotions/4AFISE25%2F26");
|
||||
assertEquals(res.status, 200);
|
||||
const data: Promotion = await res.json();
|
||||
assertEquals(data.idPromo, "4AFISE25/26");
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: GET /promotions/:id 404 when not found", async () => {
|
||||
mockFetch({
|
||||
"/promotions/UNKNOWN": {
|
||||
status: 404,
|
||||
body: { error: "Ressource introuvable" },
|
||||
},
|
||||
});
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/promotions/UNKNOWN");
|
||||
assertEquals(res.status, 404);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: POST /promotions creates promotion (201)", async () => {
|
||||
const newPromo: Promotion = { idPromo: "NEW2025", annee: "2025" };
|
||||
mockFetch({ "/promotions": { method: "POST", status: 201, body: newPromo } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/promotions", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ idPromo: "NEW2025", annee: "2025" }),
|
||||
});
|
||||
assertEquals(res.status, 201);
|
||||
const data: Promotion = await res.json();
|
||||
assertEquals(data.idPromo, "NEW2025");
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: POST /promotions 400 on missing fields", async () => {
|
||||
mockFetch({ "/promotions": { method: "POST", status: 400 } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/promotions", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ idPromo: "NEW2025" }),
|
||||
});
|
||||
assertEquals(res.status, 400);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: PUT /promotions/:id updates promotion", async () => {
|
||||
const updated = { idPromo: "4AFISE25/26", annee: "2026" };
|
||||
mockFetch({
|
||||
"/promotions/4AFISE25%2F26": { method: "PUT", status: 200, body: updated },
|
||||
});
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/promotions/4AFISE25%2F26", {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ annee: "2026" }),
|
||||
});
|
||||
assertEquals(res.status, 200);
|
||||
const data: Promotion = await res.json();
|
||||
assertEquals(data.annee, "2026");
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: DELETE /promotions/:id returns 204", async () => {
|
||||
mockFetch({ "/promotions/4AFISE25%2F26": { method: "DELETE", status: 204 } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/promotions/4AFISE25%2F26", {
|
||||
method: "DELETE",
|
||||
});
|
||||
assertEquals(res.status, 204);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
// --- Mock DB ---
|
||||
|
||||
Deno.test("mock DB: find promotion by idPromo", () => {
|
||||
const db = createMockDb({ tables: { promotions: [...promotions] } });
|
||||
const p = db.findOne<Promotion>(
|
||||
"promotions",
|
||||
(r) => r.idPromo === "4AFISE25/26",
|
||||
);
|
||||
assertExists(p);
|
||||
assertEquals(p.annee, "2025");
|
||||
});
|
||||
|
||||
Deno.test("mock DB: insert promotion", () => {
|
||||
const db = createMockDb({ tables: { promotions: [...promotions] } });
|
||||
db.insert<Promotion>("promotions", { idPromo: "NEW2025", annee: "2025" });
|
||||
assertEquals(db.getTable("promotions").length, 4);
|
||||
});
|
||||
|
||||
Deno.test("mock DB: update promotion annee", () => {
|
||||
const db = createMockDb({ tables: { promotions: [...promotions] } });
|
||||
db.updateWhere<Promotion>(
|
||||
"promotions",
|
||||
(p) => p.idPromo === "4AFISE25/26",
|
||||
{ annee: "2026" },
|
||||
);
|
||||
assertEquals(
|
||||
db.findOne<Promotion>("promotions", (p) => p.idPromo === "4AFISE25/26")
|
||||
?.annee,
|
||||
"2026",
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("mock DB: delete promotion", () => {
|
||||
const db = createMockDb({ tables: { promotions: [...promotions] } });
|
||||
const count = db.deleteWhere<Promotion>(
|
||||
"promotions",
|
||||
(p) => p.idPromo === "4AFISE25/26",
|
||||
);
|
||||
assertEquals(count, 1);
|
||||
assertEquals(db.getTable("promotions").length, 2);
|
||||
});
|
||||
@@ -0,0 +1,159 @@
|
||||
// #112 - Unit tests for /roles endpoints
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import { mockFetch, restoreFetch } from "../helpers/api_mock.ts";
|
||||
import { createMockDb } from "../helpers/db_mock.ts";
|
||||
|
||||
interface Role {
|
||||
id: number;
|
||||
nom: string;
|
||||
permissions: string[];
|
||||
}
|
||||
|
||||
const roles: Role[] = [
|
||||
{ id: 1, nom: "admin", permissions: ["student_read", "student_write"] },
|
||||
{ id: 2, nom: "employee", permissions: ["student_read"] },
|
||||
];
|
||||
|
||||
// --- Fixtures ---
|
||||
|
||||
Deno.test("roles: fixtures have correct shape", () => {
|
||||
assertEquals(roles.length, 2);
|
||||
assertEquals(typeof roles[0].id, "number");
|
||||
assertEquals(typeof roles[0].nom, "string");
|
||||
assertEquals(Array.isArray(roles[0].permissions), true);
|
||||
});
|
||||
|
||||
Deno.test("roles: permissions are strings", () => {
|
||||
assertEquals(roles[0].permissions.every((p) => typeof p === "string"), true);
|
||||
});
|
||||
|
||||
// --- Mock API ---
|
||||
|
||||
Deno.test("mock API: GET /roles returns list with permissions", async () => {
|
||||
mockFetch({ "/roles": roles });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/roles");
|
||||
assertEquals(res.status, 200);
|
||||
const data: Role[] = await res.json();
|
||||
assertEquals(data.length, 2);
|
||||
assertExists(data.find((r) => r.nom === "admin"));
|
||||
assertEquals(data[0].permissions.length, 2);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: GET /roles/:id returns role", async () => {
|
||||
mockFetch({ "/roles/1": roles[0] });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/roles/1");
|
||||
assertEquals(res.status, 200);
|
||||
const data: Role = await res.json();
|
||||
assertEquals(data.nom, "admin");
|
||||
assertEquals(data.permissions.length, 2);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: GET /roles/:id 404 when not found", async () => {
|
||||
mockFetch({
|
||||
"/roles/99": { status: 404, body: { error: "Ressource introuvable" } },
|
||||
});
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/roles/99");
|
||||
assertEquals(res.status, 404);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: POST /roles creates role (201)", async () => {
|
||||
const newRole: Role = { id: 3, nom: "viewer", permissions: [] };
|
||||
mockFetch({ "/roles": { method: "POST", status: 201, body: newRole } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/roles", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ nom: "viewer" }),
|
||||
});
|
||||
assertEquals(res.status, 201);
|
||||
const data: Role = await res.json();
|
||||
assertEquals(data.nom, "viewer");
|
||||
assertEquals(data.permissions.length, 0);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: POST /roles 400 on missing nom", async () => {
|
||||
mockFetch({ "/roles": { method: "POST", status: 400 } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/roles", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({}),
|
||||
});
|
||||
assertEquals(res.status, 400);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: PUT /roles/:id updates role and permissions", async () => {
|
||||
const updated: Role = { id: 2, nom: "teacher", permissions: ["note_read"] };
|
||||
mockFetch({ "/roles/2": { method: "PUT", status: 200, body: updated } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/roles/2", {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ nom: "teacher", permissions: ["note_read"] }),
|
||||
});
|
||||
assertEquals(res.status, 200);
|
||||
const data: Role = await res.json();
|
||||
assertEquals(data.nom, "teacher");
|
||||
assertEquals(data.permissions, ["note_read"]);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: DELETE /roles/:id returns 204", async () => {
|
||||
mockFetch({ "/roles/2": { method: "DELETE", status: 204 } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/roles/2", {
|
||||
method: "DELETE",
|
||||
});
|
||||
assertEquals(res.status, 204);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
// --- Mock DB ---
|
||||
|
||||
Deno.test("mock DB: find role by id", () => {
|
||||
const db = createMockDb({ tables: { roles: [...roles] } });
|
||||
const r = db.findOne<Role>("roles", (r) => r.id === 1);
|
||||
assertExists(r);
|
||||
assertEquals(r.nom, "admin");
|
||||
});
|
||||
|
||||
Deno.test("mock DB: insert role", () => {
|
||||
const db = createMockDb({ tables: { roles: [...roles] } });
|
||||
db.insert<Role>("roles", { id: 3, nom: "viewer", permissions: [] });
|
||||
assertEquals(db.getTable("roles").length, 3);
|
||||
});
|
||||
|
||||
Deno.test("mock DB: update role nom", () => {
|
||||
const db = createMockDb({ tables: { roles: [...roles] } });
|
||||
db.updateWhere<Role>("roles", (r) => r.id === 2, { nom: "teacher" });
|
||||
assertEquals(db.findOne<Role>("roles", (r) => r.id === 2)?.nom, "teacher");
|
||||
});
|
||||
|
||||
Deno.test("mock DB: delete role", () => {
|
||||
const db = createMockDb({ tables: { roles: [...roles] } });
|
||||
db.deleteWhere<Role>("roles", (r) => r.id === 1);
|
||||
assertEquals(db.getTable("roles").length, 1);
|
||||
});
|
||||
Reference in New Issue
Block a user