fix(test): Use 'npm run lint-fix' to fix 'mocha/no-mocha-arrows' like lint warnings
- ref: https://mochajs.org/#arrow-functions
This commit is contained in:
1 parent
319e6d6017
commit
fcda60fe5b
31 files changed
+1268
-907
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import sinon from "sinon";
|
||||
import EmailService from "../../service/emailService.js";
|
||||
|
||||
describe("EmailService - Constructor", () => {
|
||||
describe("EmailService - Constructor", function() {
|
||||
let settingsServiceMock;
|
||||
let fsMock;
|
||||
let pathMock;
|
||||
@@ -10,7 +10,7 @@ describe("EmailService - Constructor", () => {
|
||||
let nodemailerMock;
|
||||
let loggerMock;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
settingsServiceMock = {
|
||||
getSettings: sinon.stub().returns({
|
||||
systemEmailHost: "smtp.example.com",
|
||||
@@ -43,11 +43,11 @@ describe("EmailService - Constructor", () => {
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should initialize template loaders and email transporter", () => {
|
||||
it("should initialize template loaders and email transporter", function() {
|
||||
const emailService = new EmailService(
|
||||
settingsServiceMock,
|
||||
fsMock,
|
||||
@@ -79,7 +79,7 @@ describe("EmailService - Constructor", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should have undefined templates if FS fails", () => {
|
||||
it("should have undefined templates if FS fails", function() {
|
||||
fsMock = {
|
||||
readFileSync: sinon.stub().throws(new Error("File read error")),
|
||||
};
|
||||
@@ -97,7 +97,7 @@ describe("EmailService - Constructor", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("EmailService - buildAndSendEmail", () => {
|
||||
describe("EmailService - buildAndSendEmail", function() {
|
||||
let settingsServiceMock;
|
||||
let fsMock;
|
||||
let pathMock;
|
||||
@@ -106,7 +106,8 @@ describe("EmailService - buildAndSendEmail", () => {
|
||||
let nodemailerMock;
|
||||
let loggerMock;
|
||||
let emailService;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
settingsServiceMock = {
|
||||
getSettings: sinon.stub().returns({
|
||||
systemEmailHost: "smtp.example.com",
|
||||
@@ -149,11 +150,11 @@ describe("EmailService - buildAndSendEmail", () => {
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should build and send email successfully", async () => {
|
||||
it("should build and send email successfully", async function() {
|
||||
const messageId = await emailService.buildAndSendEmail(
|
||||
"welcomeEmailTemplate",
|
||||
{},
|
||||
@@ -165,7 +166,7 @@ describe("EmailService - buildAndSendEmail", () => {
|
||||
expect(nodemailerMock.createTransport().sendMail.calledOnce).to.be.true;
|
||||
});
|
||||
|
||||
it("should log error if building HTML fails", async () => {
|
||||
it("should log error if building HTML fails", async function() {
|
||||
mjml2htmlMock.throws(new Error("MJML error"));
|
||||
|
||||
const messageId = await emailService.buildAndSendEmail(
|
||||
@@ -178,7 +179,7 @@ describe("EmailService - buildAndSendEmail", () => {
|
||||
expect(loggerMock.error.getCall(0).args[0].message).to.equal("MJML error");
|
||||
});
|
||||
|
||||
it("should log error if sending email fails", async () => {
|
||||
it("should log error if sending email fails", async function() {
|
||||
nodemailerMock.createTransport().sendMail.rejects(new Error("SMTP error"));
|
||||
await emailService.buildAndSendEmail(
|
||||
"welcomeEmailTemplate",
|
||||
@@ -190,7 +191,7 @@ describe("EmailService - buildAndSendEmail", () => {
|
||||
expect(loggerMock.error.getCall(0).args[0].message).to.equal("SMTP error");
|
||||
});
|
||||
|
||||
it("should log error if both building HTML and sending email fail", async () => {
|
||||
it("should log error if both building HTML and sending email fail", async function() {
|
||||
mjml2htmlMock.throws(new Error("MJML error"));
|
||||
nodemailerMock.createTransport().sendMail.rejects(new Error("SMTP error"));
|
||||
|
||||
@@ -207,5 +208,5 @@ describe("EmailService - buildAndSendEmail", () => {
|
||||
expect(loggerMock.error.getCall(1).args[0].message).to.equal("SMTP error");
|
||||
});
|
||||
|
||||
it("should log an error if buildHtml fails", async () => {});
|
||||
it("should log an error if buildHtml fails", async function() {});
|
||||
});
|
||||
@@ -51,7 +51,7 @@ class WorkerStub {
|
||||
}
|
||||
}
|
||||
|
||||
describe("JobQueue", () => {
|
||||
describe("JobQueue", function() {
|
||||
let settingsService,
|
||||
logger,
|
||||
db,
|
||||
@@ -60,7 +60,7 @@ describe("JobQueue", () => {
|
||||
notificationService,
|
||||
jobQueue;
|
||||
|
||||
beforeEach(async () => {
|
||||
beforeEach(async function() {
|
||||
settingsService = { getSettings: sinon.stub() };
|
||||
statusService = { updateStatus: sinon.stub() };
|
||||
notificationService = { handleNotifications: sinon.stub() };
|
||||
@@ -83,12 +83,12 @@ describe("JobQueue", () => {
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("createJobQueue", () => {
|
||||
it("should create a new JobQueue and add jobs for active monitors", async () => {
|
||||
describe("createJobQueue", function() {
|
||||
it("should create a new JobQueue and add jobs for active monitors", async function() {
|
||||
db.getAllMonitors.returns([
|
||||
{ id: 1, isActive: true },
|
||||
{ id: 2, isActive: true },
|
||||
@@ -108,7 +108,7 @@ describe("JobQueue", () => {
|
||||
expect(jobQueue.queue.jobs.length).to.equal(4);
|
||||
});
|
||||
|
||||
it("should reject with an error if an error occurs", async () => {
|
||||
it("should reject with an error if an error occurs", async function() {
|
||||
db.getAllMonitors.throws("Error");
|
||||
try {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
@@ -127,7 +127,7 @@ describe("JobQueue", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should reject with an error if an error occurs, should not overwrite error data", async () => {
|
||||
it("should reject with an error if an error occurs, should not overwrite error data", async function() {
|
||||
const error = new Error("Error");
|
||||
error.service = "otherService";
|
||||
error.method = "otherMethod";
|
||||
@@ -151,14 +151,15 @@ describe("JobQueue", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("Constructor", () => {
|
||||
it("should construct a new JobQueue with default port and host if not provided", async () => {
|
||||
describe("Constructor", function() {
|
||||
it("should construct a new JobQueue with default port and host if not provided", async function() {
|
||||
settingsService.getSettings.returns({});
|
||||
|
||||
expect(jobQueue.connection.host).to.equal("127.0.0.1");
|
||||
expect(jobQueue.connection.port).to.equal(6379);
|
||||
});
|
||||
it("should construct a new JobQueue with provided port and host", async () => {
|
||||
|
||||
it("should construct a new JobQueue with provided port and host", async function() {
|
||||
settingsService.getSettings.returns({ redisHost: "localhost", redisPort: 1234 });
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
@@ -175,8 +176,8 @@ describe("JobQueue", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("isMaintenanceWindow", () => {
|
||||
it("should throw an error if error occurs", async () => {
|
||||
describe("isMaintenanceWindow", function() {
|
||||
it("should throw an error if error occurs", async function() {
|
||||
db.getMaintenanceWindowsByMonitorId.throws("Error");
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
@@ -196,7 +197,7 @@ describe("JobQueue", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should return true if in maintenance window with no repeat", async () => {
|
||||
it("should return true if in maintenance window with no repeat", async function() {
|
||||
db.getMaintenanceWindowsByMonitorId.returns([
|
||||
{
|
||||
active: true,
|
||||
@@ -219,7 +220,7 @@ describe("JobQueue", () => {
|
||||
expect(inWindow).to.be.true;
|
||||
});
|
||||
|
||||
it("should return true if in maintenance window with repeat", async () => {
|
||||
it("should return true if in maintenance window with repeat", async function() {
|
||||
db.getMaintenanceWindowsByMonitorId.returns([
|
||||
{
|
||||
active: true,
|
||||
@@ -242,7 +243,7 @@ describe("JobQueue", () => {
|
||||
expect(inWindow).to.be.true;
|
||||
});
|
||||
|
||||
it("should return false if in end < start", async () => {
|
||||
it("should return false if in end < start", async function() {
|
||||
db.getMaintenanceWindowsByMonitorId.returns([
|
||||
{
|
||||
active: true,
|
||||
@@ -264,7 +265,8 @@ describe("JobQueue", () => {
|
||||
const inWindow = await jobQueue.isInMaintenanceWindow(1);
|
||||
expect(inWindow).to.be.false;
|
||||
});
|
||||
it("should return false if not in maintenance window", async () => {
|
||||
|
||||
it("should return false if not in maintenance window", async function() {
|
||||
db.getMaintenanceWindowsByMonitorId.returns([
|
||||
{
|
||||
active: false,
|
||||
@@ -288,8 +290,8 @@ describe("JobQueue", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("createJobHandler", () => {
|
||||
it("resolve to an error if an error is thrown within", async () => {
|
||||
describe("createJobHandler", function() {
|
||||
it("resolve to an error if an error is thrown within", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -310,7 +312,7 @@ describe("JobQueue", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should log info if job is in maintenance window", async () => {
|
||||
it("should log info if job is in maintenance window", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -330,7 +332,7 @@ describe("JobQueue", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("should return if status has not changed", async () => {
|
||||
it("should return if status has not changed", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -348,7 +350,7 @@ describe("JobQueue", () => {
|
||||
expect(jobQueue.notificationService.handleNotifications.notCalled).to.be.true;
|
||||
});
|
||||
|
||||
it("should return if status has changed, but prevStatus was undefined (monitor paused)", async () => {
|
||||
it("should return if status has changed, but prevStatus was undefined (monitor paused)", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -367,7 +369,8 @@ describe("JobQueue", () => {
|
||||
await handler({ data: { _id: 1 } });
|
||||
expect(jobQueue.notificationService.handleNotifications.notCalled).to.be.true;
|
||||
});
|
||||
it("should call notification service if status changed and monitor was not paused", async () => {
|
||||
|
||||
it("should call notification service if status changed and monitor was not paused", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -388,8 +391,8 @@ describe("JobQueue", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getWorkerStats", () => {
|
||||
it("should throw an error if getRepeatable Jobs fails", async () => {
|
||||
describe("getWorkerStats", function() {
|
||||
it("should throw an error if getRepeatable Jobs fails", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -410,7 +413,8 @@ describe("JobQueue", () => {
|
||||
expect(error.method).to.equal("getWorkerStats");
|
||||
}
|
||||
});
|
||||
it("should throw an error if getRepeatable Jobs fails but respect existing error data", async () => {
|
||||
|
||||
it("should throw an error if getRepeatable Jobs fails but respect existing error data", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -436,8 +440,8 @@ describe("JobQueue", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("scaleWorkers", () => {
|
||||
it("should scale workers to 5 if no workers", async () => {
|
||||
describe("scaleWorkers", function() {
|
||||
it("should scale workers to 5 if no workers", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -450,7 +454,8 @@ describe("JobQueue", () => {
|
||||
);
|
||||
expect(jobQueue.workers.length).to.equal(5);
|
||||
});
|
||||
it("should scale workers up", async () => {
|
||||
|
||||
it("should scale workers up", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -467,7 +472,8 @@ describe("JobQueue", () => {
|
||||
});
|
||||
expect(jobQueue.workers.length).to.equal(20);
|
||||
});
|
||||
it("should scale workers down, even with error of worker.close fails", async () => {
|
||||
|
||||
it("should scale workers down, even with error of worker.close fails", async function() {
|
||||
WorkerStub.prototype.close = async () => {
|
||||
throw new Error("Error");
|
||||
};
|
||||
@@ -492,7 +498,8 @@ describe("JobQueue", () => {
|
||||
});
|
||||
expect(jobQueue.workers.length).to.equal(5);
|
||||
});
|
||||
it("should scale workers down", async () => {
|
||||
|
||||
it("should scale workers down", async function() {
|
||||
WorkerStub.prototype.close = async () => {
|
||||
return true;
|
||||
};
|
||||
@@ -517,7 +524,8 @@ describe("JobQueue", () => {
|
||||
});
|
||||
expect(jobQueue.workers.length).to.equal(5);
|
||||
});
|
||||
it("should return false if scaling doesn't happen", async () => {
|
||||
|
||||
it("should return false if scaling doesn't happen", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -534,8 +542,8 @@ describe("JobQueue", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getJobs", () => {
|
||||
it("should return jobs", async () => {
|
||||
describe("getJobs", function() {
|
||||
it("should return jobs", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -549,7 +557,8 @@ describe("JobQueue", () => {
|
||||
const jobs = await jobQueue.getJobs();
|
||||
expect(jobs.length).to.equal(0);
|
||||
});
|
||||
it("should throw an error if getRepeatableJobs fails", async () => {
|
||||
|
||||
it("should throw an error if getRepeatableJobs fails", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -571,7 +580,8 @@ describe("JobQueue", () => {
|
||||
expect(error.method).to.equal("getJobs");
|
||||
}
|
||||
});
|
||||
it("should throw an error if getRepeatableJobs fails but respect existing error data", async () => {
|
||||
|
||||
it("should throw an error if getRepeatableJobs fails but respect existing error data", async function() {
|
||||
const jobQueue = await JobQueue.createJobQueue(
|
||||
db,
|
||||
networkService,
|
||||
@@ -598,12 +608,13 @@ describe("JobQueue", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getJobStats", () => {
|
||||
it("should return job stats for no jobs", async () => {
|
||||
describe("getJobStats", function() {
|
||||
it("should return job stats for no jobs", async function() {
|
||||
const jobStats = await jobQueue.getJobStats();
|
||||
expect(jobStats).to.deep.equal({ jobs: [], workers: 5 });
|
||||
});
|
||||
it("should return job stats for jobs", async () => {
|
||||
|
||||
it("should return job stats for jobs", async function() {
|
||||
jobQueue.queue.getJobs = async () => {
|
||||
return [{ data: { url: "test" }, getState: async () => "completed" }];
|
||||
};
|
||||
@@ -613,7 +624,8 @@ describe("JobQueue", () => {
|
||||
workers: 5,
|
||||
});
|
||||
});
|
||||
it("should reject with an error if mapping jobs fails", async () => {
|
||||
|
||||
it("should reject with an error if mapping jobs fails", async function() {
|
||||
jobQueue.queue.getJobs = async () => {
|
||||
return [
|
||||
{
|
||||
@@ -632,7 +644,8 @@ describe("JobQueue", () => {
|
||||
expect(error.method).to.equal("getJobStats");
|
||||
}
|
||||
});
|
||||
it("should reject with an error if mapping jobs fails but respect existing error data", async () => {
|
||||
|
||||
it("should reject with an error if mapping jobs fails but respect existing error data", async function() {
|
||||
jobQueue.queue.getJobs = async () => {
|
||||
return [
|
||||
{
|
||||
@@ -656,12 +669,13 @@ describe("JobQueue", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("addJob", () => {
|
||||
it("should add a job to the queue", async () => {
|
||||
describe("addJob", function() {
|
||||
it("should add a job to the queue", async function() {
|
||||
jobQueue.addJob("test", { url: "test" });
|
||||
expect(jobQueue.queue.jobs.length).to.equal(1);
|
||||
});
|
||||
it("should reject with an error if adding fails", async () => {
|
||||
|
||||
it("should reject with an error if adding fails", async function() {
|
||||
jobQueue.queue.add = async () => {
|
||||
throw new Error("Error adding job");
|
||||
};
|
||||
@@ -673,7 +687,8 @@ describe("JobQueue", () => {
|
||||
expect(error.method).to.equal("addJob");
|
||||
}
|
||||
});
|
||||
it("should reject with an error if adding fails but respect existing error data", async () => {
|
||||
|
||||
it("should reject with an error if adding fails but respect existing error data", async function() {
|
||||
jobQueue.queue.add = async () => {
|
||||
const error = new Error("Error adding job");
|
||||
error.service = "otherService";
|
||||
@@ -689,8 +704,9 @@ describe("JobQueue", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("deleteJob", () => {
|
||||
it("should delete a job from the queue", async () => {
|
||||
|
||||
describe("deleteJob", function() {
|
||||
it("should delete a job from the queue", async function() {
|
||||
jobQueue.getWorkerStats = sinon.stub().returns({ load: 1, jobs: [{}] });
|
||||
jobQueue.scaleWorkers = sinon.stub();
|
||||
const monitor = { _id: 1 };
|
||||
@@ -702,7 +718,8 @@ describe("JobQueue", () => {
|
||||
// expect(jobQueue.getWorkerStats.calledOnce).to.be.true;
|
||||
// expect(jobQueue.scaleWorkers.calledOnce).to.be.true;
|
||||
});
|
||||
it("should log an error if job is not found", async () => {
|
||||
|
||||
it("should log an error if job is not found", async function() {
|
||||
jobQueue.getWorkerStats = sinon.stub().returns({ load: 1, jobs: [{}] });
|
||||
jobQueue.scaleWorkers = sinon.stub();
|
||||
const monitor = { _id: 1 };
|
||||
@@ -711,7 +728,8 @@ describe("JobQueue", () => {
|
||||
await jobQueue.deleteJob({ id_: 2 });
|
||||
expect(logger.error.calledOnce).to.be.true;
|
||||
});
|
||||
it("should reject with an error if removeRepeatable fails", async () => {
|
||||
|
||||
it("should reject with an error if removeRepeatable fails", async function() {
|
||||
jobQueue.queue.removeRepeatable = async () => {
|
||||
const error = new Error("removeRepeatable error");
|
||||
throw error;
|
||||
@@ -725,7 +743,8 @@ describe("JobQueue", () => {
|
||||
expect(error.method).to.equal("deleteJob");
|
||||
}
|
||||
});
|
||||
it("should reject with an error if removeRepeatable fails but respect existing error data", async () => {
|
||||
|
||||
it("should reject with an error if removeRepeatable fails but respect existing error data", async function() {
|
||||
jobQueue.queue.removeRepeatable = async () => {
|
||||
const error = new Error("removeRepeatable error");
|
||||
error.service = "otherService";
|
||||
@@ -742,8 +761,9 @@ describe("JobQueue", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("getMetrics", () => {
|
||||
it("should return metrics for the job queue", async () => {
|
||||
|
||||
describe("getMetrics", function() {
|
||||
it("should return metrics for the job queue", async function() {
|
||||
jobQueue.queue.getWaitingCount = async () => 1;
|
||||
jobQueue.queue.getActiveCount = async () => 2;
|
||||
jobQueue.queue.getCompletedCount = async () => 3;
|
||||
@@ -760,7 +780,8 @@ describe("JobQueue", () => {
|
||||
repeatableJobs: 3,
|
||||
});
|
||||
});
|
||||
it("should log an error if metrics operations fail", async () => {
|
||||
|
||||
it("should log an error if metrics operations fail", async function() {
|
||||
jobQueue.queue.getWaitingCount = async () => {
|
||||
throw new Error("Error");
|
||||
};
|
||||
@@ -770,8 +791,8 @@ describe("JobQueue", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("obliterate", () => {
|
||||
it("should return true if obliteration is successful", async () => {
|
||||
describe("obliterate", function() {
|
||||
it("should return true if obliteration is successful", async function() {
|
||||
jobQueue.queue.pause = async () => true;
|
||||
jobQueue.getJobs = async () => [{ key: 1, id: 1 }];
|
||||
jobQueue.queue.removeRepeatableByKey = async () => true;
|
||||
@@ -780,7 +801,8 @@ describe("JobQueue", () => {
|
||||
const obliteration = await jobQueue.obliterate();
|
||||
expect(obliteration).to.be.true;
|
||||
});
|
||||
it("should throw an error if obliteration fails", async () => {
|
||||
|
||||
it("should throw an error if obliteration fails", async function() {
|
||||
jobQueue.getMetrics = async () => {
|
||||
throw new Error("Error");
|
||||
};
|
||||
@@ -792,7 +814,8 @@ describe("JobQueue", () => {
|
||||
expect(error.method).to.equal("obliterate");
|
||||
}
|
||||
});
|
||||
it("should throw an error if obliteration fails but respect existing error data", async () => {
|
||||
|
||||
it("should throw an error if obliteration fails but respect existing error data", async function() {
|
||||
jobQueue.getMetrics = async () => {
|
||||
const error = new Error("Error");
|
||||
error.service = "otherService";
|
||||
|
||||
@@ -3,10 +3,10 @@ import NetworkService from "../../service/networkService.js";
|
||||
import { expect } from "chai";
|
||||
import http from "http";
|
||||
import { errorMessages } from "../../utils/messages.js";
|
||||
describe("Network Service", () => {
|
||||
describe("Network Service", function() {
|
||||
let axios, ping, Docker, logger, networkService;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
axios = {
|
||||
get: sinon.stub().resolves({
|
||||
data: { foo: "bar" },
|
||||
@@ -34,21 +34,23 @@ describe("Network Service", () => {
|
||||
logger = { error: sinon.stub() };
|
||||
networkService = new NetworkService(axios, ping, logger, http, Docker);
|
||||
});
|
||||
describe("constructor", () => {
|
||||
it("should create a new NetworkService instance", () => {
|
||||
|
||||
describe("constructor", function() {
|
||||
it("should create a new NetworkService instance", function() {
|
||||
const networkService = new NetworkService();
|
||||
expect(networkService).to.be.an.instanceOf(NetworkService);
|
||||
});
|
||||
});
|
||||
|
||||
describe("timeRequest", () => {
|
||||
it("should time an asynchronous operation", async () => {
|
||||
describe("timeRequest", function() {
|
||||
it("should time an asynchronous operation", async function() {
|
||||
const operation = sinon.stub().resolves("success");
|
||||
const { response, responseTime } = await networkService.timeRequest(operation);
|
||||
expect(response).to.equal("success");
|
||||
expect(responseTime).to.be.a("number");
|
||||
});
|
||||
it("should handle errors if operation throws error", async () => {
|
||||
|
||||
it("should handle errors if operation throws error", async function() {
|
||||
const error = new Error("Test error");
|
||||
const operation = sinon.stub().throws(error);
|
||||
const { response, responseTime } = await networkService.timeRequest(operation);
|
||||
@@ -58,8 +60,8 @@ describe("Network Service", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("requestPing", () => {
|
||||
it("should return a response object if ping successful", async () => {
|
||||
describe("requestPing", function() {
|
||||
it("should return a response object if ping successful", async function() {
|
||||
const pingResult = await networkService.requestPing({
|
||||
data: { url: "http://test.com", _id: "123" },
|
||||
});
|
||||
@@ -68,7 +70,8 @@ describe("Network Service", () => {
|
||||
expect(pingResult.responseTime).to.be.a("number");
|
||||
expect(pingResult.status).to.be.true;
|
||||
});
|
||||
it("should return a response object if ping unsuccessful", async () => {
|
||||
|
||||
it("should return a response object if ping unsuccessful", async function() {
|
||||
const error = new Error("Test error");
|
||||
networkService.timeRequest = sinon
|
||||
.stub()
|
||||
@@ -82,7 +85,8 @@ describe("Network Service", () => {
|
||||
expect(pingResult.status).to.be.false;
|
||||
expect(pingResult.code).to.equal(networkService.PING_ERROR);
|
||||
});
|
||||
it("should throw an error if ping cannot resolve", async () => {
|
||||
|
||||
it("should throw an error if ping cannot resolve", async function() {
|
||||
const error = new Error("test error");
|
||||
networkService.timeRequest = sinon.stub().throws(error);
|
||||
try {
|
||||
@@ -95,8 +99,9 @@ describe("Network Service", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("requestHttp", () => {
|
||||
it("should return a response object if http successful", async () => {
|
||||
|
||||
describe("requestHttp", function() {
|
||||
it("should return a response object if http successful", async function() {
|
||||
const job = { data: { url: "http://test.com", _id: "123", type: "http" } };
|
||||
const httpResult = await networkService.requestHttp(job);
|
||||
expect(httpResult.monitorId).to.equal("123");
|
||||
@@ -104,7 +109,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.responseTime).to.be.a("number");
|
||||
expect(httpResult.status).to.be.true;
|
||||
});
|
||||
it("should return a response object if http unsuccessful", async () => {
|
||||
|
||||
it("should return a response object if http unsuccessful", async function() {
|
||||
const error = new Error("Test error");
|
||||
error.response = { status: 404 };
|
||||
networkService.timeRequest = sinon
|
||||
@@ -118,7 +124,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.status).to.be.false;
|
||||
expect(httpResult.code).to.equal(404);
|
||||
});
|
||||
it("should return a response object if http unsuccessful with unknown code", async () => {
|
||||
|
||||
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
|
||||
@@ -132,7 +139,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.status).to.be.false;
|
||||
expect(httpResult.code).to.equal(networkService.NETWORK_ERROR);
|
||||
});
|
||||
it("should throw an error if an error occurs", async () => {
|
||||
|
||||
it("should throw an error if an error occurs", async function() {
|
||||
const error = new Error("test error");
|
||||
networkService.timeRequest = sinon.stub().throws(error);
|
||||
try {
|
||||
@@ -146,8 +154,8 @@ describe("Network Service", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("requestPagespeed", () => {
|
||||
it("should return a response object if pagespeed successful", async () => {
|
||||
describe("requestPagespeed", function() {
|
||||
it("should return a response object if pagespeed successful", async function() {
|
||||
const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } };
|
||||
const pagespeedResult = await networkService.requestPagespeed(job);
|
||||
expect(pagespeedResult.monitorId).to.equal("123");
|
||||
@@ -155,7 +163,8 @@ describe("Network Service", () => {
|
||||
expect(pagespeedResult.responseTime).to.be.a("number");
|
||||
expect(pagespeedResult.status).to.be.true;
|
||||
});
|
||||
it("should return a response object if pagespeed unsuccessful", async () => {
|
||||
|
||||
it("should return a response object if pagespeed unsuccessful", async function() {
|
||||
const error = new Error("Test error");
|
||||
error.response = { status: 404 };
|
||||
networkService.timeRequest = sinon
|
||||
@@ -169,7 +178,8 @@ describe("Network Service", () => {
|
||||
expect(pagespeedResult.status).to.be.false;
|
||||
expect(pagespeedResult.code).to.equal(404);
|
||||
});
|
||||
it("should return a response object if pagespeed unsuccessful with an unknown code", async () => {
|
||||
|
||||
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
|
||||
@@ -183,7 +193,8 @@ describe("Network Service", () => {
|
||||
expect(pagespeedResult.status).to.be.false;
|
||||
expect(pagespeedResult.code).to.equal(networkService.NETWORK_ERROR);
|
||||
});
|
||||
it("should throw an error if pagespeed cannot resolve", async () => {
|
||||
|
||||
it("should throw an error if pagespeed cannot resolve", async function() {
|
||||
const error = new Error("test error");
|
||||
networkService.timeRequest = sinon.stub().throws(error);
|
||||
try {
|
||||
@@ -197,8 +208,8 @@ describe("Network Service", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("requestHardware", () => {
|
||||
it("should return a response object if hardware successful", async () => {
|
||||
describe("requestHardware", function() {
|
||||
it("should return a response object if hardware successful", async function() {
|
||||
const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } };
|
||||
const httpResult = await networkService.requestHardware(job);
|
||||
expect(httpResult.monitorId).to.equal("123");
|
||||
@@ -206,7 +217,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.responseTime).to.be.a("number");
|
||||
expect(httpResult.status).to.be.true;
|
||||
});
|
||||
it("should return a response object if hardware successful and job has a secret", async () => {
|
||||
|
||||
it("should return a response object if hardware successful and job has a secret", async function() {
|
||||
const job = {
|
||||
data: {
|
||||
url: "http://test.com",
|
||||
@@ -221,7 +233,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.responseTime).to.be.a("number");
|
||||
expect(httpResult.status).to.be.true;
|
||||
});
|
||||
it("should return a response object if hardware unsuccessful", async () => {
|
||||
|
||||
it("should return a response object if hardware unsuccessful", async function() {
|
||||
const error = new Error("Test error");
|
||||
error.response = { status: 404 };
|
||||
networkService.timeRequest = sinon
|
||||
@@ -235,7 +248,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.status).to.be.false;
|
||||
expect(httpResult.code).to.equal(404);
|
||||
});
|
||||
it("should return a response object if hardware unsuccessful with unknown code", async () => {
|
||||
|
||||
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
|
||||
@@ -249,7 +263,8 @@ describe("Network Service", () => {
|
||||
expect(httpResult.status).to.be.false;
|
||||
expect(httpResult.code).to.equal(networkService.NETWORK_ERROR);
|
||||
});
|
||||
it("should throw an error if hardware cannot resolve", async () => {
|
||||
|
||||
it("should throw an error if hardware cannot resolve", async function() {
|
||||
const error = new Error("test error");
|
||||
networkService.timeRequest = sinon.stub().throws(error);
|
||||
try {
|
||||
@@ -263,8 +278,8 @@ describe("Network Service", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("requestDocker", () => {
|
||||
it("should return a response object if docker successful", async () => {
|
||||
describe("requestDocker", function() {
|
||||
it("should return a response object if docker successful", async function() {
|
||||
const job = { data: { url: "http://test.com", _id: "123", type: "docker" } };
|
||||
const dockerResult = await networkService.requestDocker(job);
|
||||
expect(dockerResult.monitorId).to.equal("123");
|
||||
@@ -273,7 +288,7 @@ describe("Network Service", () => {
|
||||
expect(dockerResult.status).to.be.true;
|
||||
});
|
||||
|
||||
it("should return a response object with status false if container not running", async () => {
|
||||
it("should return a response object with status false if container not running", async function() {
|
||||
Docker = class {
|
||||
listContainers = sinon.stub().resolves([
|
||||
{
|
||||
@@ -292,7 +307,7 @@ describe("Network Service", () => {
|
||||
expect(dockerResult.code).to.equal(200);
|
||||
});
|
||||
|
||||
it("should handle an error when fetching the container", async () => {
|
||||
it("should handle an error when fetching the container", async function() {
|
||||
Docker = class {
|
||||
listContainers = sinon.stub().resolves([
|
||||
{
|
||||
@@ -311,7 +326,7 @@ describe("Network Service", () => {
|
||||
expect(dockerResult.code).to.equal(networkService.NETWORK_ERROR);
|
||||
});
|
||||
|
||||
it("should throw an error if operations fail", async () => {
|
||||
it("should throw an error if operations fail", async function() {
|
||||
Docker = class {
|
||||
listContainers = sinon.stub().resolves([
|
||||
{
|
||||
@@ -329,7 +344,8 @@ describe("Network Service", () => {
|
||||
expect(error.message).to.equal("test error");
|
||||
}
|
||||
});
|
||||
it("should throw an error if no matching images found", async () => {
|
||||
|
||||
it("should throw an error if no matching images found", async function() {
|
||||
Docker = class {
|
||||
listContainers = sinon.stub().resolves([]);
|
||||
getContainer = sinon.stub().throws(new Error("test error"));
|
||||
@@ -344,8 +360,8 @@ describe("Network Service", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getStatus", () => {
|
||||
beforeEach(() => {
|
||||
describe("getStatus", function() {
|
||||
beforeEach(function() {
|
||||
networkService.requestPing = sinon.stub();
|
||||
networkService.requestHttp = sinon.stub();
|
||||
networkService.requestPagespeed = sinon.stub();
|
||||
@@ -353,45 +369,51 @@ describe("Network Service", () => {
|
||||
networkService.requestDocker = sinon.stub();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
it("should call requestPing if type is ping", async () => {
|
||||
|
||||
it("should call requestPing if type is ping", async function() {
|
||||
await networkService.getStatus({ data: { type: "ping" } });
|
||||
expect(networkService.requestPing.calledOnce).to.be.true;
|
||||
expect(networkService.requestDocker.notCalled).to.be.true;
|
||||
expect(networkService.requestHttp.notCalled).to.be.true;
|
||||
expect(networkService.requestPagespeed.notCalled).to.be.true;
|
||||
});
|
||||
it("should call requestHttp if type is http", async () => {
|
||||
|
||||
it("should call requestHttp if type is http", async function() {
|
||||
await networkService.getStatus({ data: { type: "http" } });
|
||||
expect(networkService.requestPing.notCalled).to.be.true;
|
||||
expect(networkService.requestDocker.notCalled).to.be.true;
|
||||
expect(networkService.requestHttp.calledOnce).to.be.true;
|
||||
expect(networkService.requestPagespeed.notCalled).to.be.true;
|
||||
});
|
||||
it("should call requestPagespeed if type is pagespeed", async () => {
|
||||
|
||||
it("should call requestPagespeed if type is pagespeed", async function() {
|
||||
await networkService.getStatus({ data: { type: "pagespeed" } });
|
||||
expect(networkService.requestPing.notCalled).to.be.true;
|
||||
expect(networkService.requestDocker.notCalled).to.be.true;
|
||||
expect(networkService.requestHttp.notCalled).to.be.true;
|
||||
expect(networkService.requestPagespeed.calledOnce).to.be.true;
|
||||
});
|
||||
it("should call requestHardware if type is hardware", async () => {
|
||||
|
||||
it("should call requestHardware if type is hardware", async function() {
|
||||
await networkService.getStatus({ data: { type: "hardware" } });
|
||||
expect(networkService.requestHardware.calledOnce).to.be.true;
|
||||
expect(networkService.requestDocker.notCalled).to.be.true;
|
||||
expect(networkService.requestPing.notCalled).to.be.true;
|
||||
expect(networkService.requestPagespeed.notCalled).to.be.true;
|
||||
});
|
||||
it("should call requestDocker if type is Docker", async () => {
|
||||
|
||||
it("should call requestDocker if type is Docker", async function() {
|
||||
await networkService.getStatus({ data: { type: "docker" } });
|
||||
expect(networkService.requestDocker.calledOnce).to.be.true;
|
||||
expect(networkService.requestHardware.notCalled).to.be.true;
|
||||
expect(networkService.requestPing.notCalled).to.be.true;
|
||||
expect(networkService.requestPagespeed.notCalled).to.be.true;
|
||||
});
|
||||
it("should throw an error if an unknown type is provided", async () => {
|
||||
|
||||
it("should throw an error if an unknown type is provided", async function() {
|
||||
try {
|
||||
await networkService.getStatus({ data: { type: "unknown" } });
|
||||
} catch (error) {
|
||||
@@ -400,7 +422,8 @@ describe("Network Service", () => {
|
||||
expect(error.message).to.equal("Unsupported type: unknown");
|
||||
}
|
||||
});
|
||||
it("should throw an error if job type is undefined", async () => {
|
||||
|
||||
it("should throw an error if job type is undefined", async function() {
|
||||
try {
|
||||
await networkService.getStatus({ data: { type: undefined } });
|
||||
} catch (error) {
|
||||
@@ -409,7 +432,8 @@ describe("Network Service", () => {
|
||||
expect(error.message).to.equal("Unsupported type: unknown");
|
||||
}
|
||||
});
|
||||
it("should throw an error if job is empty", async () => {
|
||||
|
||||
it("should throw an error if job is empty", async function() {
|
||||
try {
|
||||
await networkService.getStatus({});
|
||||
} catch (error) {
|
||||
@@ -417,7 +441,8 @@ describe("Network Service", () => {
|
||||
expect(error.message).to.equal("Unsupported type: unknown");
|
||||
}
|
||||
});
|
||||
it("should throw an error if job is null", async () => {
|
||||
|
||||
it("should throw an error if job is null", async function() {
|
||||
try {
|
||||
await networkService.getStatus(null);
|
||||
} catch (error) {
|
||||
|
||||
@@ -2,9 +2,10 @@ import sinon from "sinon";
|
||||
import NotificationService from "../../service/notificationService.js";
|
||||
import { expect } from "chai";
|
||||
|
||||
describe("NotificationService", () => {
|
||||
describe("NotificationService", function() {
|
||||
let emailService, db, logger, notificationService;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
db = {
|
||||
getNotificationsByMonitorId: sinon.stub(),
|
||||
};
|
||||
@@ -17,18 +18,19 @@ describe("NotificationService", () => {
|
||||
|
||||
notificationService = new NotificationService(emailService, db, logger);
|
||||
});
|
||||
afterEach(() => {
|
||||
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("constructor", () => {
|
||||
it("should create a new instance of NotificationService", () => {
|
||||
describe("constructor", function() {
|
||||
it("should create a new instance of NotificationService", function() {
|
||||
expect(notificationService).to.be.an.instanceOf(NotificationService);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sendEmail", async () => {
|
||||
it("should send an email notification with Up Template", async () => {
|
||||
describe("sendEmail", function() {
|
||||
it("should send an email notification with Up Template", async function() {
|
||||
const networkResponse = {
|
||||
monitor: {
|
||||
name: "Test Monitor",
|
||||
@@ -47,7 +49,8 @@ describe("NotificationService", () => {
|
||||
)
|
||||
);
|
||||
});
|
||||
it("should send an email notification with Down Template", async () => {
|
||||
|
||||
it("should send an email notification with Down Template", async function() {
|
||||
const networkResponse = {
|
||||
monitor: {
|
||||
name: "Test Monitor",
|
||||
@@ -60,7 +63,8 @@ describe("NotificationService", () => {
|
||||
await notificationService.sendEmail(networkResponse, address);
|
||||
expect(notificationService.emailService.buildAndSendEmail.calledOnce).to.be.true;
|
||||
});
|
||||
it("should send an email notification with Up Template", async () => {
|
||||
|
||||
it("should send an email notification with Up Template", async function() {
|
||||
const networkResponse = {
|
||||
monitor: {
|
||||
name: "Test Monitor",
|
||||
@@ -75,8 +79,8 @@ describe("NotificationService", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleNotifications", async () => {
|
||||
it("should handle notifications based on the network response", async () => {
|
||||
describe("handleNotifications", function() {
|
||||
it("should handle notifications based on the network response", async function() {
|
||||
notificationService.sendEmail = sinon.stub();
|
||||
const res = await notificationService.handleNotifications({
|
||||
monitor: {
|
||||
@@ -86,7 +90,8 @@ describe("NotificationService", () => {
|
||||
});
|
||||
expect(res).to.be.true;
|
||||
});
|
||||
it("should handle hardware notifications", async () => {
|
||||
|
||||
it("should handle hardware notifications", async function() {
|
||||
notificationService.sendEmail = sinon.stub();
|
||||
const res = await notificationService.handleNotifications({
|
||||
monitor: {
|
||||
@@ -97,7 +102,7 @@ describe("NotificationService", () => {
|
||||
expect(res).to.be.true;
|
||||
});
|
||||
|
||||
it("should handle an error when getting notifications", async () => {
|
||||
it("should handle an error when getting notifications", async function() {
|
||||
const testError = new Error("Test Error");
|
||||
notificationService.db.getNotificationsByMonitorId.rejects(testError);
|
||||
await notificationService.handleNotifications({ monitorId: "123" });
|
||||
@@ -105,9 +110,10 @@ describe("NotificationService", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("sendHardwareEmail", async () => {
|
||||
describe("sendHardwareEmail", function() {
|
||||
let networkResponse, address, alerts;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
networkResponse = {
|
||||
monitor: {
|
||||
name: "Test Monitor",
|
||||
@@ -120,10 +126,11 @@ describe("NotificationService", () => {
|
||||
alerts = ["test"];
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
it("should send an email notification with Hardware Template", async () => {
|
||||
|
||||
it("should send an email notification with Hardware Template", async function() {
|
||||
emailService.buildAndSendEmail.resolves(true);
|
||||
const res = await notificationService.sendHardwareEmail(
|
||||
networkResponse,
|
||||
@@ -132,7 +139,8 @@ describe("NotificationService", () => {
|
||||
);
|
||||
expect(res).to.be.true;
|
||||
});
|
||||
it("should return false if no alerts are provided", async () => {
|
||||
|
||||
it("should return false if no alerts are provided", async function() {
|
||||
alerts = [];
|
||||
emailService.buildAndSendEmail.resolves(true);
|
||||
const res = await notificationService.sendHardwareEmail(
|
||||
@@ -143,9 +151,11 @@ describe("NotificationService", () => {
|
||||
expect(res).to.be.false;
|
||||
});
|
||||
});
|
||||
describe("handleStatusNotifications", async () => {
|
||||
|
||||
describe("handleStatusNotifications", function() {
|
||||
let networkResponse;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
networkResponse = {
|
||||
monitor: {
|
||||
name: "Test Monitor",
|
||||
@@ -157,28 +167,31 @@ describe("NotificationService", () => {
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should handle status notifications", async () => {
|
||||
it("should handle status notifications", async function() {
|
||||
db.getNotificationsByMonitorId.resolves([
|
||||
{ type: "email", address: "test@test.com" },
|
||||
]);
|
||||
const res = await notificationService.handleStatusNotifications(networkResponse);
|
||||
expect(res).to.be.true;
|
||||
});
|
||||
it("should return false if status hasn't changed", async () => {
|
||||
|
||||
it("should return false if status hasn't changed", async function() {
|
||||
networkResponse.statusChanged = false;
|
||||
const res = await notificationService.handleStatusNotifications(networkResponse);
|
||||
expect(res).to.be.false;
|
||||
});
|
||||
it("should return false if prevStatus is undefined", async () => {
|
||||
|
||||
it("should return false if prevStatus is undefined", async function() {
|
||||
networkResponse.prevStatus = undefined;
|
||||
const res = await notificationService.handleStatusNotifications(networkResponse);
|
||||
expect(res).to.be.false;
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const testError = new Error("Test Error");
|
||||
db.getNotificationsByMonitorId.rejects(testError);
|
||||
try {
|
||||
@@ -190,9 +203,10 @@ describe("NotificationService", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleHardwareNotifications", async () => {
|
||||
describe("handleHardwareNotifications", function() {
|
||||
let networkResponse;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
networkResponse = {
|
||||
monitor: {
|
||||
name: "Test Monitor",
|
||||
@@ -225,26 +239,27 @@ describe("NotificationService", () => {
|
||||
},
|
||||
};
|
||||
});
|
||||
afterEach(() => {
|
||||
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("it should return false if no thresholds are set", () => {
|
||||
it("should return false if no thresholds are set", async () => {
|
||||
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);
|
||||
expect(res).to.be.false;
|
||||
});
|
||||
|
||||
it("should return false if metrics are null", async () => {
|
||||
it("should return false if metrics are null", async function() {
|
||||
networkResponse.payload.data = null;
|
||||
const res =
|
||||
await notificationService.handleHardwareNotifications(networkResponse);
|
||||
expect(res).to.be.false;
|
||||
});
|
||||
|
||||
it("should return true if request is well formed and thresholds > 0", async () => {
|
||||
it("should return true if request is well formed and thresholds > 0", async function() {
|
||||
db.getNotificationsByMonitorId.resolves([
|
||||
{
|
||||
type: "email",
|
||||
@@ -261,7 +276,7 @@ describe("NotificationService", () => {
|
||||
expect(res).to.be.true;
|
||||
});
|
||||
|
||||
it("should return true if thresholds are exceeded", async () => {
|
||||
it("should return true if thresholds are exceeded", async function() {
|
||||
db.getNotificationsByMonitorId.resolves([
|
||||
{
|
||||
type: "email",
|
||||
|
||||
@@ -4,8 +4,9 @@ import { expect } from "chai";
|
||||
import NetworkService from "../../service/networkService.js";
|
||||
const SERVICE_NAME = "SettingsService";
|
||||
|
||||
describe("SettingsService", () => {
|
||||
describe("SettingsService", function() {
|
||||
let sandbox, mockAppSettings;
|
||||
|
||||
beforeEach(function () {
|
||||
sandbox = sinon.createSandbox();
|
||||
sandbox.stub(process.env, "CLIENT_HOST").value("http://localhost");
|
||||
@@ -35,14 +36,16 @@ describe("SettingsService", () => {
|
||||
sandbox.restore();
|
||||
sinon.restore();
|
||||
});
|
||||
describe("constructor", () => {
|
||||
it("should construct a new SettingsService", () => {
|
||||
|
||||
describe("constructor", function() {
|
||||
it("should construct a new SettingsService", function() {
|
||||
const settingsService = new SettingsService(mockAppSettings);
|
||||
expect(settingsService.appSettings).to.equal(mockAppSettings);
|
||||
});
|
||||
});
|
||||
describe("loadSettings", () => {
|
||||
it("should load settings from DB when environment variables are not set", async () => {
|
||||
|
||||
describe("loadSettings", function() {
|
||||
it("should load settings from DB when environment variables are not set", async function() {
|
||||
const dbSettings = { logLevel: "debug", apiBaseUrl: "http://localhost" };
|
||||
const appSettings = { findOne: sinon.stub().returns(dbSettings) };
|
||||
const settingsService = new SettingsService(appSettings);
|
||||
@@ -50,6 +53,7 @@ describe("SettingsService", () => {
|
||||
const result = await settingsService.loadSettings();
|
||||
expect(result).to.deep.equal(dbSettings);
|
||||
});
|
||||
|
||||
it("should throw an error if settings are not found", async function () {
|
||||
const appSettings = { findOne: sinon.stub().returns(null) };
|
||||
const settingsService = new SettingsService(appSettings);
|
||||
@@ -64,7 +68,7 @@ describe("SettingsService", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should add its method and service name to error if not present", async () => {
|
||||
it("should add its method and service name to error if not present", async function() {
|
||||
const appSettings = { findOne: sinon.stub().throws(new Error("Test error")) };
|
||||
const settingsService = new SettingsService(appSettings);
|
||||
try {
|
||||
@@ -75,7 +79,8 @@ describe("SettingsService", () => {
|
||||
expect(error.method).to.equal("loadSettings");
|
||||
}
|
||||
});
|
||||
it("should not add its method and service name to error if present", async () => {
|
||||
|
||||
it("should not add its method and service name to error if present", async function() {
|
||||
const error = new Error("Test error");
|
||||
error.method = "otherMethod";
|
||||
error.service = "OTHER_SERVICE";
|
||||
@@ -89,6 +94,7 @@ describe("SettingsService", () => {
|
||||
expect(error.method).to.equal("otherMethod");
|
||||
}
|
||||
});
|
||||
|
||||
it("should merge DB settings with environment variables", async function () {
|
||||
const dbSettings = { logLevel: "debug", apiBaseUrl: "http://localhost" };
|
||||
const appSettings = { findOne: sinon.stub().returns(dbSettings) };
|
||||
@@ -99,8 +105,9 @@ describe("SettingsService", () => {
|
||||
expect(settingsService.settings.apiBaseUrl).to.equal("http://localhost");
|
||||
});
|
||||
});
|
||||
describe("reloadSettings", () => {
|
||||
it("should call loadSettings", async () => {
|
||||
|
||||
describe("reloadSettings", function() {
|
||||
it("should call loadSettings", async function() {
|
||||
const dbSettings = { logLevel: "debug", apiBaseUrl: "http://localhost" };
|
||||
const appSettings = { findOne: sinon.stub().returns(dbSettings) };
|
||||
const settingsService = new SettingsService(appSettings);
|
||||
@@ -109,8 +116,9 @@ describe("SettingsService", () => {
|
||||
expect(result).to.deep.equal(dbSettings);
|
||||
});
|
||||
});
|
||||
describe("getSettings", () => {
|
||||
it("should return the current settings", () => {
|
||||
|
||||
describe("getSettings", function() {
|
||||
it("should return the current settings", function() {
|
||||
const dbSettings = { logLevel: "debug", apiBaseUrl: "http://localhost" };
|
||||
const appSettings = { findOne: sinon.stub().returns(dbSettings) };
|
||||
const settingsService = new SettingsService(appSettings);
|
||||
@@ -118,7 +126,8 @@ describe("SettingsService", () => {
|
||||
const result = settingsService.getSettings();
|
||||
expect(result).to.deep.equal(dbSettings);
|
||||
});
|
||||
it("should throw an error if settings have not been loaded", () => {
|
||||
|
||||
it("should throw an error if settings have not been loaded", function() {
|
||||
const appSettings = { findOne: sinon.stub().returns(null) };
|
||||
const settingsService = new SettingsService(appSettings);
|
||||
settingsService.settings = null;
|
||||
|
||||
@@ -4,7 +4,8 @@ import { afterEach, describe } from "node:test";
|
||||
|
||||
describe("StatusService", () => {
|
||||
let db, logger, statusService;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
db = {
|
||||
getMonitorById: sinon.stub(),
|
||||
createCheck: sinon.stub(),
|
||||
@@ -22,31 +23,35 @@ describe("StatusService", () => {
|
||||
});
|
||||
|
||||
describe("constructor", () => {
|
||||
it("should create an instance of StatusService", () => {
|
||||
it("should create an instance of StatusService", function() {
|
||||
expect(statusService).to.be.an.instanceOf(StatusService);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getStatusString", () => {
|
||||
it("should return 'up' if status is true", () => {
|
||||
it("should return 'up' if status is true", function() {
|
||||
expect(statusService.getStatusString(true)).to.equal("up");
|
||||
});
|
||||
it("should return 'down' if status is false", () => {
|
||||
|
||||
it("should return 'down' if status is false", function() {
|
||||
expect(statusService.getStatusString(false)).to.equal("down");
|
||||
});
|
||||
it("should return 'unknown' if status is undefined or null", () => {
|
||||
|
||||
it("should return 'unknown' if status is undefined or null", function() {
|
||||
expect(statusService.getStatusString(undefined)).to.equal("unknown");
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateStatus", async () => {
|
||||
beforeEach(() => {
|
||||
describe("updateStatus", () => {
|
||||
beforeEach(function() {
|
||||
// statusService.insertCheck = sinon.stub().resolves;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
sinon.restore();
|
||||
});
|
||||
it("should throw an error if an error occurs", async () => {
|
||||
|
||||
it("should throw an error if an error occurs", async function() {
|
||||
const error = new Error("Test error");
|
||||
statusService.db.getMonitorById = sinon.stub().throws(error);
|
||||
try {
|
||||
@@ -56,7 +61,8 @@ describe("StatusService", () => {
|
||||
}
|
||||
// expect(statusService.insertCheck.calledOnce).to.be.true;
|
||||
});
|
||||
it("should return {statusChanged: false} if status hasn't changed", async () => {
|
||||
|
||||
it("should return {statusChanged: false} if status hasn't changed", async function() {
|
||||
statusService.db.getMonitorById = sinon.stub().returns({ status: true });
|
||||
const result = await statusService.updateStatus({
|
||||
monitorId: "test",
|
||||
@@ -65,7 +71,8 @@ describe("StatusService", () => {
|
||||
expect(result).to.deep.equal({ statusChanged: false });
|
||||
// expect(statusService.insertCheck.calledOnce).to.be.true;
|
||||
});
|
||||
it("should return {statusChanged: true} if status has changed from down to up", async () => {
|
||||
|
||||
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() });
|
||||
@@ -78,7 +85,8 @@ describe("StatusService", () => {
|
||||
expect(result.prevStatus).to.be.false;
|
||||
// expect(statusService.insertCheck.calledOnce).to.be.true;
|
||||
});
|
||||
it("should return {statusChanged: true} if status has changed from up to down", async () => {
|
||||
|
||||
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() });
|
||||
@@ -94,7 +102,7 @@ describe("StatusService", () => {
|
||||
});
|
||||
|
||||
describe("buildCheck", () => {
|
||||
it("should build a check object", () => {
|
||||
it("should build a check object", function() {
|
||||
const check = statusService.buildCheck({
|
||||
monitorId: "test",
|
||||
type: "test",
|
||||
@@ -111,7 +119,7 @@ describe("StatusService", () => {
|
||||
expect(check.message).to.equal("Test message");
|
||||
});
|
||||
|
||||
it("should build a check object for pagespeed type", () => {
|
||||
it("should build a check object for pagespeed type", function() {
|
||||
const check = statusService.buildCheck({
|
||||
monitorId: "test",
|
||||
type: "pagespeed",
|
||||
@@ -154,7 +162,8 @@ describe("StatusService", () => {
|
||||
tbt: { score: 1 },
|
||||
});
|
||||
});
|
||||
it("should build a check object for pagespeed type with missing data", () => {
|
||||
|
||||
it("should build a check object for pagespeed type with missing data", function() {
|
||||
const check = statusService.buildCheck({
|
||||
monitorId: "test",
|
||||
type: "pagespeed",
|
||||
@@ -186,7 +195,8 @@ describe("StatusService", () => {
|
||||
tbt: 0,
|
||||
});
|
||||
});
|
||||
it("should build a check for hardware type", () => {
|
||||
|
||||
it("should build a check for hardware type", function() {
|
||||
const check = statusService.buildCheck({
|
||||
monitorId: "test",
|
||||
type: "hardware",
|
||||
@@ -206,7 +216,8 @@ describe("StatusService", () => {
|
||||
expect(check.disk).to.equal("disk");
|
||||
expect(check.host).to.equal("host");
|
||||
});
|
||||
it("should build a check for hardware type with missing data", () => {
|
||||
|
||||
it("should build a check for hardware type with missing data", function() {
|
||||
const check = statusService.buildCheck({
|
||||
monitorId: "test",
|
||||
type: "hardware",
|
||||
@@ -227,8 +238,9 @@ describe("StatusService", () => {
|
||||
expect(check.host).to.deep.equal({});
|
||||
});
|
||||
});
|
||||
|
||||
describe("insertCheck", () => {
|
||||
it("should log an error if one is thrown", async () => {
|
||||
it("should log an error if one is thrown", async function() {
|
||||
const testError = new Error("Test error");
|
||||
statusService.db.createCheck = sinon.stub().throws(testError);
|
||||
try {
|
||||
@@ -238,7 +250,8 @@ describe("StatusService", () => {
|
||||
}
|
||||
expect(statusService.logger.error.calledOnce).to.be.true;
|
||||
});
|
||||
it("should insert a check into the database", async () => {
|
||||
|
||||
it("should insert a check into the database", async function() {
|
||||
await statusService.insertCheck({ monitorId: "test", type: "http" });
|
||||
expect(statusService.db.createCheck.calledOnce).to.be.true;
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user