Skip to content

Commit

Permalink
feat(eig): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminDNUM committed Oct 2, 2024
1 parent ce04d95 commit 751a8d7
Show file tree
Hide file tree
Showing 16 changed files with 1,175 additions and 19 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
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 = {
id: 26,
libelle: "declaration1",
dateDebut: "2024-09-01",
dateFin: "2024-09-07",
};

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();
});
});
47 changes: 47 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,47 @@
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();
});
});
52 changes: 52 additions & 0 deletions packages/backend/src/routes/__tests__/eig/get-by-ds-id.test.ds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const checkJWT = require("../../../middlewares/checkJWT");
const checkPermissionDeclarationSejour = require("../../../middlewares/checkPermissionDeclarationSejour");
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/checkPermissionDeclarationSejour");
jest.mock("../../../services/eig");

describe("GET /eig/ds/:declarationId", () => {
afterEach(() => {
jest.clearAllMocks();
});
const user = {
id: 1,
};
beforeEach(() => {
jest.resetAllMocks();
checkJWT.mockImplementation((req, res, next) => {
req.decoded = { ...user };
next();
});
checkPermissionDeclarationSejour.mockImplementation((req, res, next) => {
next();
});
});

it("should return an error 403 if you don't have dclaration sejour permission for eig", async () => {
checkPermissionDeclarationSejour.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/ds/1");
expect(response.statusCode).toBe(403);
expect(eigService.getByDsId).not.toHaveBeenCalled();
});

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

0 comments on commit 751a8d7

Please sign in to comment.