-
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.
- Loading branch information
Showing
23 changed files
with
581 additions
and
59 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,17 @@ | ||
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; | ||
|
||
// validation middleware and data assignment to be implemented | ||
|
||
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,26 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { log } from '../utils/logger'; | ||
import { isFeatureEnabled } from '../utils/isFeatureEnabled'; | ||
import * as config from '../config'; | ||
|
||
export const authentication = ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
try { | ||
// if auth is not enabled, render the not available page | ||
if (!isFeatureEnabled(config.FEATURE_FLAG_ENABLE_AUTH)) { | ||
log.infoRequest(req, 'sorry, auth service not available right now'); | ||
return res.render(config.NOT_AVAILABLE); | ||
} | ||
|
||
// If auth enabled | ||
log.infoRequest(req, 'some auth here soon!'); | ||
|
||
next(); | ||
} catch (err: any) { | ||
log.errorRequest(req, err); | ||
next(err); | ||
} | ||
}; |
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,21 @@ | ||
import { Router } from 'express'; | ||
|
||
import { authentication } from '../middleware/authentication.middleware'; | ||
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, authentication, get); | ||
validationTestRouter.post( | ||
config.VALIDATION_TEST_URL, | ||
authentication, | ||
...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,3 @@ | ||
export const isFeatureEnabled = (flag: string) => { | ||
return flag === '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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export enum ErrorMessages { | ||
TEST_INFO_ERROR = 'INFO PAGE ERROR MESSAGE TEST', | ||
TEST_FIRST_NAME = 'Enter your first name', | ||
TEST_DESCRIPTION_LENGTH = 'Description must be 1000 characters or less', | ||
|
||
} |
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,13 @@ | ||
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), | ||
body('description') | ||
.isLength({ max: 1000 }) | ||
.withMessage(ErrorMessages.TEST_DESCRIPTION_LENGTH), | ||
]; |
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,8 @@ | ||
{% extends "layout.html" %} | ||
|
||
{% block pageContent %} | ||
<h1 class="govuk-heading-l"> | ||
Sorry, the service is unavailable | ||
</h1> | ||
|
||
{% 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,47 @@ | ||
{% 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"> | ||
This is a page to test validation. | ||
</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 | ||
}) }} | ||
|
||
{{ govukTextarea({ | ||
errorMessage: errors.description if errors, | ||
value: description, | ||
name: "description", | ||
id: "description", | ||
label: { | ||
text: "Description (optional)", | ||
classes: "govuk-label--m", | ||
isPageHeading: true | ||
}, | ||
hint: { | ||
text: "Include a description of what needs to be done." | ||
} | ||
}) }} | ||
|
||
</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,88 @@ | ||
jest.mock('../../../src/middleware/logger.middleware'); | ||
jest.mock('../../../src/middleware/authentication.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 { authentication } from '../../../src/middleware/authentication.middleware'; | ||
|
||
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'; | ||
import { ErrorMessages } from '../../../src/validation/error.messages'; | ||
|
||
const mockedLogger = logger as jest.Mock<typeof logger>; | ||
mockedLogger.mockImplementation( | ||
(req: Request, res: Response, next: NextFunction) => next() | ||
); | ||
const mockedAuth = authentication as jest.Mock<typeof authentication>; | ||
mockedAuth.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); | ||
expect(mockedAuth).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); | ||
expect(mockedAuth).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: '', | ||
description: '1000chars.'.repeat(100) + ':)', | ||
}); | ||
|
||
expect(res.status).toEqual(200); | ||
expect(res.text).toContain(ErrorMessages.TEST_FIRST_NAME); | ||
expect(res.text).toContain(ErrorMessages.TEST_DESCRIPTION_LENGTH); | ||
expect(res.text).toContain(MOCK_GET_VALIDATION_TEST_RESPONSE); | ||
expect(mockedLogger).toHaveBeenCalledTimes(1); | ||
expect(mockedAuth).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); | ||
expect(mockedAuth).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
Oops, something went wrong.