This commit is contained in:
Alex Holliday committed 2025-07-21 10:34:16 -07:00
1 parent 846564a17e
commit bfa6832beb
83 files changed
+338 -1249

No files matched your search

+7 -51
View File
@@ -48,15 +48,7 @@ describe("EmailService - Constructor", function () {
});
it("should initialize template loaders and email transporter", function () {
const emailService = new EmailService(
settingsServiceMock,
fsMock,
pathMock,
compileMock,
mjml2htmlMock,
nodemailerMock,
loggerMock
);
const emailService = new EmailService(settingsServiceMock, fsMock, pathMock, compileMock, mjml2htmlMock, nodemailerMock, loggerMock);
// Verify that the settingsService is assigned correctly
expect(emailService.settingsService).to.equal(settingsServiceMock);
@@ -83,15 +75,7 @@ describe("EmailService - Constructor", function () {
fsMock = {
readFileSync: sinon.stub().throws(new Error("File read error")),
};
const emailService = new EmailService(
settingsServiceMock,
fsMock,
pathMock,
compileMock,
mjml2htmlMock,
nodemailerMock,
loggerMock
);
const emailService = new EmailService(settingsServiceMock, fsMock, pathMock, compileMock, mjml2htmlMock, nodemailerMock, loggerMock);
expect(loggerMock.error.called).to.be.true;
expect(loggerMock.error.firstCall.args[0].message).to.equal("File read error");
});
@@ -139,15 +123,7 @@ describe("EmailService - buildAndSendEmail", function () {
error: sinon.stub(),
};
emailService = new EmailService(
settingsServiceMock,
fsMock,
pathMock,
compileMock,
mjml2htmlMock,
nodemailerMock,
loggerMock
);
emailService = new EmailService(settingsServiceMock, fsMock, pathMock, compileMock, mjml2htmlMock, nodemailerMock, loggerMock);
});
afterEach(function () {
@@ -155,12 +131,7 @@ describe("EmailService - buildAndSendEmail", function () {
});
it("should build and send email successfully", async function () {
const messageId = await emailService.buildAndSendEmail(
"welcomeEmailTemplate",
{},
"recipient@example.com",
"Welcome"
);
const messageId = await emailService.buildAndSendEmail("welcomeEmailTemplate", {}, "recipient@example.com", "Welcome");
expect(messageId).to.equal("12345");
expect(nodemailerMock.createTransport().sendMail.calledOnce).to.be.true;
@@ -169,24 +140,14 @@ describe("EmailService - buildAndSendEmail", function () {
it("should log error if building HTML fails", async function () {
mjml2htmlMock.throws(new Error("MJML error"));
const messageId = await emailService.buildAndSendEmail(
"welcomeEmailTemplate",
{},
"recipient@example.com",
"Welcome"
);
const messageId = await emailService.buildAndSendEmail("welcomeEmailTemplate", {}, "recipient@example.com", "Welcome");
expect(loggerMock.error.calledOnce).to.be.true;
expect(loggerMock.error.getCall(0).args[0].message).to.equal("MJML error");
});
it("should log error if sending email fails", async function () {
nodemailerMock.createTransport().sendMail.rejects(new Error("SMTP error"));
await emailService.buildAndSendEmail(
"welcomeEmailTemplate",
{},
"recipient@example.com",
"Welcome"
);
await emailService.buildAndSendEmail("welcomeEmailTemplate", {}, "recipient@example.com", "Welcome");
expect(loggerMock.error.calledOnce).to.be.true;
expect(loggerMock.error.getCall(0).args[0].message).to.equal("SMTP error");
});
@@ -195,12 +156,7 @@ describe("EmailService - buildAndSendEmail", function () {
mjml2htmlMock.throws(new Error("MJML error"));
nodemailerMock.createTransport().sendMail.rejects(new Error("SMTP error"));
const messageId = await emailService.buildAndSendEmail(
"welcomeEmailTemplate",
{},
"recipient@example.com",
"Welcome"
);
const messageId = await emailService.buildAndSendEmail("welcomeEmailTemplate", {}, "recipient@example.com", "Welcome");
expect(messageId).to.be.undefined;
expect(loggerMock.error.calledTwice).to.be.true;
+5 -26
View File
@@ -52,13 +52,7 @@ class WorkerStub {
}
describe("JobQueue", function () {
let settingsService,
logger,
db,
networkService,
statusService,
notificationService,
jobQueue;
let settingsService, logger, db, networkService, statusService, notificationService, jobQueue;
beforeEach(async function () {
settingsService = { getSettings: sinon.stub() };
@@ -71,16 +65,7 @@ describe("JobQueue", function () {
getMaintenanceWindowsByMonitorId: sinon.stub().returns([]),
};
networkService = { getStatus: sinon.stub() };
jobQueue = await JobQueue.createJobQueue(
db,
networkService,
statusService,
notificationService,
settingsService,
logger,
QueueStub,
WorkerStub
);
jobQueue = await JobQueue.createJobQueue(db, networkService, statusService, notificationService, settingsService, logger, QueueStub, WorkerStub);
});
afterEach(function () {
@@ -327,9 +312,7 @@ describe("JobQueue", function () {
const handler = jobQueue.createJobHandler();
await handler({ data: { _id: 1 } });
expect(logger.info.calledOnce).to.be.true;
expect(logger.info.firstCall.args[0].message).to.equal(
"Monitor 1 is in maintenance window"
);
expect(logger.info.firstCall.args[0].message).to.equal("Monitor 1 is in maintenance window");
});
it("should return if status has not changed", async function () {
@@ -362,9 +345,7 @@ describe("JobQueue", function () {
WorkerStub
);
jobQueue.isInMaintenanceWindow = sinon.stub().returns(false);
statusService.updateStatus = sinon
.stub()
.returns({ statusChanged: true, prevStatus: undefined });
statusService.updateStatus = sinon.stub().returns({ statusChanged: true, prevStatus: undefined });
const handler = jobQueue.createJobHandler();
await handler({ data: { _id: 1 } });
expect(jobQueue.notificationService.handleNotifications.notCalled).to.be.true;
@@ -382,9 +363,7 @@ describe("JobQueue", function () {
WorkerStub
);
jobQueue.isInMaintenanceWindow = sinon.stub().returns(false);
statusService.updateStatus = sinon
.stub()
.returns({ statusChanged: true, prevStatus: false });
statusService.updateStatus = sinon.stub().returns({ statusChanged: true, prevStatus: false });
const handler = jobQueue.createJobHandler();
await handler({ data: { _id: 1 } });
expect(jobQueue.notificationService.handleNotifications.calledOnce).to.be.true;
+8 -24
View File
@@ -26,9 +26,7 @@ describe("Network Service", function () {
};
ping = {
promise: {
probe: sinon
.stub()
.resolves({ response: { alive: true }, responseTime: 100, alive: true }),
probe: sinon.stub().resolves({ response: { alive: true }, responseTime: 100, alive: true }),
},
};
logger = { error: sinon.stub() };
@@ -73,9 +71,7 @@ describe("Network Service", function () {
it("should return a response object if ping unsuccessful", async function () {
const error = new Error("Test error");
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
networkService.timeRequest = sinon.stub().resolves({ response: null, responseTime: 1, error });
const pingResult = await networkService.requestPing({
data: { url: "http://test.com", _id: "123" },
});
@@ -113,9 +109,7 @@ describe("Network Service", function () {
it("should return a response object if http unsuccessful", async function () {
const error = new Error("Test error");
error.response = { status: 404 };
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
networkService.timeRequest = sinon.stub().resolves({ response: null, responseTime: 1, error });
const job = { data: { url: "http://test.com", _id: "123", type: "http" } };
const httpResult = await networkService.requestHttp(job);
expect(httpResult.monitorId).to.equal("123");
@@ -128,9 +122,7 @@ describe("Network Service", function () {
it("should return a response object if http unsuccessful with unknown code", async function () {
const error = new Error("Test error");
error.response = {};
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
networkService.timeRequest = sinon.stub().resolves({ response: null, responseTime: 1, error });
const job = { data: { url: "http://test.com", _id: "123", type: "http" } };
const httpResult = await networkService.requestHttp(job);
expect(httpResult.monitorId).to.equal("123");
@@ -167,9 +159,7 @@ describe("Network Service", function () {
it("should return a response object if pagespeed unsuccessful", async function () {
const error = new Error("Test error");
error.response = { status: 404 };
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
networkService.timeRequest = sinon.stub().resolves({ response: null, responseTime: 1, error });
const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } };
const pagespeedResult = await networkService.requestPagespeed(job);
expect(pagespeedResult.monitorId).to.equal("123");
@@ -182,9 +172,7 @@ describe("Network Service", function () {
it("should return a response object if pagespeed unsuccessful with an unknown code", async function () {
const error = new Error("Test error");
error.response = {};
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
networkService.timeRequest = sinon.stub().resolves({ response: null, responseTime: 1, error });
const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } };
const pagespeedResult = await networkService.requestPagespeed(job);
expect(pagespeedResult.monitorId).to.equal("123");
@@ -237,9 +225,7 @@ describe("Network Service", function () {
it("should return a response object if hardware unsuccessful", async function () {
const error = new Error("Test error");
error.response = { status: 404 };
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
networkService.timeRequest = sinon.stub().resolves({ response: null, responseTime: 1, error });
const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } };
const httpResult = await networkService.requestHardware(job);
expect(httpResult.monitorId).to.equal("123");
@@ -252,9 +238,7 @@ describe("Network Service", function () {
it("should return a response object if hardware unsuccessful with unknown code", async function () {
const error = new Error("Test error");
error.response = {};
networkService.timeRequest = sinon
.stub()
.resolves({ response: null, responseTime: 1, error });
networkService.timeRequest = sinon.stub().resolves({ response: null, responseTime: 1, error });
const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } };
const httpResult = await networkService.requestHardware(job);
expect(httpResult.monitorId).to.equal("123");
@@ -43,10 +43,7 @@ describe("NotificationService", function () {
await notificationService.sendEmail(networkResponse, address);
expect(notificationService.emailService.buildAndSendEmail.calledOnce).to.be.true;
expect(
notificationService.emailService.buildAndSendEmail.calledWith(
"serverIsUpTemplate",
{ monitor: "Test Monitor", url: "http://test.com" }
)
notificationService.emailService.buildAndSendEmail.calledWith("serverIsUpTemplate", { monitor: "Test Monitor", url: "http://test.com" })
);
});
@@ -132,22 +129,14 @@ describe("NotificationService", function () {
it("should send an email notification with Hardware Template", async function () {
emailService.buildAndSendEmail.resolves(true);
const res = await notificationService.sendHardwareEmail(
networkResponse,
address,
alerts
);
const res = await notificationService.sendHardwareEmail(networkResponse, address, alerts);
expect(res).to.be.true;
});
it("should return false if no alerts are provided", async function () {
alerts = [];
emailService.buildAndSendEmail.resolves(true);
const res = await notificationService.sendHardwareEmail(
networkResponse,
address,
alerts
);
const res = await notificationService.sendHardwareEmail(networkResponse, address, alerts);
expect(res).to.be.false;
});
});
@@ -172,9 +161,7 @@ describe("NotificationService", function () {
});
it("should handle status notifications", async function () {
db.getNotificationsByMonitorId.resolves([
{ type: "email", address: "test@test.com" },
]);
db.getNotificationsByMonitorId.resolves([{ type: "email", address: "test@test.com" }]);
const res = await notificationService.handleStatusNotifications(networkResponse);
expect(res).to.be.true;
});
@@ -247,15 +234,13 @@ describe("NotificationService", function () {
describe("it should return false if no thresholds are set", function () {
it("should return false if no thresholds are set", async function () {
networkResponse.monitor.thresholds = undefined;
const res =
await notificationService.handleHardwareNotifications(networkResponse);
const res = await notificationService.handleHardwareNotifications(networkResponse);
expect(res).to.be.false;
});
it("should return false if metrics are null", async function () {
networkResponse.payload.data = null;
const res =
await notificationService.handleHardwareNotifications(networkResponse);
const res = await notificationService.handleHardwareNotifications(networkResponse);
expect(res).to.be.false;
});
@@ -271,8 +256,7 @@ describe("NotificationService", function () {
save: sinon.stub().resolves(),
},
]);
const res =
await notificationService.handleHardwareNotifications(networkResponse);
const res = await notificationService.handleHardwareNotifications(networkResponse);
expect(res).to.be.true;
});
@@ -293,8 +277,7 @@ describe("NotificationService", function () {
usage_memory: 0.01,
usage_disk: 0.01,
};
const res =
await notificationService.handleHardwareNotifications(networkResponse);
const res = await notificationService.handleHardwareNotifications(networkResponse);
expect(res).to.be.true;
});
});
@@ -13,9 +13,7 @@ describe("SettingsService", function () {
sandbox.stub(process.env, "JWT_SECRET").value("secret");
sandbox.stub(process.env, "REFRESH_TOKEN_SECRET").value("refreshSecret");
sandbox.stub(process.env, "DB_TYPE").value("postgres");
sandbox
.stub(process.env, "DB_CONNECTION_STRING")
.value("postgres://user:pass@localhost/db");
sandbox.stub(process.env, "DB_CONNECTION_STRING").value("postgres://user:pass@localhost/db");
sandbox.stub(process.env, "REDIS_HOST").value("localhost");
sandbox.stub(process.env, "REDIS_PORT").value("6379");
sandbox.stub(process.env, "TOKEN_TTL").value("3600");
+2 -6
View File
@@ -73,9 +73,7 @@ describe("StatusService", () => {
});
it("should return {statusChanged: true} if status has changed from down to up", async function () {
statusService.db.getMonitorById = sinon
.stub()
.returns({ status: false, save: sinon.stub() });
statusService.db.getMonitorById = sinon.stub().returns({ status: false, save: sinon.stub() });
const result = await statusService.updateStatus({
monitorId: "test",
status: true,
@@ -87,9 +85,7 @@ describe("StatusService", () => {
});
it("should return {statusChanged: true} if status has changed from up to down", async function () {
statusService.db.getMonitorById = sinon
.stub()
.returns({ status: true, save: sinon.stub() });
statusService.db.getMonitorById = sinon.stub().returns({ status: true, save: sinon.stub() });
const result = await statusService.updateStatus({
monitorId: "test",
status: false,