Skip to content

Commit

Permalink
Merge pull request #698 from companieshouse/feature/ecct-813-capture-…
Browse files Browse the repository at this point in the history
…first-time-email-in-request-body-to-cs-api

Feature/ecct 813 capture first time email in request body to confirmation statement api
  • Loading branch information
CHssmith2 authored Dec 7, 2023
2 parents ccbbf6d + 2f4c3f0 commit 05a4aa3
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 40 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"homepage": "https://github.com/companieshouse/confirmation-statement-web#readme",
"dependencies": {
"@companieshouse/api-sdk-node": "^2.0.135",
"@companieshouse/api-sdk-node": "^2.0.137",
"@companieshouse/node-session-handler": "^4.1.9",
"@companieshouse/structured-logging-node": "^1.0.8",
"@companieshouse/web-security-node": "^2.0.0",
Expand Down
8 changes: 5 additions & 3 deletions src/controllers/tasks/confirm.email.address.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ export const get = (req: Request, res: Response, next: NextFunction) => {
try {
const session = req.session as Session;
const backLinkUrl = urlUtils.getUrlToPath(PROVIDE_EMAIL_ADDRESS_PATH, req);
const emailAddress = session.getExtraData("entered-email-address");
const confirmEmailAddress = session.getExtraData("entered-email-address");
return res.render(Templates.CONFIRM_EMAIL_ADDRESS, {
templateName: Templates.CONFIRM_EMAIL_ADDRESS,
backLinkUrl, emailAddress
backLinkUrl, confirmEmailAddress
});
} catch (error) {
return next(error);
}
};

export const post = async (req: Request, res: Response, next: NextFunction) => {
const session = req.session as Session;
const confirmEmailAddress = session.getExtraData("entered-email-address");
try {
await sendUpdate(req, SECTIONS.EMAIL, SectionStatus.INITIAL_FILING);
await sendUpdate(req, SECTIONS.EMAIL, SectionStatus.INITIAL_FILING, confirmEmailAddress);
return res.redirect(urlUtils.getUrlToPath(TASK_LIST_PATH, req));
} catch (error) {
return next(error);
Expand Down
12 changes: 12 additions & 0 deletions src/utils/update.confirmation.statement.submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Request } from "express";
import { Session } from "@companieshouse/node-session-handler";
import {
ConfirmationStatementSubmission,
RegisteredEmailAddressData,
SectionStatus,
StatementOfCapitalData,
TradingStatusData
Expand Down Expand Up @@ -48,6 +49,17 @@ const generateSectionData = (section: SECTIONS, status: SectionStatus, extraData
}
return newSocData;
}

case SECTIONS.EMAIL: {
const newEmailData: RegisteredEmailAddressData = {
sectionStatus: status
};
if (extraData) {
newEmailData.registeredEmailAddress = extraData;
}
return newEmailData;
}

default: {
return {
sectionStatus: status,
Expand Down
72 changes: 41 additions & 31 deletions test/controllers/tasks/confirm.email.address.controller.unit.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
jest.mock("../../../src/middleware/company.authentication.middleware");
jest.mock("../../../src/utils/update.confirmation.statement.submission");

import request from "supertest";
import mocks from "../../mocks/all.middleware.mock";
import { companyAuthenticationMiddleware } from "../../../src/middleware/company.authentication.middleware";
Expand All @@ -8,49 +11,56 @@ import { sendUpdate } from "../../../src/utils/update.confirmation.statement.sub
import { SECTIONS } from "../../../src/utils/constants";
import { SectionStatus } from "@companieshouse/api-sdk-node/dist/services/confirmation-statement";

const mockCompanyAuthenticationMiddleware = companyAuthenticationMiddleware as jest.Mock;
mockCompanyAuthenticationMiddleware.mockImplementation((req, res, next) => next());
const mockSendUpdate = sendUpdate as jest.Mock;

const PAGE_HEADING = "Check the email address";
const EXPECTED_ERROR_TEXT = "Sorry, the service is unavailable";
const COMPANY_NUMBER = "12345678";
const TASK_LIST_URL = TASK_LIST_PATH.replace(`:${urlParams.PARAM_COMPANY_NUMBER}`, COMPANY_NUMBER);
const CONFIRM_EMAIL_ADDRESS_URL = CONFIRM_EMAIL_PATH.replace(`:${urlParams.PARAM_COMPANY_NUMBER}`, COMPANY_NUMBER);
const ENTERED_EMAIL = "[email protected]";

jest.mock("../../../src/middleware/company.authentication.middleware");
jest.mock("../../../src/utils/update.confirmation.statement.submission");
const mockCompanyAuthenticationMiddleware = companyAuthenticationMiddleware as jest.Mock;
mockCompanyAuthenticationMiddleware.mockImplementation((req, res, next) => next());
const mockSendUpdate = sendUpdate as jest.Mock;

describe("Confirm Email Address controller tests", () => {

beforeEach(() => {
mocks.mockAuthenticationMiddleware.mockClear();
mocks.mockServiceAvailabilityMiddleware.mockClear();
mocks.mockSessionMiddleware.mockClear();
});

it("Should navigate to the Confirm Email Address page", async () => {
const response = await request(app).get(CONFIRM_EMAIL_ADDRESS_URL);

expect(response.text).toContain(PAGE_HEADING);
expect(response.text).toContain("Email address");
});
describe("When entered email has been provided", () => {

beforeEach(() => {
mocks.mockAuthenticationMiddleware.mockClear();
mocks.mockServiceAvailabilityMiddleware.mockClear();
mocks.mockSessionMiddleware.mockClear();
});

it("Should return to task list page when email address is confirmed", async () => {
const response = await request(app).post(CONFIRM_EMAIL_ADDRESS_URL);
expect(mockSendUpdate.mock.calls[0][1]).toBe(SECTIONS.EMAIL);
expect(mockSendUpdate.mock.calls[0][2]).toBe(SectionStatus.INITIAL_FILING);
expect(response.status).toEqual(302);
expect(response.header.location).toEqual(TASK_LIST_URL);
});
it("Should navigate to the Confirm Email Address page", async () => {
const response = await request(app).get(CONFIRM_EMAIL_ADDRESS_URL);
expect(response.text).toContain(PAGE_HEADING);
expect(response.text).toContain("Email address");
});

it("Should redirect to an error page when error is thrown", async () => {
const spyGetUrlToPath = jest.spyOn(urlUtils, "getUrlToPath");
spyGetUrlToPath.mockImplementationOnce(() => { throw new Error(); });
const response = await request(app).get(CONFIRM_EMAIL_ADDRESS_URL);
it("Should return to task list page when email address is confirmed", async () => {
const response = await request(app).post(CONFIRM_EMAIL_ADDRESS_URL);
expect(mockSendUpdate.mock.calls[0][1]).toBe(SECTIONS.EMAIL);
expect(mockSendUpdate.mock.calls[0][2]).toBe(SectionStatus.INITIAL_FILING);
expect(response.status).toEqual(302);
expect(response.header.location).toEqual(TASK_LIST_URL);
});

expect(response.text).toContain(EXPECTED_ERROR_TEXT);
it("Should return to task list page when email address is provided", async () => {
const response = await request(app).post(CONFIRM_EMAIL_ADDRESS_URL);
expect(mockSendUpdate.mock.calls[0][3]).toBe(ENTERED_EMAIL);
expect(response.status).toEqual(302);
expect(response.header.location).toEqual(TASK_LIST_URL);
});

spyGetUrlToPath.mockRestore();
it("Should redirect to an error page when error is thrown", async () => {
const spyGetUrlToPath = jest.spyOn(urlUtils, "getUrlToPath");
spyGetUrlToPath.mockImplementationOnce(() => { throw new Error(); });
const response = await request(app).get(CONFIRM_EMAIL_ADDRESS_URL);

expect(response.text).toContain(EXPECTED_ERROR_TEXT);

spyGetUrlToPath.mockRestore();
});
});
});
1 change: 1 addition & 0 deletions test/mocks/session.middleware.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const session = new Session();
mockSessionMiddleware.mockImplementation((req: Request, res: Response, next: NextFunction) => {
req.session = session;
req.session.data.extra_data["payment-nonce"] = "123456";
req.session.data.extra_data["entered-email-address"] = "[email protected]"
next();
});

Expand Down
2 changes: 1 addition & 1 deletion views/tasks/confirm-email-address.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<h1 class="govuk-heading-l">Check the email address</h1>
<dl>
<dt class="govuk-summary-list__key">Email address</dt>
<dd class="govuk-summary-list__value">{{ emailAddress }}</dd>
<dd class="govuk-summary-list__value"> {{ confirmEmailAddress }} </dd>
<dd class="govuk-summary-list__value govuk-!-padding-left-3"><a href="{{backLinkUrl}}">Change</a></dd>
</dl>
<br>
Expand Down

0 comments on commit 05a4aa3

Please sign in to comment.