test(students): add unit, integration and e2e tests for /students (#109)
- unit: fixture shapes, mock API (GET/POST/PUT/DELETE), mock DB operations - integration: real DB CRUD via testDb (list, filter, create, get, update, delete) - e2e: handler calls directly with mock FreshContext + real DB covers auth (employee vs non-employee), 400/403/404 cases - adds test:e2e deno task and CI step - adds tests/helpers/handler.ts with makeEmployeeContext, makeContextWithAffiliation, makeGetRequest, makeJsonRequest utilities Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,285 @@
|
||||
// #109 - E2E tests for /students endpoints
|
||||
// Appelle les handlers Fresh directement avec un vrai contexte + vraie DB
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import {
|
||||
makeContextWithAffiliation,
|
||||
makeEmployeeContext,
|
||||
makeGetRequest,
|
||||
makeJsonRequest,
|
||||
} from "../helpers/handler.ts";
|
||||
import {
|
||||
seedPromotions,
|
||||
seedStudents,
|
||||
truncateAll,
|
||||
} from "../helpers/db_integration.ts";
|
||||
import { handler as studentsHandler } from "$apps/students/api/students.ts";
|
||||
import { handler as studentHandler } from "$apps/students/api/students/[numEtud].ts";
|
||||
|
||||
// --- GET /students ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: GET /students returns all students as employee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "PEIP1-2024" }]);
|
||||
await seedStudents([
|
||||
{ nom: "Dupont", prenom: "Jean", idPromo: "PEIP1-2024" },
|
||||
{ nom: "Martin", prenom: "Alice", idPromo: "PEIP1-2024" },
|
||||
]);
|
||||
|
||||
const req = makeGetRequest("/students");
|
||||
const ctx = makeEmployeeContext();
|
||||
const res = await studentsHandler.GET!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.length, 2);
|
||||
assertExists(body.find((s: { nom: string }) => s.nom === "Dupont"));
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: GET /students returns empty array for non-employee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "PEIP1-2024" }]);
|
||||
await seedStudents([
|
||||
{ nom: "Dupont", prenom: "Jean", idPromo: "PEIP1-2024" },
|
||||
]);
|
||||
|
||||
const req = makeGetRequest("/students");
|
||||
const ctx = makeContextWithAffiliation("student");
|
||||
const res = await studentsHandler.GET!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.length, 0);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: GET /students?idPromo filters by promotion",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "PEIP1-2024" }, { id: "PEIP2-2024" }]);
|
||||
await seedStudents([
|
||||
{ nom: "Dupont", prenom: "Jean", idPromo: "PEIP1-2024" },
|
||||
{ nom: "Martin", prenom: "Alice", idPromo: "PEIP1-2024" },
|
||||
{ nom: "Durand", prenom: "Claire", idPromo: "PEIP2-2024" },
|
||||
]);
|
||||
|
||||
const req = makeGetRequest("/students", { idPromo: "PEIP1-2024" });
|
||||
const ctx = makeEmployeeContext();
|
||||
const res = await studentsHandler.GET!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.length, 2);
|
||||
assertEquals(body.every((s: { idPromo: string }) => s.idPromo === "PEIP1-2024"), true);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- POST /students ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: POST /students creates a student (201)",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "INFO3-2024" }]);
|
||||
|
||||
const req = makeJsonRequest("/students", "POST", {
|
||||
nom: "Leroy",
|
||||
prenom: "Paul",
|
||||
idPromo: "INFO3-2024",
|
||||
});
|
||||
const ctx = makeEmployeeContext();
|
||||
const res = await studentsHandler.POST!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 201);
|
||||
const body = await res.json();
|
||||
assertExists(body.numEtud);
|
||||
assertEquals(body.nom, "Leroy");
|
||||
assertEquals(body.idPromo, "INFO3-2024");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: POST /students 403 for non-employee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
|
||||
const req = makeJsonRequest("/students", "POST", {
|
||||
nom: "Test",
|
||||
prenom: "User",
|
||||
idPromo: "PEIP1-2024",
|
||||
});
|
||||
const ctx = makeContextWithAffiliation("student");
|
||||
const res = await studentsHandler.POST!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 403);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: POST /students 400 when missing required fields",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
|
||||
const req = makeJsonRequest("/students", "POST", { nom: "Leroy" });
|
||||
const ctx = makeEmployeeContext();
|
||||
const res = await studentsHandler.POST!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 400);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- GET /students/:numEtud ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: GET /students/:numEtud returns student",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "INFO3-2024" }]);
|
||||
const [s] = await seedStudents([
|
||||
{ nom: "Bernard", prenom: "Lucie", idPromo: "INFO3-2024" },
|
||||
]);
|
||||
|
||||
const req = makeGetRequest(`/students/${s.numEtud}`);
|
||||
const ctx = makeEmployeeContext({ numEtud: String(s.numEtud) });
|
||||
const res = await studentHandler.GET!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.numEtud, s.numEtud);
|
||||
assertEquals(body.nom, "Bernard");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: GET /students/:numEtud 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
|
||||
const req = makeGetRequest("/students/999999");
|
||||
const ctx = makeEmployeeContext({ numEtud: "999999" });
|
||||
const res = await studentHandler.GET!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: GET /students/:numEtud 403 for non-employee",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
|
||||
const req = makeGetRequest("/students/12345");
|
||||
const ctx = makeContextWithAffiliation("student", { numEtud: "12345" });
|
||||
const res = await studentHandler.GET!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 403);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- PUT /students/:numEtud ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: PUT /students/:numEtud updates student",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "INFO3-2024" }, { id: "INFO4-2024" }]);
|
||||
const [s] = await seedStudents([
|
||||
{ nom: "Petit", prenom: "Hugo", idPromo: "INFO3-2024" },
|
||||
]);
|
||||
|
||||
const req = makeJsonRequest(`/students/${s.numEtud}`, "PUT", {
|
||||
nom: "Grand",
|
||||
prenom: "Hugo",
|
||||
idPromo: "INFO4-2024",
|
||||
});
|
||||
const ctx = makeEmployeeContext({ numEtud: String(s.numEtud) });
|
||||
const res = await studentHandler.PUT!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.nom, "Grand");
|
||||
assertEquals(body.idPromo, "INFO4-2024");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: PUT /students/:numEtud 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "INFO3-2024" }]);
|
||||
|
||||
const req = makeJsonRequest("/students/999999", "PUT", {
|
||||
nom: "Ghost",
|
||||
prenom: "Ghost",
|
||||
idPromo: "INFO3-2024",
|
||||
});
|
||||
const ctx = makeEmployeeContext({ numEtud: "999999" });
|
||||
const res = await studentHandler.PUT!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- DELETE /students/:numEtud ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: DELETE /students/:numEtud returns 204",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedPromotions([{ id: "INFO3-2024" }]);
|
||||
const [s] = await seedStudents([
|
||||
{ nom: "Thomas", prenom: "Eva", idPromo: "INFO3-2024" },
|
||||
]);
|
||||
|
||||
const req = makeGetRequest(`/students/${s.numEtud}`);
|
||||
const ctx = makeEmployeeContext({ numEtud: String(s.numEtud) });
|
||||
const res = await studentHandler.DELETE!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 204);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e students: DELETE /students/:numEtud 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
|
||||
const req = makeGetRequest("/students/999999");
|
||||
const ctx = makeEmployeeContext({ numEtud: "999999" });
|
||||
const res = await studentHandler.DELETE!(req, ctx);
|
||||
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
Reference in New Issue
Block a user