From 9a2d47679fdadd072aeba1fe72c3ee55b01811ef Mon Sep 17 00:00:00 2001 From: Jeffrey Koehler Date: Fri, 22 Oct 2021 01:28:31 -0500 Subject: [PATCH] aliyun-sms tests --- server/notification-providers/aliyun-sms.js | 12 +- .../notification-providers/aliyun-sms.spec.js | 235 +++++++++++++++++- .../notification-providers/dingding.spec.js | 8 - 3 files changed, 236 insertions(+), 19 deletions(-) diff --git a/server/notification-providers/aliyun-sms.js b/server/notification-providers/aliyun-sms.js index 6a2063200..952efecd8 100644 --- a/server/notification-providers/aliyun-sms.js +++ b/server/notification-providers/aliyun-sms.js @@ -8,7 +8,6 @@ class AliyunSMS extends NotificationProvider { name = "AliyunSMS"; async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { - let okMsg = "Sent Successfully."; try { if (heartbeatJSON != null) { @@ -18,8 +17,9 @@ class AliyunSMS extends NotificationProvider { status: this.statusToString(heartbeatJSON["status"]), msg: heartbeatJSON["msg"], }); - if (this.sendSms(notification, msgBody)) { - return okMsg; + if (await this.sendSms(notification, msgBody)) { + return this.sendSuccess; + //TODO account for cases where sendSMS returns false. } } else { let msgBody = JSON.stringify({ @@ -28,8 +28,8 @@ class AliyunSMS extends NotificationProvider { status: "", msg: msg, }); - if (this.sendSms(notification, msgBody)) { - return okMsg; + if (await this.sendSms(notification, msgBody)) { + return this.sendSuccess; } } } catch (error) { @@ -48,7 +48,7 @@ class AliyunSMS extends NotificationProvider { SignatureMethod: "HMAC-SHA1", SignatureVersion: "1.0", SignatureNonce: Math.random().toString(), - Timestamp: new Date().toISOString(), + Timestamp: new Date(Date.now()).toISOString(), Action: "SendSms", Version: "2017-05-25", }; diff --git a/server/notification-providers/aliyun-sms.spec.js b/server/notification-providers/aliyun-sms.spec.js index c8eb9488b..c58a765ca 100644 --- a/server/notification-providers/aliyun-sms.spec.js +++ b/server/notification-providers/aliyun-sms.spec.js @@ -1,13 +1,14 @@ -// jest.mock("nodemailer", () => ({ -// createTransport: jest.fn(), -// })); +jest.mock("axios"); -// const mockNodeMailer = require("nodemailer"); +const axios = require("axios"); + +const { UP } = require("../../src/util"); +const NotificationSend = require("../notification"); const AliyunSMS = require("./aliyun-sms"); beforeEach(() => { - // mockNodeMailer.createTransport.mockReset(); + axios.mockReset(); }); describe("notification default information", () => { @@ -16,3 +17,227 @@ describe("notification default information", () => { expect(notification.name).toBe("AliyunSMS"); }); }); + +describe("notification to act properly on send", () => { + it("should call axios with the proper default data", async () => { + + jest.spyOn(global.Date, "now") + .mockImplementation(() => + new Date("2019-05-14T11:01:58.135Z") + ); + + jest.spyOn(global.Math, "random") + .mockImplementation(() => + 0.0000111010100 + ); + + let response = { + data: { + Message: "OK" + } + }; + axios.mockResolvedValueOnce(response); + + let notif = new AliyunSMS(); + let notificationConf = { + appriseURL: "appriseURL", + secretKey: "abc", + webHookUrl: "https://example.com/webhook", + }; + let msg = "PassedInMessage"; + let monitorConf = { + type: "http", + url: "https://www.google.com", + name: "testing", + }; + let heartbeatConf = { + status: UP, + msg: "some message", + time: "example time", + }; + let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf); + + expect(axios).toHaveBeenCalledWith({ + data: "TemplateParam=%7B%22name%22%3A%22testing%22%2C%22time%22%3A%22example%20time%22%2C%22status%22%3A%22UP%22%2C%22msg%22%3A%22some%20message%22%7D&Format=JSON&SignatureMethod=HMAC-SHA1&SignatureVersion=1.0&SignatureNonce=0.00001110101&Timestamp=2019-05-14T11%3A01%3A58.135Z&Action=SendSms&Version=2017-05-25&Signature=73QTXvIaPHJIEo%2BCV1bzaZ5rzh4%3D", + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + method: "POST", + url: "http://dysmsapi.aliyuncs.com/", + }); + expect(res).toBe("Sent Successfully."); + }); + + it("should call axios with the proper data when monitor nil", async () => { + + jest.spyOn(global.Date, "now") + .mockImplementation(() => + new Date("2019-05-14T11:01:58.135Z") + ); + + jest.spyOn(global.Math, "random") + .mockImplementation(() => + 0.0000111010100 + ); + + let response = { + data: { + Message: "OK" + } + }; + axios.mockResolvedValueOnce(response); + + let notif = new AliyunSMS(); + let notificationConf = { + appriseURL: "appriseURL", + secretKey: "abc", + webHookUrl: "https://example.com/webhook", + }; + let msg = "PassedInMessage"; + let res = await notif.send(notificationConf, msg, null, null); + + expect(axios).toHaveBeenCalledWith({ + data: "TemplateParam=%7B%22name%22%3A%22%22%2C%22time%22%3A%22%22%2C%22status%22%3A%22%22%2C%22msg%22%3A%22PassedInMessage%22%7D&Format=JSON&SignatureMethod=HMAC-SHA1&SignatureVersion=1.0&SignatureNonce=0.00001110101&Timestamp=2019-05-14T11%3A01%3A58.135Z&Action=SendSms&Version=2017-05-25&Signature=bXj4C8u60n6Xfiqf3VhtyqtW6Fk%3D", + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + method: "POST", + url: "http://dysmsapi.aliyuncs.com/", + }); + expect(res).toBe("Sent Successfully."); + }); + + it("should call axios with the proper data when monitor nil", async () => { + + jest.spyOn(global.Date, "now") + .mockImplementation(() => + new Date("2019-05-14T11:01:58.135Z") + ); + + jest.spyOn(global.Math, "random") + .mockImplementation(() => + 0.0000111010100 + ); + + let response = { + data: { + Message: "OK" + } + }; + axios.mockResolvedValueOnce(response); + + let notif = new AliyunSMS(); + let notificationConf = { + appriseURL: "appriseURL", + secretKey: "abc", + webHookUrl: "https://example.com/webhook", + }; + let msg = "PassedInMessage"; + let res = await notif.send(notificationConf, msg, null, null); + + expect(axios).toHaveBeenCalledWith({ + data: "TemplateParam=%7B%22name%22%3A%22%22%2C%22time%22%3A%22%22%2C%22status%22%3A%22%22%2C%22msg%22%3A%22PassedInMessage%22%7D&Format=JSON&SignatureMethod=HMAC-SHA1&SignatureVersion=1.0&SignatureNonce=0.00001110101&Timestamp=2019-05-14T11%3A01%3A58.135Z&Action=SendSms&Version=2017-05-25&Signature=bXj4C8u60n6Xfiqf3VhtyqtW6Fk%3D", + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + method: "POST", + url: "http://dysmsapi.aliyuncs.com/", + }); + expect(res).toBe("Sent Successfully."); + }); + +}); + +describe("notification to act properly on error", () => { + it("should respond with an axios error on error", async () => { + jest.spyOn(global.Date, "now") + .mockImplementation(() => + new Date("2019-05-14T11:01:58.135Z") + ); + + jest.spyOn(global.Math, "random") + .mockImplementation(() => + 0.0000111010100 + ); + + axios.mockImplementation(() => { + throw new Error("Test Error"); + }); + + let notificationConf = { + appriseURL: "appriseURL", + secretKey: "abc", + webHookUrl: "https://example.com/webhook", + }; + let msg = "PassedInMessage"; + let notif = new AliyunSMS(); + + try { + await notif.send(notificationConf, msg, null, null); + expect("Error thrown").toBe(false); + } catch (e) { + //axios general error on catching another error is not the cleanest, but works. + expect(e.message).toBe("Error: Error: Test Error "); + } + + expect(axios).toHaveBeenCalledWith({ + data: "TemplateParam=%7B%22name%22%3A%22%22%2C%22time%22%3A%22%22%2C%22status%22%3A%22%22%2C%22msg%22%3A%22PassedInMessage%22%7D&Format=JSON&SignatureMethod=HMAC-SHA1&SignatureVersion=1.0&SignatureNonce=0.00001110101&Timestamp=2019-05-14T11%3A01%3A58.135Z&Action=SendSms&Version=2017-05-25&Signature=bXj4C8u60n6Xfiqf3VhtyqtW6Fk%3D", + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + method: "POST", + url: "http://dysmsapi.aliyuncs.com/", + }); + }); + +}); + +describe("notification to get proper data from Notification.send", () => { + it("should call sendMail with proper data", async () => { + jest.spyOn(global.Date, "now") + .mockImplementation(() => + new Date("2019-05-14T11:01:58.135Z") + ); + + jest.spyOn(global.Math, "random") + .mockImplementation(() => + 0.0000111010100 + ); + + let response = { + data: { + Message: "OK" + } + }; + axios.mockResolvedValueOnce(response); + let notificationConf = { + type: "AliyunSMS", + appriseURL: "appriseURL", + secretKey: "abc", + webHookUrl: "https://example.com/webhook", + }; + let monitorConf = { + type: "http", + url: "https://www.google.com", + name: "testing", + }; + let heartbeatConf = { + status: UP, + msg: "some message", + time: "example time", + }; + + NotificationSend.Notification.init(); + let res = await NotificationSend.Notification.send(notificationConf, "PassedInMessage", monitorConf, heartbeatConf); + expect(axios).toHaveBeenCalledWith({ + data: "TemplateParam=%7B%22name%22%3A%22testing%22%2C%22time%22%3A%22example%20time%22%2C%22status%22%3A%22UP%22%2C%22msg%22%3A%22some%20message%22%7D&Format=JSON&SignatureMethod=HMAC-SHA1&SignatureVersion=1.0&SignatureNonce=0.00001110101&Timestamp=2019-05-14T11%3A01%3A58.135Z&Action=SendSms&Version=2017-05-25&Signature=73QTXvIaPHJIEo%2BCV1bzaZ5rzh4%3D", + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + method: "POST", + url: "http://dysmsapi.aliyuncs.com/", + }); + expect(res).toBe("Sent Successfully."); + }); + +}); diff --git a/server/notification-providers/dingding.spec.js b/server/notification-providers/dingding.spec.js index 15275318f..686838592 100644 --- a/server/notification-providers/dingding.spec.js +++ b/server/notification-providers/dingding.spec.js @@ -1,16 +1,8 @@ -// jest.mock("crypto", () => ({ -// createTransport: jest.fn(), -// createHmac: jest.fn(), -// digest: jest.fn(), -// })); - jest.mock("axios"); const { UP } = require("../../src/util"); const NotificationSend = require("../notification"); -// const mockNodeMailer = require("nodemailer"); - const DingDing = require("./dingding"); const axios = require("axios");