-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add validation-test page with unit/integration tests
- Loading branch information
1 parent
46576a2
commit c4977a3
Showing
19 changed files
with
407 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Request, Response } from 'express'; | ||
import { log } from '../utils/logger'; | ||
import * as config from '../config'; | ||
|
||
export const get = (_req: Request, res: Response) => { | ||
return res.render(config.VALIDATION_TEST); | ||
}; | ||
|
||
export const post = (req: Request, res: Response) => { | ||
const firstName = req.body.first_name; | ||
|
||
log.info(`First Name: ${firstName}`); | ||
|
||
return res.redirect(config.LANDING_PAGE); | ||
}; |
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,40 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { validationResult, FieldValidationError } from 'express-validator'; | ||
|
||
import { log } from '../utils/logger'; | ||
import { FormattedValidationErrors } from '../model/validation.model'; | ||
|
||
export const checkValidations = ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
try { | ||
const errorList = validationResult(req); | ||
|
||
if (!errorList.isEmpty()) { | ||
const template_path = req.path.substring(1); | ||
const errors = formatValidationError( | ||
errorList.array() as FieldValidationError[] | ||
); | ||
|
||
log.info(`Validation error on ${template_path} page`); | ||
|
||
return res.render(template_path, { ...req.body, errors }); | ||
Check failure Code scanning / CodeQL Uncontrolled data used in path expression High
This path depends on a
user-provided value Error loading related location Loading |
||
} | ||
|
||
return next(); | ||
} catch (err: any) { | ||
log.error(err.message); | ||
next(err); | ||
} | ||
}; | ||
|
||
const formatValidationError = (errorList: FieldValidationError[]) => { | ||
const errors = { errorList: [] } as FormattedValidationErrors; | ||
errorList.forEach((e: FieldValidationError) => { | ||
errors.errorList.push({ href: `#${e.path}`, text: e.msg }); | ||
errors[e.path] = { text: e.msg }; | ||
}); | ||
return errors; | ||
}; |
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,9 @@ | ||
import { ErrorMessages } from '../validation/error.messages'; | ||
|
||
export interface FormattedValidationErrors { | ||
[key: string]: any; | ||
errorList: { | ||
href: string; | ||
text: ErrorMessages; | ||
}[]; | ||
} |
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 { Router } from 'express'; | ||
|
||
import { checkValidations } from '../middleware/validation.middleware'; | ||
import { validationTest } from '../validation/validation-test.validation'; | ||
import { get, post } from '../controller/validation-test.controller'; | ||
|
||
import * as config from '../config'; | ||
|
||
const validationTestRouter = Router(); | ||
|
||
validationTestRouter.get(config.VALIDATION_TEST_URL, get); | ||
validationTestRouter.post( | ||
config.VALIDATION_TEST_URL, | ||
...validationTest, | ||
checkValidations, | ||
post | ||
); | ||
|
||
export default validationTestRouter; |
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,4 @@ | ||
export enum ErrorMessages { | ||
TEST_INFO_ERROR = 'INFO PAGE ERROR MESSAGE TEST', | ||
TEST_FIRST_NAME = 'Enter your first name', | ||
} |
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,10 @@ | ||
import { body } from 'express-validator'; | ||
|
||
import { ErrorMessages } from './error.messages'; | ||
|
||
export const validationTest = [ | ||
body('first_name') | ||
.not() | ||
.isEmpty({ ignore_whitespace: true }) | ||
.withMessage(ErrorMessages.TEST_FIRST_NAME), | ||
]; |
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,9 @@ | ||
{% if errors and errors.errorList and errors.errorList.length > 0 %} | ||
{{ govukErrorSummary({ | ||
titleText: "There is a problem", | ||
errorList: errors.errorList if errors, | ||
attributes: { | ||
"tabindex": "0" | ||
} | ||
}) }} | ||
{% endif %} |
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,9 @@ | ||
{% if errors and errors.errorList and errors.errorList.length > 0 %} | ||
{{ govukErrorSummary({ | ||
titleText: "There is a problem", | ||
errorList: errors.errorList if errors, | ||
attributes: { | ||
"tabindex": "0" | ||
} | ||
}) }} | ||
{% endif %} |
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,36 @@ | ||
{% extends "layout.html" %} | ||
|
||
{% block content %} | ||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
<h1 class="govuk-heading-l">Validation Test</h1> | ||
|
||
<p class="govuk-body">Submit field empty to test validation functionality.</p> | ||
|
||
{% include "include/error-list.html" %} | ||
|
||
<form method="post"> | ||
|
||
{{ govukInput({ | ||
errorMessage: errors.first_name if errors, | ||
label: { | ||
text: "First Name", | ||
classes: "govuk-label--m" | ||
}, | ||
classes: "govuk-input--width-10", | ||
id: "first_name", | ||
name: "first_name", | ||
value: first_name | ||
}) }} | ||
|
||
{{ govukButton({ | ||
text: "Submit", | ||
attributes: { | ||
"id": "submit" | ||
} | ||
}) }} | ||
|
||
</form> | ||
</div> | ||
</div> | ||
{% endblock %} |
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,74 @@ | ||
jest.mock('../../../src/middleware/logger.middleware'); | ||
jest.mock('../../../src/utils/logger'); | ||
|
||
import { jest, beforeEach, describe, expect, test } from '@jest/globals'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import request from 'supertest'; | ||
|
||
import app from '../../../src/app'; | ||
import * as config from '../../../src/config'; | ||
import { logger } from '../../../src/middleware/logger.middleware'; | ||
import { log } from '../../../src/utils/logger'; | ||
|
||
import { | ||
MOCK_REDIRECT_MESSAGE, | ||
MOCK_GET_VALIDATION_TEST_RESPONSE, | ||
MOCK_POST_VALIDATION_TEST_RESPONSE, | ||
} from '../../mock/text.mock'; | ||
import { MOCK_POST_VALIDATION_TEST } from '../../mock/data'; | ||
|
||
const mockedLogger = logger as jest.Mock<typeof logger>; | ||
mockedLogger.mockImplementation( | ||
(_req: Request, _res: Response, next: NextFunction) => next() | ||
); | ||
|
||
describe('validation-test endpoint integration tests', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('GET tests', () => { | ||
test('renders the validation-test page', async () => { | ||
const res = await request(app).get(config.VALIDATION_TEST_URL); | ||
|
||
expect(res.status).toEqual(200); | ||
expect(res.text).toContain(MOCK_GET_VALIDATION_TEST_RESPONSE); | ||
expect(mockedLogger).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('POST tests', () => { | ||
test('Should redirect to landing page after POST request', async () => { | ||
const res = await request(app) | ||
.post(config.VALIDATION_TEST_URL) | ||
.send(MOCK_POST_VALIDATION_TEST); | ||
|
||
expect(res.status).toEqual(302); | ||
expect(res.text).toContain(MOCK_REDIRECT_MESSAGE); | ||
expect(mockedLogger).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('Should render the same page with error messages after POST request', async () => { | ||
const res = await request(app) | ||
.post(config.VALIDATION_TEST_URL) | ||
.send({ | ||
first_name: '', | ||
}); | ||
|
||
expect(res.status).toEqual(200); | ||
expect(res.text).toContain(MOCK_GET_VALIDATION_TEST_RESPONSE); | ||
expect(mockedLogger).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('Should log the First Name and More Details on POST request.', async () => { | ||
const mockLog = log.info as jest.Mock; | ||
const res = await request(app) | ||
.post(config.VALIDATION_TEST_URL) | ||
.send(MOCK_POST_VALIDATION_TEST); | ||
|
||
expect(mockLog).toBeCalledWith(MOCK_POST_VALIDATION_TEST_RESPONSE); | ||
expect(res.text).toContain(MOCK_REDIRECT_MESSAGE); | ||
expect(mockedLogger).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
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,4 @@ | ||
import { log } from '../../src/utils/logger'; | ||
|
||
export const mockLogInfo = log.info as jest.Mock; | ||
export const mockLogErrorRequest = log.errorRequest as jest.Mock; |
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 @@ | ||
export const mockID = 'c3931b00-a8b4-4d2d-a165-b9b4d148cd88'; |
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,59 @@ | ||
jest.mock('../../../src/utils/logger'); | ||
|
||
import { describe, expect, afterEach, test, jest } from '@jest/globals'; | ||
import { Request, Response } from 'express'; | ||
|
||
import { get, post } from '../../../src/controller/validation-test.controller'; | ||
import * as config from '../../../src/config'; | ||
import { log } from '../../../src/utils/logger'; | ||
|
||
import { MOCK_POST_VALIDATION_TEST } from '../../mock/data'; | ||
import { MOCK_POST_VALIDATION_TEST_RESPONSE } from '../../mock/text.mock'; | ||
|
||
const req = { | ||
body: MOCK_POST_VALIDATION_TEST, | ||
} as Request; | ||
|
||
const mockResponse = () => { | ||
const res = {} as Response; | ||
res.render = jest.fn().mockReturnValue(res) as any; | ||
res.redirect = jest.fn().mockReturnValue(res) as any; | ||
return res; | ||
}; | ||
|
||
describe('Validation test controller test suites', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('validation test GET tests', () => { | ||
test('should render validation test page', () => { | ||
const res = mockResponse(); | ||
|
||
get(req, res); | ||
|
||
expect(res.render).toHaveBeenCalledWith(config.VALIDATION_TEST); | ||
}); | ||
}); | ||
|
||
describe('validation-test POST tests', () => { | ||
test('should redirect to landing-page on POST request', () => { | ||
const res = mockResponse(); | ||
|
||
post(req, res); | ||
|
||
expect(res.redirect).toBeCalledWith(config.LANDING_PAGE); | ||
}); | ||
test('should log GitHub handle and More Details on POST request', () => { | ||
const res = mockResponse(); | ||
|
||
const mockLogInfo = log.info as jest.Mock; | ||
|
||
post(req, res); | ||
|
||
expect(mockLogInfo).toHaveBeenCalledWith( | ||
MOCK_POST_VALIDATION_TEST_RESPONSE | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.