diff --git a/api/src/__tests__/application.test.ts b/api/src/__tests__/application.test.ts index 1f12a958b3..decd1472d0 100644 --- a/api/src/__tests__/application.test.ts +++ b/api/src/__tests__/application.test.ts @@ -4,7 +4,7 @@ import getNewMissionFixture from "./fixtures/mission"; import { getNewReferentFixture } from "./fixtures/referent"; import getNewYoungFixture from "./fixtures/young"; import getNewCohortFixture from "./fixtures/cohort"; -import getAppHelper from "./helpers/app"; +import getAppHelper, { resetAppAuth } from "./helpers/app"; import { createApplication, notExistingApplicationId } from "./helpers/application"; import { dbConnect, dbClose } from "./helpers/db"; import { notExisitingMissionId, createMissionHelper, getMissionByIdHelper } from "./helpers/mission"; @@ -24,6 +24,7 @@ beforeAll(() => dbConnect(__filename.slice(__dirname.length + 1, -3))); afterAll(dbClose); describe("Application", () => { + afterEach(resetAppAuth); describe("POST /application", () => { it("should return 404 when young is not found", async () => { const application = getNewApplicationFixture(); @@ -41,30 +42,25 @@ describe("Application", () => { expect(res.status).toBe(404); }); it("should only allow young to apply for themselves", async () => { - const lastYear = new Date().getFullYear() - 1; const cohort = await createCohortHelper(getNewCohortFixture({ name: "Test" })); const young = await createYoungHelper(getNewYoungFixture({ cohort: cohort.name, cohortId: cohort._id, statusPhase1: YOUNG_STATUS_PHASE1.DONE })); const secondYoung = await createYoungHelper(getNewYoungFixture({ cohort: cohort.name, cohortId: cohort._id, statusPhase1: YOUNG_STATUS_PHASE1.DONE })); - const passport = require("passport"); - const previous = passport.user; - passport.user = young; + const mission = await createMissionHelper(getNewMissionFixture()); const application = getNewApplicationFixture(); // Successful application - let res = await request(getAppHelper()) + let res = await request(getAppHelper(young)) .post("/application") .send({ ...application, youngId: young._id, missionId: mission._id }); expect(res.status).toBe(200); expect(res.body.data.youngId).toBe(young._id.toString()); // Failed application - res = await request(getAppHelper()) + res = await request(getAppHelper(young)) .post("/application") .send({ ...application, youngId: secondYoung._id, missionId: mission._id }); expect(res.status).toBe(400); - - passport.user = previous; }); it("should create an application when priority is given", async () => { const young = await createYoungHelper(getNewYoungFixture({ cohort: "Test", statusPhase1: YOUNG_STATUS_PHASE1.DONE })); @@ -118,45 +114,32 @@ describe("Application", () => { const year = new Date().getFullYear(); const cohort = await createCohortHelper(getNewCohortFixture({ name: "Test" })); const young = await createYoungHelper(getNewYoungFixture({ cohort: cohort.name, cohortId: cohort._id, birthdateAt: new Date(`${year - 12}-01-01T00:00:00.000Z`) })); - const passport = require("passport"); - const previous = passport.user; - passport.user = young; const mission = await createMissionHelper(getNewMissionFixture()); const application = getNewApplicationFixture(); - const res = await request(getAppHelper()) + const res = await request(getAppHelper(young)) .post("/application") .send({ ...application, youngId: young._id, missionId: mission._id }); expect(res.status).toBe(403); - passport.user = previous; }); it("should return 403 when cohort is too old", async () => { const cohort = await createCohortHelper(getNewCohortFixture({ name: "2019" })); const young = await createYoungHelper(getNewYoungFixture({ cohort: cohort.name, cohortId: cohort._id })); - const passport = require("passport"); - const previous = passport.user; - passport.user = young; const mission = await createMissionHelper(getNewMissionFixture()); const application = getNewApplicationFixture(); - const res = await request(getAppHelper()) + const res = await request(getAppHelper(young)) .post("/application") .send({ ...application, youngId: young._id, missionId: mission._id }); expect(res.status).toBe(403); - passport.user = previous; }); it("should return 403 when young has not finished phase1", async () => { - const year = new Date().getFullYear(); const cohort = await createCohortHelper(getNewCohortFixture({ name: "Test" })); const young = await createYoungHelper(getNewYoungFixture({ cohort: cohort.name, cohortId: cohort._id, statusPhase1: YOUNG_STATUS_PHASE1.WAITING_AFFECTATION })); - const passport = require("passport"); - const previous = passport.user; - passport.user = young; const mission = await createMissionHelper(getNewMissionFixture()); const application = getNewApplicationFixture(); - const res = await request(getAppHelper()) + const res = await request(getAppHelper(young)) .post("/application") .send({ ...application, youngId: young._id, missionId: mission._id }); expect(res.status).toBe(403); - passport.user = previous; }); }); @@ -172,9 +155,6 @@ describe("Application", () => { it("should only allow young to update for themselves", async () => { const young = await createYoungHelper(getNewYoungFixture()); const secondYoung = await createYoungHelper(getNewYoungFixture()); - const passport = require("passport"); - const previous = passport.user; - passport.user = young; const mission = await createMissionHelper(getNewMissionFixture()); const application = await createApplication({ ...getNewApplicationFixture(), youngId: young._id, missionId: mission._id }); const secondApplication = await createApplication({ ...getNewApplicationFixture(), youngId: secondYoung._id, missionId: mission._id }); @@ -184,14 +164,12 @@ describe("Application", () => { expect(res.status).toBe(200); // Failed update (not allowed) - res = await request(getAppHelper()).put("/application").send({ priority: "1", status: "DONE", _id: secondApplication._id.toString() }); + res = await request(getAppHelper(young)).put("/application").send({ priority: "1", status: "DONE", _id: secondApplication._id.toString() }); expect(res.status).toBe(403); // Failed update (wrong young id) - res = await request(getAppHelper()).put("/application").send({ priority: "1", status: "DONE", _id: application._id.toString(), youngId: secondYoung._id }); + res = await request(getAppHelper(young)).put("/application").send({ priority: "1", status: "DONE", _id: application._id.toString(), youngId: secondYoung._id }); expect(res.status).toBe(400); - - passport.user = previous; }); it("should update young phase2NumberHoursEstimated and phase2NumberHoursDone", async () => { @@ -226,9 +204,8 @@ describe("Application", () => { expect(res.body.data.status).toBe("WAITING_VALIDATION"); }); it("should be only accessible by referent", async () => { - const passport = require("passport"); await request(getAppHelper()).put(`/application/${notExistingApplicationId}`).send(); - expect(passport.lastTypeCalledOnAuthenticate).toEqual("referent"); + expect(require("passport").lastTypeCalledOnAuthenticate).toEqual("referent"); }); }); @@ -281,19 +258,13 @@ describe("Application", () => { const secondYoung = await createYoungHelper(getNewYoungFixture()); const secondApplication = await createApplication({ ...getNewApplicationFixture(), youngId: secondYoung._id, missionId: mission._id }); - const passport = require("passport"); - const previous = passport.user; - passport.user = young; - // Successful request - let res = await request(getAppHelper()).post(`/application/${application._id}/notify/${SENDINBLUE_TEMPLATES.referent.NEW_APPLICATION}`).send({}); + let res = await request(getAppHelper(young)).post(`/application/${application._id}/notify/${SENDINBLUE_TEMPLATES.referent.NEW_APPLICATION}`).send({}); expect(res.status).toBe(200); // Failed request (not allowed) - res = await request(getAppHelper()).post(`/application/${secondApplication._id}/notify/${SENDINBLUE_TEMPLATES.referent.NEW_APPLICATION}`).send({}); + res = await request(getAppHelper(young)).post(`/application/${secondApplication._id}/notify/${SENDINBLUE_TEMPLATES.referent.NEW_APPLICATION}`).send({}); expect(res.status).toBe(403); - - passport.user = previous; }); }); }); diff --git a/api/src/__tests__/helpers/app.ts b/api/src/__tests__/helpers/app.ts index 854be65261..176fc9159d 100644 --- a/api/src/__tests__/helpers/app.ts +++ b/api/src/__tests__/helpers/app.ts @@ -3,11 +3,23 @@ import express from "express"; import cookieParser from "cookie-parser"; import bodyParser from "body-parser"; import passport from "passport"; +import { Types } from "mongoose"; +const { ObjectId } = Types; import { injectRoutes } from "../../routes"; import { UserRequest } from "../../controllers/request"; +import getNewReferentFixture from "../fixtures/referent"; +import { isReferent, isYoung } from "../../utils"; +import { ReferentDocument, YoungDocument } from "../../models"; -function getAppHelper(user?: Partial | null, authStrategy?: "young" | "referent") { +export function resetAppAuth() { + // @ts-ignore + passport.user = getNewReferentFixture(); + // @ts-ignore + passport.user._id = new ObjectId(); +} + +function getAppHelper(user?: Partial | YoungDocument | ReferentDocument | null, authStrategy?: "young" | "referent") { const app = express(); app.use(bodyParser.json()); app.use(bodyParser.text({ type: "application/x-ndjson" })); @@ -22,8 +34,14 @@ function getAppHelper(user?: Partial | // @ts-ignore passport.user = { _id: "123" }; } - // @ts-ignore - passport.user = { ...passport.user, ...user }; + // instance of model + if (isYoung(user) || isReferent(user)) { + // @ts-ignore + passport.user = user; + } else { + // @ts-ignore + passport.user = { ...passport.user, ...user }; + } } if (authStrategy) { // @ts-ignore