batchCooking/apps/api/features/step-definitions/health.steps.ts
Nicolas 1d03effc77 API: endpoints foyer/profil (nom, régime, allergènes) (step 2/6)
- GET/PATCH /house/current — renomme le foyer de l'utilisateur connecté.
  PATCH avec houseId null -> 404 HOUSE_NOT_FOUND.
- PATCH /profile/diet { dietId: number | null } — régime du profil ;
  null l'efface (étape skippable du parcours). dietId invalide ->
  404 DIET_NOT_FOUND.
- GET/PATCH /profile/allergies — allergènes/intolérances, liste d'IDs ;
  PATCH remplace l'ensemble complet (pas une fusion, cohérent avec un
  multi-select). ID invalide -> 404 ALLERGY_NOT_FOUND.
- 3 nouveaux ErrorCode (4041-4043) + libellés fr.
- Extraction de toSafeProfile() dans src/lib/safe-profile.ts —
  auparavant dupliqué dans auth.service.ts et require-auth.ts,
  profile.service.ts le réutilise aussi.
- Tests Mocha (28 passing) + Cucumber (15 scenarios) — même convention
  que le reste, doc README.

Deuxième commit de la feature profil/foyer/régime/allergènes —
composants front partagés dans le commit suivant.
2026-08-16 23:18:46 +02:00

38 lines
1.6 KiB
TypeScript

import assert from "node:assert/strict";
import { ErrorCode } from "@batch-cooking/shared";
import { Then, When } from "@cucumber/cucumber";
import request from "supertest";
import type { CustomWorld } from "../support/world.js";
When("I send a GET request to {string}", async function (this: CustomWorld, path: string) {
this.response = await request(this.app).get(path);
});
When(
"I send a PATCH request to {string} with body:",
async function (this: CustomWorld, path: string, body: string) {
this.response = await request(this.app).patch(path).send(JSON.parse(body));
},
);
Then("the response status should be {int}", function (this: CustomWorld, status: number) {
assert.equal(this.response.status, status);
});
Then("the response body should be:", function (this: CustomWorld, expectedJson: string) {
assert.deepEqual(this.response.body, JSON.parse(expectedJson));
});
// Generic enough to be reused by any feature asserting on the shared
// ApiErrorResponse contract's `code` field — not health-specific, but this
// file is where the other generic response-assertion steps already live.
//
// `code` here is the enum *member name* (readable in the .feature file,
// e.g. "EMAIL_ALREADY_IN_USE") — ErrorCode[name] resolves it to the real
// numeric value via TypeScript's reverse enum lookup, so this never
// compares against a hardcoded number.
Then("the response error code should be {string}", function (this: CustomWorld, code: string) {
const expected = ErrorCode[code as keyof typeof ErrorCode];
assert.notEqual(expected, undefined, `Unknown ErrorCode member: "${code}"`);
assert.equal(this.response.body.code, expected);
});