test : changed test format + added playwright support
This commit is contained in:
+208
-27
@@ -1,57 +1,238 @@
|
||||
// E2E tests for /users endpoints — handler + real DB
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import {
|
||||
closeTestPool,
|
||||
makeEmployeeContext,
|
||||
makeGetRequest,
|
||||
makeJsonRequest,
|
||||
} from "../helpers/handler.ts";
|
||||
import {
|
||||
seedRoles,
|
||||
seedUsers,
|
||||
testDb,
|
||||
truncateAll,
|
||||
} from "../helpers/db_integration.ts";
|
||||
import { users } from "$root/databases/schema.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: "integration: GET /users - DB round trip",
|
||||
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 },
|
||||
{ id: "dupont.jean", nom: "Dupont", prenom: "Jean" },
|
||||
{ id: "martin.alice", nom: "Martin", prenom: "Alice" },
|
||||
]);
|
||||
|
||||
const rows = await testDb.select().from(users);
|
||||
assertEquals(rows.length, 2);
|
||||
assertExists(rows.find((u) => u.id === "dupont.jean"));
|
||||
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: "integration: INSERT user and retrieve by id",
|
||||
name: "e2e users: GET /users returns empty when no users",
|
||||
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();
|
||||
|
||||
assertExists(created);
|
||||
assertEquals(created.id, "durand.claire");
|
||||
assertEquals(created.nom, "Durand");
|
||||
const res = await usersHandler.GET!(
|
||||
makeGetRequest("/users"),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.length, 0);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration: cleanup - close pool",
|
||||
name: "e2e users: GET /users?idRole filters by role",
|
||||
async fn() {
|
||||
await closeTestPool();
|
||||
await truncateAll();
|
||||
const [role1] = await seedRoles([{ nom: "admin" }]);
|
||||
const [role2] = await seedRoles([{ nom: "employee" }]);
|
||||
await seedUsers([
|
||||
{ id: "admin.user", nom: "Admin", prenom: "User", idRole: role1.id },
|
||||
{ id: "emp.user", nom: "Emp", prenom: "User", 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, "admin.user");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- POST /users ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e users: POST /users creates user (201)",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await usersHandler.POST!(
|
||||
makeJsonRequest("/users", "POST", {
|
||||
id: "new.user",
|
||||
nom: "New",
|
||||
prenom: "User",
|
||||
}),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 201);
|
||||
const body = await res.json();
|
||||
assertEquals(body.id, "new.user");
|
||||
assertEquals(body.nom, "New");
|
||||
},
|
||||
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,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e users: POST /users 409 on duplicate id",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedUsers([{ id: "dupont.jean", nom: "Dupont", prenom: "Jean" }]);
|
||||
const res = await usersHandler.POST!(
|
||||
makeJsonRequest("/users", "POST", {
|
||||
id: "dupont.jean",
|
||||
nom: "Doublon",
|
||||
prenom: "X",
|
||||
}),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 409);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- GET /users/:id ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e users: GET /users/:id returns user",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedUsers([{ id: "bernard.lucie", nom: "Bernard", prenom: "Lucie" }]);
|
||||
const res = await userHandler.GET!(
|
||||
makeGetRequest("/users/bernard.lucie"),
|
||||
makeEmployeeContext({ id: "bernard.lucie" }),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.id, "bernard.lucie");
|
||||
assertEquals(body.nom, "Bernard");
|
||||
},
|
||||
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.user"),
|
||||
makeEmployeeContext({ id: "ghost.user" }),
|
||||
);
|
||||
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();
|
||||
await seedUsers([{ id: "thomas.eva", nom: "Thomas", prenom: "Eva" }]);
|
||||
const res = await userHandler.PUT!(
|
||||
makeJsonRequest("/users/thomas.eva", "PUT", {
|
||||
nom: "Thomas-Modifié",
|
||||
prenom: "Eva",
|
||||
idRole: null,
|
||||
}),
|
||||
makeEmployeeContext({ id: "thomas.eva" }),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.nom, "Thomas-Modifié");
|
||||
},
|
||||
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.user", "PUT", {
|
||||
nom: "X",
|
||||
prenom: "Y",
|
||||
idRole: null,
|
||||
}),
|
||||
makeEmployeeContext({ id: "ghost.user" }),
|
||||
);
|
||||
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();
|
||||
await seedUsers([{ id: "petit.hugo", nom: "Petit", prenom: "Hugo" }]);
|
||||
const res = await userHandler.DELETE!(
|
||||
makeGetRequest("/users/petit.hugo"),
|
||||
makeEmployeeContext({ id: "petit.hugo" }),
|
||||
);
|
||||
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.user"),
|
||||
makeEmployeeContext({ id: "ghost.user" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
|
||||
Reference in New Issue
Block a user