Skip to content

Commit

Permalink
Merge branch 'main' into NTRNL-321-add-validation-to-add-member-page
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMurray97 committed Feb 13, 2024
2 parents df3c21d + 8e0bf97 commit b2aaf23
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/routes/add-team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { authentication } from '../middleware/authentication.middleware';

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

const addTeamRouter = Router();

addTeamRouter.get(config.ADD_TEAM_URL, authentication, get);
addTeamRouter.post(config.ADD_TEAM_URL, authentication, post);
addTeamRouter.post(config.ADD_TEAM_URL, authentication, ...addTeam, checkValidations, post);

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

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

export const addTeam = [
body('team_name').notEmpty({ ignore_whitespace: true }).withMessage(ErrorMessages.TEAM_NAME),
body('team_maintainer_github_handle').not().isEmpty({ ignore_whitespace: true }).withMessage(ErrorMessages.TEAM_MAINTAINER_GITHUB_HANDLE),
...descriptionValidation
];
4 changes: 3 additions & 1 deletion src/validation/error.messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ export enum ErrorMessages {
CONTRACTOR_DATE= 'Enter the GitHub account holders contract start date in the correct format',
CONTRACTOR_DATE_TIME= 'The contract start date cannot be more than one year in the future',
REPO_NAME = 'Enter the repository name',
VISIBILITY = 'Select a visibility option'
VISIBILITY = 'Select a visibility option',
TEAM_MAINTAINER_GITHUB_HANDLE = 'Enter the team maintainer GitHub handle',
TEAM_NAME = 'Enter the team name',
}
52 changes: 28 additions & 24 deletions src/views/add-team.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 @@ -16,46 +13,53 @@ <h1 class="govuk-heading-l">Add a GitHub Team</h1>
Github Teams can be used to manage repository permissions and mentions for groups of members.
</p>

<form method="post" novalidate>
{% 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"
id: "team_name",
name: "team_name",
value: team_name
}) }}

{{ govukInput({
errorMessage: errors.team_maintainer_github_handle if errors,
label: {
text: "Team maintainer GitHub handle",
classes: "govuk-label--m"
},
classes: "govuk-input--width-10",
id: "team-maintainer-github-handle",
name: "team_maintainer_github_handle"
id: "team_maintainer_github_handle",
name: "team_maintainer_github_handle",
value: team_maintainer_github_handle
}) }}

{{ govukTextarea({
name: "description",
id: "description",
label: {
text: "Description (optional)",
classes: "govuk-label--m",
isPageHeading: true
},
hint: {
text: "Include more details to why this GitHub team needs to be
added."
}
}) }}

{{ govukButton({
text: "Save"
errorMessage: errors.description if errors,
value: description,
name: "description",
id: "description",
label: {
text: "Description (optional)",
classes: "govuk-label--m",
isPageHeading: true
},
hint: {
text: "Include more details to why this GitHub repository needs to be
added."
}
}) }}

{% include "include/save-button.html" %}


</form>
</div>
</div>
Expand Down
21 changes: 18 additions & 3 deletions test/integration/routes/add-team.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import { logger } from '../../../src/middleware/logger.middleware';
import { log } from '../../../src/utils/logger';
import { authentication } from '../../../src/middleware/authentication.middleware';

import { MOCK_REDIRECT_MESSAGE as MOCK_REDIRECT_MESSAGE, MOCK_POST_ADD_TEAM_RESPONSE, MOCK_GET_ADD_TEAM_RESPONSE } from '../../mock/text.mock';
import { MOCK_REDIRECT_MESSAGE, MOCK_GET_ADD_TEAM_RESPONSE, MOCK_POST_ADD_TEAM_RESPONSE } from '../../mock/text.mock';
import { MOCK_POST_ADD_TEAM } 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 @@ -45,13 +46,27 @@ describe('add-team endpoint integration tests', () => {
expect(mockedAuth).toHaveBeenCalledTimes(1);
});

test('Should log the Team Name and Team Maintainer GitHub handle on POST request.', async () => {
test('Should render the same page with error messages after POST request', async () => {
const res = await request(app).post(config.ADD_TEAM_URL).send({
repo_name: '',
team_maintainer_github_handle: '',
});

expect(res.status).toEqual(200);
expect(res.text).toContain(ErrorMessages.TEAM_NAME);
expect(res.text).toContain(ErrorMessages.TEAM_MAINTAINER_GITHUB_HANDLE);
expect(res.text).toContain(MOCK_GET_ADD_TEAM_RESPONSE);
expect(mockedLogger).toHaveBeenCalledTimes(1);
expect(mockedAuth).toHaveBeenCalledTimes(1);
});

test('Should log the add team details POST request', async () => {
const res = await request(app).post(config.ADD_TEAM_URL).send(MOCK_POST_ADD_TEAM);

const mockLog = log.info as jest.Mock;

expect(res.text).toContain(MOCK_REDIRECT_MESSAGE);
expect(mockLog).toBeCalledWith(MOCK_POST_ADD_TEAM_RESPONSE);
expect(res.text).toContain(MOCK_REDIRECT_MESSAGE);
expect(mockedLogger).toHaveBeenCalledTimes(1);
expect(mockedAuth).toHaveBeenCalledTimes(1);
});
Expand Down

0 comments on commit b2aaf23

Please sign in to comment.