diff --git a/src/config/index.ts b/src/config/index.ts index e10239d..23c1189 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -60,6 +60,10 @@ export const CONTACT_US_URL = '/contact-us'; export const SERVICE_URL = `${BASE_URL}${HOME_URL}`; +// Query Params + +export const PREVIOUS_PAGE_QUERY_PARAM = 'previousPage'; + // Feature Flags export const FEATURE_FLAG_ENABLE_AUTH = getEnvironmentValue('FEATURE_FLAG_ENABLE_AUTH', 'false'); diff --git a/src/config/nunjucks.ts b/src/config/nunjucks.ts index d0a5006..bb545e6 100644 --- a/src/config/nunjucks.ts +++ b/src/config/nunjucks.ts @@ -14,10 +14,17 @@ export const configureNunjucks = (app: express.Application, viewsPath: string) = express: app } ); - + nunjucksEnv.addGlobal('PARAMS', { + previousPage: config.PREVIOUS_PAGE_QUERY_PARAM + }); + nunjucksEnv.addGlobal('ROUTES', { + HOME: config.HOME_URL, + CHECK_YOUR_REQUESTS_URL: config.CHECK_YOUR_REQUESTS_URL + }); nunjucksEnv.addGlobal('CDN_HOST', config.CDN_HOST); nunjucksEnv.addGlobal('SERVICE_URL', config.SERVICE_URL); nunjucksEnv.addGlobal('SERVICE_NAME', config.SERVICE_NAME); nunjucksEnv.addGlobal('DEPARTMENT_NAME', config.DEPARTMENT_NAME); nunjucksEnv.addGlobal('GITHUB_ORG_NAME', config.GITHUB_ORG_NAME); + }; diff --git a/src/controller/add-collaborator.controller.ts b/src/controller/add-collaborator.controller.ts index 29d292c..787cf1b 100644 --- a/src/controller/add-collaborator.controller.ts +++ b/src/controller/add-collaborator.controller.ts @@ -9,6 +9,7 @@ import { v4 as uuidv4 } from 'uuid'; import * as config from '../config'; import { log } from '../utils/logger'; +import { getPreviousPageUrl } from '../utils/getPreviousPageUrl'; import { AddCollaborator, AddCollaboratorKey } from '../model/add-collaborator.model'; @@ -60,7 +61,7 @@ export const postById = (req: Request, res: Response, next: NextFunction) => { setApplicationDataByID(req.session, { ...req.body, [config.ID]: collaboratorID }, AddCollaboratorKey, collaboratorID); - return res.redirect(config.HOME_URL); + return res.redirect(getPreviousPageUrl(req)); } catch (err: any) { log.errorRequest(req, err.message); next(err); diff --git a/src/controller/add-member.controller.ts b/src/controller/add-member.controller.ts index 313674d..1b9d08a 100644 --- a/src/controller/add-member.controller.ts +++ b/src/controller/add-member.controller.ts @@ -9,6 +9,7 @@ import { v4 as uuidv4 } from 'uuid'; import * as config from '../config'; import { log } from '../utils/logger'; +import { getPreviousPageUrl } from '../utils/getPreviousPageUrl'; import { AddMember, AddMemberKey } from '../model/add-member.model'; @@ -71,7 +72,7 @@ export const postById = (req: Request, res: Response, next: NextFunction) => { AddMemberKey, memberID ); - return res.redirect(config.HOME_URL); + return res.redirect(getPreviousPageUrl(req)); } catch (err: any) { log.errorRequest(req, err.message); next(err); diff --git a/src/controller/add-repo.controller.ts b/src/controller/add-repo.controller.ts index 333f9eb..a651068 100644 --- a/src/controller/add-repo.controller.ts +++ b/src/controller/add-repo.controller.ts @@ -9,6 +9,7 @@ import { v4 as uuidv4 } from 'uuid'; import * as config from '../config'; import { log } from '../utils/logger'; +import { getPreviousPageUrl } from '../utils/getPreviousPageUrl'; import { AddRepo, AddRepoKey } from '../model/add-repo.model'; @@ -56,7 +57,7 @@ export const postById = (req: Request, res: Response, next: NextFunction) => { setApplicationDataByID(req.session, { ...req.body, [config.ID]: repoID }, AddRepoKey, repoID); - return res.redirect(config.HOME_URL); + return res.redirect(getPreviousPageUrl(req)); } catch (err: any) { log.errorRequest(req, err.message); next(err); diff --git a/src/controller/add-team-member.controller.ts b/src/controller/add-team-member.controller.ts index d9f81e6..4147c75 100644 --- a/src/controller/add-team-member.controller.ts +++ b/src/controller/add-team-member.controller.ts @@ -11,6 +11,7 @@ import * as config from '../config'; import { log } from '../utils/logger'; import { AddTeamMember, AddTeamMemberKey } from '../model/add-team-member.model'; +import { getPreviousPageUrl } from '../utils/getPreviousPageUrl'; export const get = (_req: Request, res: Response) => { return res.render(config.ADD_TEAM_MEMBER); @@ -57,7 +58,7 @@ export const postById = (req: Request, res: Response, next: NextFunction) => { setApplicationDataByID(req.session, { ...req.body, [config.ID]: teamMembersID }, AddTeamMemberKey, teamMembersID); - return res.redirect(config.HOME_URL); + return res.redirect(getPreviousPageUrl(req)); } catch (err: any) { log.errorRequest(req, err.message); next(err); diff --git a/src/controller/add-team.controller.ts b/src/controller/add-team.controller.ts index 0b47e0a..b51cac8 100644 --- a/src/controller/add-team.controller.ts +++ b/src/controller/add-team.controller.ts @@ -11,6 +11,7 @@ import * as config from '../config'; import { log } from '../utils/logger'; import { AddTeam, AddTeamKey } from '../model/add-team.model'; +import { getPreviousPageUrl } from '../utils/getPreviousPageUrl'; export const get = (_req: Request, res: Response) => { return res.render(config.ADD_TEAM); @@ -56,7 +57,7 @@ export const postById = (req: Request, res: Response, next: NextFunction) => { setApplicationDataByID(req.session, { ...req.body, [config.ID]: teamID }, AddTeamKey, teamID); - return res.redirect(config.HOME_URL); + return res.redirect(getPreviousPageUrl(req)); } catch (err: any) { log.errorRequest(req, err.message); next(err); diff --git a/src/controller/additional-requests.controller.ts b/src/controller/additional-requests.controller.ts index 2190dad..541a056 100644 --- a/src/controller/additional-requests.controller.ts +++ b/src/controller/additional-requests.controller.ts @@ -11,6 +11,7 @@ import * as config from '../config'; import { log } from '../utils/logger'; import { AdditionalRequests, AdditionalRequestsKey } from '../model/additional-requests.model'; +import { getPreviousPageUrl } from '../utils/getPreviousPageUrl'; export const get = (_req: Request, res: Response) => { return res.render(config.ADDITIONAL_REQUESTS); @@ -55,7 +56,7 @@ export const postById = (req: Request, res: Response, next: NextFunction) => { setApplicationDataByID(req.session, { ...req.body, [config.ID]: additionalRequestsID }, AdditionalRequestsKey, additionalRequestsID); - return res.redirect(config.HOME_URL); + return res.redirect(getPreviousPageUrl(req)); } catch (err: any) { log.errorRequest(req, err.message); next(err); diff --git a/src/utils/getPreviousPageUrl.ts b/src/utils/getPreviousPageUrl.ts new file mode 100644 index 0000000..6046449 --- /dev/null +++ b/src/utils/getPreviousPageUrl.ts @@ -0,0 +1,4 @@ +import { Request } from 'express'; +import * as config from '../config'; + +export const getPreviousPageUrl = (req: Request): string => (config.PREVIOUS_PAGE_QUERY_PARAM in req.query) ? config.CHECK_YOUR_REQUESTS_URL : config.HOME_URL; diff --git a/src/views/include/check-your-requests/add-collaborator.html b/src/views/include/check-your-requests/add-collaborator.html index ff3e61c..b3d1049 100644 --- a/src/views/include/check-your-requests/add-collaborator.html +++ b/src/views/include/check-your-requests/add-collaborator.html @@ -1,99 +1,101 @@ +{% set previousPageQueryParam = "?" + PARAMS.previousPage + "=" + ROUTES.CHECK_YOUR_REQUESTS_URL %} + {% for collaborator in add_collaborator %} -{% set firstNameHref = "/add-collaborator/" + collaborator.id + "#first_name" %} -{% set lastNameHref = "/add-collaborator/" + collaborator.id + "#last_name" %} -{% set githubHandleHref = "/add-collaborator/" + collaborator.id + "#github_handle" %} -{% set emailAddressHref = "/add-collaborator/" + collaborator.id + "#email_address" %} -{% set repoHref = "/add-collaborator/" + collaborator.id + "#repo_name" %} + {% set firstNameHref = "/add-collaborator/" + collaborator.id + previousPageQueryParam + "#first_name" %} + {% set lastNameHref = "/add-collaborator/" + collaborator.id + previousPageQueryParam + "#last_name" %} + {% set githubHandleHref = "/add-collaborator/" + collaborator.id + previousPageQueryParam + "#github_handle" %} + {% set emailAddressHref = "/add-collaborator/" + collaborator.id + previousPageQueryParam + "#email_address" %} + {% set repoHref = "/add-collaborator/" + collaborator.id + previousPageQueryParam + "#repo_name" %} -
-{{ govukSummaryList({ - rows: [ - { - key: { - text: "First Name" - }, - value: { - text: collaborator.first_name - }, - actions: { - items: [ - { - href: firstNameHref, - text: "Change", - visuallyHiddenText: "First Name" - } - ] - } - }, - { - key: { - text: "Last Name" - }, - value: { - text: collaborator.last_name +
+ {{ govukSummaryList({ + rows: [ + { + key: { + text: "First Name" + }, + value: { + text: collaborator.first_name + }, + actions: { + items: [ + { + href: firstNameHref, + text: "Change", + visuallyHiddenText: "First Name" + } + ] + } }, - actions: { - items: [ - { - href: lastNameHref, - text: "Change", - visuallyHiddenText: "Last Name" - } - ] - } - }, - { - key: { - text: "GitHub Handle" + { + key: { + text: "Last Name" + }, + value: { + text: collaborator.last_name + }, + actions: { + items: [ + { + href: lastNameHref, + text: "Change", + visuallyHiddenText: "Last Name" + } + ] + } }, - value: { - text: collaborator.github_handle - }, - actions: { - items: [ - { - href: githubHandleHref, - text: "Change", - visuallyHiddenText: "Github Handle" - } - ] - } - }, - { - key: { - text: "Email Address" - }, - value: { - text: collaborator.email_address - }, - actions: { - items: [ - { - href: emailAddressHref, - text: "Change", - visuallyHiddenText: "Email Address" - } - ] - } - }, - { - key: { - text: "Repository Name" + { + key: { + text: "GitHub Handle" + }, + value: { + text: collaborator.github_handle + }, + actions: { + items: [ + { + href: githubHandleHref, + text: "Change", + visuallyHiddenText: "Github Handle" + } + ] + } }, - value: { - text: collaborator.repo_name + { + key: { + text: "Email Address" + }, + value: { + text: collaborator.email_address + }, + actions: { + items: [ + { + href: emailAddressHref, + text: "Change", + visuallyHiddenText: "Email Address" + } + ] + } }, - actions: { - items: [ - { - href: repoHref, - text: "Change", - visuallyHiddenText: "Repository name" - } - ] + { + key: { + text: "Repository Name" + }, + value: { + text: collaborator.repo_name + }, + actions: { + items: [ + { + href: repoHref, + text: "Change", + visuallyHiddenText: "Repository name" + } + ] + } } - } - ] -}) }} + ] + }) }} {% endfor %} \ No newline at end of file diff --git a/src/views/include/check-your-requests/add-member.html b/src/views/include/check-your-requests/add-member.html index e675487..4960c9e 100644 --- a/src/views/include/check-your-requests/add-member.html +++ b/src/views/include/check-your-requests/add-member.html @@ -1,117 +1,119 @@ +{% set previousPageQueryParam = "?" + PARAMS.previousPage + "=" + ROUTES.CHECK_YOUR_REQUESTS_URL %} + {% for member in add_member %} -{% set firstNameHref = "/add-member/" + member.id + "#first_name" %} -{% set lastNameHref = "/add-member/" + member.id + "#last_name" %} -{% set githubHandleHref = "/add-member/" + member.id + "#github_handle" %} -{% set emailAddressHref = "/add-member/" + member.id + "#email_address" %} -{% set contractTypeHref = "/add-member/" + member.id + "#contract_type" %} -{% set descriptionHref = "/add-member/" + member.id + "#description" %} + {% set firstNameHref = "/add-member/" + member.id + previousPageQueryParam + "#first_name" %} + {% set lastNameHref = "/add-member/" + member.id + previousPageQueryParam + "#last_name" %} + {% set githubHandleHref = "/add-member/" + member.id + previousPageQueryParam + "#github_handle" %} + {% set emailAddressHref = "/add-member/" + member.id + previousPageQueryParam + "#email_address" %} + {% set contractTypeHref = "/add-member/" + member.id + previousPageQueryParam + "#contract_type" %} + {% set descriptionHref = "/add-member/" + member.id + previousPageQueryParam + "#description" %} -
-{{ govukSummaryList({ - rows: [ - { - key: { - text: "First Name" - }, - value: { - text: member.first_name +
+ {{ govukSummaryList({ + rows: [ + { + key: { + text: "First Name" + }, + value: { + text: member.first_name + }, + actions: { + items: [ + { + href: firstNameHref, + text: "Change", + visuallyHiddenText: "First Name" + } + ] + } }, - actions: { - items: [ - { - href: firstNameHref, - text: "Change", - visuallyHiddenText: "First Name" - } - ] - } - }, - { - key: { - text: "Last Name" + { + key: { + text: "Last Name" + }, + value: { + text: member.last_name + }, + actions: { + items: [ + { + href: lastNameHref, + text: "Change", + visuallyHiddenText: "Last Name" + } + ] + } }, - value: { - text: member.last_name + { + key: { + text: "GitHub Handle" + }, + value: { + text: member.github_handle + }, + actions: { + items: [ + { + href: githubHandleHref, + text: "Change", + visuallyHiddenText: "Github Handle" + } + ] + } }, - actions: { - items: [ - { - href: lastNameHref, - text: "Change", - visuallyHiddenText: "Last Name" - } - ] - } - }, - { - key: { - text: "GitHub Handle" - }, - value: { - text: member.github_handle - }, - actions: { - items: [ - { - href: githubHandleHref, - text: "Change", - visuallyHiddenText: "Github Handle" - } - ] - } - }, - { - key: { - text: "Email Address" - }, - value: { - text: member.email_address - }, - actions: { - items: [ - { - href: emailAddressHref, - text: "Change", - visuallyHiddenText: "Email Address" - } - ] - } - }, - { - key: { - text: "Contract type" - }, - value: { - text: member.contract_type | capitalize | replace("_", "-") - }, - actions: { - items: [ - { - href: contractTypeHref, - text: "Change", - visuallyHiddenText: "Contract type" - } - ] - } - }, - { - key: { - text: "Description" + { + key: { + text: "Email Address" + }, + value: { + text: member.email_address + }, + actions: { + items: [ + { + href: emailAddressHref, + text: "Change", + visuallyHiddenText: "Email Address" + } + ] + } }, - value: { - html: member.description + { + key: { + text: "Contract type" + }, + value: { + text: member.contract_type | capitalize | replace("_", "-") + }, + actions: { + items: [ + { + href: contractTypeHref, + text: "Change", + visuallyHiddenText: "Contract type" + } + ] + } }, - actions: { - items: [ - { - href: descriptionHref, - text: "Change", - visuallyHiddenText: "description" - } - ] + { + key: { + text: "Description" + }, + value: { + html: member.description + }, + actions: { + items: [ + { + href: descriptionHref, + text: "Change", + visuallyHiddenText: "description" + } + ] + } } - } - ] -}) }} + ] + }) }} {% endfor %} \ No newline at end of file diff --git a/src/views/include/check-your-requests/add-repo.html b/src/views/include/check-your-requests/add-repo.html index 0e53ac1..888910b 100644 --- a/src/views/include/check-your-requests/add-repo.html +++ b/src/views/include/check-your-requests/add-repo.html @@ -1,63 +1,65 @@ +{% set previousPageQueryParam = "?" + PARAMS.previousPage + "=" + ROUTES.CHECK_YOUR_REQUESTS_URL %} + {% for repo in add_repo %} -{% set repoHref = "/add-repo/" + repo.id + "#repo_name" %} -{% set visibilityHref = "/add-repo/" + repo.id + "#visibility" %} -{% set descriptionHref = "/add-repo/" + repo.id + "#description" %} + {% set repoHref = "/add-repo/" + repo.id + previousPageQueryParam + "#repo_name" %} + {% set visibilityHref = "/add-repo/" + repo.id + previousPageQueryParam + "#visibility" %} + {% set descriptionHref = "/add-repo/" + repo.id + previousPageQueryParam + "#description" %} -
-{{ govukSummaryList({ - rows: [ - { - key: { - text: "Repository name" - }, - value: { - text: repo.repo_name - }, - actions: { - items: [ - { - href: repoHref, - text: "Change", - visuallyHiddenText: "Repository name" - } - ] - } - }, - { - key: { - text: "Repository visibility" - }, - value: { - text: repo.visibility | capitalize - }, - actions: { - items: [ - { - href: visibilityHref, - text: "Change", - visuallyHiddenText: "Repository visibility" - } - ] - } - }, - { - key: { - text: "Description" +
+ {{ govukSummaryList({ + rows: [ + { + key: { + text: "Repository name" + }, + value: { + text: repo.repo_name + }, + actions: { + items: [ + { + href: repoHref, + text: "Change", + visuallyHiddenText: "Repository name" + } + ] + } }, - value: { - html: repo.description + { + key: { + text: "Repository visibility" + }, + value: { + text: repo.visibility | capitalize + }, + actions: { + items: [ + { + href: visibilityHref, + text: "Change", + visuallyHiddenText: "Repository visibility" + } + ] + } }, - actions: { - items: [ - { - href: descriptionHref, - text: "Change", - visuallyHiddenText: "description" - } - ] + { + key: { + text: "Description" + }, + value: { + html: repo.description + }, + actions: { + items: [ + { + href: descriptionHref, + text: "Change", + visuallyHiddenText: "description" + } + ] + } } - } - ] -}) }} + ] + }) }} {% endfor %} \ No newline at end of file diff --git a/src/views/include/check-your-requests/add-team-member.html b/src/views/include/check-your-requests/add-team-member.html index aa04443..9b51ac7 100644 --- a/src/views/include/check-your-requests/add-team-member.html +++ b/src/views/include/check-your-requests/add-team-member.html @@ -1,45 +1,47 @@ +{% set previousPageQueryParam = "?" + PARAMS.previousPage + "=" + ROUTES.CHECK_YOUR_REQUESTS_URL %} + {% for team_member in add_team_member %} -{% set teamNameHref = "/add-team-member/" + teamMember.id + "#team_name" %} -{% set githubHandlesHref = "/add-team-member/" + teamMember.id + "#github_handles" %} + {% set teamNameHref = "/add-team-member/" + team_member.id + previousPageQueryParam + "#team_name" %} + {% set githubHandlesHref = "/add-team-member/" + team_member.id + previousPageQueryParam + "#github_handles" %} -
-{{ govukSummaryList({ - rows: [ - { - key: { - text: "Team name" - }, - value: { - text: team_member.team_name - }, - actions: { - items: [ - { - href: teamNameHref, - text: "Change", - visuallyHiddenText: "Team name" - } - ] - } - }, - { - key: { - text: "GitHub Handle(s)" - }, - value: { - text: team_member.github_handles +
+ {{ govukSummaryList({ + rows: [ + { + key: { + text: "Team name" + }, + value: { + text: team_member.team_name + }, + actions: { + items: [ + { + href: teamNameHref, + text: "Change", + visuallyHiddenText: "Team name" + } + ] + } }, - actions: { - items: [ - { - href: githubHandlesHref, - text: "Change", - visuallyHiddenText: "Github Handles" - } - ] + { + key: { + text: "GitHub Handle(s)" + }, + value: { + text: team_member.github_handles + }, + actions: { + items: [ + { + href: githubHandlesHref, + text: "Change", + visuallyHiddenText: "Github Handles" + } + ] + } } - } - ] -}) }} + ] + }) }} {% endfor %} \ No newline at end of file diff --git a/src/views/include/check-your-requests/add-team.html b/src/views/include/check-your-requests/add-team.html index 16047ed..128f763 100644 --- a/src/views/include/check-your-requests/add-team.html +++ b/src/views/include/check-your-requests/add-team.html @@ -1,63 +1,66 @@ +{% set previousPageQueryParam = "?" + PARAMS.previousPage + "=" + ROUTES.CHECK_YOUR_REQUESTS_URL %} + + {% for team in add_team %} -{% set teamHref = "/add-team/" + team.id + "#team_name" %} -{% set githubHandleHref = "/add-team/" + team.id + "#github_handle" %} -{% set descriptionHref = "/add-team/" + team.id + "#description" %} + {% set teamHref = "/add-team/" + team.id + previousPageQueryParam + "#team_name" %} + {% set githubHandleHref = "/add-team/" + team.id + previousPageQueryParam + "#github_handle" %} + {% set descriptionHref = "/add-team/" + team.id + previousPageQueryParam + "#description" %} -
-{{ govukSummaryList({ - rows: [ - { - key: { - text: "Team name" - }, - value: { - text: team.team_name - }, - actions: { - items: [ - { - href: teamHref, - text: "Change", - visuallyHiddenText: "Team name" - } - ] - } - }, - { - key: { - text: "GitHub Handle" - }, - value: { - text: team.github_handle - }, - actions: { - items: [ - { - href: githubHandleHref, - text: "Change", - visuallyHiddenText: "Github Handle" - } - ] - } - }, - { - key: { - text: "Description" +
+ {{ govukSummaryList({ + rows: [ + { + key: { + text: "Team name" + }, + value: { + text: team.team_name + }, + actions: { + items: [ + { + href: teamHref, + text: "Change", + visuallyHiddenText: "Team name" + } + ] + } }, - value: { - html: team.description + { + key: { + text: "GitHub Handle" + }, + value: { + text: team.github_handle + }, + actions: { + items: [ + { + href: githubHandleHref, + text: "Change", + visuallyHiddenText: "Github Handle" + } + ] + } }, - actions: { - items: [ - { - href: descriptionHref, - text: "Change", - visuallyHiddenText: "description" - } - ] + { + key: { + text: "Description" + }, + value: { + html: team.description + }, + actions: { + items: [ + { + href: descriptionHref, + text: "Change", + visuallyHiddenText: "description" + } + ] + } } - } - ] -}) }} + ] + }) }} {% endfor %} \ No newline at end of file diff --git a/src/views/include/check-your-requests/additional-requests.html b/src/views/include/check-your-requests/additional-requests.html index 3be898a..42dc8ba 100644 --- a/src/views/include/check-your-requests/additional-requests.html +++ b/src/views/include/check-your-requests/additional-requests.html @@ -1,45 +1,47 @@ +{% set previousPageQueryParam = "?" + PARAMS.previousPage + "=" + ROUTES.CHECK_YOUR_REQUESTS_URL %} + {% for additional_request in additional_requests %} -{% set contextHref = "/additional-requests/" + additional_request.id + "#context" %} -{% set descriptionHref = "/additional-requests/" + additional_request.id + "#description" %} + {% set contextHref = "/additional-requests/" + additional_request.id + previousPageQueryParam + "#context" %} + {% set descriptionHref = "/additional-requests/" + additional_request.id + previousPageQueryParam + "#description" %} -
-{{ govukSummaryList({ - rows: [ - { - key: { - text: "Context" - }, - value: { - text: additional_request.context | capitalize - }, - actions: { - items: [ - { - href: contextHref, - text: "Change", - visuallyHiddenText: "Context" - } - ] - } - }, - { - key: { - text: "Description" - }, - value: { - html: additional_request.description +
+ {{ govukSummaryList({ + rows: [ + { + key: { + text: "Context" + }, + value: { + text: additional_request.context | capitalize + }, + actions: { + items: [ + { + href: contextHref, + text: "Change", + visuallyHiddenText: "Context" + } + ] + } }, - actions: { - items: [ - { - href: descriptionHref, - text: "Change", - visuallyHiddenText: "description" - } - ] + { + key: { + text: "Description" + }, + value: { + html: additional_request.description + }, + actions: { + items: [ + { + href: descriptionHref, + text: "Change", + visuallyHiddenText: "description" + } + ] + } } - } - ] -}) }} + ] + }) }} {% endfor %} \ No newline at end of file diff --git a/test/unit/controller/add-collaborator.controller.spec.ts b/test/unit/controller/add-collaborator.controller.spec.ts index 5131cca..0d86620 100644 --- a/test/unit/controller/add-collaborator.controller.spec.ts +++ b/test/unit/controller/add-collaborator.controller.spec.ts @@ -1,4 +1,5 @@ jest.mock('../../../src/utils/logger'); +jest.mock('../../../src/utils/getPreviousPageUrl'); jest.mock('@co-digital/login'); jest.mock('uuid'); @@ -8,6 +9,8 @@ import { get, getById, post, postById, removeById } from '../../../src/controlle import { AddCollaboratorKey } from '../../../src/model/add-collaborator.model'; import * as config from '../../../src/config'; +import { getPreviousPageUrl } from '../../../src/utils/getPreviousPageUrl'; + import { MOCK_POST_ADD_COLLABORATOR } from '../../mock/data'; import { MOCK_POST_ADD_COLLABORATOR_RESPONSE, @@ -33,6 +36,8 @@ import { mockLogErrorRequest } from '../../mock/log.mock'; +const mockGetPreviousPageUrl = getPreviousPageUrl as jest.Mock; + describe('add-collaborator controller test suites', () => { afterEach(() => { @@ -94,12 +99,16 @@ describe('add-collaborator controller test suites', () => { describe('add-collaborator POST ById tests', () => { - test('should redirect to home page on POST ById request', () => { + test('should redirect to previous page on POST ById request', () => { + + mockGetPreviousPageUrl.mockReturnValue(config.CHECK_YOUR_REQUESTS_URL); + const res = mockResponse(); const req = { ...mockRequest(MOCK_POST_ADD_COLLABORATOR), session: {}, - params: { id: mockID } + params: { id: mockID }, + query: { previousPage: config.CHECK_YOUR_REQUESTS_URL } } as any; postById(req, res, mockNext); @@ -108,8 +117,9 @@ describe('add-collaborator controller test suites', () => { id: mockID, ...MOCK_POST_ADD_COLLABORATOR }, AddCollaboratorKey, mockID); + expect(mockGetPreviousPageUrl).toHaveBeenCalledWith(req); - expect(res.redirect).toBeCalledWith(config.HOME_URL); + expect(res.redirect).toBeCalledWith(config.CHECK_YOUR_REQUESTS_URL); expect(mockNext).not.toHaveBeenCalled(); }); diff --git a/test/unit/controller/add-member.controller.spec.ts b/test/unit/controller/add-member.controller.spec.ts index 3c17321..caa369d 100644 --- a/test/unit/controller/add-member.controller.spec.ts +++ b/test/unit/controller/add-member.controller.spec.ts @@ -1,4 +1,5 @@ jest.mock('../../../src/utils/logger'); +jest.mock('../../../src/utils/getPreviousPageUrl'); jest.mock('@co-digital/login'); jest.mock('uuid'); @@ -8,6 +9,8 @@ import { get, getById, post, postById, removeById } from '../../../src/controlle import { AddMemberKey } from '../../../src/model/add-member.model'; import * as config from '../../../src/config'; +import { getPreviousPageUrl } from '../../../src/utils/getPreviousPageUrl'; + import { MOCK_POST_ADD_MEMBER } from '../../mock/data'; import { MOCK_POST_ADD_MEMBER_RESPONSE, @@ -33,6 +36,8 @@ import { mockLogErrorRequest } from '../../mock/log.mock'; +const mockGetPreviousPageUrl = getPreviousPageUrl as jest.Mock; + describe('add-member controller test suites', () => { afterEach(() => { @@ -110,12 +115,16 @@ describe('add-member controller test suites', () => { describe('add-member POST ById tests', () => { - test('should redirect to home page on POST ById request', () => { + test('should redirect to previous page on POST ById request', () => { + + mockGetPreviousPageUrl.mockReturnValue(config.CHECK_YOUR_REQUESTS_URL); + const res = mockResponse(); const req = { ...mockRequest(MOCK_POST_ADD_MEMBER), session: {}, - params: { id: mockID } + params: { id: mockID }, + query: { previousPage: config.CHECK_YOUR_REQUESTS_URL } } as any; postById(req, res, mockNext); @@ -124,8 +133,9 @@ describe('add-member controller test suites', () => { id: mockID, ...MOCK_POST_ADD_MEMBER }, AddMemberKey, mockID); + expect(mockGetPreviousPageUrl).toHaveBeenCalledWith(req); - expect(res.redirect).toBeCalledWith(config.HOME_URL); + expect(res.redirect).toBeCalledWith(config.CHECK_YOUR_REQUESTS_URL); expect(mockNext).not.toHaveBeenCalled(); }); diff --git a/test/unit/controller/add-repo.controller.spec.ts b/test/unit/controller/add-repo.controller.spec.ts index 4d1721b..4e63b7d 100644 --- a/test/unit/controller/add-repo.controller.spec.ts +++ b/test/unit/controller/add-repo.controller.spec.ts @@ -1,4 +1,5 @@ jest.mock('../../../src/utils/logger'); +jest.mock('../../../src/utils/getPreviousPageUrl'); jest.mock('@co-digital/login'); jest.mock('uuid'); @@ -8,6 +9,8 @@ import { get, getById, post, postById, removeById } from '../../../src/controlle import { AddRepoKey } from '../../../src/model/add-repo.model'; import * as config from '../../../src/config'; +import { getPreviousPageUrl } from '../../../src/utils/getPreviousPageUrl'; + import { MOCK_POST_ADD_REPO } from '../../mock/data'; import { MOCK_LOG_ERROR_REQUEST, MOCK_ADD_REPO_RESPONSE, MOCK_BY_ID_REPO_RESPONSE } from '../../mock/text.mock'; import { mockBadRequest, mockRequest, mockResponse, mockNext } from '../../mock/express.mock'; @@ -25,6 +28,8 @@ import { mockLogErrorRequest } from '../../mock/log.mock'; +const mockGetPreviousPageUrl = getPreviousPageUrl as jest.Mock; + describe('Add-repo controller test suites', () => { afterEach(() => { jest.resetAllMocks(); @@ -86,12 +91,16 @@ describe('Add-repo controller test suites', () => { describe('add-repo POST ById tests', () => { - test('should redirect to home page on POST ById request', () => { + test('should redirect to previous page on POST ById request', () => { + + mockGetPreviousPageUrl.mockReturnValue(config.CHECK_YOUR_REQUESTS_URL); + const res = mockResponse(); const req = { ...mockRequest(MOCK_POST_ADD_REPO), session: {}, - params: { id: mockID } + params: { id: mockID }, + query: { previousPage: config.CHECK_YOUR_REQUESTS_URL } } as any; postById(req, res, mockNext); @@ -100,8 +109,9 @@ describe('Add-repo controller test suites', () => { id: mockID, ...MOCK_POST_ADD_REPO }, AddRepoKey, mockID); + expect(mockGetPreviousPageUrl).toHaveBeenCalledWith(req); - expect(res.redirect).toBeCalledWith(config.HOME_URL); + expect(res.redirect).toBeCalledWith(config.CHECK_YOUR_REQUESTS_URL); expect(mockNext).not.toHaveBeenCalled(); }); diff --git a/test/unit/controller/add-team-member.controller.spec.ts b/test/unit/controller/add-team-member.controller.spec.ts index 475e803..799b7b9 100644 --- a/test/unit/controller/add-team-member.controller.spec.ts +++ b/test/unit/controller/add-team-member.controller.spec.ts @@ -1,4 +1,5 @@ jest.mock('../../../src/utils/logger'); +jest.mock('../../../src/utils/getPreviousPageUrl'); jest.mock('@co-digital/login'); jest.mock('uuid'); @@ -6,9 +7,10 @@ import { describe, expect, afterEach, test, jest } from '@jest/globals'; import { get, getById, post, postById, removeById } from '../../../src/controller/add-team-member.controller'; import { AddTeamMemberKey } from '../../../src/model/add-team-member.model'; - import * as config from '../../../src/config'; +import { getPreviousPageUrl } from '../../../src/utils/getPreviousPageUrl'; + import { MOCK_POST_ADD_TEAM_MEMBER } from '../../mock/data'; import { MOCK_POST_ADD_TEAM_MEMBER_RESPONSE, MOCK_LOG_ERROR_REQUEST, MOCK_BY_ID_TEAM_MEMBER_RESPONSE } from '../../mock/text.mock'; import { mockRequest, mockResponse, mockNext, mockBadRequest } from '../../mock/express.mock'; @@ -27,6 +29,8 @@ import { mockLogErrorRequest } from '../../mock/log.mock'; +const mockGetPreviousPageUrl = getPreviousPageUrl as jest.Mock; + describe('add-team-member controller test suites', () => { afterEach(() => { jest.resetAllMocks(); @@ -128,12 +132,16 @@ describe('add-team-member controller test suites', () => { describe('add-team-member POST ById tests', () => { - test('should redirect to home page on POST ById request', () => { + test('should redirect to previous page on POST ById request', () => { + + mockGetPreviousPageUrl.mockReturnValue(config.CHECK_YOUR_REQUESTS_URL); + const res = mockResponse(); const req = { ...mockRequest(MOCK_POST_ADD_TEAM_MEMBER), session: {}, - params: { id: mockID } + params: { id: mockID }, + query: { previousPage: config.CHECK_YOUR_REQUESTS_URL } } as any; postById(req, res, mockNext); @@ -142,8 +150,9 @@ describe('add-team-member controller test suites', () => { id: mockID, ...MOCK_POST_ADD_TEAM_MEMBER }, AddTeamMemberKey, mockID); + expect(mockGetPreviousPageUrl).toHaveBeenCalledWith(req); - expect(res.redirect).toBeCalledWith(config.HOME_URL); + expect(res.redirect).toBeCalledWith(config.CHECK_YOUR_REQUESTS_URL); expect(mockNext).not.toHaveBeenCalled(); }); diff --git a/test/unit/controller/add-team.controller.spec.ts b/test/unit/controller/add-team.controller.spec.ts index 53ef054..3d68db6 100644 --- a/test/unit/controller/add-team.controller.spec.ts +++ b/test/unit/controller/add-team.controller.spec.ts @@ -1,4 +1,5 @@ jest.mock('../../../src/utils/logger'); +jest.mock('../../../src/utils/getPreviousPageUrl'); jest.mock('@co-digital/login'); jest.mock('uuid'); @@ -8,6 +9,8 @@ import { get, getById, post, postById, removeById } from '../../../src/controlle import { AddTeamKey } from '../../../src/model/add-team.model'; import * as config from '../../../src/config'; +import { getPreviousPageUrl } from '../../../src/utils/getPreviousPageUrl'; + import { MOCK_POST_ADD_TEAM } from '../../mock/data'; import { MOCK_POST_ADD_TEAM_RESPONSE, MOCK_LOG_ERROR_REQUEST, MOCK_BY_ID_TEAM_RESPONSE } from '../../mock/text.mock'; import { mockRequest, mockResponse, mockNext, mockBadRequest } from '../../mock/express.mock'; @@ -25,6 +28,8 @@ import { mockLogErrorRequest } from '../../mock/log.mock'; +const mockGetPreviousPageUrl = getPreviousPageUrl as jest.Mock; + describe('add-team controller test suites', () => { afterEach(() => { @@ -88,12 +93,16 @@ describe('add-team controller test suites', () => { describe('add-team POST ById tests', () => { - test('should redirect to home page on POST ById request', () => { + test('should redirect to previous page on POST ById request', () => { + + mockGetPreviousPageUrl.mockReturnValue(config.CHECK_YOUR_REQUESTS_URL); + const res = mockResponse(); const req = { ...mockRequest(MOCK_POST_ADD_TEAM), session: {}, - params: { id: mockID } + params: { id: mockID }, + query: { previousPage: config.CHECK_YOUR_REQUESTS_URL } } as any; postById(req, res, mockNext); @@ -102,8 +111,9 @@ describe('add-team controller test suites', () => { id: mockID, ...MOCK_POST_ADD_TEAM }, AddTeamKey, mockID); + expect(mockGetPreviousPageUrl).toHaveBeenCalledWith(req); - expect(res.redirect).toBeCalledWith(config.HOME_URL); + expect(res.redirect).toBeCalledWith(config.CHECK_YOUR_REQUESTS_URL); expect(mockNext).not.toHaveBeenCalled(); }); diff --git a/test/unit/controller/additional-requests.controller.spec.ts b/test/unit/controller/additional-requests.controller.spec.ts index 28ec524..ca84f5e 100644 --- a/test/unit/controller/additional-requests.controller.spec.ts +++ b/test/unit/controller/additional-requests.controller.spec.ts @@ -1,4 +1,5 @@ jest.mock('../../../src/utils/logger'); +jest.mock('../../../src/utils/getPreviousPageUrl'); jest.mock('@co-digital/login'); jest.mock('uuid'); @@ -8,6 +9,8 @@ import { get, getById, post, postById, removeById } from '../../../src/controlle import { AdditionalRequestsKey } from '../../../src/model/additional-requests.model'; import * as config from '../../../src/config'; +import { getPreviousPageUrl } from '../../../src/utils/getPreviousPageUrl'; + import { MOCK_POST_ADDITIONAL_REQUESTS } from '../../mock/data'; import { MOCK_POST_ADDITIONAL_REQUESTS_RESPONSE, MOCK_LOG_ERROR_REQUEST, MOCK_BY_ID_ADDITIONAL_REQUESTS_RESPONSE } from '../../mock/text.mock'; import { mockRequest, mockResponse, mockNext, mockBadRequest } from '../../mock/express.mock'; @@ -26,6 +29,8 @@ import { mockLogErrorRequest } from '../../mock/log.mock'; +const mockGetPreviousPageUrl = getPreviousPageUrl as jest.Mock; + describe('additional-requests controller test suites', () => { afterEach(() => { @@ -86,7 +91,10 @@ describe('additional-requests controller test suites', () => { describe('additional-requests POST ById tests', () => { - test('should redirect to home page on POST ById request', () => { + test('should redirect to previous page on POST ById request', () => { + + mockGetPreviousPageUrl.mockReturnValue(config.CHECK_YOUR_REQUESTS_URL); + const res = mockResponse(); const req = { ...mockRequest(MOCK_POST_ADDITIONAL_REQUESTS), @@ -100,8 +108,9 @@ describe('additional-requests controller test suites', () => { id: mockID, ...MOCK_POST_ADDITIONAL_REQUESTS }, AdditionalRequestsKey, mockID); + expect(mockGetPreviousPageUrl).toHaveBeenCalledWith(req); - expect(res.redirect).toBeCalledWith(config.HOME_URL); + expect(res.redirect).toBeCalledWith(config.CHECK_YOUR_REQUESTS_URL); expect(mockNext).not.toHaveBeenCalled(); }); diff --git a/test/unit/utils/getPreviousPageUrl.spec.ts b/test/unit/utils/getPreviousPageUrl.spec.ts new file mode 100644 index 0000000..d5e92d9 --- /dev/null +++ b/test/unit/utils/getPreviousPageUrl.spec.ts @@ -0,0 +1,34 @@ +import { describe, afterEach, expect, test, jest } from '@jest/globals'; + +import { Request } from 'express'; + +import { getPreviousPageUrl } from '../../../src/utils/getPreviousPageUrl'; +import * as config from '../../../src/config'; + +import { mockRequest } from '../../mock/express.mock'; + +describe('getPreviousPage test suite', () => { + afterEach(() => { + jest.resetAllMocks(); + }); + + test('Should return home page URL if the previousPage query param is not present', () => { + + const mockRequestWithNoQueryParam = { ...mockRequest(), query: {} } as unknown as Request; + + const previousPageUrl = getPreviousPageUrl(mockRequestWithNoQueryParam); + + expect(previousPageUrl).toBe(config.HOME_URL); + + }); + + test('Should return check-your-answers page URL if the previousPage query param is present', () => { + + const mockRequestWithQueryParam = { ...mockRequest(), query: { previousPage: config.PREVIOUS_PAGE_QUERY_PARAM } } as unknown as Request; + + const previousPageUrl = getPreviousPageUrl(mockRequestWithQueryParam); + + expect(previousPageUrl).toBe(config.CHECK_YOUR_REQUESTS_URL); + + }); +});