From ceda4172466e4d99d8a58913a033e05e5df0e441 Mon Sep 17 00:00:00 2001 From: lapenna-bjss Date: Mon, 14 Apr 2025 15:22:58 +0100 Subject: [PATCH 1/2] CCM-8894: Update sandbox tests to reflect backend --- sandbox/__test__/batch_send.spec.js | 416 +++++++++++++++++- sandbox/__test__/messages.spec.js | 380 +++++++++++++++- .../override_contact_details.js | 136 +++++- 3 files changed, 893 insertions(+), 39 deletions(-) diff --git a/sandbox/__test__/batch_send.spec.js b/sandbox/__test__/batch_send.spec.js index 25b04c6ce..d5a1c2941 100644 --- a/sandbox/__test__/batch_send.spec.js +++ b/sandbox/__test__/batch_send.spec.js @@ -1047,12 +1047,12 @@ describe("/api/v1/send", () => { }, }) .expect(400, { - message: "Invalid recipient contact details. Field 'sms': Invalid", + message: "Invalid recipient contact details. Field 'sms': Input is not a string", errors: [ { code: 'CM_INVALID_VALUE', field: "/data/attributes/messages/0/recipient/contactDetails/sms", - message: "Invalid", + message: "Input is not a string", title: "Invalid value", statusCode: 400, }, @@ -1156,12 +1156,12 @@ describe("/api/v1/send", () => { }, }) .expect(400, { - message: "Invalid recipient contact details. Field 'email': Invalid", + message: "Invalid recipient contact details. Field 'email': Input is not a string", errors: [ { code: 'CM_INVALID_VALUE', field: "/data/attributes/messages/0/recipient/contactDetails/email", - message: "Invalid", + message: "Input is not a string", title: "Invalid value", statusCode: 400, }, @@ -1308,7 +1308,7 @@ describe("/api/v1/send", () => { }) .expect(400, { message: - "Invalid recipient contact details. Field 'lines': 'lines' is missing", + "Invalid recipient contact details. Field 'address': 'lines' is missing", errors: [ { code: 'CM_MISSING_VALUE', @@ -1351,7 +1351,7 @@ describe("/api/v1/send", () => { }) .expect(400, { message: - "Invalid recipient contact details. Field 'lines': Too few address lines were provided", + "Invalid recipient contact details. Field 'address': Too few address lines were provided", errors: [ { code: 'CM_TOO_FEW_ITEMS', @@ -1393,7 +1393,7 @@ describe("/api/v1/send", () => { }, }) .expect(400, { - message: "Invalid recipient contact details. Field 'lines': Too many address lines were provided", + message: "Invalid recipient contact details. Field 'address': Too many address lines were provided", errors: [ { code: 'CM_INVALID_VALUE', @@ -1435,12 +1435,12 @@ describe("/api/v1/send", () => { }, }) .expect(400, { - message: "Invalid recipient contact details. Field 'lines': Invalid", + message: "Invalid recipient contact details. Field 'address': Lines contain non-string or empty line", errors: [ { code: 'CM_INVALID_VALUE', field: "/data/attributes/messages/0/recipient/contactDetails/address", - message: "Invalid", + message: "Lines contain non-string or empty line", title: "Invalid value", statusCode: 400, }, @@ -1477,7 +1477,7 @@ describe("/api/v1/send", () => { }) .expect(400, { message: - "Invalid recipient contact details. Field 'postcode': 'postcode' is missing", + "Invalid recipient contact details. Field 'address': 'postcode' is missing", errors: [ { code: 'CM_MISSING_VALUE', @@ -1519,11 +1519,84 @@ describe("/api/v1/send", () => { }, }) .expect(400, { - message: "Invalid recipient contact details. Field 'postcode': Invalid", + message: "Invalid recipient contact details. Field 'address': 'postcode' is not a string", errors: [ { code: 'CM_INVALID_VALUE', - field: "/data/attributes/messages/0/recipient/contactDetails/address/postcode", + field: "/data/attributes/messages/0/recipient/contactDetails/address", + message: "'postcode' is not a string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 200 when name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/send") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "MessageBatch", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageBatchReference: "request-id", + messages: [ + { + messageReference: "1", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: "John", + middleNames: "Little", + lastName: "Smith", + suffix: "Jr" + }, + }, + }, + }, + ], + }, + }, + }) + .expect(200) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when string for name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/send") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "MessageBatch", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageBatchReference: "request-id", + messages: [ + { + messageReference: "1", + recipient: { + nhsNumber: "1", + contactDetails: { + name: "hello", + }, + }, + }, + ], + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': Invalid", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/messages/0/recipient/contactDetails/name", message: "Invalid", title: "Invalid value", statusCode: 400, @@ -1532,6 +1605,316 @@ describe("/api/v1/send", () => { }) .expect("Content-Type", /json/, done); }); + + it("returns a 400 when array for name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/send") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "MessageBatch", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageBatchReference: "request-id", + messages: [ + { + messageReference: "1", + recipient: { + nhsNumber: "1", + contactDetails: { + name: [], + }, + }, + }, + ], + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': Invalid", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/messages/0/recipient/contactDetails/name", + message: "Invalid", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when prefix contains non-string value in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/send") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "MessageBatch", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageBatchReference: "request-id", + messages: [ + { + messageReference: "1", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: 0, + firstName: "John", + middleNames: "Little", + lastName: "Smith", + suffix: "Jr" + }, + }, + }, + }, + ], + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': 'prefix' is not a string", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/messages/0/recipient/contactDetails/name", + message: "'prefix' is not a string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when firstName contains non-string value in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/send") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "MessageBatch", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageBatchReference: "request-id", + messages: [ + { + messageReference: "1", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: 0, + middleNames: "Little", + lastName: "Smith", + suffix: "Jr" + }, + }, + }, + }, + ], + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': 'firstName' is not a string", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/messages/0/recipient/contactDetails/name", + message: "'firstName' is not a string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when middleNames contains non-string value in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/send") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "MessageBatch", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageBatchReference: "request-id", + messages: [ + { + messageReference: "1", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: "John", + middleNames: 0, + lastName: "Smith", + suffix: "Jr" + }, + }, + }, + }, + ], + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': 'middleNames' is not a string", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/messages/0/recipient/contactDetails/name", + message: "'middleNames' is not a string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when no lastName is provided in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/send") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "MessageBatch", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageBatchReference: "request-id", + messages: [ + { + messageReference: "1", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: "John", + middleNames: "Little", + suffix: "Jr" + }, + }, + }, + }, + ], + }, + }, + }) + .expect(400, { + message: + "Invalid recipient contact details. Field 'name': 'lastName' is missing", + errors: [ + { + code: 'CM_MISSING_VALUE', + field: "/data/attributes/messages/0/recipient/contactDetails/name", + message: "`lastName` is missing", + title: "Missing value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when lastName contains non-string value in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/send") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "MessageBatch", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageBatchReference: "request-id", + messages: [ + { + messageReference: "1", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: "John", + middleNames: "Little", + lastName: 0, + suffix: "Jr" + }, + }, + }, + }, + ], + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': 'lastName' must be a non-empty string", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/messages/0/recipient/contactDetails/name", + message: "'lastName' must be a non-empty string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when suffix contains non-string value in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/send") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "MessageBatch", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageBatchReference: "request-id", + messages: [ + { + messageReference: "1", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: "John", + middleNames: "Little", + lastName: "Smith", + suffix: 0 + }, + }, + }, + }, + ], + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': 'suffix' is not a string", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/messages/0/recipient/contactDetails/name", + message: "'suffix' is not a string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + it("returns a 400 and multiple errors when there are multiple issues in contact details provided", (done) => { request(server) .post("/api/v1/send") @@ -1562,7 +1945,7 @@ describe("/api/v1/send", () => { }) .expect(400, { message: - "Invalid recipient contact details. Field 'email': Input failed format check. Field 'lines': Too few address lines were provided", + "Invalid recipient contact details. Field 'email': Input failed format check. Field 'address': Too few address lines were provided", errors: [ { code: 'CM_INVALID_VALUE', @@ -1605,6 +1988,13 @@ describe("/api/v1/send", () => { lines: ["1", "2"], postcode: "hello", }, + name: { + prefix: "Mr", + firstName: "John", + middleNames: "Little", + lastName: "Smith", + suffix: "Jr" + }, }, }, }, diff --git a/sandbox/__test__/messages.spec.js b/sandbox/__test__/messages.spec.js index a489569d3..6d3477548 100644 --- a/sandbox/__test__/messages.spec.js +++ b/sandbox/__test__/messages.spec.js @@ -740,12 +740,12 @@ describe("/api/v1/messages", () => { }, }) .expect(400, { - message: "Invalid recipient contact details. Field 'sms': Invalid", + message: "Invalid recipient contact details. Field 'sms': Input is not a string", errors: [ { code: 'CM_INVALID_VALUE', field: "/data/attributes/recipient/contactDetails/sms", - message: "Invalid", + message: "Input is not a string", title: "Invalid value", statusCode: 400, }, @@ -837,12 +837,12 @@ describe("/api/v1/messages", () => { }, }) .expect(400, { - message: "Invalid recipient contact details. Field 'email': Invalid", + message: "Invalid recipient contact details. Field 'email': Input is not a string", errors: [ { code: 'CM_INVALID_VALUE', field: "/data/attributes/recipient/contactDetails/email", - message: "Invalid", + message: "Input is not a string", title: "Invalid value", statusCode: 400, }, @@ -973,7 +973,7 @@ describe("/api/v1/messages", () => { }) .expect(400, { message: - "Invalid recipient contact details. Field 'lines': 'lines' is missing", + "Invalid recipient contact details. Field 'address': 'lines' is missing", errors: [ { code: 'CM_MISSING_VALUE', @@ -1012,7 +1012,7 @@ describe("/api/v1/messages", () => { }) .expect(400, { message: - "Invalid recipient contact details. Field 'lines': Too few address lines were provided", + "Invalid recipient contact details. Field 'address': Too few address lines were provided", errors: [ { code: 'CM_TOO_FEW_ITEMS', @@ -1050,7 +1050,7 @@ describe("/api/v1/messages", () => { }, }) .expect(400, { - message: "Invalid recipient contact details. Field 'lines': Too many address lines were provided", + message: "Invalid recipient contact details. Field 'address': Too many address lines were provided", errors: [ { code: 'CM_INVALID_VALUE', @@ -1088,12 +1088,12 @@ describe("/api/v1/messages", () => { }, }) .expect(400, { - message: "Invalid recipient contact details. Field 'lines': Invalid", + message: "Invalid recipient contact details. Field 'address': Lines contain non-string or empty line", errors: [ { code: 'CM_INVALID_VALUE', field: "/data/attributes/recipient/contactDetails/address", - message: "Invalid", + message: "Lines contain non-string or empty line", title: "Invalid value", statusCode: 400, }, @@ -1126,7 +1126,7 @@ describe("/api/v1/messages", () => { }) .expect(400, { message: - "Invalid recipient contact details. Field 'postcode': 'postcode' is missing", + "Invalid recipient contact details. Field 'address': 'postcode' is missing", errors: [ { code: 'CM_MISSING_VALUE', @@ -1164,11 +1164,76 @@ describe("/api/v1/messages", () => { }, }) .expect(400, { - message: "Invalid recipient contact details. Field 'postcode': Invalid", + message: "Invalid recipient contact details. Field 'address': 'postcode' is not a string", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/recipient/contactDetails/address", + message: "'postcode' is not a string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 201 when valid value for name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/messages") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "Message", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageReference: "b5bb84b9-a522-41e9-aa8b-ad1b6a454243", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: "John", + middleNames: "Little", + lastName: "Smith", + suffix: "Jr" + }, + }, + }, + personalisation: {}, + }, + }, + }) + .expect(201) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when string for name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/messages") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "Message", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageReference: "b5bb84b9-a522-41e9-aa8b-ad1b6a454243", + recipient: { + nhsNumber: "1", + contactDetails: { + name: "hello", + }, + }, + personalisation: {}, + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': Invalid", errors: [ { code: 'CM_INVALID_VALUE', - field: "/data/attributes/recipient/contactDetails/address/postcode", + field: "/data/attributes/recipient/contactDetails/name", message: "Invalid", title: "Invalid value", statusCode: 400, @@ -1177,6 +1242,288 @@ describe("/api/v1/messages", () => { }) .expect("Content-Type", /json/, done); }); + + it("returns a 400 when array for name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/messages") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "Message", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageReference: "b5bb84b9-a522-41e9-aa8b-ad1b6a454243", + recipient: { + nhsNumber: "1", + contactDetails: { + name: [], + }, + }, + personalisation: {}, + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': Invalid", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/recipient/contactDetails/name", + message: "Invalid", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when prefix contains non-string value in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/messages") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "Message", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageReference: "b5bb84b9-a522-41e9-aa8b-ad1b6a454243", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: 0, + firstName: "John", + middleNames: "Little", + lastName: "Smith", + suffix: "Jr" + }, + }, + }, + personalisation: {}, + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': 'prefix' is not a string", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/recipient/contactDetails/name", + message: "'prefix' is not a string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when firstName contains non-string value in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/messages") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "Message", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageReference: "b5bb84b9-a522-41e9-aa8b-ad1b6a454243", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: 0, + middleNames: "Little", + lastName: "Smith", + suffix: "Jr" + }, + }, + }, + personalisation: {}, + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': 'firstName' is not a string", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/recipient/contactDetails/name", + message: "'firstName' is not a string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when middleNames contains non-string value in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/messages") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "Message", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageReference: "b5bb84b9-a522-41e9-aa8b-ad1b6a454243", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: "John", + middleNames: 0, + lastName: "Smith", + suffix: "Jr" + }, + }, + }, + personalisation: {}, + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': 'middleNames' is not a string", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/recipient/contactDetails/name", + message: "'middleNames' is not a string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when no lastName is provided in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/messages") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "Message", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageReference: "b5bb84b9-a522-41e9-aa8b-ad1b6a454243", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: "John", + middleNames: "Little", + suffix: "Jr" + }, + }, + }, + personalisation: {}, + }, + }, + }) + .expect(400, { + message: + "Invalid recipient contact details. Field 'name': 'lastName' is missing", + errors: [ + { + code: 'CM_MISSING_VALUE', + field: "/data/attributes/recipient/contactDetails/name", + message: "`lastName` is missing", + title: "Missing value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when lastName contains non-string value in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/messages") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "Message", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageReference: "b5bb84b9-a522-41e9-aa8b-ad1b6a454243", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: "John", + middleNames: "Little", + lastName: [], + suffix: "Jr" + }, + }, + }, + personalisation: {}, + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': 'lastName' must be a non-empty string", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/recipient/contactDetails/name", + message: "'lastName' must be a non-empty string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + + it("returns a 400 when suffix contains non-string value in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/messages") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "Message", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageReference: "b5bb84b9-a522-41e9-aa8b-ad1b6a454243", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: "John", + middleNames: "Little", + lastName: "Smith", + suffix: 0 + }, + }, + }, + personalisation: {}, + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': 'suffix' is not a string", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/recipient/contactDetails/name", + message: "'suffix' is not a string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + it("returns a 400 and multiple errors when there are multiple issues in contact details provided", (done) => { request(server) .post("/api/v1/messages") @@ -1203,7 +1550,7 @@ describe("/api/v1/messages", () => { }) .expect(400, { message: - "Invalid recipient contact details. Field 'email': Input failed format check. Field 'lines': Too few address lines were provided", + "Invalid recipient contact details. Field 'email': Input failed format check. Field 'address': Too few address lines were provided", errors: [ { code: 'CM_INVALID_VALUE', @@ -1244,6 +1591,13 @@ describe("/api/v1/messages", () => { lines: ["1", "2"], postcode: "hello", }, + name: { + prefix: "Mr", + firstName: "John", + middleNames: "Little", + lastName: "Smith", + suffix: "Jr" + }, }, }, }, diff --git a/sandbox/handlers/error_scenarios/override_contact_details.js b/sandbox/handlers/error_scenarios/override_contact_details.js index 5fbf4b742..c633010d9 100644 --- a/sandbox/handlers/error_scenarios/override_contact_details.js +++ b/sandbox/handlers/error_scenarios/override_contact_details.js @@ -38,10 +38,10 @@ function smsValidation(sms, path) { title: "Invalid value", code: 'CM_INVALID_VALUE', field: `${path}/recipient/contactDetails/sms`, - message: "Invalid", + message: "Input is not a string", statusCode: 400, }, - `Field 'sms': Invalid`, + `Field 'sms': Input is not a string`, ]); } return validationSuccess(); @@ -69,10 +69,10 @@ function emailValidation(email, path) { title: "Invalid value", code: 'CM_INVALID_VALUE', field: `${path}/recipient/contactDetails/email`, - message: "Invalid", + message: "Input is not a string", statusCode: 400, }, - `Field 'email': Invalid`, + `Field 'email': Input is not a string`, ]); } return validationSuccess(); @@ -105,7 +105,7 @@ function addressValidation(address, path) { message: "`lines` is missing", statusCode: 400, }, - `Field 'lines': 'lines' is missing`, + `Field 'address': 'lines' is missing`, ]); } if (!Array.isArray(address.lines)) { @@ -117,7 +117,7 @@ function addressValidation(address, path) { message: "`lines` is missing", statusCode: 400, }, - `Field 'lines': 'lines' is missing`, + `Field 'address': 'lines' is missing`, ]); } @@ -127,10 +127,10 @@ function addressValidation(address, path) { title: "Invalid value", code: 'CM_INVALID_VALUE', field: `${path}/recipient/contactDetails/address`, - message: "Invalid", + message: "Lines contain non-string or empty line", statusCode: 400, }, - `Field 'lines': Invalid`, + `Field 'address': Lines contain non-string or empty line`, ]); } @@ -143,7 +143,7 @@ function addressValidation(address, path) { message: "Too few address lines were provided", statusCode: 400, }, - `Field 'lines': Too few address lines were provided`, + `Field 'address': Too few address lines were provided`, ]); } if (address.lines.length > 5) { @@ -155,7 +155,7 @@ function addressValidation(address, path) { message: "Too many address lines were provided", statusCode: 400, }, - `Field 'lines': Too many address lines were provided`, + `Field 'address': Too many address lines were provided`, ]); } @@ -168,7 +168,7 @@ function addressValidation(address, path) { message: "`postcode` is missing", statusCode: 400, }, - `Field 'postcode': 'postcode' is missing`, + `Field 'address': 'postcode' is missing`, ]); } @@ -177,11 +177,110 @@ function addressValidation(address, path) { { title: "Invalid value", code: 'CM_INVALID_VALUE', - field: `${path}/recipient/contactDetails/address/postcode`, + field: `${path}/recipient/contactDetails/address`, + message: "'postcode' is not a string", + statusCode: 400, + }, + `Field 'address': 'postcode' is not a string`, + ]); + } + return validationSuccess(); +} + +function nameValidation(name, path) { + + if (!name) { + return validationSuccess(); + } + + if (typeof name !== "object" || Array.isArray(name)) { + return validationFailure([ + { + title: "Invalid value", + code: 'CM_INVALID_VALUE', + field: `${path}/recipient/contactDetails/name`, message: "Invalid", statusCode: 400, }, - `Field 'postcode': Invalid`, + `Field 'name': Invalid`, + ]); + } + + if (typeof name.prefix !== "string") { + return validationFailure([ + { + title: "Invalid value", + code: 'CM_INVALID_VALUE', + field: `${path}/recipient/contactDetails/name`, + message: "'prefix' is not a string", + statusCode: 400, + }, + `Field 'name': 'prefix' is not a string`, + ]); + } + + if (typeof name.firstName !== "string") { + return validationFailure([ + { + title: "Invalid value", + code: 'CM_INVALID_VALUE', + field: `${path}/recipient/contactDetails/name`, + message: "'firstName' is not a string", + statusCode: 400, + }, + `Field 'name': 'firstName' is not a string`, + ]); + } + + if (typeof name.middleNames !== "string") { + return validationFailure([ + { + title: "Invalid value", + code: 'CM_INVALID_VALUE', + field: `${path}/recipient/contactDetails/name`, + message: "'middleNames' is not a string", + statusCode: 400, + }, + `Field 'name': 'middleNames' is not a string`, + ]); + } + + if (typeof name.lastName === "undefined") { + return validationFailure([ + { + title: "Missing value", + code: 'CM_MISSING_VALUE', + field: `${path}/recipient/contactDetails/name`, + message: "`lastName` is missing", + statusCode: 400, + }, + `Field 'name': 'lastName' is missing`, + ]); + } + + if (typeof name.lastName !== "string") { + return validationFailure([ + { + title: "Invalid value", + code: 'CM_INVALID_VALUE', + field: `${path}/recipient/contactDetails/name`, + message: "'lastName' must be a non-empty string", + statusCode: 400, + }, + `Field 'name': 'lastName' must be a non-empty string`, + ]); + } + + if (typeof name.suffix !== "string") { + return validationFailure([ + { + title: "Invalid value", + code: 'CM_INVALID_VALUE', + field: `${path}/recipient/contactDetails/name`, + message: "'suffix' is not a string", + statusCode: 400, + }, + `Field 'name': 'suffix' is not a string`, ]); } return validationSuccess(); @@ -248,6 +347,17 @@ export function getAlternateContactDetailsError( validationErrorMessages.push(errorMessage); } + const { valid: nameValid, result: nameErrors } = nameValidation( + contactDetails.name, + path + ); + + if (!nameValid) { + const [errors, errorMessage] = nameErrors; + validationErrors.push(errors); + validationErrorMessages.push(errorMessage); + } + if (validationErrors.length) { return [400, validationErrorMessages.join(". "), validationErrors]; } From 647f3a6955314569286efcfbade054fd3751d52d Mon Sep 17 00:00:00 2001 From: lapenna-bjss Date: Wed, 16 Apr 2025 10:29:50 +0100 Subject: [PATCH 2/2] CCM-8894: Test lastName required and not empty --- sandbox/__test__/messages.spec.js | 67 +++++++++++++++++++ .../override_contact_details.js | 10 +-- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/sandbox/__test__/messages.spec.js b/sandbox/__test__/messages.spec.js index 6d3477548..d47d18d0e 100644 --- a/sandbox/__test__/messages.spec.js +++ b/sandbox/__test__/messages.spec.js @@ -1208,6 +1208,32 @@ describe("/api/v1/messages", () => { .expect("Content-Type", /json/, done); }); + it("returns a 201 when only lastName is provided for name alternate contact details", (done) => { + request(server) + .post("/api/v1/messages") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "Message", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageReference: "b5bb84b9-a522-41e9-aa8b-ad1b6a454243", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + lastName: "Smith" + }, + }, + }, + personalisation: {}, + }, + }, + }) + .expect(201) + .expect("Content-Type", /json/, done); + }); + it("returns a 400 when string for name alternate contact detail is provided", (done) => { request(server) .post("/api/v1/messages") @@ -1483,6 +1509,47 @@ describe("/api/v1/messages", () => { .expect("Content-Type", /json/, done); }); + it("returns a 400 when lastName contains empty-string value in name alternate contact detail is provided", (done) => { + request(server) + .post("/api/v1/messages") + .set({ Authorization: "allowedContactDetailOverride" }) + .send({ + data: { + type: "Message", + attributes: { + routingPlanId: "b838b13c-f98c-4def-93f0-515d4e4f4ee1", + messageReference: "b5bb84b9-a522-41e9-aa8b-ad1b6a454243", + recipient: { + nhsNumber: "1", + contactDetails: { + name: { + prefix: "Mr", + firstName: "John", + middleNames: "Little", + lastName: "", + suffix: "Jr" + }, + }, + }, + personalisation: {}, + }, + }, + }) + .expect(400, { + message: "Invalid recipient contact details. Field 'name': 'lastName' must be a non-empty string", + errors: [ + { + code: 'CM_INVALID_VALUE', + field: "/data/attributes/recipient/contactDetails/name", + message: "'lastName' must be a non-empty string", + title: "Invalid value", + statusCode: 400, + }, + ], + }) + .expect("Content-Type", /json/, done); + }); + it("returns a 400 when suffix contains non-string value in name alternate contact detail is provided", (done) => { request(server) .post("/api/v1/messages") diff --git a/sandbox/handlers/error_scenarios/override_contact_details.js b/sandbox/handlers/error_scenarios/override_contact_details.js index c633010d9..b9189dc5c 100644 --- a/sandbox/handlers/error_scenarios/override_contact_details.js +++ b/sandbox/handlers/error_scenarios/override_contact_details.js @@ -206,7 +206,7 @@ function nameValidation(name, path) { ]); } - if (typeof name.prefix !== "string") { + if ("prefix" in name && typeof name.prefix !== "string") { return validationFailure([ { title: "Invalid value", @@ -219,7 +219,7 @@ function nameValidation(name, path) { ]); } - if (typeof name.firstName !== "string") { + if ("firstName" in name && typeof name.firstName !== "string") { return validationFailure([ { title: "Invalid value", @@ -232,7 +232,7 @@ function nameValidation(name, path) { ]); } - if (typeof name.middleNames !== "string") { + if ("middleNames" in name && typeof name.middleNames !== "string") { return validationFailure([ { title: "Invalid value", @@ -258,7 +258,7 @@ function nameValidation(name, path) { ]); } - if (typeof name.lastName !== "string") { + if (typeof name.lastName !== "string" || name.lastName.trim() === "") { return validationFailure([ { title: "Invalid value", @@ -271,7 +271,7 @@ function nameValidation(name, path) { ]); } - if (typeof name.suffix !== "string") { + if ("suffix" in name && typeof name.suffix !== "string") { return validationFailure([ { title: "Invalid value",