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:
@@ -1,24 +1,24 @@
|
||||
// #111 - Integration tests for /users endpoints
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import {
|
||||
closeTestPool,
|
||||
seedRoles,
|
||||
seedUsers,
|
||||
testDb,
|
||||
truncateAll,
|
||||
} from "../helpers/db_integration.ts";
|
||||
import { users } from "$root/databases/schema.ts";
|
||||
import { eq } from "npm:drizzle-orm@0.45.2";
|
||||
|
||||
Deno.test({
|
||||
name: "integration: GET /users - DB round trip",
|
||||
name: "integration users: list 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 rows = await testDb.select().from(users);
|
||||
assertEquals(rows.length, 2);
|
||||
assertExists(rows.find((u) => u.id === "dupont.jean"));
|
||||
@@ -28,30 +28,95 @@ Deno.test({
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration: INSERT user and retrieve by id",
|
||||
name: "integration users: filter by idRole",
|
||||
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 [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 rows = await testDb
|
||||
.select()
|
||||
.from(users)
|
||||
.where(eq(users.idRole, role1.id));
|
||||
assertEquals(rows.length, 1);
|
||||
assertEquals(rows[0].id, "u1");
|
||||
},
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "integration: cleanup - close pool",
|
||||
name: "integration users: create and retrieve by id",
|
||||
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,
|
||||
sanitizeOps: false,
|
||||
|
||||
Reference in New Issue
Block a user