Skip to content

Commit

Permalink
Merge pull request #1982 from alphagov/remove-require-leading-slash
Browse files Browse the repository at this point in the history
Remove require leading slash
  • Loading branch information
HannahJMWood committed Feb 17, 2023
2 parents ce3bf60 + 6ba2195 commit 4c4634f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
13 changes: 6 additions & 7 deletions lib/manage-prototype-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ function getTemplatesInstallHandler (req, res) {
singleSlash: 'Path must not be a single forward slash (/)',
endsWithSlash: 'Path must not end in a forward slash (/)',
multipleSlashes: 'must not include a slash followed by another slash (//)',
slash: 'Path must begin with a forward slash (/)',
invalid: 'Path must not include !$&\'()*+,;=:?#[]@.% or space'
})[req.query.errorType],
chosenUrl: req.query['chosen-url']
Expand All @@ -297,7 +296,7 @@ async function postTemplatesInstallHandler (req, res) {
const templateConfig = locateTemplateConfig(req)
const templatePath = path.join(projectDir, 'node_modules', templateConfig.path)

const chosenUrl = req.body['chosen-url'].trim().normalize()
let chosenUrl = req.body['chosen-url'].trim().normalize()

const installLocation = path.join(projectDir, 'app', 'views', `${chosenUrl}.html`)

Expand All @@ -321,11 +320,6 @@ async function postTemplatesInstallHandler (req, res) {
return
}

if (chosenUrl[0] !== '/') {
renderError('slash')
return
}

if (chosenUrl[chosenUrl.length - 1] === '/') {
renderError('endsWithSlash')
return
Expand All @@ -350,6 +344,11 @@ async function postTemplatesInstallHandler (req, res) {
await fse.ensureDir(path.dirname(installLocation))
await fse.copy(templatePath, installLocation)

// Inject a forward slash if the user hasn't included one
if (chosenUrl[0] !== '/') {
chosenUrl = '/' + chosenUrl
}

res.redirect(`/manage-prototype/templates/post-install?chosen-url=${encodeURIComponent(chosenUrl)}`)
}

Expand Down
31 changes: 21 additions & 10 deletions lib/manage-prototype-handlers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ describe('manage-prototype-handlers', () => {
singleSlash: 'Path must not be a single forward slash (/)',
endsWithSlash: 'Path must not end in a forward slash (/)',
multipleSlashes: 'must not include a slash followed by another slash (//)',
slash: 'Path must begin with a forward slash (/)',
invalid: 'Path must not include !$&\'()*+,;=:?#[]@.% or space'
}).forEach(([errorType, errorMessage]) => {
it(`error type is ${errorType}`, async () => {
Expand All @@ -283,13 +282,26 @@ describe('manage-prototype-handlers', () => {
})

describe('postTemplatesInstallHandler', () => {
it('chosen url success', async () => {
req.body['chosen-url'] = chosenUrl
fse.exists.mockResolvedValue(false)
await postTemplatesInstallHandler(req, res)
expect(res.redirect).toHaveBeenCalledWith(
`/manage-prototype/templates/post-install?chosen-url=${encodeURIComponent(chosenUrl)}`
)
describe('chosen url success', () => {
beforeEach(() => {
fse.exists.mockResolvedValue(false)
})

it('where chosen url starts with a forward slash', async () => {
req.body['chosen-url'] = chosenUrl
await postTemplatesInstallHandler(req, res)
expect(res.redirect).toHaveBeenCalledWith(
`/manage-prototype/templates/post-install?chosen-url=${encodeURIComponent(chosenUrl)}`
)
})

it('where chosen url does not start with a forward slash', async () => {
req.body['chosen-url'] = 'no-forward-slash'
await postTemplatesInstallHandler(req, res)
expect(res.redirect).toHaveBeenCalledWith(
`/manage-prototype/templates/post-install?chosen-url=${encodeURIComponent('/no-forward-slash')}`
)
})
})

describe('chosen url failures', () => {
Expand All @@ -308,8 +320,7 @@ describe('manage-prototype-handlers', () => {
missing: '',
singleSlash: '/',
endsWithSlash: '/slash-at-end/',
multipleSlashes: '//multiple-slashes',
slash: 'no-forward-slash'
multipleSlashes: '//multiple-slashes'
}).forEach(testPostTemplatesInstallHandler)

// Test each invalid character
Expand Down

0 comments on commit 4c4634f

Please sign in to comment.