mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-06-15 17:06:48 +02:00
aliyun-sms tests
This commit is contained in:
parent
2dac1e95a2
commit
9a2d47679f
3 changed files with 236 additions and 19 deletions
|
@ -8,7 +8,6 @@ class AliyunSMS extends NotificationProvider {
|
||||||
name = "AliyunSMS";
|
name = "AliyunSMS";
|
||||||
|
|
||||||
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
||||||
let okMsg = "Sent Successfully.";
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (heartbeatJSON != null) {
|
if (heartbeatJSON != null) {
|
||||||
|
@ -18,8 +17,9 @@ class AliyunSMS extends NotificationProvider {
|
||||||
status: this.statusToString(heartbeatJSON["status"]),
|
status: this.statusToString(heartbeatJSON["status"]),
|
||||||
msg: heartbeatJSON["msg"],
|
msg: heartbeatJSON["msg"],
|
||||||
});
|
});
|
||||||
if (this.sendSms(notification, msgBody)) {
|
if (await this.sendSms(notification, msgBody)) {
|
||||||
return okMsg;
|
return this.sendSuccess;
|
||||||
|
//TODO account for cases where sendSMS returns false.
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let msgBody = JSON.stringify({
|
let msgBody = JSON.stringify({
|
||||||
|
@ -28,8 +28,8 @@ class AliyunSMS extends NotificationProvider {
|
||||||
status: "",
|
status: "",
|
||||||
msg: msg,
|
msg: msg,
|
||||||
});
|
});
|
||||||
if (this.sendSms(notification, msgBody)) {
|
if (await this.sendSms(notification, msgBody)) {
|
||||||
return okMsg;
|
return this.sendSuccess;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -48,7 +48,7 @@ class AliyunSMS extends NotificationProvider {
|
||||||
SignatureMethod: "HMAC-SHA1",
|
SignatureMethod: "HMAC-SHA1",
|
||||||
SignatureVersion: "1.0",
|
SignatureVersion: "1.0",
|
||||||
SignatureNonce: Math.random().toString(),
|
SignatureNonce: Math.random().toString(),
|
||||||
Timestamp: new Date().toISOString(),
|
Timestamp: new Date(Date.now()).toISOString(),
|
||||||
Action: "SendSms",
|
Action: "SendSms",
|
||||||
Version: "2017-05-25",
|
Version: "2017-05-25",
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
// jest.mock("nodemailer", () => ({
|
jest.mock("axios");
|
||||||
// createTransport: jest.fn(),
|
|
||||||
// }));
|
|
||||||
|
|
||||||
// const mockNodeMailer = require("nodemailer");
|
const axios = require("axios");
|
||||||
|
|
||||||
|
const { UP } = require("../../src/util");
|
||||||
|
const NotificationSend = require("../notification");
|
||||||
|
|
||||||
const AliyunSMS = require("./aliyun-sms");
|
const AliyunSMS = require("./aliyun-sms");
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// mockNodeMailer.createTransport.mockReset();
|
axios.mockReset();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("notification default information", () => {
|
describe("notification default information", () => {
|
||||||
|
@ -16,3 +17,227 @@ describe("notification default information", () => {
|
||||||
expect(notification.name).toBe("AliyunSMS");
|
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.");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
|
@ -1,16 +1,8 @@
|
||||||
// jest.mock("crypto", () => ({
|
|
||||||
// createTransport: jest.fn(),
|
|
||||||
// createHmac: jest.fn(),
|
|
||||||
// digest: jest.fn(),
|
|
||||||
// }));
|
|
||||||
|
|
||||||
jest.mock("axios");
|
jest.mock("axios");
|
||||||
|
|
||||||
const { UP } = require("../../src/util");
|
const { UP } = require("../../src/util");
|
||||||
const NotificationSend = require("../notification");
|
const NotificationSend = require("../notification");
|
||||||
|
|
||||||
// const mockNodeMailer = require("nodemailer");
|
|
||||||
|
|
||||||
const DingDing = require("./dingding");
|
const DingDing = require("./dingding");
|
||||||
|
|
||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
|
|
Loading…
Add table
Reference in a new issue