Skip to content

Commit

Permalink
feat: add debugging option to skip validation when generating Digital…
Browse files Browse the repository at this point in the history
…PlanningPayload (#353)

See theopensystemslab/planx-new#3021
  • Loading branch information
jessicamcinchak authored Apr 16, 2024
1 parent 9910b2c commit 15bde6b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
9 changes: 7 additions & 2 deletions src/export/digitalPlanning/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import { ExportParams } from "..";
import { DigitalPlanning } from "./model";
import { DigitalPlanningApplication as DigitalPlanningPayload } from "./schema/types";

interface DigitalPlanningExportParams extends ExportParams {
skipValidation?: boolean;
}

export async function generateDigitalPlanningPayload({
client,
sessionId,
}: ExportParams): Promise<DigitalPlanningPayload> {
skipValidation,
}: DigitalPlanningExportParams): Promise<DigitalPlanningPayload> {
const session = await getSessionById(client, sessionId);
if (!session) throw new Error(`Cannot find session ${sessionId}`);

Expand All @@ -28,5 +33,5 @@ export async function generateDigitalPlanningPayload({
breadcrumbs,
flow,
metadata,
}).getPayload();
}).getPayload(skipValidation);
}
36 changes: 29 additions & 7 deletions src/export/digitalPlanning/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,36 @@ describe("DigitalPlanning", () => {
});
});

describe("skipping validation", () => {
test("shouldn't throw an error", () => {
const instance = new DigitalPlanning(mockParams);

// @ts-expect-error - The operand of a 'delete' operator must be optional
delete instance.payload.data.applicant;

expect(() => instance.getPayload(true)).not.toThrow();
});

test("should return invalid JSON data", () => {
const instance = new DigitalPlanning(mockParams);

// @ts-expect-error - Type 'number' is not assignable to type 'string'
instance.payload.data.applicant.name = 12345;

const payload = instance.getPayload(true);

expect(payload).toHaveProperty("data.applicant.name", 12345);
});
});

describe("invalid payloads", () => {
test("missing values", () => {
const instance = new DigitalPlanning(mockParams);

// @ts-expect-error - The operand of a 'delete' operator must be optional
delete instance.payload.data.applicant;

expect(() => instance.getPayload()).toThrowError(
expect(() => instance.getPayload()).toThrow(
/Invalid DigitalPlanning payload/,
);
});
Expand All @@ -124,7 +146,7 @@ describe("DigitalPlanning", () => {
// @ts-expect-error - Type 'undefined' is not assignable to type 'Applicant'
instance.payload.data.applicant = undefined;

expect(() => instance.getPayload()).toThrowError(
expect(() => instance.getPayload()).toThrow(
/Invalid DigitalPlanning payload/,
);
});
Expand All @@ -135,7 +157,7 @@ describe("DigitalPlanning", () => {
// @ts-expect-error - Type 'number' is not assignable to type 'string'
instance.payload.data.applicant.name = 12345;

expect(() => instance.getPayload()).toThrowError(
expect(() => instance.getPayload()).toThrow(
/Invalid DigitalPlanning payload/,
);
});
Expand All @@ -146,7 +168,7 @@ describe("DigitalPlanning", () => {
instance.payload.metadata.schema =
"not a valid URL, but still a string";

expect(() => instance.getPayload()).toThrowError(
expect(() => instance.getPayload()).toThrow(
/Invalid DigitalPlanning payload/,
);
});
Expand All @@ -157,7 +179,7 @@ describe("DigitalPlanning", () => {
instance.payload.metadata.submittedAt =
"not a valid datetime, but still a string";

expect(() => instance.getPayload()).toThrowError(
expect(() => instance.getPayload()).toThrow(
/Invalid DigitalPlanning payload/,
);
});
Expand All @@ -167,7 +189,7 @@ describe("DigitalPlanning", () => {

instance.payload.metadata.submittedAt = "2023-01-01 00:00:00";

expect(() => instance.getPayload()).toThrowError(
expect(() => instance.getPayload()).toThrow(
/Invalid DigitalPlanning payload/,
);
});
Expand All @@ -178,7 +200,7 @@ describe("DigitalPlanning", () => {
// @ts-expect-error Type '"invalid enum"' is not assignable to type '"applicant" | "agent" | "proxy"'
instance.payload.data.user.role = "tester";

expect(() => instance.getPayload()).toThrowError(
expect(() => instance.getPayload()).toThrow(
/Invalid DigitalPlanning payload/,
);
});
Expand Down
10 changes: 7 additions & 3 deletions src/export/digitalPlanning/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ export class DigitalPlanning {
this.payload = this.mapPassportToPayload();
}

getPayload(): Payload {
this.validatePayload();
return this.payload;
getPayload(skipValidation: boolean = false): Payload {
if (skipValidation) {
return this.payload;
} else {
this.validatePayload();
return this.payload;
}
}

mapPassportToPayload(): Payload {
Expand Down
7 changes: 6 additions & 1 deletion src/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ export class ExportClient {

digitalPlanningDataPayload(
sessionId: string,
skipValidation?: boolean,
): Promise<DigitalPlanningApplication> {
return generateDigitalPlanningPayload({ client: this.client, sessionId });
return generateDigitalPlanningPayload({
client: this.client,
sessionId,
skipValidation,
});
}

async oneAppPayload(sessionId: string): Promise<string> {
Expand Down
12 changes: 6 additions & 6 deletions src/export/oneApp/OneApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ test("Parsing error", () => {
sessionId: "abc123",
passport: new Passport({ data: { invalid: "invalid" } }),
};
expect(() => new OneAppPayload(invalidConfig).buildXML()).toThrowError(
expect(() => new OneAppPayload(invalidConfig).buildXML()).toThrow(
/Invalid OneApp Payload/,
);
});
Expand All @@ -783,7 +783,7 @@ test("Unhandled error", () => {
payload.getXMLBuilder = jest.fn().mockImplementation(() => {
throw Error();
});
expect(() => payload.buildXML()).toThrowError(/Unhandled exception/);
expect(() => payload.buildXML()).toThrow(/Unhandled exception/);
});

describe("Refinement rules", () => {
Expand All @@ -802,7 +802,7 @@ describe("Refinement rules", () => {
sessionId,
passport,
}).buildXML(),
).toThrowError(
).toThrow(
/An email address must be supplied for either applicant or agent/,
);
});
Expand All @@ -820,7 +820,7 @@ describe("Refinement rules", () => {
sessionId,
passport,
}).buildXML(),
).not.toThrowError();
).not.toThrow();
});

test("An error is thrown if no telephone values are submitted", () => {
Expand All @@ -836,7 +836,7 @@ describe("Refinement rules", () => {
sessionId,
passport,
}).buildXML(),
).toThrowError(
).toThrow(
/A telephone number must be supplied for either applicant or agent/,
);
});
Expand All @@ -854,6 +854,6 @@ describe("Refinement rules", () => {
sessionId,
passport,
}).buildXML(),
).not.toThrowError();
).not.toThrow();
});
});

0 comments on commit 15bde6b

Please sign in to comment.