Skip to content

Commit

Permalink
chore: Retire 'test test' workaround (#1726)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored May 26, 2023
1 parent aa8ea83 commit cc4f3e2
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 111 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/sync-staging-db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: Weekly staging database sync

on:
schedule:
# Runs Sunday at 00:00 (https://crontab.guru/#0_0_*_*_0)
- cron: '0 0 * * 0'
# Runs nightly at 00:00 (https://crontab.guru/#0_0_*_*_*)
- cron: '0 0 * * *'
workflow_dispatch:

jobs:
Expand Down
29 changes: 29 additions & 0 deletions editor.planx.uk/src/@planx/components/ContactInput/Public.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,32 @@ it("should not have any accessibility violations while in the error state", asyn
const results = await axe(container);
expect(results).toHaveNoViolations();
});

test("does not allow the name 'Test Test' to be used", async () => {
const handleSubmit = jest.fn();
const dataField = "applicant";

const { user } = setup(
<ContactInput
handleSubmit={handleSubmit}
title="Enter your contact details"
fn={dataField}
/>
);

await fillInFieldsUsingLabel(user, {
"First name": "Test",
"Last name": "Test",
"Phone number": "0123456789",
"Email address": "[email protected]",
});

await user.click(screen.getByTestId("continue-button"));

expect(handleSubmit).not.toHaveBeenCalled();

const errorMessage = await screen.findByText(
"'Test Test' is not a valid name - please submit test applications via the staging environment"
);
expect(errorMessage).toBeVisible();
});
16 changes: 16 additions & 0 deletions editor.planx.uk/src/@planx/components/ContactInput/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ export const userDataSchema: SchemaOf<Contact> = object({
"Enter an email address in the correct format, like [email protected]"
)
.required("Enter an email address"),
}).test({
name: "Test Test is not used for applications",
test: ({ firstName, lastName }, context) => {
const isValid =
[firstName, lastName]
.map((x) => String(x).toLowerCase().trim())
.join("|") !== "test|test";

if (isValid) return true;

return context.createError({
path: "firstName",
message:
"'Test Test' is not a valid name - please submit test applications via the staging environment",
});
},
});

export interface ContactInput extends MoreInformation {
Expand Down
11 changes: 3 additions & 8 deletions editor.planx.uk/src/@planx/components/Pay/Public/Pay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, { useEffect, useReducer } from "react";
import type { GovUKPayment, Passport, Session } from "types";
import { PaymentStatus } from "types";

import { makeData, useStagingUrlIfTestApplication } from "../../shared/utils";
import { makeData } from "../../shared/utils";
import {
createPayload,
GOV_PAY_PASSPORT_KEY,
Expand Down Expand Up @@ -157,7 +157,6 @@ function Component(props: Props) {
sessionId,
flowId,
teamSlug,
passport,
paymentId: govUkPayment?.payment_id,
})
);
Expand Down Expand Up @@ -223,7 +222,7 @@ function Component(props: Props) {
}
await axios
.post(
getGovUkPayUrlForTeam({ sessionId, flowId, teamSlug, passport }),
getGovUkPayUrlForTeam({ sessionId, flowId, teamSlug }),
createPayload(fee, sessionId)
)
.then(async (res) => {
Expand Down Expand Up @@ -294,18 +293,14 @@ function getGovUkPayUrlForTeam({
sessionId,
flowId,
teamSlug,
passport,
paymentId,
}: {
sessionId: string;
flowId: string;
teamSlug: string;
passport: Passport;
paymentId?: string;
}): string {
const baseURL = useStagingUrlIfTestApplication(passport)(
`${GOV_UK_PAY_URL}/${teamSlug}`
);
const baseURL = `${GOV_UK_PAY_URL}/${teamSlug}`;
const queryString = `?sessionId=${sessionId}&flowId=${flowId}`;
if (paymentId) {
return `${baseURL}/${paymentId}${queryString}`;
Expand Down
7 changes: 2 additions & 5 deletions editor.planx.uk/src/@planx/components/Send/Public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React, { useEffect } from "react";
import { useAsync } from "react-use";

import Card from "../shared/Preview/Card";
import { makeData, useStagingUrlIfTestApplication } from "../shared/utils";
import { makeData } from "../shared/utils";
import { PublicProps } from "../ui";
import {
DEFAULT_DESTINATION,
Expand Down Expand Up @@ -36,10 +36,7 @@ const SendComponent: React.FC<Props> = ({
sessionId,
});

return axios.post(
useStagingUrlIfTestApplication(passport)(url),
combinedEventsPayload
);
return axios.post(url, combinedEventsPayload);
});

useEffect(() => {
Expand Down
59 changes: 0 additions & 59 deletions editor.planx.uk/src/@planx/components/shared/utils.test.ts

This file was deleted.

33 changes: 0 additions & 33 deletions editor.planx.uk/src/@planx/components/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import isNil from "lodash/isNil";
import { Store } from "pages/FlowEditor/lib/store";

import { bopsDictionary } from "../Send/bops";

export const validateEmail = (email: string) => {
// eslint-disable-next-line
Expand Down Expand Up @@ -49,36 +46,6 @@ export const makeData = <T>(
};
};

/**
* Replaces a URL containing planx.uk with planx.dev
* if the passport appears to contain test data.
* Explanation: https://bit.ly/3AypxGW
*
* @example
* useStagingUrlIfTestApplication(passport)("https://api.editor.planx.uk/test")
* // when applicant's full name is 'Test Test'
* // => "https://api.editor.planx.dev/test"
* // otherwise
* // => "https://api.editor.planx.uk/test"
*/
export const useStagingUrlIfTestApplication =
(passport: Store.passport) => (urlThatMightBeReplaced: string) => {
if (
[
passport.data?.[bopsDictionary.applicant_first_name],
passport.data?.[bopsDictionary.applicant_last_name],
]
.map((x) => String(x).toLowerCase().trim())
.join("|") === "test|test"
) {
const url = new URL(urlThatMightBeReplaced);
url.hostname = url.hostname.replace("planx.uk", "planx.dev");
return url.href;
}

return urlThatMightBeReplaced;
};

export const getPreviouslySubmittedData = ({
id,
fn,
Expand Down
5 changes: 1 addition & 4 deletions editor.planx.uk/src/api/upload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useStagingUrlIfTestApplication } from "@planx/components/shared/utils";
import axios from "axios";
import { useStore } from "pages/FlowEditor/lib/store";

Expand Down Expand Up @@ -41,9 +40,7 @@ function handleUpload(
// Private uploads for test applications should be handled by the staging environment
const paths = {
public: `${process.env.REACT_APP_API_URL}/public-file-upload`,
private: useStagingUrlIfTestApplication(passport)(
`${process.env.REACT_APP_API_URL}/private-file-upload`
),
private: `${process.env.REACT_APP_API_URL}/private-file-upload`,
};

const endpoint = paths[path];
Expand Down

0 comments on commit cc4f3e2

Please sign in to comment.