batchCooking/apps/api/features/step-definitions/household.steps.ts
Nicolas 3363cfad75 Tests API: couverture Mocha + Cucumber pour le foyer et la suppression de compte (step 4/8)
- house.test.ts réécrit (le foyer n'est plus auto-créé) + POST /house,
  POST /house/join, POST /house/leave, DELETE /house/current,
  DELETE /house/members/:id
- auth.test.ts: signup renvoie houseId=null, DELETE /auth/me (mauvais
  mot de passe, suppression, transfert d'admin)
- planning.test.ts/steps.ts: création explicite du foyer (POST /house)
- household.feature: scénarios créer/rejoindre/quitter/supprimer/
  retirer un membre, via un second agent (CustomWorld.secondAgent)
- auth.feature: scénarios de suppression de compte
2026-08-17 10:40:23 +02:00

90 lines
3.5 KiB
TypeScript

import assert from "node:assert/strict";
import { ErrorCode } from "@batch-cooking/shared";
import { Given, Then, When } from "@cucumber/cucumber";
import type { CustomWorld } from "../support/world.js";
Given("I have a household named {string}", async function (this: CustomWorld, name: string) {
const res = await this.agent.post("/house").send({ name });
assert.equal(res.status, 201, JSON.stringify(res.body));
});
When("I create a household named {string}", async function (this: CustomWorld, name: string) {
this.response = await this.agent.post("/house").send({ name });
});
When("I rename my household to {string}", async function (this: CustomWorld, name: string) {
this.response = await this.agent.patch("/house/current").send({ name });
});
When("I leave the household", async function (this: CustomWorld) {
this.response = await this.agent.post("/house/leave");
});
When("I remove the second user from my household", async function (this: CustomWorld) {
const secondMe = await this.secondAgent.get("/auth/me");
this.response = await this.agent.delete(`/house/members/${secondMe.body.id}`);
});
Then("my household should be named {string}", async function (this: CustomWorld, name: string) {
const res = await this.agent.get("/house/current");
assert.equal(res.body.name, name);
});
// --- Steps involving a second, independently signed-in user ---------------
// The first ("a profile already exists with email ...") step is reused
// as-is for the second user too — it just inserts a row, agent-agnostic.
When(
"the second user logs in with email {string} and password {string}",
async function (this: CustomWorld, email: string, password: string) {
this.secondResponse = await this.secondAgent.post("/auth/login").send({ email, password });
},
);
When(
"the second user joins my household using its invite code",
async function (this: CustomWorld) {
const house = await this.agent.get("/house/current");
this.secondResponse = await this.secondAgent
.post("/house/join")
.send({ inviteCode: house.body.inviteCode });
},
);
When("the second user tries to delete the household", async function (this: CustomWorld) {
this.secondResponse = await this.secondAgent.delete("/house/current");
});
Then(
"the second user's response status should be {int}",
function (this: CustomWorld, status: number) {
assert.equal(this.secondResponse.status, status);
},
);
Then(
"the second user's 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.secondResponse.body.code, expected);
},
);
Then("the second user should be a member of my household", async function (this: CustomWorld) {
const house = await this.agent.get("/house/current");
const secondMe = await this.secondAgent.get("/auth/me");
const memberIds = (house.body.members as Array<{ id: number }>).map((member) => member.id);
assert.ok(memberIds.includes(secondMe.body.id));
});
Then("the second user should be the household's admin", async function (this: CustomWorld) {
const secondMe = await this.secondAgent.get("/auth/me");
const house = await this.secondAgent.get("/house/current");
assert.equal(house.body.adminId, secondMe.body.id);
});
Then("the second user should have no household", async function (this: CustomWorld) {
const secondMe = await this.secondAgent.get("/auth/me");
assert.equal(secondMe.body.houseId, null);
});