mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-06-15 08:56:47 +02:00
Matrix tests
This commit is contained in:
parent
4f12e942a7
commit
e0d211cbb4
2 changed files with 159 additions and 7 deletions
|
@ -7,7 +7,6 @@ class Matrix extends NotificationProvider {
|
||||||
name = "matrix";
|
name = "matrix";
|
||||||
|
|
||||||
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
||||||
let okMsg = "Sent Successfully.";
|
|
||||||
|
|
||||||
const size = 20;
|
const size = 20;
|
||||||
const randomString = encodeURIComponent(
|
const randomString = encodeURIComponent(
|
||||||
|
@ -35,7 +34,7 @@ class Matrix extends NotificationProvider {
|
||||||
};
|
};
|
||||||
|
|
||||||
await axios.put(`${notification.homeserverUrl}/_matrix/client/r0/rooms/${roomId}/send/m.room.message/${randomString}`, data, config);
|
await axios.put(`${notification.homeserverUrl}/_matrix/client/r0/rooms/${roomId}/send/m.room.message/${randomString}`, data, config);
|
||||||
return okMsg;
|
return this.sendSuccess;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.throwGeneralAxiosError(error);
|
this.throwGeneralAxiosError(error);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,20 @@
|
||||||
// jest.mock("nodemailer", () => ({
|
jest.mock("axios", () => ({
|
||||||
// createTransport: jest.fn(),
|
put: jest.fn(),
|
||||||
// }));
|
}));
|
||||||
|
jest.mock("crypto", () => ({
|
||||||
|
randomBytes: jest.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
// const mockNodeMailer = require("nodemailer");
|
const axios = require("axios");
|
||||||
|
const { UP, DOWN } = require("../../src/util");
|
||||||
|
const NotificationSend = require("../notification");
|
||||||
|
|
||||||
const Matrix = require("./matrix");
|
const Matrix = require("./matrix");
|
||||||
|
const Crypto = require("crypto");
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// mockNodeMailer.createTransport.mockReset();
|
axios.put.mockReset();
|
||||||
|
Crypto.randomBytes.mockReset();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("notification default information", () => {
|
describe("notification default information", () => {
|
||||||
|
@ -16,3 +23,149 @@ describe("notification default information", () => {
|
||||||
expect(notification.name).toBe("matrix");
|
expect(notification.name).toBe("matrix");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("notification to act properly on send", () => {
|
||||||
|
it("should call axios with the proper default data", async () => {
|
||||||
|
|
||||||
|
let response = {
|
||||||
|
data: {
|
||||||
|
Message: "OK"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
axios.put.mockResolvedValueOnce(response);
|
||||||
|
Crypto.randomBytes.mockReturnValueOnce(new Buffer("abcd"));
|
||||||
|
|
||||||
|
let notif = new Matrix();
|
||||||
|
|
||||||
|
let msg = "PassedInMessage";
|
||||||
|
let notificationConf = {
|
||||||
|
type: "matrix",
|
||||||
|
internalRoomId: "1234",
|
||||||
|
accessToken: "abcd",
|
||||||
|
homeserverUrl: "www.example.com",
|
||||||
|
};
|
||||||
|
let monitorConf = {
|
||||||
|
};
|
||||||
|
let heartbeatConf = {
|
||||||
|
};
|
||||||
|
let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf);
|
||||||
|
|
||||||
|
expect(axios.put).toHaveBeenCalledWith("www.example.com/_matrix/client/r0/rooms/1234/send/m.room.message/YWJjZA%3D%3D", {
|
||||||
|
body: "PassedInMessage",
|
||||||
|
msgtype: "m.text"
|
||||||
|
}, {
|
||||||
|
headers: {
|
||||||
|
"Authorization": "Bearer abcd"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
expect(res).toBe("Sent Successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should call axios with the proper data when monitor nil", async () => {
|
||||||
|
let response = {
|
||||||
|
data: {
|
||||||
|
Message: "OK"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
axios.put.mockResolvedValueOnce(response);
|
||||||
|
Crypto.randomBytes.mockReturnValueOnce(new Buffer("abcd"));
|
||||||
|
|
||||||
|
let notif = new Matrix();
|
||||||
|
let notificationConf = {
|
||||||
|
type: "matrix",
|
||||||
|
internalRoomId: "1234",
|
||||||
|
accessToken: "abcd",
|
||||||
|
homeserverUrl: "www.example.com",
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage";
|
||||||
|
|
||||||
|
let res = await notif.send(notificationConf, msg, null, null);
|
||||||
|
|
||||||
|
expect(axios.put).toHaveBeenCalledWith("www.example.com/_matrix/client/r0/rooms/1234/send/m.room.message/YWJjZA%3D%3D", {
|
||||||
|
body: "PassedInMessage",
|
||||||
|
msgtype: "m.text"
|
||||||
|
}, {
|
||||||
|
headers: {
|
||||||
|
"Authorization": "Bearer abcd"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
expect(res).toBe("Sent Successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("notification to act properly on error", () => {
|
||||||
|
it("should respond with an axios error on error", async () => {
|
||||||
|
|
||||||
|
axios.put.mockImplementation(() => {
|
||||||
|
throw new Error("Test Error");
|
||||||
|
});
|
||||||
|
Crypto.randomBytes.mockReturnValueOnce(new Buffer("abcd"));
|
||||||
|
|
||||||
|
let notif = new Matrix();
|
||||||
|
let notificationConf = {
|
||||||
|
type: "matrix",
|
||||||
|
internalRoomId: "1234",
|
||||||
|
accessToken: "abcd",
|
||||||
|
homeserverUrl: "www.example.com",
|
||||||
|
};
|
||||||
|
let monitorConf = {
|
||||||
|
};
|
||||||
|
let heartbeatConf = {
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage";
|
||||||
|
|
||||||
|
try {
|
||||||
|
await notif.send(notificationConf, msg, null, null);
|
||||||
|
expect("Error thrown").toBe(false);
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.message).toBe("Error: Error: Test Error ");
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(axios.put).toHaveBeenCalledWith("www.example.com/_matrix/client/r0/rooms/1234/send/m.room.message/YWJjZA%3D%3D", {
|
||||||
|
body: "PassedInMessage",
|
||||||
|
msgtype: "m.text"
|
||||||
|
}, {
|
||||||
|
headers: {
|
||||||
|
"Authorization": "Bearer abcd"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("notification to get proper data from Notification.send", () => {
|
||||||
|
it("should call axios with proper data", async () => {
|
||||||
|
let response = {
|
||||||
|
data: {
|
||||||
|
Message: "OK"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
axios.put.mockResolvedValueOnce(response);
|
||||||
|
Crypto.randomBytes.mockReturnValueOnce(new Buffer("abcd"));
|
||||||
|
|
||||||
|
let notificationConf = {
|
||||||
|
type: "matrix",
|
||||||
|
internalRoomId: "1234",
|
||||||
|
accessToken: "abcd",
|
||||||
|
homeserverUrl: "www.example.com",
|
||||||
|
};
|
||||||
|
let monitorConf = {
|
||||||
|
};
|
||||||
|
let heartbeatConf = {
|
||||||
|
};
|
||||||
|
|
||||||
|
NotificationSend.Notification.init();
|
||||||
|
let res = await NotificationSend.Notification.send(notificationConf, "PassedInMessage", monitorConf, heartbeatConf);
|
||||||
|
expect(axios.put).toHaveBeenCalledWith("www.example.com/_matrix/client/r0/rooms/1234/send/m.room.message/YWJjZA%3D%3D", {
|
||||||
|
body: "PassedInMessage",
|
||||||
|
msgtype: "m.text"
|
||||||
|
}, {
|
||||||
|
headers: {
|
||||||
|
"Authorization": "Bearer abcd"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
expect(res).toBe("Sent Successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue