-
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.
adding some initial pages, and unit test framework for hapi server
- Loading branch information
Showing
16 changed files
with
211 additions
and
100 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,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 } |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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 |
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,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 |
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,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 | ||
} |
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 @@ | ||
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) | ||
}) | ||
}) | ||
}) |
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
8 changes: 4 additions & 4 deletions
8
server/routes/welcome.js → server/routes/report-water-pollution.js
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,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 | ||
} | ||
] |
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,18 @@ | ||
{{ govukFooter({ | ||
meta: { | ||
items: [ | ||
{ | ||
href: "/notices/privacy", | ||
text: "Privacy notice" | ||
}, | ||
{ | ||
href: "/notices/cookies", | ||
text: "Cookies" | ||
}, | ||
{ | ||
href: "/notices/accessibility", | ||
text: "Accessibility statement" | ||
} | ||
] | ||
} | ||
}) }} |
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,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 %} |
Oops, something went wrong.