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)
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
// #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,
|
||||
});
|
||||
Reference in New Issue
Block a user