-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tmi2 364/add redirection logic for new mandatory question journey (#89)
* initial commit * add redirection logic, and mock page for new journey * add test coverage * ignore temporary snyk dependency check, rename page * fix indentation * update snyk ignore policy * update snyk rule * fix start page url * add more test coverage, in case of error we now redirect to an error page
- Loading branch information
Showing
8 changed files
with
271 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/applicant/src/pages/api/redirect-from-find.page.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { merge } from 'lodash'; | ||
import { AdvertDto, getAdvertBySlug } from '../../services/GrantAdvertService'; | ||
import { getJwtFromCookies } from '../../utils/jwt'; | ||
import handler from './redirect-from-find.page'; | ||
|
||
// Mock the getAdvertBySlug function (you may need to adjust this based on your actual implementation) | ||
jest.mock('../../services/GrantAdvertService'); | ||
jest.mock('../../utils/jwt'); | ||
const mockedRedirect = jest.fn(); | ||
const mockedSetHeader = jest.fn(); | ||
const mockedSend = jest.fn(); | ||
|
||
const req = (overrides: any = {}) => | ||
merge({ | ||
query: { | ||
slug: 'slug', | ||
}, | ||
}); | ||
|
||
const res = (overrides: any = {}) => | ||
merge( | ||
{ | ||
redirect: mockedRedirect, | ||
setHeader: mockedSetHeader, | ||
send: mockedSend, | ||
}, | ||
overrides | ||
); | ||
|
||
describe('API Handler Tests', () => { | ||
beforeEach(() => { | ||
process.env.HOST = 'http://localhost'; | ||
jest.resetAllMocks(); | ||
}); | ||
it('should redirect to /applications/<applicationId> when advert is version 1 and have internal application', async () => { | ||
const advertDTO: AdvertDto = { | ||
id: '123', | ||
version: 1, | ||
grantApplicationId: 123, | ||
isInternal: true, | ||
grantSchemeId: 456, | ||
externalSubmissionUrl: 'http://example.com', | ||
}; | ||
|
||
(getAdvertBySlug as jest.Mock).mockResolvedValue(advertDTO); | ||
(getJwtFromCookies as jest.Mock).mockReturnValue('testJwt'); | ||
await handler(req(), res()); | ||
|
||
expect(mockedRedirect).toHaveBeenCalledWith( | ||
'http://localhost/applications/123' | ||
); | ||
}); | ||
it('should redirect to the external Submission Url when advert is version 1 and have internal application', async () => { | ||
const advertDTO: AdvertDto = { | ||
id: '123', | ||
version: 1, | ||
grantApplicationId: 123, | ||
isInternal: false, | ||
grantSchemeId: 456, | ||
externalSubmissionUrl: 'http://example.com', | ||
}; | ||
|
||
(getAdvertBySlug as jest.Mock).mockResolvedValue(advertDTO); | ||
(getJwtFromCookies as jest.Mock).mockReturnValue('testJwt'); | ||
await handler(req(), res()); | ||
|
||
expect(mockedRedirect).toHaveBeenCalledWith('http://example.com'); | ||
}); | ||
|
||
it('should redirect to the new Mandatory Question journey start page when advert is version 2 and have internal application', async () => { | ||
const advertDTO: AdvertDto = { | ||
id: '123', | ||
version: 2, | ||
grantApplicationId: 123, | ||
isInternal: true, | ||
grantSchemeId: 456, | ||
externalSubmissionUrl: 'http://example.com', | ||
}; | ||
|
||
(getAdvertBySlug as jest.Mock).mockResolvedValue(advertDTO); | ||
(getJwtFromCookies as jest.Mock).mockReturnValue('testJwt'); | ||
await handler(req(), res()); | ||
|
||
expect(mockedRedirect).toHaveBeenCalledWith( | ||
'http://localhost/mandatory-questions/start?schemeId=456' | ||
); | ||
}); | ||
|
||
it('should redirect to the new Mandatory Question journey start page when advert is version 2 and have external application', async () => { | ||
const advertDTO: AdvertDto = { | ||
id: '123', | ||
version: 2, | ||
grantApplicationId: 123, | ||
isInternal: false, | ||
grantSchemeId: 456, | ||
externalSubmissionUrl: 'http://example.com', | ||
}; | ||
|
||
(getAdvertBySlug as jest.Mock).mockResolvedValue(advertDTO); | ||
(getJwtFromCookies as jest.Mock).mockReturnValue('testJwt'); | ||
await handler(req(), res()); | ||
|
||
expect(mockedRedirect).toHaveBeenCalledWith( | ||
'http://localhost/mandatory-questions/start?schemeId=456' | ||
); | ||
}); | ||
it('should redirect to the service Error when there is an error in the call to the backend', async () => { | ||
(getAdvertBySlug as jest.Mock).mockRejectedValue(new Error('error')); | ||
(getJwtFromCookies as jest.Mock).mockReturnValue('testJwt'); | ||
await handler(req(), res()); | ||
const serviceErrorProps = { | ||
errorInformation: 'There was an error in the service', | ||
linkAttributes: { | ||
href: '/dashboard', | ||
linkText: 'Go back to your dashboard', | ||
linkInformation: '', | ||
}, | ||
}; | ||
expect(mockedRedirect).toHaveBeenCalledWith( | ||
`http://localhost/service-error?serviceErrorProps=${JSON.stringify( | ||
serviceErrorProps | ||
)}` | ||
); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
packages/applicant/src/pages/api/redirect-from-find.page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { getAdvertBySlug } from '../../services/GrantAdvertService'; | ||
import { getJwtFromCookies } from '../../utils/jwt'; | ||
import { routes } from '../../utils/routes'; | ||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const slug = req.query.slug as string; | ||
try { | ||
const { | ||
externalSubmissionUrl, | ||
version, | ||
grantApplicationId, | ||
isInternal, | ||
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() | ||
)}` | ||
); | ||
} | ||
} catch (e) { | ||
const serviceErrorProps = { | ||
errorInformation: 'There was an error in the service', | ||
linkAttributes: { | ||
href: routes.dashboard, | ||
linkText: 'Go back to your dashboard', | ||
linkInformation: '', | ||
}, | ||
}; | ||
console.log(e); | ||
res.redirect( | ||
`${process.env.HOST}${routes.serviceError(serviceErrorProps)}` | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/applicant/src/pages/mandatory-questions/start.page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Layout from '../../components/partials/Layout'; | ||
import Meta from '../../components/partials/Meta'; | ||
import { routes } from '../../utils/routes'; | ||
|
||
const FirstMandatoryQuestion = () => { | ||
return ( | ||
<> | ||
<Meta title="View my applications - Apply for a grant" /> | ||
|
||
<Layout backBtnUrl={routes.dashboard}> | ||
<>NEW PAGE</> | ||
</Layout> | ||
</> | ||
); | ||
}; | ||
|
||
export default FirstMandatoryQuestion; |
48 changes: 48 additions & 0 deletions
48
packages/applicant/src/services/GrantAdvertService.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import axios from 'axios'; | ||
import { AdvertDto, getAdvertBySlug } from './GrantAdvertService'; | ||
|
||
jest.mock('axios'); | ||
|
||
const BACKEND_HOST = process.env.BACKEND_HOST + '/grant-adverts'; | ||
const advertDTO: AdvertDto = { | ||
id: '123', | ||
version: 2, | ||
grantApplicationId: 123, | ||
isInternal: true, | ||
grantSchemeId: 456, | ||
externalSubmissionUrl: 'http://example.com', | ||
}; | ||
describe('GrantAdvert Service', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('getGrantBeneficiary', () => { | ||
it('Should call axios', async () => { | ||
(axios.get as jest.Mock).mockResolvedValue({}); | ||
|
||
await getAdvertBySlug('testJwt', 'slug'); | ||
|
||
expect(axios.get as jest.Mock).toHaveBeenNthCalledWith( | ||
1, | ||
`${BACKEND_HOST}?contentfulSlug=slug`, | ||
{ | ||
headers: { | ||
Accept: 'application/json', | ||
Authorization: 'Bearer testJwt', | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
it('Should return a GrantBeneficiary', async () => { | ||
(axios.get as jest.Mock).mockResolvedValue({ | ||
data: advertDTO, | ||
}); | ||
|
||
const response = await getAdvertBySlug('testJwt', 'slug'); | ||
|
||
expect(response).toStrictEqual(advertDTO); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import axios from 'axios'; | ||
import getConfig from 'next/config'; | ||
import { axiosConfig } from '../utils/jwt'; | ||
|
||
const { serverRuntimeConfig } = getConfig(); | ||
const BACKEND_HOST = serverRuntimeConfig.backendHost; | ||
|
||
export async function getAdvertBySlug( | ||
jwt: string, | ||
slug: string | ||
): Promise<AdvertDto> { | ||
const { data } = await axios.get<AdvertDto>( | ||
`${BACKEND_HOST}/grant-adverts?contentfulSlug=${slug}`, | ||
axiosConfig(jwt) | ||
); | ||
return data; | ||
} | ||
|
||
export interface AdvertDto { | ||
id: string; | ||
externalSubmissionUrl: string; | ||
version: number; | ||
grantApplicationId: number | null; | ||
isInternal: boolean; | ||
grantSchemeId: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters