diff --git a/src/routes/add-team.ts b/src/routes/add-team.ts index 5859007..19e9468 100644 --- a/src/routes/add-team.ts +++ b/src/routes/add-team.ts @@ -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; diff --git a/src/validation/add-team.validation.ts b/src/validation/add-team.validation.ts new file mode 100644 index 0000000..0ef4e55 --- /dev/null +++ b/src/validation/add-team.validation.ts @@ -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 +]; diff --git a/src/validation/error.messages.ts b/src/validation/error.messages.ts index 6182f47..021a48d 100644 --- a/src/validation/error.messages.ts +++ b/src/validation/error.messages.ts @@ -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', } diff --git a/src/views/add-team.html b/src/views/add-team.html index 5247ff6..d85a583 100644 --- a/src/views/add-team.html +++ b/src/views/add-team.html @@ -1,10 +1,7 @@ {% extends "layout.html" %} {% block beforeContent %} - {{ govukBackLink({ - text: "Back", - href: "/landing-page" - }) }} + {% include "include/back-link.html" %} {% endblock %} {% block content %} @@ -16,46 +13,53 @@

Add a GitHub Team

Github Teams can be used to manage repository permissions and mentions for groups of members.

-
+ {% include "include/error-list.html" %} + + {{ 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" %} + +
diff --git a/test/integration/routes/add-team.spec.ts b/test/integration/routes/add-team.spec.ts index 3682ef8..e02097a 100644 --- a/test/integration/routes/add-team.spec.ts +++ b/test/integration/routes/add-team.spec.ts @@ -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; mockedLogger.mockImplementation((_req: Request, _res: Response, next: NextFunction) => next()); @@ -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); });