Compare commits

...

3 Commits

Author SHA1 Message Date
djalim 33a1ec9666 style: fix deno fmt and lint
Check Deno code / Check Deno code (pull_request) Failing after 8s
Tests / Unit tests (pull_request) Successful in 10s
Tests / Integration tests (pull_request) Successful in 54s
2026-04-26 14:18:55 +02:00
djalim 0d2361d7a7 test(users): add integration and e2e tests for /users (#111)
- integration: list, filter by role, create, get, update, delete, not-found
- e2e: handler calls with mock context + real DB, covers 400/409/404 cases
  (unit tests already present from teammates)
2026-04-26 14:13:51 +02:00
djalim ec975fc748 test(users): add unit tests for users API 2026-04-26 14:03:31 +02:00
4 changed files with 568 additions and 20 deletions
+3 -1
View File
@@ -2,7 +2,9 @@ import { defineConfig } from "drizzle-kit";
import process from "node:process"; import process from "node:process";
const url = process.env.DATABASE_URL ?? const url = process.env.DATABASE_URL ??
`postgresql://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASS}@${process.env.POSTGRES_HOST ?? "localhost"}:${process.env.POSTGRES_PORT ?? 5432}/${process.env.POSTGRES_DB}`; `postgresql://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASS}@${
process.env.POSTGRES_HOST ?? "localhost"
}:${process.env.POSTGRES_PORT ?? 5432}/${process.env.POSTGRES_DB}`;
export default defineConfig({ export default defineConfig({
dialect: "postgresql", dialect: "postgresql",
+250
View File
@@ -0,0 +1,250 @@
// #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,
});
+99 -19
View File
@@ -1,24 +1,24 @@
// #111 - Integration tests for /users endpoints
import { assertEquals, assertExists } from "@std/assert"; import { assertEquals, assertExists } from "@std/assert";
import { import {
closeTestPool,
seedRoles, seedRoles,
seedUsers, seedUsers,
testDb, testDb,
truncateAll, truncateAll,
} from "../helpers/db_integration.ts"; } from "../helpers/db_integration.ts";
import { users } from "$root/databases/schema.ts"; import { users } from "$root/databases/schema.ts";
import { eq } from "npm:drizzle-orm@0.45.2";
Deno.test({ Deno.test({
name: "integration: GET /users - DB round trip", name: "integration users: list all users",
async fn() { async fn() {
await truncateAll(); await truncateAll();
const [role] = await seedRoles([{ nom: "employee" }]); const [role] = await seedRoles([{ nom: "employee" }]);
await seedUsers([ await seedUsers([
{ id: "dupont.jean", nom: "Dupont", prenom: "Jean", idRole: role.id }, { id: "dupont.jean", nom: "Dupont", prenom: "Jean", idRole: role.id },
{ id: "martin.alice", nom: "Martin", prenom: "Alice", idRole: role.id }, { id: "martin.alice", nom: "Martin", prenom: "Alice", idRole: role.id },
]); ]);
const rows = await testDb.select().from(users); const rows = await testDb.select().from(users);
assertEquals(rows.length, 2); assertEquals(rows.length, 2);
assertExists(rows.find((u) => u.id === "dupont.jean")); assertExists(rows.find((u) => u.id === "dupont.jean"));
@@ -28,30 +28,110 @@ Deno.test({
}); });
Deno.test({ Deno.test({
name: "integration: INSERT user and retrieve by id", name: "integration users: filter by idRole",
async fn() { async fn() {
await truncateAll(); await truncateAll();
const [role1] = await seedRoles([{ nom: "admin" }]);
const [role] = await seedRoles([{ nom: "admin" }]); const [role2] = await seedRoles([{ nom: "employee" }]);
const [created] = await testDb.insert(users).values({ await seedUsers([
id: "durand.claire", { id: "u1", nom: "A", prenom: "A", idRole: role1.id },
nom: "Durand", { id: "u2", nom: "B", prenom: "B", idRole: role2.id },
prenom: "Claire", ]);
idRole: role.id, const rows = await testDb
}).returning(); .select()
.from(users)
assertExists(created); .where(eq(users.idRole, role1.id));
assertEquals(created.id, "durand.claire"); assertEquals(rows.length, 1);
assertEquals(created.nom, "Durand"); assertEquals(rows[0].id, "u1");
}, },
sanitizeResources: false, sanitizeResources: false,
sanitizeOps: false, sanitizeOps: false,
}); });
Deno.test({ Deno.test({
name: "integration: cleanup - close pool", name: "integration users: create and retrieve by id",
async fn() { async fn() {
await closeTestPool(); 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();
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);
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration users: get by id returns null when not found",
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);
}, },
sanitizeResources: false, sanitizeResources: false,
sanitizeOps: false, sanitizeOps: false,
+216
View File
@@ -0,0 +1,216 @@
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();
}
});