Skip to content

Commit

Permalink
feat: add an admin endpoint for the Digital Planning Application form…
Browse files Browse the repository at this point in the history
…at (#2322)
  • Loading branch information
jessicamcinchak authored Oct 19, 2023
1 parent d86cd87 commit 93cf90e
Show file tree
Hide file tree
Showing 5 changed files with 1,836 additions and 86 deletions.
53 changes: 53 additions & 0 deletions api.planx.uk/admin/session/digitalPlanningData.test.ts
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),
);
});
});
35 changes: 35 additions & 0 deletions api.planx.uk/admin/session/digitalPlanningData.ts
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,
});
}
};
5 changes: 5 additions & 0 deletions api.planx.uk/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import webhookRoutes from "./modules/webhooks/routes";
import analyticsRoutes from "./modules/analytics/routes";
import { useSwaggerDocs } from "./docs";
import { Role } from "@opensystemslab/planx-core/types";
import { getDigitalPlanningApplicationPayload } from "./admin/session/digitalPlanningData";
import { $public } from "./client";

const router = express.Router();
Expand Down Expand Up @@ -213,6 +214,10 @@ app.get("/admin/session/:sessionId/html", getHTMLExport);
app.get("/admin/session/:sessionId/html-redacted", getRedactedHTMLExport);
app.get("/admin/session/:sessionId/zip", generateZip);
app.get("/admin/session/:sessionId/summary", getSessionSummary);
app.get(
"/admin/session/:sessionId/digital-planning-application",
getDigitalPlanningApplicationPayload,
);

app.post("/flows/:flowId/copy", useTeamEditorAuth, copyFlow);

Expand Down
Loading

0 comments on commit 93cf90e

Please sign in to comment.