Files
djalim f038e4020b
Check Deno code / Check Deno code (pull_request) Successful in 6s
Tests / Unit tests (pull_request) Successful in 11s
Tests / Integration tests (pull_request) Successful in 59s
Check Deno code / Check Deno code (push) Successful in 6s
Tests / Unit tests (push) Successful in 11s
Tests / Integration tests (push) Successful in 1m2s
style: fix deno fmt and lint
2026-04-26 13:34:09 +00:00

124 lines
3.6 KiB
TypeScript

// #112 - Integration tests for /roles endpoints
import { assertEquals, assertExists } from "@std/assert";
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";
Deno.test({
name: "integration roles: list all roles",
async fn() {
await truncateAll();
await seedRoles([{ nom: "admin" }, { nom: "employee" }]);
const rows = await testDb.select().from(roles);
assertEquals(rows.length, 2);
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration roles: create and retrieve by id",
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);
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration roles: assign and retrieve 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);
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration roles: update role nom",
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),
);
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");
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration roles: delete role removes it",
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);
},
sanitizeResources: false,
sanitizeOps: false,
});