fix(security): harden Contact Us PET notifications
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
const assert = require("assert");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const {
|
||||
createAxiosMock,
|
||||
createLoggerMock,
|
||||
@@ -460,6 +462,109 @@ test("notify/sendContactUsConfirmation logs and rethrows on failure", async () =
|
||||
assert.strictEqual(logger.calls.length, 1);
|
||||
});
|
||||
|
||||
test("notify/sendContactUsPETNotification posts only the dedicated PET payload", async () => {
|
||||
const axios = createAxiosMock();
|
||||
const logger = createLoggerMock();
|
||||
|
||||
axios.requestHandler = async () => ({ data: { sent: true } });
|
||||
|
||||
const notify = loadServiceModule("notifyDirectService.js", {
|
||||
axios,
|
||||
consoleLogger: logger.consoleLogger
|
||||
});
|
||||
const formValues = {
|
||||
contactEmail: "person@example.com",
|
||||
contactSubject: "Questions about Planning legislation or policy",
|
||||
contactbody: "Please review https://example.invalid",
|
||||
typeOfContact: "plq",
|
||||
ignored: "not sent"
|
||||
};
|
||||
|
||||
const result = await notify.sendContactUsPETNotification(
|
||||
formValues,
|
||||
"pet@example.com"
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(normalize(result), { sent: true });
|
||||
assert.strictEqual(
|
||||
axios.calls[0].config.url,
|
||||
"/api/email/contactus-pet-notification"
|
||||
);
|
||||
assert.strictEqual(axios.calls[0].config.method, "post");
|
||||
assert.deepStrictEqual(normalize(axios.calls[0].config.data), {
|
||||
petEmail: "pet@example.com",
|
||||
contactEmail: "person@example.com",
|
||||
contactSubject: "Questions about Planning legislation or policy",
|
||||
contactbody: "Please review https://example.invalid",
|
||||
typeOfContact: "plq"
|
||||
});
|
||||
});
|
||||
|
||||
test("notify/sendContactUsPETNotification logs and rethrows on failure", async () => {
|
||||
const axios = createAxiosMock();
|
||||
const logger = createLoggerMock();
|
||||
const error = createAxiosError(400, "Bad Request");
|
||||
|
||||
axios.requestHandler = async () => Promise.reject(error);
|
||||
|
||||
const notify = loadServiceModule("notifyDirectService.js", {
|
||||
axios,
|
||||
consoleLogger: logger.consoleLogger
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
notify.sendContactUsPETNotification(
|
||||
{
|
||||
contactEmail: "person@example.com",
|
||||
contactSubject: "Questions about Planning legislation or policy",
|
||||
contactbody: "Message",
|
||||
typeOfContact: "plq"
|
||||
},
|
||||
"pet@example.com"
|
||||
),
|
||||
(thrown) => thrown === error
|
||||
);
|
||||
assert.strictEqual(logger.calls.length, 1);
|
||||
});
|
||||
|
||||
test("public and portal Contact Us pages preserve PET gating and submission ordering", async () => {
|
||||
const rootDir = path.resolve(__dirname, "..", "..");
|
||||
|
||||
for (const relativePath of ["pages/contactus.js", "pages/myportal/contactus.js"]) {
|
||||
const source = fs.readFileSync(path.join(rootDir, relativePath), "utf8");
|
||||
const confirmationCall = source.indexOf("sendContactEmail(formValues);");
|
||||
const crmCall = source.indexOf("createCRMTask(formValues);");
|
||||
const petCondition = source.indexOf(
|
||||
'formValues.typeOfContact == "plq" &&'
|
||||
);
|
||||
const petCall = source.indexOf(
|
||||
"sendPETEmail(formValues, props.petEmail);"
|
||||
);
|
||||
|
||||
assert.notStrictEqual(confirmationCall, -1);
|
||||
assert.notStrictEqual(crmCall, -1);
|
||||
assert.notStrictEqual(petCondition, -1);
|
||||
assert.notStrictEqual(petCall, -1);
|
||||
assert.strictEqual(
|
||||
confirmationCall < crmCall &&
|
||||
crmCall < petCondition &&
|
||||
petCondition < petCall,
|
||||
true
|
||||
);
|
||||
assert.strictEqual(
|
||||
source.includes(
|
||||
"sendContactUsPETNotification(formValues, petEmail)"
|
||||
),
|
||||
true
|
||||
);
|
||||
assert.strictEqual(
|
||||
source.includes("sendContactUsConfirmation("),
|
||||
true
|
||||
);
|
||||
assert.strictEqual(source.includes("sendEmail(templateId,"), false);
|
||||
}
|
||||
});
|
||||
|
||||
test("integration/createCRMTask posts payload and returns data", async () => {
|
||||
const axios = createAxiosMock();
|
||||
const logger = createLoggerMock();
|
||||
|
||||
Reference in New Issue
Block a user