Skip to content

Commit

Permalink
adding some initial pages, and unit test framework for hapi server
Browse files Browse the repository at this point in the history
  • Loading branch information
teddmason committed Jan 11, 2024
1 parent d3d09dd commit 78e0902
Show file tree
Hide file tree
Showing 16 changed files with 211 additions and 100 deletions.
29 changes: 29 additions & 0 deletions .jest/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createServer, init } from "../server/index.js"
import serverOptions from '../server/__test-helpers__/server-options.js'
const ORIGINAL_ENV = process.env

let server, context

beforeEach(async () => {
jest.resetAllMocks()
// add any common mockage here, eg json POSTs
server = await createServer(serverOptions)
await init(server)
})

afterEach(async () => {
try {
if (server) {
await server.stop()
}
} finally {
// reset environment variables after test
process.env = { ...ORIGINAL_ENV }
}
})

const getServer = () => server

const getContext = () => context

export { getServer, getContext }
16 changes: 14 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,28 @@
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest"
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"--runInBand",
"--testTimeout",
"999999",
"--silent=false"
],
"outputCapture": "std"
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": [
"--runInBand",
"--testTimeout",
"999999",
"--silent=false",
"${fileBasenameNoExtension}"
]
],
"outputCapture": "std"
}
]
}
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"watch:nodejs": "nodemon --delay 1000ms --signal SIGTERM -r dotenv/config index.js",
"dev": "concurrently 'npm:watch:css' 'npm:watch:nodejs'",
"lint": "standard",
"unit-test": "jest --verbose",
"unit-test": "jest --runInBand --verbose",
"test": "npm run lint && npm run unit-test",
"prettier": "prettier-config-standard",
"prepare": "husky install",
Expand Down Expand Up @@ -64,6 +64,7 @@
},
"devDependencies": {
"@babel/preset-env": "^7.23.8",
"@hapi/catbox-memory": "^6.0.1",
"concurrently": "^8.2.2",
"husky": "^8.0.3",
"jest": "^29.7.0",
Expand Down Expand Up @@ -107,6 +108,12 @@
],
"setupFiles": [
"./.jest/test.env.js"
],
"setupFilesAfterEnv": [
"./.jest/setup.js"
],
"testPathIgnorePatterns": [
"__test-helpers__"
]
}
}
19 changes: 19 additions & 0 deletions server/__test-helpers__/on-pre-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// import application from '../__mock-data__/test-application.js'

// Adds pre auth functionality to server that loads session with mock application data or sessionData object provided from test.
// This should really be done in a pre handler but only one pre handler can be registered with a Hapi.js server.
const onPreAuth = (sessionData) => {
return {
plugin: {
name: 'on-pre-auth',
register: (server, _options) => {
server.ext('onPreAuth', function (request, h) {
// request.yar._store = sessionData || JSON.parse(application.dataString)
return h.continue
})
}
}
}
}

export default onPreAuth
15 changes: 15 additions & 0 deletions server/__test-helpers__/server-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Engine } from '@hapi/catbox-memory'

// use catbox-memory cache strategy for testing
const serverOptions = {
cache: [
{
name: 'redis_cache',
provider: {
constructor: Engine
}
}
]
}

export default serverOptions
35 changes: 35 additions & 0 deletions server/__test-helpers__/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getServer } from '../../.jest/setup.js'
import onPreAuth from './on-pre-auth.js'

const submitGetRequest = async (options, expectedResponseCode = 200, sessionData) => {
// await addOnPreAuth(sessionData)
options.method = 'GET'
return submitRequest(options, expectedResponseCode)
}

const submitPostRequest = async (options, expectedResponseCode = 302, sessionData) => {
if (sessionData && Object.keys(sessionData).length > 0) {
await addOnPreAuth(sessionData)
}
options.method = 'POST'
return submitRequest(options, expectedResponseCode)
}

const submitRequest = async (options, expectedResponseCode) => {
const response = await getServer().inject(options)
expect(response.statusCode).toBe(expectedResponseCode)
return response
}

const addOnPreAuth = async (sessionData) => {
// Add session injection using on pre auth functionality.
// This shoud be done using a pre handler but only one pre handler can be registered
// with a Hapi.js server.
// Using on pre auth functionaliy is acceptable for testing purposes.
await getServer().register(onPreAuth(sessionData))
}

export {
submitGetRequest,
submitPostRequest
}
1 change: 1 addition & 0 deletions server/plugins/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default {
serviceNameUrl: constants.urls.GOV_UK_SERVICE_HOME,
serviceName: config.serviceName,
pageTitle: `${config.serviceName} - GOV.UK`,
titleSuffix: ' - GOV.UK',
analyticsAccount
}
}
Expand Down
12 changes: 12 additions & 0 deletions server/routes/__tests__/home.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { submitGetRequest } from '../../__test-helpers__/server.js'
import constants from '../../utils/constants.js'
const url = '/'

describe(url, () => {
describe('GET', () => {
it(`Should redirect to ${constants.routes.REPORT_WATER_POLLUTION}`, async () => {
await submitGetRequest({ url }, 302)
expect(true)
})
})
})
2 changes: 1 addition & 1 deletion server/routes/home.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import constants from '../utils/constants.js'

const handlers = {
get: async (_request, h) => h.redirect(constants.routes.WELCOME)
get: async (_request, h) => h.redirect(constants.routes.REPORT_WATER_POLUTION)
}

export default [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import constants from '../utils/constants.js'

const handlers = {
get: async (request, h) => {
get: async (_request, h) => {
const context = _getContext()
return h.view(constants.views.WELCOME, {
return h.view(constants.views.REPORT_WATER_POLUTION, {
...context
})
}
}

const _getContext = () => {
return {
pageTitle: 'Report an environmental incident',
pageHeading: 'Report water pollution in England',
hideBackLink: true
}
}

export default [
{
method: 'GET',
path: constants.routes.WELCOME,
path: constants.routes.REPORT_WATER_POLUTION,
handler: handlers.get
}
]
4 changes: 2 additions & 2 deletions server/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const COOKIES = 'notices/cookies'
const PRIVACY = 'notices/privacy'

const PUBLIC = 'public'
const WELCOME = 'welcome'
const REPORT_WATER_POLUTION = 'report-water-pollution'
const HOME = 'home'
const INCIDENT_TYPE = 'incident-type'
const WATER_TYPE = 'water-quality/water-type'
Expand All @@ -20,7 +20,7 @@ const routes = {
COOKIES,
PRIVACY,
PUBLIC,
WELCOME,
REPORT_WATER_POLUTION,
HOME,
INCIDENT_TYPE,
WATER_TYPE
Expand Down
18 changes: 18 additions & 0 deletions server/views/footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{ govukFooter({
meta: {
items: [
{
href: "/notices/privacy",
text: "Privacy notice"
},
{
href: "/notices/cookies",
text: "Cookies"
},
{
href: "/notices/accessibility",
text: "Accessibility statement"
}
]
}
}) }}
32 changes: 6 additions & 26 deletions server/views/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<!-- Version: {{ appVersion }} -->
<!-- Environment: {{ env }} -->
<meta name="robots" content="noindex, nofollow">
<meta name="description" content="Report pollution in rivers, lakes, canals and other watercourses in England">

<!--[if !IE 8]><!-->
<link href="{{ assetPath }}/stylesheets/application.css" rel="stylesheet" />
Expand All @@ -25,25 +26,21 @@

{% block header %}
{{ govukHeader({
homepageUrl: govUkHome,
homepageUrl: "/",
containerClasses: "govuk-width-container",
serviceName: serviceName,
serviceUrl: serviceNameUrl
serviceUrl: "/"
}) }}
{% endblock %}

{% block pageTitle %}
{% if fieldErrors %}
Error:
{% endif %}

{{ pageTitle }} - {{ serviceName }} – GOV.UK
{% if err %} Error: {% endif %} {{ pageHeading }} {{ titleSuffix }}
{% endblock %}

{% block beforeContent %}
{{ govukPhaseBanner({
tag: {
text: "Alpha"
text: "beta"
},
html: 'This is a new service – <a class="govuk-link" href="https://defragroup.eu.qualtrics.com/jfe/form/SV_cMxUuwi2OGTwAfQ" target="_blank" rel="noopener noreferrer">give feedback (opens new tab)</a> to help us to improve it.'
}) }}
Expand Down Expand Up @@ -71,24 +68,7 @@ <h1 id="pageTitle" class="govuk-heading-xl">Default page template</h1>
{% endblock %}

{% block footer %}
{{ govukFooter({
meta: {
items: [
{
href: "/notices/privacy",
text: "Privacy notice"
},
{
href: "/notices/cookies",
text: "Cookies"
},
{
href: "/notices/accessibility",
text: "Accessibility statement"
}
]
}
}) }}
{% include "footer.html" %}
{% endblock %}

{% block bodyEnd %}
Expand Down
46 changes: 46 additions & 0 deletions server/views/report-water-pollution.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{% extends 'form-layout.html' %}

{% set pageHeading = 'Report water pollution in England' %}

{% block content %}

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl">
Report water pollution
</h1>
<p>Use this service to report pollution in the sea, lakes, reservoirs, rivers or other watercourses in England.</p>
<p>Pollution can include:</p>
<ul class="govuk-list govuk-list--bullet">
<li>sewage</li>
<li>'run-off' from farms</li>
<li>discharges from industry</li>
<li>leaks from objects in the river, such as vehicles</li>
</ul>
<div class="govuk-inset-text">There’s a different way to report <a rel="external" href="https://naturalresources.wales/about-us/contact-us/report-an-incident/?lang=en">water pollution in Wales</a>, <a rel="external" href="https://www.mygov.scot/report-environmental-incident">water pollution in Scotland</a> or <a rel="external" href="https://www.nidirect.gov.uk/information-and-services/environment-and-outdoors/environmental-quality-your-area">water pollution in Northern Ireland</a>.</div>
<h2 class="govuk-heading-m">Before you start</h2>
<p>You need to be able to describe the location of the pollution or we will not be able to investigate it.</p>
<div class="govuk-inset-text">Do not use this service to report littering in rivers. <a href="https://www.gov.uk/find-local-council">Contact your local council</a> instead.</div>
{{ govukButton({
text: "Start now",
isStartButton: true
}) }}
<h2 class="govuk-heading-l">If you cannot report online</h2>
<p>You can report the problem to the Environment Agency over the phone.</p>
<div class="contact">
<p>Environment Agency incident hotline<br />
Telephone: 0800 80 70 60<br />
24 hours</p>
</div>
<h2 class="govuk-heading-l">Other problems with your environment</h2>
<p>How you report other problems affecting the environment depends on the type of problem it is and where it's happening. You may have to report it to:
<ul class="govuk-list govuk-list--bullet">
<li>the Environment Agency</li>
<li>your local council</li>
<li>your utilities company</li>
</ul>
<p><a href="browse">Find out how to report other problems with your environment</a></p>
</div>
</div>

{% endblock %}
Loading

0 comments on commit 78e0902

Please sign in to comment.