Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an admin endpoint for the Digital Planning Application format #2322

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading