-
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.
Merge pull request #793 from companieshouse/sec-112-enable-add-csrf-m…
…iddleware Sec 112 enable add csrf middleware
- Loading branch information
Showing
13 changed files
with
102 additions
and
3 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,36 @@ | ||
jest.mock("../../src/controllers/confirm.company.controller"); | ||
|
||
import mocks from "../mocks/all.middleware.mock"; | ||
import request from "supertest"; | ||
import app from "../../src/app"; | ||
import * as pageUrls from "../../src/types/page.urls"; | ||
import { CsrfError } from "@companieshouse/web-security-node"; | ||
import * as confirmCompanyController from "../../src/controllers/confirm.company.controller"; | ||
|
||
const mockGetConfirmCompany = confirmCompanyController.get as jest.Mock; | ||
|
||
const CSRF_TOKEN_ERROR = "CSRF token mismatch"; | ||
const CSRF_ERROR_PAGE_TEXT = "We have not been able to save the information you submitted on the previous screen."; | ||
const CSRF_ERROR_PAGE_HEADING = "Sorry, something went wrong"; | ||
|
||
describe("ERROR controller", () => { | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
// clearing the mocks will initialise them for first use as well as between tests | ||
mocks.mockAuthenticationMiddleware.mockClear(); | ||
mocks.mockServiceAvailabilityMiddleware.mockClear(); | ||
mocks.mockSessionMiddleware.mockClear(); | ||
}); | ||
|
||
describe("CSRF error page tests", () => { | ||
|
||
test("Should render the CSRF error page", async () => { | ||
mockGetConfirmCompany.mockImplementationOnce(() => { throw new CsrfError(CSRF_TOKEN_ERROR); }); | ||
const response = await request(app).get(pageUrls.CONFIRM_COMPANY_PATH); | ||
expect(response.status).toEqual(403); | ||
expect(response.text).toContain(CSRF_ERROR_PAGE_HEADING); | ||
expect(response.text).toContain(CSRF_ERROR_PAGE_TEXT); | ||
}); | ||
}); | ||
}); |
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
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,9 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import { CsrfProtectionMiddleware } from "@companieshouse/web-security-node"; | ||
|
||
jest.mock("@companieshouse/web-security-node"); | ||
|
||
const mockCsrfProtectionMiddleware = CsrfProtectionMiddleware as jest.Mock; | ||
mockCsrfProtectionMiddleware.mockImplementation((_opts) => (req: Request, res: Response, next: NextFunction) => next()); | ||
|
||
export default mockCsrfProtectionMiddleware; |
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,6 @@ | ||
{% from "web-security-node/components/csrf-error/macro.njk" import csrfError %} | ||
{% extends "layout.html" %} | ||
|
||
{% block content %} | ||
{{ csrfError({}) }} | ||
{% endblock %} |