Skip to content

Commit

Permalink
TMI2-353|add tests and fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ConorFayleAND committed Oct 13, 2023
1 parent fb1dd92 commit ada0eb9
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 22 deletions.
12 changes: 8 additions & 4 deletions packages/applicant/src/middleware.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -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({
Expand Down
30 changes: 29 additions & 1 deletion packages/applicant/src/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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
);
});
});
28 changes: 14 additions & 14 deletions packages/applicant/src/pages/api/redirect-from-find.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
50 changes: 50 additions & 0 deletions packages/applicant/src/pages/dashboard/index.page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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`
);
});
});
4 changes: 2 additions & 2 deletions packages/applicant/src/pages/dashboard/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -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,
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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(<MandatoryQuestionsBeforeYouStart {...getProps(getDefaultProps)} />);

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',
});
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FC } from 'react';
import { GetServerSideProps } from 'next';
import { routes } from '../../utils/routes';
import Layout from '../../components/partials/Layout';
Expand All @@ -12,7 +13,13 @@ export const getServerSideProps: GetServerSideProps = async ({ query }) => {
};
};

const MandatoryQuestionsBeforeYouStart = () => {
export type MandatoryQuestionsProps = {
schemeId: string;
};

const MandatoryQuestionsBeforeYouStart: FC<MandatoryQuestionsProps> = ({
schemeId,
}) => {
return (
<>
<Meta title="Before you start" />
Expand Down

0 comments on commit ada0eb9

Please sign in to comment.