Skip to content

Commit

Permalink
take two
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Oct 17, 2023
1 parent 7dba169 commit 8fcf950
Show file tree
Hide file tree
Showing 10 changed files with 1,823 additions and 27 deletions.
48 changes: 48 additions & 0 deletions api.planx.uk/admin/session/digitalPlanningData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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));
});
});
31 changes: 31 additions & 0 deletions api.planx.uk/admin/session/digitalPlanningData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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,
});
}
};
2 changes: 1 addition & 1 deletion api.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"dependencies": {
"@airbrake/node": "^2.1.8",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#3a85966",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#280aaff",
"@types/isomorphic-fetch": "^0.0.36",
"adm-zip": "^0.5.10",
"aws-sdk": "^2.1467.0",
Expand Down
9 changes: 5 additions & 4 deletions api.planx.uk/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api.planx.uk/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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";

const router = express.Router();

Expand Down Expand Up @@ -213,6 +214,7 @@ 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 8fcf950

Please sign in to comment.