diff --git a/packages/applicant/src/middleware.page.ts b/packages/applicant/src/middleware.page.ts index 9b478e704..18524b752 100644 --- a/packages/applicant/src/middleware.page.ts +++ b/packages/applicant/src/middleware.page.ts @@ -46,13 +46,12 @@ export function buildMiddlewareResponse(req: NextRequest, redirectUri: string) { } ); } else if ( - process.env.MANDATORY_QUESTIONS_ENABLED && + process.env.MANDATORY_QUESTIONS_ENABLED === 'true' && redirectFromFindPattern.test({ pathname: req.nextUrl.pathname }) ) { res.cookies.set( process.env.FIND_REDIRECT_COOKIE, - redirectFromFindPattern.exec({ pathname: req.nextUrl.pathname }).pathname - .groups.slug, + grabSlug(req.nextUrl.search), { path: '/', secure: true, @@ -65,6 +64,11 @@ export function buildMiddlewareResponse(req: NextRequest, redirectUri: string) { return res; } +const grabSlug = (string: string) => { + const queryParams = new URLSearchParams(string); + return queryParams.get('slug'); +}; + export const getJwtFromMiddlewareCookies = (req: NextRequest) => { const COOKIE_SECRET = process.env.COOKIE_SECRET; @@ -134,7 +138,7 @@ const newApplicationPattern = new URLPattern({ }); const redirectFromFindPattern = new URLPattern({ - pathname: '/api/redirect-from-find?slug=:slug([a-z0-9-]+)', + pathname: '/api/redirect-from-find', }); const signInDetailsPage = new URLPattern({ diff --git a/packages/applicant/src/middleware.test.ts b/packages/applicant/src/middleware.test.ts index 579c436f9..accd0c5e7 100644 --- a/packages/applicant/src/middleware.test.ts +++ b/packages/applicant/src/middleware.test.ts @@ -48,7 +48,7 @@ describe('Middleware', () => { ); }); - it('sets redirect cookie if URL matches new application pattern', async () => { + it('sets application redirect cookie if URL matches new application pattern', async () => { const pathname = 'applications/123'; const applicationId = '123'; @@ -61,4 +61,32 @@ describe('Middleware', () => { applicationId ); }); + + it('sets find redirect cookie if URL matches find redirect pattern and FF enabled', async () => { + process.env.MANDATORY_QUESTIONS_ENABLED = 'true'; + const slug = 'slug-123'; + const pathname = 'api/redirect-from-find?slug=' + slug; + + const req = new NextRequest( + new Request(`https://some.website.com/${pathname}`) + ); + const res = buildMiddlewareResponse(req, process.env.HOST); + + expect(res.cookies.get(process.env.FIND_REDIRECT_COOKIE)).toEqual(slug); + }); + + it('does not set find redirect cookie if URL matches find redirect pattern but FF disabled', async () => { + process.env.MANDATORY_QUESTIONS_ENABLED = 'false'; + const slug = 'slug-123'; + const pathname = 'api/redirect-from-find?slug=' + slug; + + const req = new NextRequest( + new Request(`https://some.website.com/${pathname}`) + ); + const res = buildMiddlewareResponse(req, process.env.HOST); + + expect(res.cookies.get(process.env.FIND_REDIRECT_COOKIE)).toEqual( + undefined + ); + }); }); diff --git a/packages/applicant/src/pages/api/redirect-from-find.page.tsx b/packages/applicant/src/pages/api/redirect-from-find.page.tsx index cb46c2326..7dcc845eb 100644 --- a/packages/applicant/src/pages/api/redirect-from-find.page.tsx +++ b/packages/applicant/src/pages/api/redirect-from-find.page.tsx @@ -16,20 +16,20 @@ export default async function handler( grantSchemeId, } = await getAdvertBySlug(getJwtFromCookies(req), slug); - if (version === 1) { - const redirectUrl = isInternal - ? `${process.env.HOST}${routes.applications}/${grantApplicationId}` - : externalSubmissionUrl; - res.redirect(redirectUrl); - } - - if (version === 2) { - res.redirect( - `${process.env.HOST}${routes.mandatoryQuestions.startPage( - grantSchemeId.toString() - )}` - ); - } + // if (version === 1) { + // const redirectUrl = isInternal + // ? `${process.env.HOST}${routes.applications}/${grantApplicationId}` + // : externalSubmissionUrl; + // res.redirect(redirectUrl); + // } + // + // if (version === 2) { + res.redirect( + `${process.env.HOST}${routes.mandatoryQuestions.startPage( + grantSchemeId.toString() + )}` + ); + // } } catch (e) { const serviceErrorProps = { errorInformation: 'There was an error in the service', diff --git a/packages/applicant/src/pages/dashboard/index.page.test.tsx b/packages/applicant/src/pages/dashboard/index.page.test.tsx index aa620c0f1..ab1e1b91e 100644 --- a/packages/applicant/src/pages/dashboard/index.page.test.tsx +++ b/packages/applicant/src/pages/dashboard/index.page.test.tsx @@ -97,6 +97,7 @@ describe('getServerSideProps', () => { }); it('should return a DescriptionListProps object', async () => { + process.env.ONE_LOGIN_ENABLED = 'false'; const getGrantApplicantSpy = jest .spyOn(GrantApplicantService.prototype, 'getGrantApplicant') .mockResolvedValue(MOCK_GRANT_APPLICANT); @@ -123,6 +124,7 @@ describe('getServerSideProps', () => { }); it('should return a DescriptionListProps object with detail null', async () => { + process.env.ONE_LOGIN_ENABLED = 'false'; const getGrantApplicantSpy = jest .spyOn(GrantApplicantService.prototype, 'getGrantApplicant') .mockResolvedValue(MOCK_GRANT_APPLICANT_NO_LEGAL_NAME); @@ -181,4 +183,52 @@ describe('getServerSideProps', () => { }, }); }); + + it('should redirect to find redirect page', async () => { + process.env.MANDATORY_QUESTIONS_ENABLED = 'true'; + const result = await getServerSideProps( + getContext(getDefaultContext, { + req: { + cookies: { + [process.env.FIND_REDIRECT_COOKIE]: 'slug-123', + }, + }, + res: { + setHeader: mockSetHeader, + }, + }) + ); + + expect(mockSetHeader).toBeCalledWith( + 'Set-Cookie', + `${process.env.FIND_REDIRECT_COOKIE}=deleted; Path=/; Max-Age=0` + ); + expectObjectEquals(result, { + redirect: { + destination: '/api/redirect-after-find?slug=slug-123', + statusCode: 307, + }, + }); + }); + + it('should not redirect to find redirect page', async () => { + process.env.MANDATORY_QUESTIONS_ENABLED = 'false'; + const result = await getServerSideProps( + getContext(getDefaultContext, { + req: { + cookies: { + [process.env.FIND_REDIRECT_COOKIE]: 'slug-123', + }, + }, + res: { + setHeader: mockSetHeader, + }, + }) + ); + + expect(mockSetHeader).not.toBeCalledWith( + 'Set-Cookie', + `${process.env.FIND_REDIRECT_COOKIE}=deleted; Path=/; Max-Age=0` + ); + }); }); diff --git a/packages/applicant/src/pages/dashboard/index.page.tsx b/packages/applicant/src/pages/dashboard/index.page.tsx index 9f85fe509..b7986757c 100644 --- a/packages/applicant/src/pages/dashboard/index.page.tsx +++ b/packages/applicant/src/pages/dashboard/index.page.tsx @@ -30,7 +30,7 @@ export const getServerSideProps = async ({ }; } - if (process.env.MANDATORY_QUESTIONS_ENABLED) { + if (process.env.MANDATORY_QUESTIONS_ENABLED === 'true') { const findRedirectCookie = process.env.FIND_REDIRECT_COOKIE; if (req.cookies[findRedirectCookie]) { @@ -41,7 +41,7 @@ export const getServerSideProps = async ({ ); return { redirect: { - destination: `/api/redirect-after-find/${slug}`, + destination: `/api/redirect-after-find?slug=${slug}`, statusCode: 307, }, }; diff --git a/packages/applicant/src/pages/mandatory-questions/start.page.test.tsx b/packages/applicant/src/pages/mandatory-questions/start.page.test.tsx new file mode 100644 index 000000000..35c182d8e --- /dev/null +++ b/packages/applicant/src/pages/mandatory-questions/start.page.test.tsx @@ -0,0 +1,45 @@ +import '@testing-library/jest-dom'; +import { render, screen } from '@testing-library/react'; +import MandatoryQuestionsBeforeYouStart, { + MandatoryQuestionsProps, +} from './start.page'; +import { getProps } from 'gap-web-ui'; + +function getDefaultProps(): MandatoryQuestionsProps { + return { + schemeId: '1', + }; +} + +describe('Mandatory Questions Start', () => { + test('should render page', () => { + render(); + + screen.getByRole('heading', { + name: 'Before you start', + }); + screen.getByRole('paragraph', { + name: 'Before you start, we’d like to ask you a few questions.', + }); + screen.getByRole('paragraph', { + name: + 'These questions will be used by the grant’s administrators to prevent fraud ' + + 'and help Find a grant understand the demand for this grant.', + }); + screen.getByRole('paragraph', { + name: 'You will need:', + }); + screen.getByRole('listitem', { + name: 'your organisation’s address', + }); + screen.getByRole('listitem', { + name: 'your Companies House number (if you have one)', + }); + screen.getByRole('listitem', { + name: 'your Charity Commission number (if you have one)', + }); + screen.getByRole('button', { + name: 'Continue', + }); + }); +}); diff --git a/packages/applicant/src/pages/mandatory-questions/start.page.tsx b/packages/applicant/src/pages/mandatory-questions/start.page.tsx index 3d2a90de2..2476eb13a 100644 --- a/packages/applicant/src/pages/mandatory-questions/start.page.tsx +++ b/packages/applicant/src/pages/mandatory-questions/start.page.tsx @@ -1,3 +1,4 @@ +import { FC } from 'react'; import { GetServerSideProps } from 'next'; import { routes } from '../../utils/routes'; import Layout from '../../components/partials/Layout'; @@ -12,7 +13,13 @@ export const getServerSideProps: GetServerSideProps = async ({ query }) => { }; }; -const MandatoryQuestionsBeforeYouStart = () => { +export type MandatoryQuestionsProps = { + schemeId: string; +}; + +const MandatoryQuestionsBeforeYouStart: FC = ({ + schemeId, +}) => { return ( <>