Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ntrnl 324 add validation to team request page #27

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/routes/team-request.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Router } from 'express';

import { authentication } from '../middleware/authentication.middleware';

import { checkValidations } from '../middleware/validation.middleware';
import { teamRequest } from '../validation/team-request.validation';
import { get, post } from '../controller/team-request.controller';
import * as config from '../config';

const teamRequestRouter = Router();

teamRequestRouter.get(config.TEAM_REQUEST_URL, authentication, get);
teamRequestRouter.post(config.TEAM_REQUEST_URL, authentication, post);
teamRequestRouter.post(config.TEAM_REQUEST_URL, authentication, ...teamRequest, checkValidations, post);

export default teamRequestRouter;
9 changes: 9 additions & 0 deletions src/validation/team-request.validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { body } from 'express-validator';

import { ErrorMessages } from './error.messages';
import { descriptionValidation } from './fields/description.validation';

export const teamRequest = [
body('team_name').not().isEmpty({ ignore_whitespace: true }).withMessage(ErrorMessages.TEAM_NAME),
...descriptionValidation
];
19 changes: 9 additions & 10 deletions src/views/team-request.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{% extends "layout.html" %}

{% block beforeContent %}
{{ govukBackLink({
text: "Back",
href: "/landing-page"
}) }}
{% include "include/back-link.html" %}
{% endblock %}

{% block content %}
Expand All @@ -15,22 +12,26 @@ <h1 class="govuk-heading-l">Additional Team Request</h1>
<p class="govuk-body">
Use this page to request a more specific request that is not covered by the other options.
</p>

{% include "include/error-list.html" %}
<form method="post" novalidate>

{{ govukInput({
errorMessage: errors.team_name if errors,
label: {
text: "Team Name",
classes: "govuk-label--m"
},
classes: "govuk-input--width-10",
id: "team-name",
name: "team_name"
mel-am marked this conversation as resolved.
Show resolved Hide resolved
id: "team_name",
name: "team_name",
value: team_name
}) }}

{{ govukTextarea({
errorMessage: errors.description if errors,
name: "description",
id: "description",
value: description,
label: {
text: "Description - Permission changes",
classes: "govuk-label--m",
Expand All @@ -41,9 +42,7 @@ <h1 class="govuk-heading-l">Additional Team Request</h1>
}
}) }}

{{ govukButton({
text: "Save"
}) }}
{% include "include/save-button.html" %}

</form>
</div>
Expand Down
14 changes: 14 additions & 0 deletions test/integration/routes/team-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { authentication } from '../../../src/middleware/authentication.middlewar

import { MOCK_REDIRECT_MESSAGE, MOCK_GET_TEAM_REQUEST_RESPONSE, MOCK_POST_TEAM_REQUEST_RESPONSE } from '../../mock/text.mock';
import { MOCK_POST_TEAM_REQUEST } 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());
Expand Down Expand Up @@ -44,6 +45,19 @@ describe('Team-request endpoint integration tests', () => {
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.TEAM_REQUEST_URL).send({
team_name: '',
description: '1000chars.'.repeat(100) + ':)'
});

expect(res.status).toEqual(200);
expect(res.text).toContain(ErrorMessages.TEAM_NAME);
expect(res.text).toContain(ErrorMessages.DESCRIPTION_LENGTH);
expect(res.text).toContain(MOCK_GET_TEAM_REQUEST_RESPONSE);
expect(mockedLogger).toHaveBeenCalledTimes(1);
expect(mockedAuth).toHaveBeenCalledTimes(1);
});
test('Should log the team name and description on POST request.', async () => {
const res = await request(app).post(config.TEAM_REQUEST_URL).send(MOCK_POST_TEAM_REQUEST);

Expand Down
Loading