diff --git a/lib/utils.js b/lib/utils.js index 4f1dfbb..ae8b8b7 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -2,7 +2,6 @@ const { existsSync, readFileSync } = require('fs'); const { basename } = require('path'); const cds = require("@sap/cds"); const LOG = cds.log('notifications'); -const { executeHttpRequest } = require("@sap-cloud-sdk/http-client"); const { getDestination } = require("@sap-cloud-sdk/connectivity"); const PRIORITIES = ["LOW", "NEUTRAL", "MEDIUM", "HIGH"]; @@ -23,12 +22,12 @@ const messages = { }; function validateNotificationTypes(notificationTypes) { - notificationTypes.forEach((notificationType) => { + for(notificationType of notificationTypes){ if (!("NotificationTypeKey" in notificationType)) { LOG._warn && LOG.warn(messages.INVALID_NOTIFICATION_TYPES); return false; } - }); + } return true; } @@ -128,22 +127,6 @@ function getNotificationTypesKeyWithPrefix(notificationTypeKey) { return `${prefix}/${notificationTypeKey}`; } -async function executeRequest(httpMethod, targetUrl, payload, notificationDestination, csrfHeaders) { - let response = {}; - try { - response = await executeHttpRequest(notificationDestination, { - url: targetUrl, - method: httpMethod, - data: payload, - headers: csrfHeaders, - }); - } catch (e) { - console.log(e); - response = e.response.data; - } - return response; -} - function buildDefaultNotification( recipients, priority = "NEUTRAL", @@ -255,6 +238,5 @@ module.exports = { getNotificationDestination, getPrefix, getNotificationTypesKeyWithPrefix, - executeRequest, buildNotification }; diff --git a/srv/notifyToRest.js b/srv/notifyToRest.js index 8135ecf..efd93d0 100644 --- a/srv/notifyToRest.js +++ b/srv/notifyToRest.js @@ -1,29 +1,20 @@ -const NotificationService = require('./service'); +const NotificationService = require("./service"); const { buildNotification } = require("../lib/utils"); -const { postNotification } = require('../lib/notifications'); +const { postNotification } = require("../lib/notifications"); module.exports = class NotifyToRest extends NotificationService { async init() { - // call NotificationService's init await super.init(); - this.on('postNotificationEvent', async function(req) { - const { data } = req; - - try { - await postNotification(data); - } catch (err) { - throw err; - } - }); + this.on("postNotificationEvent", async (req) => await postNotification(req.data)); } async notify(notificationData) { const notification = buildNotification(notificationData); if (notification) { - await this.emit({ event: 'postNotificationEvent', data: notification }); + await this.emit({ event: "postNotificationEvent", data: notification }); } } -} +}; diff --git a/test/lib/util.test.js b/test/lib/util.test.js deleted file mode 100644 index 1977c27..0000000 --- a/test/lib/util.test.js +++ /dev/null @@ -1,562 +0,0 @@ -const { buildNotification } = require("./../../lib/utils"); - - -const expectedDefaultNotificationWithoutDescription = { - NotificationTypeKey: "Default", - NotificationTypeVersion: "1", - Priority: "NEUTRAL", - Properties: [ - { - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - }, - { - Key:"description", - IsSensitive:false, - Language :"en", - Value :"", - Type :"String" - } - ], - Recipients: [ - { - RecipientId: "test.mail@mail.com" - } - ] - }; - -const expectedDefaultNotificationWithDescription = { - NotificationTypeKey: "Default", - NotificationTypeVersion: "1", - Priority: "NEUTRAL", - Properties: [ - { - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - }, - { - Key:"description", - IsSensitive:false, - Language :"en", - Value :"Some Test Description", - Type :"String" - } - ], - Recipients: [ - { - RecipientId: "test.mail@mail.com" - } - ] -}; - -describe("Test buildNotification functionality", () => { - test("When recipients, priority, title are passed to buildNotification", () => { - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - title: "Some Test Title", - priority: "NEUTRAL" - } - )) - .toMatchObject(expectedDefaultNotificationWithoutDescription); - }) - - test("When recipients, priority, title, description are passed to buildNotification", () => { - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - title: "Some Test Title", - priority: "NEUTRAL", - description: "Some Test Description" - } - )) - .toMatchObject(expectedDefaultNotificationWithDescription); - }) - - test("When recipients, title are passed to buildNotification", () => { - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - title: "Some Test Title" - } - )) - .toMatchObject(expectedDefaultNotificationWithoutDescription); - }) - - test("When recipients, title, description are passed to buildNotification", () => { - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - title: "Some Test Title", - description: "Some Test Description" - } - )) - .toMatchObject(expectedDefaultNotificationWithDescription); - }) - - test("When recipients, type, properties are passed to buildNotification", () => { - const expectedNotification = { - NotificationTypeKey: "notifications/TestNotificationType", - NotificationTypeVersion: "1", - Priority: "NEUTRAL", - Properties: [ - { - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - } - ], - Recipients: [ - { - RecipientId: "test.mail@mail.com" - } - ] - }; - - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - type: "TestNotificationType", - properties: [{ - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - }] - } - )) - .toMatchObject(expectedNotification); - }) - - test("When recipients, type, properties, navigation are passed to buildNotification", () => { - const expectedNotification = { - NotificationTypeKey: "notifications/TestNotificationType", - NotificationTypeVersion: "1", - NavigationTargetAction: "TestTargetAction", - NavigationTargetObject: "TestTargetObject", - Priority: "NEUTRAL", - Properties: [ - { - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - } - ], - Recipients: [ - { - RecipientId: "test.mail@mail.com" - } - ] - }; - - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - type: "TestNotificationType", - properties: [{ - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - }], - navigation: { - NavigationTargetAction: "TestTargetAction", - NavigationTargetObject: "TestTargetObject" - } - } - )) - .toMatchObject(expectedNotification); - }) - - test("When recipients, type, properties, navigation, priority are passed to buildNotification", () => { - const expectedNotification = { - NotificationTypeKey: "notifications/TestNotificationType", - NotificationTypeVersion: "1", - NavigationTargetAction: "TestTargetAction", - NavigationTargetObject: "TestTargetObject", - Priority: "HIGH", - Properties: [ - { - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - } - ], - Recipients: [ - { - RecipientId: "test.mail@mail.com" - } - ] - }; - - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - type: "TestNotificationType", - properties: [{ - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - }], - navigation: { - NavigationTargetAction: "TestTargetAction", - NavigationTargetObject: "TestTargetObject" - }, - priority: "HIGH" - } - )) - .toMatchObject(expectedNotification); - }) - - test("When recipients, type, properties, navigation, priority, payload are passed to buildNotification", () => { - const expectedNotification = { - Id: "01234567-89ab-cdef-0123-456789abcdef", - OriginId: "01234567-89ab-cdef-0123-456789abcdef", - NotificationTypeId: "01234567-89ab-cdef-0123-456789abcdef", - NotificationTypeKey: "notifications/TestNotificationType", - NotificationTypeVersion: "1", - NavigationTargetAction: "TestTargetAction", - NavigationTargetObject: "TestTargetObject", - Priority: "HIGH", - ProviderId: "SAMPLEPROVIDER", - ActorId: "BACKENDACTORID", - ActorDisplayText: "ActorName", - ActorImageURL: "https://some-url", - NotificationTypeTimestamp: "2022-03-15T09:58:42.807Z", - Properties: [ - { - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - } - ], - Recipients: [ - { - RecipientId: "test.mail@mail.com" - } - ], - TargetParameters: [ - { - "Key": "string", - "Value": "string" - } - ] - }; - - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - type: "TestNotificationType", - properties: [{ - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - }], - navigation: { - NavigationTargetAction: "TestTargetAction", - NavigationTargetObject: "TestTargetObject" - }, - priority: "HIGH", - payload: { - Id: "01234567-89ab-cdef-0123-456789abcdef", - OriginId: "01234567-89ab-cdef-0123-456789abcdef", - NotificationTypeId: "01234567-89ab-cdef-0123-456789abcdef", - ProviderId: "SAMPLEPROVIDER", - ActorId: "BACKENDACTORID", - ActorDisplayText: "ActorName", - ActorImageURL: "https://some-url", - NotificationTypeTimestamp: "2022-03-15T09:58:42.807Z", - TargetParameters: [ - { - "Key": "string", - "Value": "string" - } - ] - } - } - )) - .toMatchObject(expectedNotification); - }) - - test("When recipients, type, properties, navigation, priority, but with not all the payload are passed to buildNotification", () => { - const expectedNotification = { - NotificationTypeId: "01234567-89ab-cdef-0123-456789abcdef", - NotificationTypeKey: "notifications/TestNotificationType", - NotificationTypeVersion: "1", - NavigationTargetAction: "TestTargetAction", - NavigationTargetObject: "TestTargetObject", - Priority: "HIGH", - ProviderId: "SAMPLEPROVIDER", - ActorId: "BACKENDACTORID", - ActorDisplayText: "ActorName", - ActorImageURL: "https://some-url", - NotificationTypeTimestamp: "2022-03-15T09:58:42.807Z", - Properties: [ - { - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - } - ], - Recipients: [ - { - RecipientId: "test.mail@mail.com" - } - ], - TargetParameters: [ - { - "Key": "string", - "Value": "string" - } - ] - }; - - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - type: "TestNotificationType", - properties: [{ - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - }], - navigation: { - NavigationTargetAction: "TestTargetAction", - NavigationTargetObject: "TestTargetObject" - }, - priority: "HIGH", - payload: { - NotificationTypeId: "01234567-89ab-cdef-0123-456789abcdef", - ProviderId: "SAMPLEPROVIDER", - ActorId: "BACKENDACTORID", - ActorDisplayText: "ActorName", - ActorImageURL: "https://some-url", - NotificationTypeTimestamp: "2022-03-15T09:58:42.807Z", - TargetParameters: [ - { - "Key": "string", - "Value": "string" - } - ] - } - } - )) - .toMatchObject(expectedNotification); - }) - - test("When whole notification object is passed to buildNotification", () => { - const expectedNotification = { - NotificationTypeKey: "notifications/TestNotificationType", - NotificationTypeVersion: "1", - Priority: "NEUTRAL", - Properties: [ - { - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - }, - { - Key:"description", - IsSensitive:false, - Language :"en", - Value :"Some Test Description", - Type :"String" - } - ], - Recipients: [ - { - RecipientId: "test.mail@mail.com" - } - ] - }; - - expect(buildNotification( - { - NotificationTypeKey: "TestNotificationType", - NotificationTypeVersion: "1", - Priority: "NEUTRAL", - Properties: [ - { - Key:"title", - IsSensitive:false, - Language :"en", - Value :"Some Test Title", - Type :"String" - }, - { - Key:"description", - IsSensitive:false, - Language :"en", - Value :"Some Test Description", - Type :"String" - } - ], - Recipients: [ - { - RecipientId: "test.mail@mail.com" - } - ] - } - )) - .toMatchObject(expectedNotification); - }) - - test("When empty object is passed to buildNotification", () => { - expect(buildNotification({} || undefined)).toBeFalsy(); - }) - - test("When not all mandatory parameters for default notification are passed to buildNotification", () => { - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - priority: "NEUTRAL" - } - || undefined)).toBeFalsy(); - }) - - test("When empty recipients array for default notification is passed to buildNotification", () => { - expect(buildNotification( - { - recipients: [], - title: "Some Test Title", - priority: "NEUTRAL" - } - || undefined)).toBeFalsy(); - }) - - test("When string is passed as recipients for default notification is passed to buildNotification", () => { - expect(buildNotification( - { - recipients: "invalid", - title: "Some Test Title", - priority: "NEUTRAL" - } - || undefined)).toBeFalsy(); - }) - - test("When invalid priority for default notification is passed to buildNotification", () => { - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - title: "Some Test Title", - priority: "INVALID" - } - || undefined)).toBeFalsy(); - }) - - test("When invalid description for default notification is passed to buildNotification", () => { - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - title: "Some Test Title", - priority: "NEUTRAL", - description: {invalid: "invalid"} - } - || undefined)).toBeFalsy(); - }) - - test("When invalid title for default notification is passed to buildNotification", () => { - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - title: {invalid: "invalid"}, - priority: "NEUTRAL" - } - || undefined)).toBeFalsy(); - }) - - test("When not all mandatory parameters for custom notification are passed to buildNotification", () => { - expect(buildNotification( - { - type: "TestNotificationType" - } - || undefined)).toBeFalsy(); - }) - - test("When empty array of recipients for custom notification is passed to buildNotification", () => { - expect(buildNotification( - { - recipients: [], - type: "TestNotificationType" - } - || undefined)).toBeFalsy(); - }) - - test("When invalid recipients for custom notification are passed to buildNotification", () => { - expect(buildNotification( - { - recipients: "invalid", - type: "TestNotificationType" - } - || undefined)).toBeFalsy(); - }) - - test("When invalid priority for custom notification is passed to buildNotification", () => { - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - type: "TestNotificationType", - priority: "invalid" - } - || undefined)).toBeFalsy(); - }) - - test("When invalid properties for custom notification is passed to buildNotification", () => { - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - type: "TestNotificationType", - priority: "NEUTRAL", - properties: "invalid" - } - || undefined)).toBeFalsy(); - }) - - test("When invalid navigation for custom notification is passed to buildNotification", () => { - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - type: "TestNotificationType", - priority: "NEUTRAL", - navigation: "invalid" - } - || undefined)).toBeFalsy(); - }) - - test("When invalid payload for custom notification is passed to buildNotification", () => { - expect(buildNotification( - { - recipients: ["test.mail@mail.com"], - type: "TestNotificationType", - priority: "NEUTRAL", - payload: "invalid" - } - || undefined)).toBeFalsy(); - }) -}) \ No newline at end of file diff --git a/test/lib/utils.test.js b/test/lib/utils.test.js new file mode 100644 index 0000000..6ac4261 --- /dev/null +++ b/test/lib/utils.test.js @@ -0,0 +1,615 @@ +const { buildNotification, validateNotificationTypes, doesKeyExist, readFile, getNotificationDestination } = require("../../lib/utils"); +const { existsSync, readFileSync } = require("fs"); +const { getDestination } = require("@sap-cloud-sdk/connectivity"); + +jest.mock("fs"); +jest.mock("@sap-cloud-sdk/connectivity"); + +const expectedDefaultNotificationWithoutDescription = { + NotificationTypeKey: "Default", + NotificationTypeVersion: "1", + Priority: "NEUTRAL", + Properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + }, + { + Key: "description", + IsSensitive: false, + Language: "en", + Value: "", + Type: "String" + } + ], + Recipients: [ + { + RecipientId: "test.mail@mail.com" + } + ] +}; + +const expectedDefaultNotificationWithDescription = { + NotificationTypeKey: "Default", + NotificationTypeVersion: "1", + Priority: "NEUTRAL", + Properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + }, + { + Key: "description", + IsSensitive: false, + Language: "en", + Value: "Some Test Description", + Type: "String" + } + ], + Recipients: [ + { + RecipientId: "test.mail@mail.com" + } + ] +}; + +describe("Test utils", () => { + test("When recipients, priority, title are passed to buildNotification", () => { + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + title: "Some Test Title", + priority: "NEUTRAL" + }) + ).toMatchObject(expectedDefaultNotificationWithoutDescription); + }); + + test("When recipients, priority, title, description are passed to buildNotification", () => { + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + title: "Some Test Title", + priority: "NEUTRAL", + description: "Some Test Description" + }) + ).toMatchObject(expectedDefaultNotificationWithDescription); + }); + + test("When recipients, title are passed to buildNotification", () => { + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + title: "Some Test Title" + }) + ).toMatchObject(expectedDefaultNotificationWithoutDescription); + }); + + test("When recipients, title, description are passed to buildNotification", () => { + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + title: "Some Test Title", + description: "Some Test Description" + }) + ).toMatchObject(expectedDefaultNotificationWithDescription); + }); + + test("When recipients, type, properties are passed to buildNotification", () => { + const expectedNotification = { + NotificationTypeKey: "notifications/TestNotificationType", + NotificationTypeVersion: "1", + Priority: "NEUTRAL", + Properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + } + ], + Recipients: [ + { + RecipientId: "test.mail@mail.com" + } + ] + }; + + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + type: "TestNotificationType", + properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + } + ] + }) + ).toMatchObject(expectedNotification); + }); + + test("When recipients, type, properties, navigation are passed to buildNotification", () => { + const expectedNotification = { + NotificationTypeKey: "notifications/TestNotificationType", + NotificationTypeVersion: "1", + NavigationTargetAction: "TestTargetAction", + NavigationTargetObject: "TestTargetObject", + Priority: "NEUTRAL", + Properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + } + ], + Recipients: [ + { + RecipientId: "test.mail@mail.com" + } + ] + }; + + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + type: "TestNotificationType", + properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + } + ], + navigation: { + NavigationTargetAction: "TestTargetAction", + NavigationTargetObject: "TestTargetObject" + } + }) + ).toMatchObject(expectedNotification); + }); + + test("When recipients, type, properties, navigation, priority are passed to buildNotification", () => { + const expectedNotification = { + NotificationTypeKey: "notifications/TestNotificationType", + NotificationTypeVersion: "1", + NavigationTargetAction: "TestTargetAction", + NavigationTargetObject: "TestTargetObject", + Priority: "HIGH", + Properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + } + ], + Recipients: [ + { + RecipientId: "test.mail@mail.com" + } + ] + }; + + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + type: "TestNotificationType", + properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + } + ], + navigation: { + NavigationTargetAction: "TestTargetAction", + NavigationTargetObject: "TestTargetObject" + }, + priority: "HIGH" + }) + ).toMatchObject(expectedNotification); + }); + + test("When recipients, type, properties, navigation, priority, payload are passed to buildNotification", () => { + const expectedNotification = { + Id: "01234567-89ab-cdef-0123-456789abcdef", + OriginId: "01234567-89ab-cdef-0123-456789abcdef", + NotificationTypeId: "01234567-89ab-cdef-0123-456789abcdef", + NotificationTypeKey: "notifications/TestNotificationType", + NotificationTypeVersion: "1", + NavigationTargetAction: "TestTargetAction", + NavigationTargetObject: "TestTargetObject", + Priority: "HIGH", + ProviderId: "SAMPLEPROVIDER", + ActorId: "BACKENDACTORID", + ActorDisplayText: "ActorName", + ActorImageURL: "https://some-url", + NotificationTypeTimestamp: "2022-03-15T09:58:42.807Z", + Properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + } + ], + Recipients: [ + { + RecipientId: "test.mail@mail.com" + } + ], + TargetParameters: [ + { + Key: "string", + Value: "string" + } + ] + }; + + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + type: "TestNotificationType", + properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + } + ], + navigation: { + NavigationTargetAction: "TestTargetAction", + NavigationTargetObject: "TestTargetObject" + }, + priority: "HIGH", + payload: { + Id: "01234567-89ab-cdef-0123-456789abcdef", + OriginId: "01234567-89ab-cdef-0123-456789abcdef", + NotificationTypeId: "01234567-89ab-cdef-0123-456789abcdef", + ProviderId: "SAMPLEPROVIDER", + ActorId: "BACKENDACTORID", + ActorDisplayText: "ActorName", + ActorImageURL: "https://some-url", + NotificationTypeTimestamp: "2022-03-15T09:58:42.807Z", + TargetParameters: [ + { + Key: "string", + Value: "string" + } + ] + } + }) + ).toMatchObject(expectedNotification); + }); + + test("When recipients, type, properties, navigation, priority, but with not all the payload are passed to buildNotification", () => { + const expectedNotification = { + NotificationTypeId: "01234567-89ab-cdef-0123-456789abcdef", + NotificationTypeKey: "notifications/TestNotificationType", + NotificationTypeVersion: "1", + NavigationTargetAction: "TestTargetAction", + NavigationTargetObject: "TestTargetObject", + Priority: "HIGH", + ProviderId: "SAMPLEPROVIDER", + ActorId: "BACKENDACTORID", + ActorDisplayText: "ActorName", + ActorImageURL: "https://some-url", + NotificationTypeTimestamp: "2022-03-15T09:58:42.807Z", + Properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + } + ], + Recipients: [ + { + RecipientId: "test.mail@mail.com" + } + ], + TargetParameters: [ + { + Key: "string", + Value: "string" + } + ] + }; + + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + type: "TestNotificationType", + properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + } + ], + navigation: { + NavigationTargetAction: "TestTargetAction", + NavigationTargetObject: "TestTargetObject" + }, + priority: "HIGH", + payload: { + NotificationTypeId: "01234567-89ab-cdef-0123-456789abcdef", + ProviderId: "SAMPLEPROVIDER", + ActorId: "BACKENDACTORID", + ActorDisplayText: "ActorName", + ActorImageURL: "https://some-url", + NotificationTypeTimestamp: "2022-03-15T09:58:42.807Z", + TargetParameters: [ + { + Key: "string", + Value: "string" + } + ] + } + }) + ).toMatchObject(expectedNotification); + }); + + test("When whole notification object is passed to buildNotification", () => { + const expectedNotification = { + NotificationTypeKey: "notifications/TestNotificationType", + NotificationTypeVersion: "1", + Priority: "NEUTRAL", + Properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + }, + { + Key: "description", + IsSensitive: false, + Language: "en", + Value: "Some Test Description", + Type: "String" + } + ], + Recipients: [ + { + RecipientId: "test.mail@mail.com" + } + ] + }; + + expect( + buildNotification({ + NotificationTypeKey: "TestNotificationType", + NotificationTypeVersion: "1", + Priority: "NEUTRAL", + Properties: [ + { + Key: "title", + IsSensitive: false, + Language: "en", + Value: "Some Test Title", + Type: "String" + }, + { + Key: "description", + IsSensitive: false, + Language: "en", + Value: "Some Test Description", + Type: "String" + } + ], + Recipients: [ + { + RecipientId: "test.mail@mail.com" + } + ] + }) + ).toMatchObject(expectedNotification); + }); + + test("When empty object is passed to buildNotification", () => { + expect(buildNotification({})).toBeFalsy(); + }); + + test("When not all mandatory parameters for default notification are passed to buildNotification", () => { + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + priority: "NEUTRAL" + }) + ).toBeFalsy(); + }); + + test("When empty recipients array for default notification is passed to buildNotification", () => { + expect( + buildNotification({ + recipients: [], + title: "Some Test Title", + priority: "NEUTRAL" + }) + ).toBeFalsy(); + }); + + test("When string is passed as recipients for default notification is passed to buildNotification", () => { + expect( + buildNotification({ + recipients: "invalid", + title: "Some Test Title", + priority: "NEUTRAL" + }) + ).toBeFalsy(); + }); + + test("When invalid priority for default notification is passed to buildNotification", () => { + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + title: "Some Test Title", + priority: "INVALID" + }) + ).toBeFalsy(); + }); + + test("When invalid description for default notification is passed to buildNotification", () => { + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + title: "Some Test Title", + priority: "NEUTRAL", + description: { invalid: "invalid" } + }) + ).toBeFalsy(); + }); + + test("When invalid title for default notification is passed to buildNotification", () => { + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + title: { invalid: "invalid" }, + priority: "NEUTRAL" + }) + ).toBeFalsy(); + }); + + test("When not all mandatory parameters for custom notification are passed to buildNotification", () => { + expect( + buildNotification({ + type: "TestNotificationType" + }) + ).toBeFalsy(); + }); + + test("When empty array of recipients for custom notification is passed to buildNotification", () => { + expect( + buildNotification({ + recipients: [], + type: "TestNotificationType" + }) + ).toBeFalsy(); + }); + + test("When invalid recipients for custom notification are passed to buildNotification", () => { + expect( + buildNotification({ + recipients: "invalid", + type: "TestNotificationType" + }) + ).toBeFalsy(); + }); + + test("When invalid priority for custom notification is passed to buildNotification", () => { + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + type: "TestNotificationType", + priority: "invalid" + }) + ).toBeFalsy(); + }); + + test("When invalid properties for custom notification is passed to buildNotification", () => { + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + type: "TestNotificationType", + priority: "NEUTRAL", + properties: "invalid" + }) + ).toBeFalsy(); + }); + + test("When invalid navigation for custom notification is passed to buildNotification", () => { + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + type: "TestNotificationType", + priority: "NEUTRAL", + navigation: "invalid" + }) + ).toBeFalsy(); + }); + + test("When invalid payload for custom notification is passed to buildNotification", () => { + expect( + buildNotification({ + recipients: ["test.mail@mail.com"], + type: "TestNotificationType", + priority: "NEUTRAL", + payload: "invalid" + }) + ).toBeFalsy(); + }); + + test("When no notification data for custom notification is passed to buildNotification", () => { + expect(buildNotification(undefined)).toBeFalsy(); + expect(buildNotification(null)).toBeFalsy(); + }); + + test("Given invalid NTypes | When validateNotificationTypes is called | Then false is returned", () => { + expect(validateNotificationTypes([{ NotificationTypeKey: "Test" }, { blabla: "Test2" }])).toEqual(false); + }); + + test("Given valid NTypes | When validateNotificationTypes is called | Then true is returned", () => { + expect(validateNotificationTypes([])).toEqual(true); + expect(validateNotificationTypes([{ NotificationTypeKey: "Test" }, { NotificationTypeKey: "Test2" }])).toEqual(true); + }); + + test("Given invalid inputs | When doesKeyExist is called | Then false is returned", () => { + expect(doesKeyExist({ test: "test1" }, {})).toEqual(false); + expect(doesKeyExist([{ test: "test1" }], "test")).toEqual(false); + }); + + test("Given that key does not exist | When doesKeyExist is called | Then false is returned", () => { + expect(doesKeyExist({ test: "test1" }, "doesnotexist")).toEqual(false); + expect(doesKeyExist({ test: "test1" }, "test1")).toEqual(false); + }); + + test("Given that key does exist | When doesKeyExist is called | Then true is returned", () => { + expect(doesKeyExist({ test: "test1" }, "test")).toEqual(true); + }); + + test("Given that file does not exist | When readFile is called | Then empty array is returned", () => { + existsSync.mockReturnValue(false); + expect(readFile("test.json")).toMatchObject([]); + }); + + test("Given that file does exist | When readFile is called | Then correct array is returned", () => { + existsSync.mockReturnValue(true); + readFileSync.mockReturnValue('[{ "test": "test" }]'); + expect(readFile("test.json")).toMatchObject([{ test: "test" }]); + }); + + test("Given that destination does exist | When getNotificationDestination is called | Then correct destination is returned", async () => { + getDestination.mockReturnValue({ "mock-destination": "mock-destination" }); + expect(await getNotificationDestination()).toMatchObject({ "mock-destination": "mock-destination" }); + }); + + test("Given that destination does not exist | When getNotificationDestination is called | Then error is thrown", async () => { + getDestination.mockReturnValue(undefined); + await expect(() => getNotificationDestination()).rejects.toThrow("Failed to get destination: SAP_Notifications"); + }); +}); diff --git a/test/srv/notifyToRest.test.js b/test/srv/notifyToRest.test.js index 80eed23..b2a0883 100644 --- a/test/srv/notifyToRest.test.js +++ b/test/srv/notifyToRest.test.js @@ -1,20 +1,22 @@ -const NotifyToRest = require('../../srv/notifyToRest'); -const { messages } = require('../../lib/utils') +const NotifyToRest = require("../../srv/notifyToRest"); +const { messages, buildNotification } = require("../../lib/utils"); +const { postNotification } = require("../../lib/notifications"); -describe('notify to rest', () => { +jest.mock("./../../lib/notifications"); - it('when no object is passed', async () => { +describe("notify to rest", () => { + it("when no object is passed", async () => { const notifyToRest = new NotifyToRest(); - const warnSpy = jest.spyOn(global.console, 'warn'); + const warnSpy = jest.spyOn(global.console, "warn"); notifyToRest.notify(); expect(warnSpy).toHaveBeenCalled(); expect(warnSpy).toHaveBeenCalledWith("[notifications] -", messages.NO_OBJECT_FOR_NOTIFY); warnSpy.mockClear(); }); - it('when empty object is passed', async () => { + it("when empty object is passed", async () => { const notifyToRest = new NotifyToRest(); - const warnSpy = jest.spyOn(global.console, 'warn'); + const warnSpy = jest.spyOn(global.console, "warn"); notifyToRest.notify({}); expect(warnSpy).toHaveBeenCalled(); expect(warnSpy).toHaveBeenCalledWith("[notifications] -", messages.EMPTY_OBJECT_FOR_NOTIFY); @@ -23,37 +25,48 @@ describe('notify to rest', () => { it(`when recipients or title isn't passed in default notification`, async () => { const notifyToRest = new NotifyToRest(); - const warnSpy = jest.spyOn(global.console, 'warn'); + const warnSpy = jest.spyOn(global.console, "warn"); notifyToRest.notify({ dummy: true }); expect(warnSpy).toHaveBeenCalled(); - expect(warnSpy).toHaveBeenCalledWith("[notifications] -", messages.MANDATORY_PARAMETER_NOT_PASSED_FOR_DEFAULT_NOTIFICATION) + expect(warnSpy).toHaveBeenCalledWith("[notifications] -", messages.MANDATORY_PARAMETER_NOT_PASSED_FOR_DEFAULT_NOTIFICATION); warnSpy.mockClear(); }); it(`when title isn't a string in default notification`, async () => { const notifyToRest = new NotifyToRest(); - const warnSpy = jest.spyOn(global.console, 'warn'); - notifyToRest.notify({ title: 1, recipients: [ "abc@abc.com" ] }); + const warnSpy = jest.spyOn(global.console, "warn"); + notifyToRest.notify({ title: 1, recipients: ["abc@abc.com"] }); expect(warnSpy).toHaveBeenCalled(); - expect(warnSpy).toHaveBeenCalledWith("[notifications] -", messages.TITLE_IS_NOT_STRING) + expect(warnSpy).toHaveBeenCalledWith("[notifications] -", messages.TITLE_IS_NOT_STRING); warnSpy.mockClear(); }); it(`when priority isn't valid in default notification`, async () => { const notifyToRest = new NotifyToRest(); - const warnSpy = jest.spyOn(global.console, 'warn'); - notifyToRest.notify({ title: "abc", recipients: [ "abc@abc.com" ], priority: "abc" }); + const warnSpy = jest.spyOn(global.console, "warn"); + notifyToRest.notify({ title: "abc", recipients: ["abc@abc.com"], priority: "abc" }); expect(warnSpy).toHaveBeenCalled(); - expect(warnSpy).toHaveBeenCalledWith("[notifications] -", "Invalid priority abc. Allowed priorities are LOW, NEUTRAL, MEDIUM, HIGH") + expect(warnSpy).toHaveBeenCalledWith("[notifications] -", "Invalid priority abc. Allowed priorities are LOW, NEUTRAL, MEDIUM, HIGH"); warnSpy.mockClear(); }); it(`when description isn't valid in default notification`, async () => { const notifyToRest = new NotifyToRest(); - const warnSpy = jest.spyOn(global.console, 'warn'); - notifyToRest.notify({ title: "abc", recipients: [ "abc@abc.com" ], priority: "low", description: true }); + const warnSpy = jest.spyOn(global.console, "warn"); + notifyToRest.notify({ title: "abc", recipients: ["abc@abc.com"], priority: "low", description: true }); expect(warnSpy).toHaveBeenCalled(); - expect(warnSpy).toHaveBeenCalledWith("[notifications] -", messages.DESCRIPTION_IS_NOT_STRING) + expect(warnSpy).toHaveBeenCalledWith("[notifications] -", messages.DESCRIPTION_IS_NOT_STRING); warnSpy.mockClear(); }); -}) \ No newline at end of file + + it(`When correct body is send | Then notification is posted`, async () => { + postNotification.mockImplementation(() => undefined); + const body = { title: "abc", recipients: ["abc@abc.com"], priority: "low" }; + + const notifyToRest = new NotifyToRest(); + await notifyToRest.init(); + await notifyToRest.notify(body); + expect(postNotification).toHaveBeenCalled(); + expect(postNotification.mock.calls[0][0]).toMatchObject(buildNotification(body)); + }); +});