All checks were successful
PR 2/2 de l'issue #80 — la vue admin en lecture seule des messages remontés via le formulaire de contact (PR 1 = #123). - Contrat partagé : `listContactMessagesQuerySchema` (filtre `type` optionnel) + `types/contact.ts` `ContactMessageAdminView` (`author` = `{ id, firstName, lastName, email } | null`, `null` = compte supprimé). - API : `admin-contact.{routes,service}.ts` — `GET /admin/api/contact?type=` (`requireAdmin`), tri `createdAt desc`, plafond `CONTACT_MESSAGES_PAGE_SIZE = 200` (même esprit que `CORRECTIONS_PAGE_SIZE`). Branché dans `admin.routes.ts` (`adminRouter.use(/contact, …)`). Aucune route d'écriture. - Front : page `/admin/messages` (`MessagesPage` + `.scss`) — tableau lecture seule (date, expéditeur ou « Compte supprimé », badge de type, message, page, user-agent tronqué + titre au survol), un filtre par type. Entrée de nav « Messages » (icône `Inbox`) dans `AdminLayout` + `adminApiClient.getContactMessages` + i18n `admin.nav.messages` / `admin.messages.*`. - Tests : `apps/api/test/admin-contact.test.ts` (Mocha — 401 sans session admin, tri récent d'abord + `author` résolu, filtre `type`, type inconnu → 400, plafond 200, `author: null` après suppression du compte), `apps/web/cypress/e2e/admin-messages.cy.ts` (rendu lecture seule, filtre par type via le query param). - Docs : `backend-architecture.md` (section `admin` + event `contact.submitted` dans la liste). Closes #80. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
180 lines
5.7 KiB
TypeScript
180 lines
5.7 KiB
TypeScript
import { faker } from "@faker-js/faker";
|
|
import { expect } from "chai";
|
|
import request from "supertest";
|
|
import { createApp } from "../src/app.js";
|
|
import { env } from "../src/config/env.js";
|
|
import { prisma } from "../src/db/prisma.js";
|
|
import { hashAdminPassword } from "../src/modules/admin/admin-auth.service.js";
|
|
import { resetDatabase } from "../test-support/reset-db.js";
|
|
|
|
async function seedAdmin(): Promise<{ email: string; password: string }> {
|
|
const email = faker.internet.email().toLowerCase();
|
|
const password = faker.internet.password({ length: 16 });
|
|
await prisma.adminUser.create({
|
|
data: { email, name: faker.person.fullName(), passwordHash: await hashAdminPassword(password) },
|
|
});
|
|
return { email, password };
|
|
}
|
|
|
|
/** Creates a `UserProfile` to attribute contact messages to. */
|
|
async function seedUser() {
|
|
return prisma.userProfile.create({
|
|
data: {
|
|
firstName: faker.person.firstName(),
|
|
lastName: faker.person.lastName(),
|
|
email: `${faker.string.uuid()}@example.test`,
|
|
passwordHash: "x",
|
|
},
|
|
});
|
|
}
|
|
|
|
/** One `ContactMessage`, `createdAt` overridable so ordering is deterministic. */
|
|
async function seedMessage(overrides: {
|
|
authorId: number | null;
|
|
type?: string;
|
|
message?: string;
|
|
createdAt?: Date;
|
|
}) {
|
|
return prisma.contactMessage.create({
|
|
data: {
|
|
authorId: overrides.authorId,
|
|
type: overrides.type ?? "bug",
|
|
message: overrides.message ?? faker.lorem.sentence(),
|
|
pageUrl: "/",
|
|
userAgent: "test-agent/1.0",
|
|
appVersion: "0.2.1",
|
|
...(overrides.createdAt ? { createdAt: overrides.createdAt } : {}),
|
|
},
|
|
});
|
|
}
|
|
|
|
const adminSecretConfigured = env.ADMIN_JWT_SECRET !== undefined;
|
|
|
|
describe("Admin contact messages", () => {
|
|
const app = createApp();
|
|
|
|
beforeEach(async () => {
|
|
await resetDatabase();
|
|
});
|
|
|
|
after(async () => {
|
|
await prisma.$disconnect();
|
|
});
|
|
|
|
async function adminAgent() {
|
|
const { email, password } = await seedAdmin();
|
|
const agent = request.agent(app);
|
|
await agent.post("/admin/api/auth/login").send({ email, password });
|
|
return agent;
|
|
}
|
|
|
|
it("rejects the route without an admin session", async () => {
|
|
const res = await request(app).get("/admin/api/contact");
|
|
expect(res.status).to.equal(401);
|
|
});
|
|
|
|
it("returns messages newest first, with the author resolved", async function () {
|
|
if (!adminSecretConfigured) {
|
|
// biome-ignore lint/suspicious/noExplicitAny: mocha's `this.skip()` isn't typed here.
|
|
(this as any).skip();
|
|
return;
|
|
}
|
|
const user = await seedUser();
|
|
await seedMessage({
|
|
authorId: user.id,
|
|
message: "le plus ancien",
|
|
createdAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
});
|
|
await seedMessage({
|
|
authorId: user.id,
|
|
message: "le plus récent",
|
|
createdAt: new Date("2026-06-01T00:00:00.000Z"),
|
|
});
|
|
|
|
const agent = await adminAgent();
|
|
const res = await agent.get("/admin/api/contact");
|
|
|
|
expect(res.status).to.equal(200);
|
|
expect(res.body.map((row: { message: string }) => row.message)).to.deep.equal([
|
|
"le plus récent",
|
|
"le plus ancien",
|
|
]);
|
|
expect(res.body[0].author).to.deep.equal({
|
|
id: user.id,
|
|
firstName: user.firstName,
|
|
lastName: user.lastName,
|
|
email: user.email,
|
|
});
|
|
});
|
|
|
|
it("filters by type", async function () {
|
|
if (!adminSecretConfigured) {
|
|
// biome-ignore lint/suspicious/noExplicitAny: mocha's `this.skip()` isn't typed here.
|
|
(this as any).skip();
|
|
return;
|
|
}
|
|
const user = await seedUser();
|
|
await seedMessage({ authorId: user.id, type: "bug" });
|
|
await seedMessage({ authorId: user.id, type: "suggestion" });
|
|
await seedMessage({ authorId: user.id, type: "suggestion" });
|
|
|
|
const agent = await adminAgent();
|
|
const res = await agent.get("/admin/api/contact").query({ type: "suggestion" });
|
|
|
|
expect(res.status).to.equal(200);
|
|
expect(res.body).to.have.lengthOf(2);
|
|
expect(res.body.every((row: { type: string }) => row.type === "suggestion")).to.equal(true);
|
|
});
|
|
|
|
it("rejects an unknown type filter with 400", async function () {
|
|
if (!adminSecretConfigured) {
|
|
// biome-ignore lint/suspicious/noExplicitAny: mocha's `this.skip()` isn't typed here.
|
|
(this as any).skip();
|
|
return;
|
|
}
|
|
const agent = await adminAgent();
|
|
const res = await agent.get("/admin/api/contact").query({ type: "complaint" });
|
|
expect(res.status).to.equal(400);
|
|
});
|
|
|
|
it("caps the page at 200 rows", async function () {
|
|
if (!adminSecretConfigured) {
|
|
// biome-ignore lint/suspicious/noExplicitAny: mocha's `this.skip()` isn't typed here.
|
|
(this as any).skip();
|
|
return;
|
|
}
|
|
const user = await seedUser();
|
|
await prisma.contactMessage.createMany({
|
|
data: Array.from({ length: 205 }, () => ({
|
|
authorId: user.id,
|
|
type: "bug",
|
|
message: faker.lorem.sentence(),
|
|
})),
|
|
});
|
|
|
|
const agent = await adminAgent();
|
|
const res = await agent.get("/admin/api/contact");
|
|
|
|
expect(res.status).to.equal(200);
|
|
expect(res.body).to.have.lengthOf(200);
|
|
});
|
|
|
|
it("reports author: null once the sender's account is deleted", async function () {
|
|
if (!adminSecretConfigured) {
|
|
// biome-ignore lint/suspicious/noExplicitAny: mocha's `this.skip()` isn't typed here.
|
|
(this as any).skip();
|
|
return;
|
|
}
|
|
const user = await seedUser();
|
|
await seedMessage({ authorId: user.id, message: "signalé puis compte supprimé" });
|
|
await prisma.userProfile.delete({ where: { id: user.id } });
|
|
|
|
const agent = await adminAgent();
|
|
const res = await agent.get("/admin/api/contact");
|
|
|
|
expect(res.status).to.equal(200);
|
|
expect(res.body).to.have.lengthOf(1);
|
|
expect(res.body[0].message).to.equal("signalé puis compte supprimé");
|
|
expect(res.body[0].author).to.equal(null);
|
|
});
|
|
});
|