-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2492 from alphagov/PP-7445-upgrade-previous-url-s…
…tructure PP-7445 Update accounts URL utility
- Loading branch information
Showing
11 changed files
with
144 additions
and
8 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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict' | ||
function flattenNestedValues (target) { | ||
return Object.values(target).reduce((aggregate, value) => { | ||
const valueIsNestedObject = typeof value === 'object' && value !== null | ||
if (valueIsNestedObject) { | ||
return [ ...aggregate, ...flattenNestedValues(value) ] | ||
} | ||
return [ ...aggregate, value ] | ||
}, []) | ||
} | ||
|
||
module.exports = flattenNestedValues |
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,19 @@ | ||
const { expect } = require('chai') | ||
const flattenNestedValues = require('./flatten-nested-values') | ||
describe('flatten nested values utility', () => { | ||
it('correctly flattens nested values', () => { | ||
const nested = { | ||
one: { | ||
two: { | ||
index: 'path-1', | ||
secondPage: 'path-2' | ||
}, | ||
three: 'path-3' | ||
}, | ||
four: 'path-4' | ||
} | ||
const flat = flattenNestedValues(nested) | ||
expect(flat.length).to.equal(4) | ||
expect(flat).to.have.members(['path-1', 'path-2', 'path-3', 'path-4']) | ||
}) | ||
}) |
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 @@ | ||
'use strict' | ||
// check if a missed URL (404) is a URL that has been upgraded during the | ||
// account URL structure change. When this utility is reporting few or no | ||
// upgrades it can be removed | ||
const urlJoin = require('url-join') | ||
const paths = require('../paths') | ||
const formattedPathFor = require('./replace-params-in-path') | ||
const flattenNestedValues = require('./flatten-nested-values') | ||
|
||
// only flatten paths once given the singleton module export patten, these | ||
// should never change after the server spins up | ||
const allAccountPaths = flattenNestedValues(paths.account) | ||
const templatedAccountPaths = allAccountPaths.filter((path) => path.includes(':')) | ||
|
||
const removeEmptyValues = (value) => !!value | ||
|
||
function isLegacyAccountsUrl (url) { | ||
if (allAccountPaths.includes(url)) { | ||
return true | ||
} else { | ||
// the path isn't directly in the list, check to see if it's a templated value | ||
const numberOfUrlParts = url.split('/').filter(removeEmptyValues).length | ||
return templatedAccountPaths.some((templatedPath) => { | ||
const parts = templatedPath.split('/').filter(removeEmptyValues) | ||
const matches = parts | ||
|
||
// remove variable sections | ||
.filter((part) => !part.startsWith(':')) | ||
|
||
// ensure every part of the url structure is present in the url we're comparing against | ||
.every((part) => url.includes(part)) | ||
|
||
// verify it matches and is not a subset (has less length) | ||
return matches && parts.length === numberOfUrlParts | ||
}) | ||
} | ||
} | ||
|
||
function getUpgradedAccountStructureUrl (url, gatewayAccountExternalId) { | ||
const base = formattedPathFor(paths.account.root, gatewayAccountExternalId) | ||
return urlJoin(base, url) | ||
} | ||
|
||
module.exports = { | ||
isLegacyAccountsUrl, | ||
getUpgradedAccountStructureUrl | ||
} |
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,16 @@ | ||
const { expect } = require('chai') | ||
|
||
const accountsUrl = require('./gateway-account-urls') | ||
describe('account URL checker', () => { | ||
it('correctly identifies an original account URL', () => { | ||
const url = '/billing-address' | ||
const result = accountsUrl.isLegacyAccountsUrl(url) | ||
expect(result).to.be.true //eslint-disable-line | ||
}) | ||
|
||
it('correctly upgrades a URL to the account structure', () => { | ||
const url = '/create-payment-link/manage/some-product-external-id/add-reporting-column/some-metadata-key' | ||
const gatewayAccountExternalId = 'some-account-external-id' | ||
expect(accountsUrl.getUpgradedAccountStructureUrl(url, gatewayAccountExternalId)).to.equal('/account/some-account-external-id/create-payment-link/manage/some-product-external-id/add-reporting-column/some-metadata-key') | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const request = require('supertest') | ||
|
||
const { getApp } = require('../../server') | ||
const session = require('../test-helpers/mock-session.js') | ||
const app = session.getAppWithLoggedInUser(getApp(), session.getUser()) | ||
|
||
describe('URL upgrade utility', () => { | ||
it('correctly upgrades URLs in the account specific paths', () => { | ||
return request(app) | ||
.get('/billing-address') | ||
.expect(302) | ||
.then((res) => { | ||
res.header['location'].should.include('/account/external-id-set-by-create-app-with-session/billing-address') // eslint-disable-line | ||
}) | ||
}) | ||
|
||
it('correctly 404s as expected for non account specific paths', () => { | ||
return request(app) | ||
.get('/unknown-address') | ||
.expect(404) | ||
}) | ||
}) |
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