Skip to content

Commit

Permalink
feat(eig): add tests (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminDNUM authored Oct 16, 2024
2 parents 6414103 + 728204d commit ad8fb5d
Show file tree
Hide file tree
Showing 20 changed files with 1,185 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/eig/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = async (req, res, next) => {
log.i("IN", { body: req.body });
const { parametre } = req.body;

if (!parametre.declarationId) {
if (!parametre?.declarationId) {
return res.status(400).send({
errors: "Le champs declarationId est obligatoire",
name: "UnexpectedError",
Expand Down
12 changes: 6 additions & 6 deletions packages/backend/src/controllers/eig/depose.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,33 @@ module.exports = async (req, res, next) => {
emailsDDETS?.length &&
(await Send(
MailUtils.bo.eig.sendToDDETS({
dest: emailsDDETS,
communeName,
declarationSejour: ds,
departementName,
dest: emailsDDETS,
eig,
declarationSejour: ds,
regionName,
communeName,
}),
));

emailsDREETS?.length > 0 &&
(await Send(
MailUtils.bo.eig.sendToDREETS({
communeName,
declarationSejour: ds,
departementName,
dest: emailsDREETS,
eig,
declarationSejour: ds,
communeName,
}),
));

emailsOrganisateur?.length > 0 &&
(await Send(
MailUtils.bo.eig.sendToOrganisme({
declarationSejour: ds,
dest: [...emailsOrganisateur, ds.responsableSejour.email],
eig,
userName,
declarationSejour: ds,
}),
));

Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/eig/mark-as-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ module.exports = async function markAsRead(req, res, next) {
MailUtils.bo.eig.sendMarkAsRead({
dest: destinataires,
eig,
typeReader,
territoireCode,
territoireName: territoireName.text,
typeReader,
}),
));
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/controllers/eig/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ module.exports = async (req, res, next) => {
switch (type) {
case UpdateTypes.DECLARATION_SEJOUR:
updatedEigId = await eigService.updateDS(eigId, {
date: eig.date,
declarationId: eig.declarationId,
departement: eig.departement,
date: eig.date,
});
break;
case UpdateTypes.TYPE_EVENEMENT:
Expand Down
70 changes: 70 additions & 0 deletions packages/backend/src/helpers/__tests__/eig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const { isDeclarationligibleToEig } = require("../eig");
const { statuts: dsStatuts } = require("../ds-statuts");
const { expect } = require("@playwright/test");

describe("isDeclarationligibleToEig", () => {
const declaration = {
dateDebut: "2024-09-01",
dateFin: "2024-09-07",
id: 26,
libelle: "declaration1",
};

const allStatuts = Object.values(dsStatuts);

const okStatuts = [
dsStatuts.SEJOUR_EN_COURS,
dsStatuts.VALIDEE_8J,
dsStatuts.TERMINEE,
];

const badStatus = allStatuts.filter((s) => !okStatuts.includes(s));

it.each(okStatuts)(
"should return true for statut %p and good date range",
(statut) => {
declaration.statut = statut;
jest.useFakeTimers().setSystemTime(new Date("2024-09-01"));
expect(isDeclarationligibleToEig(declaration)).toBe(true);

jest.useFakeTimers().setSystemTime(new Date("2024-09-06"));
expect(isDeclarationligibleToEig(declaration)).toBe(true);

jest.useFakeTimers().setSystemTime(new Date("2024-09-07"));
expect(isDeclarationligibleToEig(declaration)).toBe(true);

jest.useFakeTimers().setSystemTime(new Date("2024-09-14"));
expect(isDeclarationligibleToEig(declaration)).toBe(true);
},
);

it.each(badStatus)(
"should return false for statut %p and good date range",
(statut) => {
declaration.statut = statut;
jest.useFakeTimers().setSystemTime(new Date("2024-09-01"));
expect(isDeclarationligibleToEig(declaration)).toBe(false);

jest.useFakeTimers().setSystemTime(new Date("2024-09-06"));
expect(isDeclarationligibleToEig(declaration)).toBe(false);

jest.useFakeTimers().setSystemTime(new Date("2024-09-07"));
expect(isDeclarationligibleToEig(declaration)).toBe(false);

jest.useFakeTimers().setSystemTime(new Date("2024-09-14"));
expect(isDeclarationligibleToEig(declaration)).toBe(false);
},
);

it.each(allStatuts)(
"should return false for statut %p and bad date range",
(statut) => {
declaration.statut = statut;
jest.useFakeTimers().setSystemTime(new Date("2024-08-31"));
expect(isDeclarationligibleToEig(declaration)).toBe(false);

jest.useFakeTimers().setSystemTime(new Date("2024-09-15"));
expect(isDeclarationligibleToEig(declaration)).toBe(false);
},
);
});
60 changes: 60 additions & 0 deletions packages/backend/src/routes/__tests__/eig/delete.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const checkJWT = require("../../../middlewares/checkJWT");
const checkPermissionEIG = require("../../../middlewares/checkPermissionEIG");
const canUpdateEig = require("../../../middlewares/can-update-or-delete-eig");
const AppError = require("../../../utils/error");
const request = require("supertest");
const app = require("../../../app");
const eigService = require("../../../services/eig");

jest.mock("../../../middlewares/checkJWT");
jest.mock("../../../middlewares/checkPermissionEIG");
jest.mock("../../../middlewares/can-update-or-delete-eig");
jest.mock("../../../services/eig");
jest.mock("../../../services/DemandeSejour");
jest.mock("../../../helpers/eigMail");
jest.mock("../../../utils/mail");
jest.mock("../../../services/geo/Commune");
jest.mock("../../../services/mail");

describe("DELETE /eig/:id", () => {
const user = {
id: 1,
};
beforeEach(() => {
jest.clearAllMocks();
checkJWT.mockImplementation((req, res, next) => {
req.decoded = { ...user };
next();
});
checkPermissionEIG.mockImplementation((req, res, next) => {
next();
});
canUpdateEig.mockImplementation((req, res, next) => {
next();
});
});

afterEach(() => {
jest.clearAllMocks();
});

it("should return an error 403 if you don't have permission for eig", async () => {
checkPermissionEIG.mockImplementation((req, res, next) => {
return next(
new AppError("Vous n'êtes pas autorisé à accéder à cet EIG", {
statusCode: 403,
}),
);
});

const response = await request(app).delete("/eig/1");
expect(response.statusCode).toBe(403);
expect(eigService.delete).not.toHaveBeenCalled();
});

it("should depose an eig if everything is ok", async () => {
const response = await request(app).delete("/eig/1");
expect(response.statusCode).toBe(200);
expect(eigService.delete).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const boCheckJWT = require("../../../middlewares/bo-check-JWT");
const checkPermissionBODeclarationSejour = require("../../../middlewares/checkPermissionBODeclarationSejour");
const getDepartements = require("../../../middlewares/getDepartements");
const request = require("supertest");
const app = require("../../../app");
const eigService = require("../../../services/eig");

jest.mock("../../../middlewares/bo-check-JWT");

jest.mock("../../../middlewares/checkPermissionBODeclarationSejour");
jest.mock("../../../middlewares/getDepartements");
jest.mock("../../../services/eig");

describe("GET /eig/admin/ds/:declarationId", () => {
afterEach(() => {
jest.clearAllMocks();
});
const user = {
id: 1,
};
beforeEach(() => {
jest.resetAllMocks();
boCheckJWT.mockImplementation((req, res, next) => {
req.decoded = {
...user,
roles: ["eig", "DemandeSejour_Lecture", "DemandeSejour_Ecriture"],
};
next();
});

getDepartements.mockImplementation((req, res, next) => {
next();
});
checkPermissionBODeclarationSejour.mockImplementation((req, res, next) => {
next();
});
});

it("shoud return a 403 if the admin don't have role eig", async () => {
boCheckJWT.mockImplementationOnce((req, res, next) => {
req.decoded = {
...user,
roles: ["DemandeSejour_Lecture", "DemandeSejour_Ecriture"],
};
next();
});
const response = await request(app).get("/eig/admin/ds/1");
expect(response.statusCode).toBe(403);
expect(eigService.getByDsIdAdmin).not.toHaveBeenCalled();
});

it("should call getByDsIdAdmin if everything is ok", async () => {
const response = await request(app).get("/eig/admin/ds/1");
expect(response.statusCode).toBe(200);
expect(eigService.getByDsIdAdmin).toHaveBeenCalled();
});
});
77 changes: 77 additions & 0 deletions packages/backend/src/routes/__tests__/eig/get-admin-by-id.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const boCheckJWT = require("../../../middlewares/bo-check-JWT");
const checkPermissionBOEIG = require("../../../middlewares/checkPermissionBOEIG");
const request = require("supertest");
const app = require("../../../app");
const eigService = require("../../../services/eig");
const AppError = require("../../../utils/error");
const { getEmails } = require("../../../helpers/eigMail");

jest.mock("../../../middlewares/bo-check-JWT");

jest.mock("../../../middlewares/checkPermissionBODeclarationSejour");
jest.mock("../../../middlewares/getDepartements");
jest.mock("../../../services/eig");
jest.mock("../../../middlewares/checkPermissionBOEIG");
jest.mock("../../../helpers/eigMail");

describe("GET /eig/admin/id", () => {
afterEach(() => {
jest.clearAllMocks();
});
const user = {
id: 1,
};
beforeEach(() => {
jest.resetAllMocks();
boCheckJWT.mockImplementation((req, res, next) => {
req.decoded = {
...user,
roles: ["eig", "DemandeSejour_Lecture", "DemandeSejour_Ecriture"],
};
next();
});
checkPermissionBOEIG.mockImplementation((req, res, next) => {
next();
});
getEmails.mockResolvedValue({});
eigService.getById.mockResolvedValue({
declarationId: 1,
});
});

it("shoud return a 403 if the admin don't have role eig", async () => {
boCheckJWT.mockImplementationOnce((req, res, next) => {
req.decoded = {
...user,
roles: ["DemandeSejour_Lecture", "DemandeSejour_Ecriture"],
};
next();
});
const response = await request(app).get("/eig/admin/1");
expect(response.statusCode).toBe(403);
expect(eigService.getById).not.toHaveBeenCalled();
});

it("should return an error 403 if you don't have dclaration sejour permission for eig", async () => {
checkPermissionBOEIG.mockImplementation((req, res, next) => {
return next(
new AppError(
"Vous n'êtes pas autorisé à accéder à cette déclaration de séjour",
{
statusCode: 403,
},
),
);
});

const response = await request(app).get("/eig/admin/1");
expect(response.statusCode).toBe(403);
expect(eigService.getById).not.toHaveBeenCalled();
});

it("should call getById if everything is ok", async () => {
const response = await request(app).get("/eig/admin/1");
expect(response.statusCode).toBe(200);
expect(eigService.getById).toHaveBeenCalled();
});
});
45 changes: 45 additions & 0 deletions packages/backend/src/routes/__tests__/eig/get-admin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const boCheckJWT = require("../../../middlewares/bo-check-JWT");
const request = require("supertest");
const app = require("../../../app");
const eigService = require("../../../services/eig");

jest.mock("../../../middlewares/bo-check-JWT");
jest.mock("../../../services/eig");

describe("GET /eig/admin", () => {
afterEach(() => {
jest.clearAllMocks();
});
const user = {
id: 1,
};
beforeEach(() => {
jest.resetAllMocks();
boCheckJWT.mockImplementation((req, res, next) => {
req.decoded = {
...user,
roles: ["eig", "DemandeSejour_Lecture", "DemandeSejour_Ecriture"],
};
next();
});
});

it("shoud return a 403 if the admin don't have role eig", async () => {
boCheckJWT.mockImplementationOnce((req, res, next) => {
req.decoded = {
...user,
roles: ["DemandeSejour_Lecture", "DemandeSejour_Ecriture"],
};
next();
});
const response = await request(app).get("/eig/admin");
expect(response.statusCode).toBe(403);
expect(eigService.getAdmin).not.toHaveBeenCalled();
});

it("should call getByDsIdAdmin if everything is ok", async () => {
const response = await request(app).get("/eig/admin");
expect(response.statusCode).toBe(200);
expect(eigService.getAdmin).toHaveBeenCalled();
});
});
Loading

0 comments on commit ad8fb5d

Please sign in to comment.