-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an admin endpoint for the Digital Planning Application form…
…at (#2322)
- Loading branch information
1 parent
d86cd87
commit 93cf90e
Showing
5 changed files
with
1,836 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import supertest from "supertest"; | ||
import app from "../../server"; | ||
import { authHeader } from "../../tests/mockJWT"; | ||
import { expectedPlanningPermissionPayload } from "../../tests/mocks/digitalPlanningDataMocks"; | ||
|
||
const endpoint = (strings: TemplateStringsArray) => | ||
`/admin/session/${strings[0]}/digital-planning-application`; | ||
|
||
const mockGenerateDigitalPlanningApplicationPayload = jest | ||
.fn() | ||
.mockResolvedValue(expectedPlanningPermissionPayload); | ||
|
||
jest.mock("@opensystemslab/planx-core", () => { | ||
return { | ||
CoreDomainClient: jest.fn().mockImplementation(() => ({ | ||
export: { | ||
digitalPlanningDataPayload: () => | ||
mockGenerateDigitalPlanningApplicationPayload(), | ||
}, | ||
})), | ||
}; | ||
}); | ||
|
||
describe("Digital Planning Application payload admin endpoint", () => { | ||
it("requires a user to be logged in", async () => { | ||
await supertest(app) | ||
.get(endpoint`123`) | ||
.expect(401) | ||
.then((res) => | ||
expect(res.body).toEqual({ | ||
error: "No authorization token was found", | ||
}), | ||
); | ||
}); | ||
|
||
it("requires a user to have the 'platformAdmin' role", async () => { | ||
await supertest(app) | ||
.get(endpoint`123`) | ||
.set(authHeader({ role: "teamEditor" })) | ||
.expect(403); | ||
}); | ||
|
||
it("returns a valid JSON payload", async () => { | ||
await supertest(app) | ||
.get(endpoint`123`) | ||
.set(authHeader({ role: "platformAdmin" })) | ||
.expect(200) | ||
.expect("content-type", "application/json; charset=utf-8") | ||
.then((res) => | ||
expect(res.body).toEqual(expectedPlanningPermissionPayload), | ||
); | ||
}); | ||
}); |
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,35 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import { $api } from "../../client"; | ||
|
||
/** | ||
* @swagger | ||
* /admin/session/{sessionId}/digital-planning-application: | ||
* get: | ||
* summary: Generates a Digital Planning Application payload | ||
* description: Generates a Digital Planning Application payload and validates it against the Digital Planning Data JSON Schema | ||
* tags: | ||
* - admin | ||
* parameters: | ||
* - $ref: '#/components/parameters/sessionId' | ||
* security: | ||
* - bearerAuth: [] | ||
*/ | ||
export const getDigitalPlanningApplicationPayload = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
try { | ||
const data = await $api.export.digitalPlanningDataPayload( | ||
req.params.sessionId, | ||
); | ||
res.set("content-type", "application/json"); | ||
return res.send(data); | ||
} catch (error) { | ||
return next({ | ||
message: | ||
"Failed to make Digital Planning Application payload: " + | ||
(error as Error).message, | ||
}); | ||
} | ||
}; |
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
Oops, something went wrong.