Skip to content

Commit

Permalink
fix: adjust BOPS & XML admin endpoints for debugging (#2309)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak authored Oct 16, 2023
1 parent 7780848 commit 6d8e0fe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 75 deletions.
11 changes: 4 additions & 7 deletions api.planx.uk/admin/session/bops.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Request, Response, NextFunction } from "express";
import { getClient } from "../../client";
import { NextFunction, Request, Response } from "express";
import { $api } from "../../client";

/**
* @swagger
* /admin/session/{sessionId}/bops:
* get:
* summary: Generates a Back Office Planning System (BOPS) payload
* description: Generates a BOPS payload, relies on a submission record in `bops_applications`
* description: Generates a BOPS payload
* tags:
* - admin
* parameters:
Expand All @@ -20,10 +20,7 @@ export const getBOPSPayload = async (
next: NextFunction,
) => {
try {
const $client = getClient();
const { exportData } = await $client.export.bopsPayload(
req.params.sessionId,
);
const { exportData } = await $api.export.bopsPayload(req.params.sessionId);
res.set("content-type", "application/json");
return res.send(exportData);
} catch (error) {
Expand Down
52 changes: 15 additions & 37 deletions api.planx.uk/admin/session/oneAppXML.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
import supertest from "supertest";
import app from "../../server";
import { queryMock } from "../../tests/graphqlQueryMock";
import { authHeader } from "../../tests/mockJWT";

const endpoint = (strings: TemplateStringsArray) =>
`/admin/session/${strings[0]}/xml`;

describe("OneApp XML endpoint", () => {
beforeEach(() => {
queryMock.mockQuery({
name: "GetMostRecentUniformApplicationBySessionID",
variables: {
submission_reference: "abc123",
},
data: {
uniform_applications: [{ xml: "<dummy:xml></dummy:xml>" }],
},
});

queryMock.mockQuery({
name: "GetMostRecentUniformApplicationBySessionID",
variables: {
submission_reference: "xyz789",
},
data: {
uniform_applications: [],
},
});
});
const mockGenerateOneAppXML = jest
.fn()
.mockResolvedValue("<dummy:xml></dummy:xml>");

afterEach(() => jest.clearAllMocks());
const auth = authHeader({ role: "platformAdmin" });
jest.mock("../../client", () => {
return {
$api: {
generateOneAppXML: () => mockGenerateOneAppXML(),
},
};
});

describe("OneApp XML endpoint", () => {
it("requires a user to be logged in", async () => {
await supertest(app)
.get(endpoint`abc123`)
.get(endpoint`123`)
.expect(401)
.then((res) =>
expect(res.body).toEqual({
Expand All @@ -45,23 +31,15 @@ describe("OneApp XML endpoint", () => {

it("requires a user to have the 'platformAdmin' role", async () => {
await supertest(app)
.get(endpoint`abc123`)
.get(endpoint`123`)
.set(authHeader({ role: "teamEditor" }))
.expect(403);
});

it("returns an error if sessionID is invalid", async () => {
await supertest(app)
.get(endpoint`xyz789`)
.set(auth)
.expect(500)
.then((res) => expect(res.body.error).toMatch(/Invalid sessionID/));
});

it("returns XML", async () => {
await supertest(app)
.get(endpoint`abc123`)
.set(auth)
.get(endpoint`123`)
.set(authHeader({ role: "platformAdmin" }))
.expect(200)
.expect("content-type", "text/xml; charset=utf-8")
.then((res) => {
Expand Down
34 changes: 3 additions & 31 deletions api.planx.uk/admin/session/oneAppXML.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { gql } from "graphql-request";
import { Request, Response, NextFunction } from "express";
import { adminGraphQLClient as client } from "../../hasura";
import { $api } from "../../client";

/**
* @swagger
* /admin/session/{sessionId}/xml:
* get:
* summary: Generates a OneApp XML
* description: Generates a OneApp XML, relies on a submission record in `uniform_applications`
* description: Generates a OneApp XML
* tags:
* - admin
* parameters:
Expand All @@ -21,7 +20,7 @@ export const getOneAppXML = async (
next: NextFunction,
) => {
try {
const xml = await fetchSessionXML(req.params.sessionId);
const xml = await $api.generateOneAppXML(req.params.sessionId);
res.set("content-type", "text/xml");
return res.send(xml);
} catch (error) {
Expand All @@ -30,30 +29,3 @@ export const getOneAppXML = async (
});
}
};

const fetchSessionXML = async (sessionId: string) => {
try {
const query = gql`
query GetMostRecentUniformApplicationBySessionID(
$submission_reference: String
) {
uniform_applications(
where: { submission_reference: { _eq: $submission_reference } }
order_by: { created_at: desc }
limit: 1
) {
xml
}
}
`;
const { uniform_applications } = await client.request(query, {
submission_reference: sessionId,
});

if (!uniform_applications?.length) throw Error("Invalid sessionID");

return uniform_applications[0].xml;
} catch (error) {
throw Error("Unable to fetch session XML: " + (error as Error).message);
}
};

0 comments on commit 6d8e0fe

Please sign in to comment.