Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Tool: 'back' link takes user to referrer page , when arriving via deep link. #584

Merged
merged 7 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/controllers/deepLinkController.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ class DeepLinkController extends PageController {
req.sessionModel.set('dataset', dataset)
const datasetInfo = datasets.get(dataset) ?? { dataSubject: '', requiresGeometryTypeSelection: false }
req.sessionModel.set('data-subject', datasetInfo.dataSubject)
req.sessionModel.set(this.checkToolDeepLinkSessionKey,
{ 'data-subject': datasetInfo.dataSubject, orgName, dataset, datasetName: datasetInfo.text })
const sessionData = { 'data-subject': datasetInfo.dataSubject, orgName, dataset, datasetName: datasetInfo.text }
if (req.headers.referer) {
sessionData.referrer = req.headers.referer
}
req.sessionModel.set(this.checkToolDeepLinkSessionKey, sessionData)
rosado marked this conversation as resolved.
Show resolved Hide resolved

this.#addHistoryStep(req, '/check/dataset')

Expand Down
34 changes: 30 additions & 4 deletions src/controllers/pageController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ import hmpoFormWizard from 'hmpo-form-wizard'
import { logPageView } from '../utils/logging.js'
const { Controller } = hmpoFormWizard

/**
* If we arrived at the page via deep from another page, we'll use that page as the back link.
*
* @param {string} url current page URL
* @param {{ referrer?: string, dataset: string }} deepLinkInfo deep link info from the session
* @returns {string|undefined} back link URL
*/
function wizardBackLink (currentUrl, deepLinkInfo) {
if (deepLinkInfo && 'referrer' in deepLinkInfo) {
const { referrer, dataset } = deepLinkInfo
if (dataset === 'tree' && currentUrl === '/check/geometry-type') {
return referrer
}
if (dataset !== 'tree' && currentUrl === '/check/upload-method') {
return referrer
}
}

return undefined
}
rosado marked this conversation as resolved.
Show resolved Hide resolved

class PageController extends Controller {
checkToolDeepLinkSessionKey = 'check-tool-deep-link'

Expand All @@ -11,11 +32,16 @@ class PageController extends Controller {
}

locals (req, res, next) {
if (this.options.backLink) {
req.form.options.lastPage = this.options.backLink
let backLink
const deepLinkInfo = req?.sessionModel?.get(this.checkToolDeepLinkSessionKey)
if (deepLinkInfo) {
req.form.options.deepLink = deepLinkInfo
backLink = wizardBackLink(req.originalUrl, deepLinkInfo)
}
if (req.sessionModel) {
req.form.options.deepLink = req.sessionModel.get(this.checkToolDeepLinkSessionKey)

backLink = backLink ?? this.options.backLink
if (backLink) {
req.form.options.lastPage = backLink
rosado marked this conversation as resolved.
Show resolved Hide resolved
}
super.locals(req, res, next)
}
Expand Down
45 changes: 45 additions & 0 deletions test/unit/PageController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,48 @@ describe('PageController', () => {
expect(callArgs.service).toEqual('lpa-data-validation-frontend')
})
})

describe('Correctly detects the wizard back link', () => {
const referrer = '/this-is-where-we-came-from'
const makeReq = () => {
return ({
originalUrl: '/check/upload-method',
sessionID: '123',
sessionModel: {
get: vi.fn().mockReturnValue({ referrer })
},
form: {
options: {}
}
})
}
rosado marked this conversation as resolved.
Show resolved Hide resolved

it('arriving at the deep link entry point', () => {
const pageController = new PageController({ route: '/upload-method' })
const req = makeReq()
pageController.locals(req, {}, vi.fn())
expect(req.form.options.lastPage).toEqual(referrer)
})

it('arriving at some other step', () => {
const pageController = new PageController({ route: '/upload-method' })
const req = { ...makeReq(), originalUrl: '/check/another-step' }
pageController.locals(req, {}, vi.fn())
expect(req.form.options.lastPage).toBe(undefined)
})

it('don\'t touch the back link if there\'s no deep link session info (lastPage not set)', () => {
const pageController = new PageController({ route: '/upload-method' })
const req = { ...makeReq(), sessionModel: new Map() }
pageController.locals(req, {}, vi.fn())
expect(req.form.options.lastPage).toEqual(undefined)
})

it('don\'t touch the back link if there\'s no deep link session info (lastPage set)', () => {
const pageController = new PageController({ route: '/upload-method' })
pageController.options.backLink = '/go-back'
const req = { ...makeReq(), sessionModel: new Map() }
pageController.locals(req, {}, vi.fn())
expect(req.form.options.lastPage).toEqual('/go-back')
})
})
2 changes: 1 addition & 1 deletion test/unit/deepLinkController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import DeepLinkController from '../../src/controllers/deepLinkController.js'
function mockRequestObject () {
const sessionModel = new Map()
const journeyModel = new Map()
return { sessionModel, journeyModel, query: {} }
return { sessionModel, journeyModel, query: {}, headers: {} }
}

function mockMiddlewareArgs (reqOpts) {
Expand Down