test : changed test format + added playwright support
Check Deno code / Check Deno code (pull_request) Has been cancelled
Tests / Unit tests (pull_request) Has been cancelled
Tests / Integration tests (pull_request) Has been cancelled
Check Deno code / Check Deno code (push) Has been cancelled
Tests / Unit tests (push) Has been cancelled
Tests / Integration tests (push) Has been cancelled
Check Deno code / Check Deno code (pull_request) Has been cancelled
Tests / Unit tests (pull_request) Has been cancelled
Tests / Integration tests (pull_request) Has been cancelled
Check Deno code / Check Deno code (push) Has been cancelled
Tests / Unit tests (push) Has been cancelled
Tests / Integration tests (push) Has been cancelled
This commit was merged in pull request #153.
This commit is contained in:
+121
-69
@@ -1,122 +1,174 @@
|
||||
// #112 - Integration tests for /roles endpoints
|
||||
// #112 - E2E tests for /roles endpoints
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import {
|
||||
makeEmployeeContext,
|
||||
makeGetRequest,
|
||||
makeJsonRequest,
|
||||
} from "../helpers/handler.ts";
|
||||
import { seedRoles, testDb, truncateAll } from "../helpers/db_integration.ts";
|
||||
import { permissions, rolePermissions, roles } from "$root/databases/schema.ts";
|
||||
import { eq } from "npm:drizzle-orm@0.45.2";
|
||||
import { permissions } from "$root/databases/schema.ts";
|
||||
import { handler as rolesHandler } from "$apps/admin/api/roles.ts";
|
||||
import { handler as roleHandler } from "$apps/admin/api/roles/[idRole].ts";
|
||||
|
||||
// --- GET /roles ---
|
||||
|
||||
Deno.test({
|
||||
name: "integration roles: list all roles",
|
||||
name: "e2e roles: GET /roles returns all with permissions",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
await seedRoles([{ nom: "admin" }, { nom: "employee" }]);
|
||||
const rows = await testDb.select().from(roles);
|
||||
assertEquals(rows.length, 2);
|
||||
const res = await rolesHandler.GET!(
|
||||
makeGetRequest("/roles"),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.length, 2);
|
||||
assertExists(body[0].permissions);
|
||||
assertEquals(Array.isArray(body[0].permissions), true);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- POST /roles ---
|
||||
|
||||
Deno.test({
|
||||
name: "integration roles: create and retrieve by id",
|
||||
name: "e2e roles: POST /roles creates role (201)",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [created] = await testDb.insert(roles).values({ nom: "viewer" })
|
||||
.returning();
|
||||
assertExists(created.id);
|
||||
assertEquals(created.nom, "viewer");
|
||||
const row = await testDb
|
||||
.select()
|
||||
.from(roles)
|
||||
.where(eq(roles.id, created.id))
|
||||
.then((r) => r[0] ?? null);
|
||||
assertExists(row);
|
||||
const res = await rolesHandler.POST!(
|
||||
makeJsonRequest("/roles", "POST", { nom: "viewer" }),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 201);
|
||||
const body = await res.json();
|
||||
assertExists(body.id);
|
||||
assertEquals(body.nom, "viewer");
|
||||
assertEquals(body.permissions, []);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration roles: assign and retrieve permissions",
|
||||
name: "e2e roles: POST /roles 400 on missing nom",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await rolesHandler.POST!(
|
||||
makeJsonRequest("/roles", "POST", {}),
|
||||
makeEmployeeContext(),
|
||||
);
|
||||
assertEquals(res.status, 400);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- GET /roles/:id ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: GET /roles/:id returns role with permissions",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [role] = await seedRoles([{ nom: "admin" }]);
|
||||
await testDb.insert(permissions).values([
|
||||
{ id: "student_read", nom: "Consulter les élèves" },
|
||||
{ id: "student_write", nom: "Gérer les élèves" },
|
||||
]);
|
||||
await testDb.insert(rolePermissions).values([
|
||||
{ idRole: role.id, idPermission: "student_read" },
|
||||
{ idRole: role.id, idPermission: "student_write" },
|
||||
]);
|
||||
const perms = await testDb
|
||||
.select()
|
||||
.from(rolePermissions)
|
||||
.where(eq(rolePermissions.idRole, role.id));
|
||||
assertEquals(perms.length, 2);
|
||||
const res = await roleHandler.GET!(
|
||||
makeGetRequest(`/roles/${role.id}`),
|
||||
makeEmployeeContext({ idRole: String(role.id) }),
|
||||
);
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.nom, "admin");
|
||||
assertEquals(Array.isArray(body.permissions), true);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration roles: update role nom",
|
||||
name: "e2e roles: GET /roles/:id 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await roleHandler.GET!(
|
||||
makeGetRequest("/roles/9999"),
|
||||
makeEmployeeContext({ idRole: "9999" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- PUT /roles/:id ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: PUT /roles/:id updates nom and permissions",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [role] = await seedRoles([{ nom: "employee" }]);
|
||||
const [updated] = await testDb
|
||||
.update(roles)
|
||||
.set({ nom: "teacher" })
|
||||
.where(eq(roles.id, role.id))
|
||||
.returning();
|
||||
assertEquals(updated.nom, "teacher");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration roles: reset permissions on update",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [role] = await seedRoles([{ nom: "admin" }]);
|
||||
await testDb.insert(permissions).values([
|
||||
{ id: "note_read", nom: "Consulter les notes" },
|
||||
{ id: "note_write", nom: "Gérer les notes" },
|
||||
]);
|
||||
await testDb.insert(rolePermissions).values([
|
||||
{ idRole: role.id, idPermission: "note_read" },
|
||||
]);
|
||||
// reset
|
||||
await testDb.delete(rolePermissions).where(
|
||||
eq(rolePermissions.idRole, role.id),
|
||||
const res = await roleHandler.PUT!(
|
||||
makeJsonRequest(`/roles/${role.id}`, "PUT", {
|
||||
nom: "teacher",
|
||||
permissions: ["note_read"],
|
||||
}),
|
||||
makeEmployeeContext({ idRole: String(role.id) }),
|
||||
);
|
||||
await testDb.insert(rolePermissions).values([
|
||||
{ idRole: role.id, idPermission: "note_write" },
|
||||
]);
|
||||
const perms = await testDb
|
||||
.select()
|
||||
.from(rolePermissions)
|
||||
.where(eq(rolePermissions.idRole, role.id));
|
||||
assertEquals(perms.length, 1);
|
||||
assertEquals(perms[0].idPermission, "note_write");
|
||||
assertEquals(res.status, 200);
|
||||
const body = await res.json();
|
||||
assertEquals(body.nom, "teacher");
|
||||
assertEquals(body.permissions, ["note_read"]);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration roles: delete role removes it",
|
||||
name: "e2e roles: PUT /roles/:id 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await roleHandler.PUT!(
|
||||
makeJsonRequest("/roles/9999", "PUT", { nom: "ghost", permissions: [] }),
|
||||
makeEmployeeContext({ idRole: "9999" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
// --- DELETE /roles/:id ---
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: DELETE /roles/:id returns 204",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const [role] = await seedRoles([{ nom: "moderator" }]);
|
||||
await testDb.delete(roles).where(eq(roles.id, role.id));
|
||||
const row = await testDb
|
||||
.select()
|
||||
.from(roles)
|
||||
.where(eq(roles.id, role.id))
|
||||
.then((r) => r[0] ?? null);
|
||||
assertEquals(row, null);
|
||||
const res = await roleHandler.DELETE!(
|
||||
makeGetRequest(`/roles/${role.id}`),
|
||||
makeEmployeeContext({ idRole: String(role.id) }),
|
||||
);
|
||||
assertEquals(res.status, 204);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "e2e roles: DELETE /roles/:id 404 when not found",
|
||||
async fn() {
|
||||
await truncateAll();
|
||||
const res = await roleHandler.DELETE!(
|
||||
makeGetRequest("/roles/9999"),
|
||||
makeEmployeeContext({ idRole: "9999" }),
|
||||
);
|
||||
assertEquals(res.status, 404);
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
|
||||
Reference in New Issue
Block a user