Skip to content

Commit

Permalink
Add Express.js locals to Manage prototype Nunjucks context
Browse files Browse the repository at this point in the history
Management pages use locals like `serviceName` which custom Nunjucks environments don’t provide

Here we’re restoring the same `app.locals` that the route’s `res.render()` included by default previously
  • Loading branch information
colinrotherham committed Oct 6, 2023
1 parent 12c107c commit 293015b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
12 changes: 9 additions & 3 deletions lib/manage-prototype-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function getCsrfTokenHandler (req, res) {

// Clear all data in session
function getClearDataHandler (req, res) {
res.send(nunjucksManagementEnv.render(getManagementView('clear-data.njk')))
res.send(nunjucksManagementEnv.render(getManagementView('clear-data.njk'), req.app.locals))
}

function postClearDataHandler (req, res) {
Expand All @@ -103,7 +103,7 @@ function postClearDataHandler (req, res) {
function getPasswordHandler (req, res) {
const returnURL = req.query.returnURL || '/'
const error = req.query.error
res.send(nunjucksManagementEnv.render(getManagementView('password.njk'), { returnURL, error }))
res.send(nunjucksManagementEnv.render(getManagementView('password.njk'), { ...req.app.locals, returnURL, error }))
}

// Check authentication password
Expand Down Expand Up @@ -133,7 +133,7 @@ function developmentOnlyMiddleware (req, res, next) {
if (config.getConfig().isDevelopment || req.url.startsWith('/dependencies/govuk-frontend')) {
next()
} else {
res.send(nunjucksManagementEnv.render(getManagementView('manage-prototype-not-available.njk')))
res.send(nunjucksManagementEnv.render(getManagementView('manage-prototype-not-available.njk'), req.app.locals))
}
}

Expand Down Expand Up @@ -181,6 +181,7 @@ async function getHomeHandler (req, res) {
const kitPackage = await lookupPackageInfo('govuk-prototype-kit')

const viewData = {
...req.app.locals,
currentUrl: req.originalUrl,
currentSection: pageName,
links: managementLinks,
Expand Down Expand Up @@ -251,6 +252,7 @@ async function getTemplatesHandler (req, res) {
}

res.send(nunjucksManagementEnv.render(getManagementView('templates.njk'), {
...req.app.locals,
currentSection: pageName,
links: managementLinks,
availableTemplates,
Expand Down Expand Up @@ -303,6 +305,7 @@ function getTemplatesInstallHandler (req, res) {

if (templateConfig) {
res.send(nunjucksManagementEnv.render(getManagementView('template-install.njk'), {
...req.app.locals,
currentSection: 'Templates',
pageName: `Create new ${templateConfig.name}`,
currentUrl: req.originalUrl,
Expand Down Expand Up @@ -392,6 +395,7 @@ function getTemplatesPostInstallHandler (req, res) {
const chosenUrl = req.query['chosen-url']

res.send(nunjucksManagementEnv.render(getManagementView('template-post-install.njk'), {
...req.app.locals,
currentSection: 'Templates',
pageName,
links: managementLinks,
Expand Down Expand Up @@ -520,6 +524,7 @@ async function getPluginsHandler (req, res) {
const foundMessage = found === 1 ? found + ' Plugin found' : found + ' Plugins found'
const updatesMessage = updates ? updates === 1 ? updates + ' UPDATE AVAILABLE' : updates + ' UPDATES AVAILABLE' : ''
const model = {
...req.app.locals,
currentSection: pageName,
links: managementLinks,
isInstalledPage,
Expand Down Expand Up @@ -626,6 +631,7 @@ async function getPluginsModeHandler (req, res) {
}

res.send(nunjucksManagementEnv.render(getManagementView('plugin-install-or-uninstall.njk'), {
...req.app.locals,
currentSection: 'Plugins',
pageName,
currentUrl: req.originalUrl,
Expand Down
30 changes: 26 additions & 4 deletions lib/manage-prototype-handlers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ describe('manage-prototype-handlers', () => {
fse.exists.mockResolvedValue(true)
fse.readJsonSync.mockReturnValue({})
req = {
app: {
locals: {
serviceName: 'Service name goes here'
}
},
headers: {},
body: {},
query: {},
Expand All @@ -154,7 +159,8 @@ describe('manage-prototype-handlers', () => {
it('getClearDataHandler', () => {
getClearDataHandler(req, res)
expect(mockNunjucksRender).toHaveBeenCalledWith(
'views/manage-prototype/clear-data.njk'
'views/manage-prototype/clear-data.njk',
req.app.locals
)
})

Expand All @@ -174,7 +180,11 @@ describe('manage-prototype-handlers', () => {
getPasswordHandler(req, res)
expect(mockNunjucksRender).toHaveBeenCalledWith(
'views/manage-prototype/password.njk',
{ error: undefined, returnURL: '/' }
expect.objectContaining({
...req.app.locals,
error: undefined,
returnURL: '/'
})
)
})

Expand Down Expand Up @@ -207,15 +217,20 @@ describe('manage-prototype-handlers', () => {
await getHomeHandler(req, res)
expect(mockNunjucksRender).toHaveBeenCalledWith(
'views/manage-prototype/index.njk',
expect.objectContaining({ currentSection: 'Home', latestAvailableKit: '1.0.0' })
expect.objectContaining({
...req.app.locals,
currentSection: 'Home',
latestAvailableKit: '1.0.0'
})
)
})

describe('developmentOnlyMiddleware', () => {
it('in production', () => {
developmentOnlyMiddleware(req, res, next)
expect(mockNunjucksRender).toHaveBeenCalledWith(
'views/manage-prototype/manage-prototype-not-available.njk'
'views/manage-prototype/manage-prototype-not-available.njk',
req.app.locals
)
})

Expand Down Expand Up @@ -253,6 +268,7 @@ describe('manage-prototype-handlers', () => {
expect(mockNunjucksRender).toHaveBeenCalledWith(
'views/manage-prototype/templates.njk',
expect.objectContaining({
...req.app.locals,
currentSection: 'Templates',
availableTemplates: [{
packageName,
Expand All @@ -275,6 +291,7 @@ describe('manage-prototype-handlers', () => {
expect(mockNunjucksRender).toHaveBeenCalledWith(
path.join(packageName, templatePath),
expect.objectContaining({
...req.app.locals,
serviceName: 'Service name goes here'
})
)
Expand All @@ -300,6 +317,7 @@ describe('manage-prototype-handlers', () => {
expect(mockNunjucksRender).toHaveBeenCalledWith(
'views/manage-prototype/template-install.njk',
expect.objectContaining({
...req.app.locals,
currentSection: 'Templates',
pageName: 'Create new A page with everything',
chosenUrl,
Expand Down Expand Up @@ -393,6 +411,7 @@ describe('manage-prototype-handlers', () => {
expect(mockNunjucksRender).toHaveBeenCalledWith(
'views/manage-prototype/template-post-install.njk',
expect.objectContaining({
...req.app.locals,
currentSection: 'Templates',
pageName: 'Page created',
filePath: path.join(`app/views${chosenUrl}.html`)
Expand Down Expand Up @@ -445,6 +464,7 @@ describe('manage-prototype-handlers', () => {
expect(mockNunjucksRender).toHaveBeenCalledWith(
'views/manage-prototype/plugins.njk',
expect.objectContaining({
...req.app.locals,
currentSection: 'Plugins',
isSearchPage: false,
isInstalledPage: true,
Expand All @@ -460,6 +480,7 @@ describe('manage-prototype-handlers', () => {
expect(mockNunjucksRender).toHaveBeenCalledWith(
'views/manage-prototype/plugins.njk',
expect.objectContaining({
...req.app.locals,
currentSection: 'Plugins',
isSearchPage: true,
isInstalledPage: false,
Expand Down Expand Up @@ -488,6 +509,7 @@ describe('manage-prototype-handlers', () => {
expect(mockNunjucksRender).toHaveBeenCalledWith(
'views/manage-prototype/plugin-install-or-uninstall.njk',
expect.objectContaining({
...req.app.locals,
chosenPlugin: availablePlugin,
command: `npm install ${packageName} --save-exact`,
currentSection: 'Plugins',
Expand Down

0 comments on commit 293015b

Please sign in to comment.