Compare commits

..

3 Commits

Author SHA1 Message Date
djalim be2d8ad9d2 fix: remove unused assertExists import
Check Deno code / Check Deno code (pull_request) Failing after 7s
Tests / Unit tests (pull_request) Successful in 11s
Tests / Integration tests (pull_request) Successful in 56s
2026-04-26 14:19:11 +02:00
djalim 5acd0ac113 style: fix deno fmt and lint 2026-04-26 14:18:55 +02:00
djalim 88edbe0956 test(promotions): add unit, integration and e2e tests for /promotions (#110)
- unit: fixture shapes, mock API (GET/POST/PUT/DELETE), mock DB CRUD
- integration: real DB list, create, get, update, delete, not-found cases
- e2e: handler calls with mock context + real DB, covers 400/403/404 cases
2026-04-26 14:13:48 +02:00
8 changed files with 555 additions and 561 deletions
+27
View File
@@ -0,0 +1,27 @@
name: "Build and push image"
on:
push:
branches:
- main
jobs:
deploy:
name: "Build Docker image"
runs-on: ubuntu-latest
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: registry.docker.polytech.djalim.fr
username: ${{ secrets.registry_login }}
password: ${{ secrets.registry_pass }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push
uses: docker/build-push-action@v6
with:
push: true
tags: registry.docker.polytech.djalim.fr/polympr:latest
+29
View File
@@ -0,0 +1,29 @@
name: "Check Deno code"
on:
pull_request:
branches:
- main
permissions:
contents: read
jobs:
check-code:
name: "Check Deno code"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v2
with:
deno-version: v2.x
- name: Check formatting
run: deno fmt --check
- name: Check linting
run: deno lint
- name: Run tests
run: deno test -A --no-check tests/
+212
View File
@@ -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,
});
-250
View File
@@ -1,250 +0,0 @@
// #111 - E2E tests for /users endpoints
import { assertEquals, assertExists } from "@std/assert";
import {
makeEmployeeContext,
makeGetRequest,
makeJsonRequest,
} from "../helpers/handler.ts";
import {
seedRoles,
seedUsers,
truncateAll,
} from "../helpers/db_integration.ts";
import { handler as usersHandler } from "$apps/admin/api/users.ts";
import { handler as userHandler } from "$apps/admin/api/users/[id].ts";
// --- GET /users ---
Deno.test({
name: "e2e users: GET /users returns all users",
async fn() {
await truncateAll();
const [role] = await seedRoles([{ nom: "employee" }]);
await seedUsers([
{ id: "dupont.jean", nom: "Dupont", prenom: "Jean", idRole: role.id },
{ id: "martin.alice", nom: "Martin", prenom: "Alice", idRole: role.id },
]);
const res = await usersHandler.GET!(
makeGetRequest("/users"),
makeEmployeeContext(),
);
assertEquals(res.status, 200);
const body = await res.json();
assertEquals(body.length, 2);
assertExists(body.find((u: { id: string }) => u.id === "dupont.jean"));
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "e2e users: GET /users?idRole filters by role",
async fn() {
await truncateAll();
const [role1] = await seedRoles([{ nom: "admin" }]);
const [role2] = await seedRoles([{ nom: "employee" }]);
await seedUsers([
{ id: "u1", nom: "A", prenom: "A", idRole: role1.id },
{ id: "u2", nom: "B", prenom: "B", idRole: role2.id },
]);
const res = await usersHandler.GET!(
makeGetRequest("/users", { idRole: String(role1.id) }),
makeEmployeeContext(),
);
assertEquals(res.status, 200);
const body = await res.json();
assertEquals(body.length, 1);
assertEquals(body[0].id, "u1");
},
sanitizeResources: false,
sanitizeOps: false,
});
// --- POST /users ---
Deno.test({
name: "e2e users: POST /users creates user (201)",
async fn() {
await truncateAll();
const [role] = await seedRoles([{ nom: "employee" }]);
const res = await usersHandler.POST!(
makeJsonRequest("/users", "POST", {
id: "nouveau.user",
nom: "Nouveau",
prenom: "User",
idRole: role.id,
}),
makeEmployeeContext(),
);
assertEquals(res.status, 201);
const body = await res.json();
assertEquals(body.id, "nouveau.user");
assertExists(body.nom);
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "e2e users: POST /users 409 on duplicate id",
async fn() {
await truncateAll();
const [role] = await seedRoles([{ nom: "employee" }]);
await seedUsers([{
id: "dup.user",
nom: "A",
prenom: "A",
idRole: role.id,
}]);
const res = await usersHandler.POST!(
makeJsonRequest("/users", "POST", {
id: "dup.user",
nom: "B",
prenom: "B",
idRole: role.id,
}),
makeEmployeeContext(),
);
assertEquals(res.status, 409);
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "e2e users: POST /users 400 on missing fields",
async fn() {
await truncateAll();
const res = await usersHandler.POST!(
makeJsonRequest("/users", "POST", { id: "x" }),
makeEmployeeContext(),
);
assertEquals(res.status, 400);
},
sanitizeResources: false,
sanitizeOps: false,
});
// --- GET /users/:id ---
Deno.test({
name: "e2e users: GET /users/:id returns user",
async fn() {
await truncateAll();
const [role] = await seedRoles([{ nom: "employee" }]);
await seedUsers([{
id: "test.user",
nom: "Test",
prenom: "User",
idRole: role.id,
}]);
const res = await userHandler.GET!(
makeGetRequest("/users/test.user"),
makeEmployeeContext({ id: "test.user" }),
);
assertEquals(res.status, 200);
const body = await res.json();
assertEquals(body.id, "test.user");
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "e2e users: GET /users/:id 404 when not found",
async fn() {
await truncateAll();
const res = await userHandler.GET!(
makeGetRequest("/users/ghost"),
makeEmployeeContext({ id: "ghost" }),
);
assertEquals(res.status, 404);
},
sanitizeResources: false,
sanitizeOps: false,
});
// --- PUT /users/:id ---
Deno.test({
name: "e2e users: PUT /users/:id updates user",
async fn() {
await truncateAll();
const [role] = await seedRoles([{ nom: "employee" }]);
await seedUsers([{
id: "upd.user",
nom: "Old",
prenom: "Name",
idRole: role.id,
}]);
const res = await userHandler.PUT!(
makeJsonRequest("/users/upd.user", "PUT", {
nom: "New",
prenom: "Name",
idRole: role.id,
}),
makeEmployeeContext({ id: "upd.user" }),
);
assertEquals(res.status, 200);
const body = await res.json();
assertEquals(body.nom, "New");
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "e2e users: PUT /users/:id 404 when not found",
async fn() {
await truncateAll();
const res = await userHandler.PUT!(
makeJsonRequest("/users/ghost", "PUT", {
nom: "X",
prenom: "Y",
idRole: 1,
}),
makeEmployeeContext({ id: "ghost" }),
);
assertEquals(res.status, 404);
},
sanitizeResources: false,
sanitizeOps: false,
});
// --- DELETE /users/:id ---
Deno.test({
name: "e2e users: DELETE /users/:id returns 204",
async fn() {
await truncateAll();
const [role] = await seedRoles([{ nom: "employee" }]);
await seedUsers([{
id: "del.user",
nom: "Del",
prenom: "Me",
idRole: role.id,
}]);
const res = await userHandler.DELETE!(
makeGetRequest("/users/del.user"),
makeEmployeeContext({ id: "del.user" }),
);
assertEquals(res.status, 204);
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "e2e users: DELETE /users/:id 404 when not found",
async fn() {
await truncateAll();
const res = await userHandler.DELETE!(
makeGetRequest("/users/ghost"),
makeEmployeeContext({ id: "ghost" }),
);
assertEquals(res.status, 404);
},
sanitizeResources: false,
sanitizeOps: false,
});
+112
View File
@@ -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,
});
+15 -95
View File
@@ -1,24 +1,24 @@
// #111 - Integration tests for /users endpoints
import { assertEquals, assertExists } from "@std/assert";
import {
closeTestPool,
seedRoles,
seedUsers,
testDb,
truncateAll,
} from "../helpers/db_integration.ts";
import { users } from "$root/databases/schema.ts";
import { eq } from "npm:drizzle-orm@0.45.2";
Deno.test({
name: "integration users: list all users",
name: "integration: GET /users - DB round trip",
async fn() {
await truncateAll();
const [role] = await seedRoles([{ nom: "employee" }]);
await seedUsers([
{ id: "dupont.jean", nom: "Dupont", prenom: "Jean", idRole: role.id },
{ id: "martin.alice", nom: "Martin", prenom: "Alice", idRole: role.id },
]);
const rows = await testDb.select().from(users);
assertEquals(rows.length, 2);
assertExists(rows.find((u) => u.id === "dupont.jean"));
@@ -28,110 +28,30 @@ Deno.test({
});
Deno.test({
name: "integration users: filter by idRole",
name: "integration: INSERT user and retrieve by id",
async fn() {
await truncateAll();
const [role1] = await seedRoles([{ nom: "admin" }]);
const [role2] = await seedRoles([{ nom: "employee" }]);
await seedUsers([
{ id: "u1", nom: "A", prenom: "A", idRole: role1.id },
{ id: "u2", nom: "B", prenom: "B", idRole: role2.id },
]);
const rows = await testDb
.select()
.from(users)
.where(eq(users.idRole, role1.id));
assertEquals(rows.length, 1);
assertEquals(rows[0].id, "u1");
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration users: create and retrieve by id",
async fn() {
await truncateAll();
const [role] = await seedRoles([{ nom: "admin" }]);
const [created] = await testDb
.insert(users)
.values({
id: "durand.claire",
nom: "Durand",
prenom: "Claire",
idRole: role.id,
})
.returning();
const [created] = await testDb.insert(users).values({
id: "durand.claire",
nom: "Durand",
prenom: "Claire",
idRole: role.id,
}).returning();
assertExists(created);
assertEquals(created.id, "durand.claire");
const row = await testDb
.select()
.from(users)
.where(eq(users.id, "durand.claire"))
.then((r) => r[0] ?? null);
assertExists(row);
assertEquals(created.nom, "Durand");
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration users: get by id returns null when not found",
name: "integration: cleanup - close pool",
async fn() {
await truncateAll();
const row = await testDb
.select()
.from(users)
.where(eq(users.id, "nonexistent"))
.then((r) => r[0] ?? null);
assertEquals(row, null);
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration users: update user fields",
async fn() {
await truncateAll();
const [role] = await seedRoles([{ nom: "employee" }]);
await seedUsers([{
id: "test.user",
nom: "Test",
prenom: "User",
idRole: role.id,
}]);
const [updated] = await testDb
.update(users)
.set({ nom: "Updated", prenom: "Name" })
.where(eq(users.id, "test.user"))
.returning();
assertExists(updated);
assertEquals(updated.nom, "Updated");
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration users: delete user",
async fn() {
await truncateAll();
const [role] = await seedRoles([{ nom: "employee" }]);
await seedUsers([{
id: "to.delete",
nom: "Del",
prenom: "Me",
idRole: role.id,
}]);
await testDb.delete(users).where(eq(users.id, "to.delete"));
const row = await testDb
.select()
.from(users)
.where(eq(users.id, "to.delete"))
.then((r) => r[0] ?? null);
assertEquals(row, null);
await closeTestPool();
},
sanitizeResources: false,
sanitizeOps: false,
+160
View File
@@ -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);
});
-216
View File
@@ -1,216 +0,0 @@
import { assertEquals } from "@std/assert";
import { getFetchCalls, mockFetch, restoreFetch } from "../helpers/api_mock.ts";
const BASE = "http://localhost/apps/admin/api/users";
const users = [
{ id: "dupont.jean", nom: "Dupont", prenom: "Jean", idRole: 1 },
{ id: "martin.alice", nom: "Martin", prenom: "Alice", idRole: 2 },
];
// --- GET /users ---
Deno.test("GET /users - returns all users", async () => {
mockFetch({ [BASE]: users });
try {
const res = await fetch(BASE);
assertEquals(res.status, 200);
const data = await res.json();
assertEquals(data.length, 2);
assertEquals(data[0].id, "dupont.jean");
} finally {
restoreFetch();
}
});
Deno.test("GET /users - filters by idRole", async () => {
const filtered = users.filter((u) => u.idRole === 1);
mockFetch({ [`${BASE}?idRole=1`]: filtered });
try {
const res = await fetch(`${BASE}?idRole=1`);
assertEquals(res.status, 200);
const data = await res.json();
assertEquals(data.length, 1);
assertEquals(data[0].idRole, 1);
} finally {
restoreFetch();
}
});
// --- POST /users ---
Deno.test("POST /users - creates a user and returns 201", async () => {
const newUser = {
id: "durand.claire",
nom: "Durand",
prenom: "Claire",
idRole: 1,
};
mockFetch({ [BASE]: { method: "POST", status: 201, body: newUser } });
try {
const res = await fetch(BASE, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(newUser),
});
assertEquals(res.status, 201);
const data = await res.json();
assertEquals(data.id, "durand.claire");
assertEquals(data.nom, "Durand");
} finally {
restoreFetch();
}
});
Deno.test("POST /users - returns 409 on duplicate id", async () => {
mockFetch({
[BASE]: {
method: "POST",
status: 409,
body: { error: "Un utilisateur avec cet identifiant existe déjà" },
},
});
try {
const res = await fetch(BASE, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(users[0]),
});
assertEquals(res.status, 409);
const data = await res.json();
assertEquals(typeof data.error, "string");
} finally {
restoreFetch();
}
});
Deno.test("POST /users - returns 400 on missing fields", async () => {
mockFetch({ [BASE]: { method: "POST", status: 400 } });
try {
const res = await fetch(BASE, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ id: "x" }),
});
assertEquals(res.status, 400);
} finally {
restoreFetch();
}
});
// --- GET /users/{id} ---
Deno.test("GET /users/{id} - returns a user by id", async () => {
mockFetch({ [`${BASE}/dupont.jean`]: users[0] });
try {
const res = await fetch(`${BASE}/dupont.jean`);
assertEquals(res.status, 200);
const data = await res.json();
assertEquals(data.id, "dupont.jean");
assertEquals(data.prenom, "Jean");
} finally {
restoreFetch();
}
});
Deno.test("GET /users/{id} - returns 404 for unknown id", async () => {
mockFetch({
[`${BASE}/inconnu`]: {
status: 404,
body: { error: "Ressource introuvable" },
},
});
try {
const res = await fetch(`${BASE}/inconnu`);
assertEquals(res.status, 404);
const data = await res.json();
assertEquals(typeof data.error, "string");
} finally {
restoreFetch();
}
});
// --- PUT /users/{id} ---
Deno.test("PUT /users/{id} - updates a user", async () => {
const updated = { ...users[0], prenom: "Jean-Pierre" };
mockFetch({
[`${BASE}/dupont.jean`]: { method: "PUT", status: 200, body: updated },
});
try {
const res = await fetch(`${BASE}/dupont.jean`, {
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify({ nom: "Dupont", prenom: "Jean-Pierre", idRole: 1 }),
});
assertEquals(res.status, 200);
const data = await res.json();
assertEquals(data.prenom, "Jean-Pierre");
} finally {
restoreFetch();
}
});
Deno.test("PUT /users/{id} - returns 404 for unknown id", async () => {
mockFetch({
[`${BASE}/inconnu`]: {
method: "PUT",
status: 404,
body: { error: "Ressource introuvable" },
},
});
try {
const res = await fetch(`${BASE}/inconnu`, {
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify({ nom: "X", prenom: "Y", idRole: 1 }),
});
assertEquals(res.status, 404);
} finally {
restoreFetch();
}
});
// --- DELETE /users/{id} ---
Deno.test("DELETE /users/{id} - deletes a user and returns 204", async () => {
mockFetch({
[`${BASE}/dupont.jean`]: { method: "DELETE", status: 204 },
});
try {
const res = await fetch(`${BASE}/dupont.jean`, { method: "DELETE" });
assertEquals(res.status, 204);
} finally {
restoreFetch();
}
});
Deno.test("DELETE /users/{id} - returns 404 for unknown id", async () => {
mockFetch({
[`${BASE}/inconnu`]: {
method: "DELETE",
status: 404,
body: { error: "Ressource introuvable" },
},
});
try {
const res = await fetch(`${BASE}/inconnu`, { method: "DELETE" });
assertEquals(res.status, 404);
} finally {
restoreFetch();
}
});
// --- getFetchCalls ---
Deno.test("GET /users - call is tracked", async () => {
mockFetch({ [BASE]: users });
try {
await fetch(BASE);
const calls = getFetchCalls();
assertEquals(calls.length, 1);
assertEquals(calls[0].method, "GET");
} finally {
restoreFetch();
}
});