test(modules): add unit, integration and e2e tests for /modules (#113)
- unit: fixture shapes, mock API (GET/POST/PUT/DELETE + 409), mock DB CRUD - integration: list, create, get, duplicate rejection, update, delete - e2e: handler calls with mock context + real DB, covers 400/403/404/409
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
// #113 - E2E tests for /modules endpoints
|
||||
|
||||
import { assertEquals, assertExists } 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,
|
||||
});
|
||||
Reference in New Issue
Block a user