-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,185 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
57 changes: 57 additions & 0 deletions
57
packages/backend/src/routes/__tests__/eig/get-admin-by-ds-id.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
77
packages/backend/src/routes/__tests__/eig/get-admin-by-id.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
packages/backend/src/routes/__tests__/eig/get-admin.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
Oops, something went wrong.