-
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.
SIR-932 : Story – Odour – Remit - Can you give details about the sour…
…ce of the smell? (#175) * Technical implementation for SIR-932 * fixing flakey date test * Fixed sonar issue * Fixed lint issue * Fixed sonar issue --------- Co-authored-by: Tedd Mason <[email protected]>
- Loading branch information
Showing
6 changed files
with
393 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { submitGetRequest, submitPostRequest } from '../../../__test-helpers__/server.js' | ||
import { questionSets } from '../../../utils/question-sets.js' | ||
import constants from '../../../utils/constants.js' | ||
|
||
const url = constants.routes.SMELL_SOURCE_DETAILS | ||
const question = questionSets.SMELL.questions.SMELL_SOURCE_DETAILS | ||
const header = question.text | ||
const baseAnswer = { | ||
questionId: question.questionId, | ||
questionAsked: question.text, | ||
questionResponse: true | ||
} | ||
|
||
const payload = { | ||
answerId: 'yes', | ||
siteName: 'Site name', | ||
sourceAddress: 'Address Line', | ||
sourceTown: 'town or city', | ||
sourcePostcode: 'WA4 1HT' | ||
} | ||
|
||
describe(url, () => { | ||
describe('GET', () => { | ||
it(`Should return success response and correct view for ${url}`, async () => { | ||
await submitGetRequest({ url }, header) | ||
}) | ||
}) | ||
|
||
describe('POST', () => { | ||
it('Happy: accepts valid answerId no and redirects to smell/contact-local-council', async () => { | ||
const answerId = 'no' | ||
const options = { | ||
url, | ||
payload: { | ||
answerId | ||
} | ||
} | ||
const response = await submitPostRequest(options) | ||
expect(response.headers.location).toEqual(constants.routes.SMELL_CONTACT_LOCAL_COUNCIL) | ||
}) | ||
|
||
// Happy: Accepts a complete address | ||
it('Happy: accepts valid answerId yes and complete address with valid postcode', async () => { | ||
const options = { | ||
url, | ||
payload | ||
} | ||
const response = await submitPostRequest(options) | ||
expect(response.headers.location).toEqual(constants.routes.SMELL_LOCATION_HOME) | ||
expect(response.request.yar.get(constants.redisKeys.SMELL_SOURCE_DETAILS)).toEqual([{ | ||
...baseAnswer, | ||
answerId: question.answers.siteName.answerId, | ||
otherDetails: payload.siteName | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.sourceAddress.answerId, | ||
otherDetails: payload.sourceAddress | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.sourceTown.answerId, | ||
otherDetails: payload.sourceTown | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.sourcePostcode.answerId, | ||
otherDetails: payload.sourcePostcode | ||
}]) | ||
}) | ||
// Happy: Accepts a partial address, but with complete mandatory fields | ||
it('Happy: accepts valid answerId yes and complete address with valid postcode', async () => { | ||
const partialPayload = JSON.parse(JSON.stringify(payload)) | ||
partialPayload.sourceAddress = '' | ||
|
||
const options = { | ||
url, | ||
payload: partialPayload | ||
} | ||
const response = await submitPostRequest(options) | ||
expect(response.headers.location).toEqual(constants.routes.SMELL_LOCATION_HOME) | ||
expect(response.request.yar.get(constants.redisKeys.SMELL_SOURCE_DETAILS)).toEqual([{ | ||
...baseAnswer, | ||
answerId: question.answers.siteName.answerId, | ||
otherDetails: payload.siteName | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.sourceAddress.answerId, | ||
otherDetails: '' | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.sourceTown.answerId, | ||
otherDetails: payload.sourceTown | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.sourcePostcode.answerId, | ||
otherDetails: payload.sourcePostcode | ||
}]) | ||
}) | ||
it('Happy: accepts valid answerId yes and strips out postcode with special characters', async () => { | ||
const partialPayload = JSON.parse(JSON.stringify(payload)) | ||
partialPayload.sourcePostcode = 'WA4 &^%$%$--1HT' | ||
|
||
const options = { | ||
url, | ||
payload: partialPayload | ||
} | ||
const response = await submitPostRequest(options) | ||
expect(response.headers.location).toEqual(constants.routes.SMELL_LOCATION_HOME) | ||
expect(response.request.yar.get(constants.redisKeys.SMELL_SOURCE_DETAILS)).toEqual([{ | ||
...baseAnswer, | ||
answerId: question.answers.siteName.answerId, | ||
otherDetails: payload.siteName | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.sourceAddress.answerId, | ||
otherDetails: payload.sourceAddress | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.sourceTown.answerId, | ||
otherDetails: payload.sourceTown | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.sourcePostcode.answerId, | ||
otherDetails: 'WA4 1HT' | ||
}]) | ||
}) | ||
it('Sad: errors on no fields provided', async () => { | ||
const options = { | ||
url, | ||
payload: {} | ||
} | ||
const response = await submitPostRequest(options, constants.statusCodes.OK) | ||
expect(response.payload).toContain('There is a problem') | ||
expect(response.payload).toContain('Answer yes if you can give details about where the smell is coming from') | ||
}) | ||
it('Sad: valid answerId yes but errors on no fields provided', async () => { | ||
const options = { | ||
url, | ||
payload: { | ||
answerId: 'yes' | ||
} | ||
} | ||
const response = await submitPostRequest(options, constants.statusCodes.OK) | ||
expect(response.payload).toContain('There is a problem') | ||
expect(response.payload).toContain('Enter a name') | ||
expect(response.payload).toContain('Enter a town or city') | ||
expect(response.payload).toContain('Enter a postcode') | ||
}) | ||
it('Sad: errors on invalid postcode provided', async () => { | ||
const options = { | ||
url, | ||
payload: { | ||
...payload, | ||
sourcePostcode: 'sdgfsfdgfdsgfdg' | ||
} | ||
} | ||
const response = await submitPostRequest(options, constants.statusCodes.OK) | ||
expect(response.payload).toContain('There is a problem') | ||
expect(response.payload).toContain('Enter a full UK postcode') | ||
}) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,130 @@ | ||
import constants from '../../utils/constants.js' | ||
import { getErrorSummary } from '../../utils/helpers.js' | ||
import { questionSets } from '../../utils/question-sets.js' | ||
|
||
const question = questionSets.SMELL.questions.SMELL_SOURCE_DETAILS | ||
const postcodeRegExp = /^([A-Za-z][A-Ha-hJ-Yj-y]?\d[A-Za-z0-9]? ?\d[A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})$/ // https://stackoverflow.com/a/51885364 | ||
|
||
const baseAnswer = { | ||
questionId: question.questionId, | ||
questionAsked: question.text, | ||
questionResponse: true | ||
} | ||
|
||
const handlers = { | ||
get: async (_request, h) => { | ||
return h.view(constants.views.SMELL_SOURCE_DETAILS) | ||
return h.view(constants.views.SMELL_SOURCE_DETAILS, { | ||
...getContext() | ||
}) | ||
}, | ||
post: async (request, h) => { | ||
// cleanse postcode for special characters https://design-system.service.gov.uk/patterns/addresses/#allow-different-postcode-formats | ||
if (request.payload.sourcePostcode) { | ||
request.payload.sourcePostcode = request.payload.sourcePostcode.replace(/[^\w\s]/gi, '') | ||
} | ||
|
||
// validate payload for errors | ||
const errorSummary = validatePayload(request.payload) | ||
if (errorSummary.errorList.length > 0) { | ||
return h.view(constants.views.SMELL_SOURCE_DETAILS, { | ||
...getContext(), | ||
errorSummary, | ||
...request.payload, | ||
yesChecked: request.payload.answerId === 'yes' | ||
}) | ||
} | ||
|
||
// handle redirects | ||
if (request.payload.answerId === 'yes') { | ||
// set answer in session | ||
request.yar.set(constants.redisKeys.SMELL_SOURCE_DETAILS, buildAnswers(request.payload)) | ||
return h.redirect(constants.routes.SMELL_LOCATION_HOME) | ||
} else if (request.payload.answerId === 'no') { | ||
return h.redirect(constants.routes.SMELL_CONTACT_LOCAL_COUNCIL) | ||
} else { | ||
// do nothing | ||
} | ||
|
||
return null | ||
} | ||
} | ||
|
||
const getContext = () => { | ||
return { | ||
question | ||
} | ||
} | ||
|
||
const validatePayload = payload => { | ||
const errorSummary = getErrorSummary() | ||
if (!payload.answerId) { | ||
errorSummary.errorList.push({ | ||
text: 'Answer yes if you can give details about where the smell is coming from', | ||
href: '#answerId' | ||
}) | ||
} else if (payload.answerId === 'yes') { | ||
if (!payload.siteName) { | ||
errorSummary.errorList.push({ | ||
text: 'Enter a name', | ||
href: '#siteName' | ||
}) | ||
} | ||
if (!payload.sourceTown) { | ||
errorSummary.errorList.push({ | ||
text: 'Enter a town or city', | ||
href: '#sourceTown' | ||
}) | ||
} | ||
if (!payload.sourcePostcode) { | ||
errorSummary.errorList.push({ | ||
text: 'Enter a postcode', | ||
href: '#sourcePostcode' | ||
}) | ||
} else if (!postcodeRegExp.test(payload.sourcePostcode)) { | ||
errorSummary.errorList.push({ | ||
text: 'Enter a full UK postcode', | ||
href: '#sourcePostcode' | ||
}) | ||
} else { | ||
// do nothing | ||
} | ||
} else { | ||
// do nothing | ||
} | ||
|
||
return errorSummary | ||
} | ||
|
||
const buildAnswers = payload => { | ||
return [{ | ||
...baseAnswer, | ||
answerId: question.answers.siteName.answerId, | ||
otherDetails: payload.siteName | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.sourceAddress.answerId, | ||
otherDetails: payload.sourceAddress | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.sourceTown.answerId, | ||
otherDetails: payload.sourceTown | ||
}, | ||
{ | ||
...baseAnswer, | ||
answerId: question.answers.sourcePostcode.answerId, | ||
otherDetails: payload.sourcePostcode | ||
}] | ||
} | ||
|
||
export default [ | ||
{ | ||
method: 'GET', | ||
path: constants.routes.SMELL_SOURCE_DETAILS, | ||
handler: handlers.get | ||
}, | ||
{ | ||
method: 'POST', | ||
path: constants.routes.SMELL_SOURCE_DETAILS, | ||
handler: handlers.post | ||
} | ||
] |
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
Oops, something went wrong.