Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification tests #760

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
bfff6d0
SMTP notification tests
deefdragon Oct 21, 2021
27ce95d
add check for what email data was passed to nodemailer
deefdragon Oct 21, 2021
1ff1fc6
add tests for SMTP errors
deefdragon Oct 21, 2021
6be97fb
Starting test file for all notifications.
deefdragon Oct 21, 2021
bc4f0f9
Test for calling notification from Notification.send
deefdragon Oct 21, 2021
833feaf
apprise tests
deefdragon Oct 22, 2021
965ce62
dingding tests and minor fix
deefdragon Oct 22, 2021
2dac1e9
discord tests
deefdragon Oct 22, 2021
9a2d476
aliyun-sms tests
deefdragon Oct 22, 2021
66f2847
Feishu tests
deefdragon Oct 23, 2021
266333e
gotify tests
deefdragon Oct 23, 2021
37f486d
Line Tests
deefdragon Oct 23, 2021
4f12e94
lunasea tests
deefdragon Oct 24, 2021
e0d211c
Matrix tests
deefdragon Oct 24, 2021
ba3fefa
mattermost tests
deefdragon Oct 24, 2021
6ce6d03
octopush tests
deefdragon Oct 24, 2021
d818ce8
promosms tests
deefdragon Oct 25, 2021
b229f36
pushbullet test
deefdragon Oct 25, 2021
a6f14f5
pushover tests
deefdragon Oct 25, 2021
60ad285
pushy tests
deefdragon Oct 26, 2021
8b0116b
rocket chat tests
deefdragon Oct 26, 2021
fb67dc7
signal tests
deefdragon Oct 26, 2021
96740bc
slack tests
deefdragon Oct 26, 2021
171069f
teams tests
deefdragon Oct 26, 2021
9ea0a9c
telegram tests
deefdragon Oct 26, 2021
c86e80d
webhook tests
deefdragon Oct 26, 2021
76eea7b
NotificationProvider tests
deefdragon Oct 26, 2021
782c943
fix #859 and update tests to match
deefdragon Nov 3, 2021
effbcc8
Merge remote-tracking branch 'louis/master' into notif-tests
deefdragon Nov 3, 2021
d7edafb
Fix tests to account for addition of tls.rejectUnauthorized in SMTP
deefdragon Nov 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/jest-backend.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
"rootDir": "..",
"testRegex": "./test/backend.spec.js",
"testRegex": ["./test/backend.spec.js", "./server/.*.spec.js"],
};

12 changes: 6 additions & 6 deletions server/notification-providers/aliyun-sms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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({
Expand All @@ -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) {
Expand All @@ -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",
};
Expand Down
243 changes: 243 additions & 0 deletions server/notification-providers/aliyun-sms.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
jest.mock("axios");

const axios = require("axios");

const { UP } = require("../../src/util");
const NotificationSend = require("../notification");

const AliyunSMS = require("./aliyun-sms");

beforeEach(() => {
axios.mockReset();
});

describe("notification default information", () => {
it("should have the correct name", () => {
let notification = new 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.");
});

});
8 changes: 4 additions & 4 deletions server/notification-providers/apprise.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const NotificationProvider = require("./notification-provider");
const child_process = require("child_process");
const childProcess = require("child_process");

class Apprise extends NotificationProvider {

name = "apprise";

async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let s = child_process.spawnSync("apprise", [ "-vv", "-b", msg, notification.appriseURL])
let s = childProcess.spawnSync("apprise", [ "-vv", "-b", msg, notification.appriseURL]);

let output = (s.stdout) ? s.stdout.toString() : "ERROR: maybe apprise not found";

if (output) {

if (! output.includes("ERROR")) {
return "Sent Successfully";
return this.sendSuccess;
}

throw new Error(output)
throw new Error(output);
} else {
return "No output from apprise";
}
Expand Down
Loading