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
@@ -13,8 +13,8 @@ import Monitor from "../../db/models/Monitor.js";
|
||||
import User from "../../db/models/User.js";
|
||||
import logger from "../../utils/logger.js";
|
||||
|
||||
describe("checkModule", () => {
|
||||
describe("createCheck", () => {
|
||||
describe("checkModule", function() {
|
||||
describe("createCheck", function() {
|
||||
let checkCountDocumentsStub, checkSaveStub, monitorFindByIdStub, monitorSaveStub;
|
||||
const mockMonitor = {
|
||||
_id: "123",
|
||||
@@ -23,31 +23,33 @@ describe("checkModule", () => {
|
||||
save: () => this,
|
||||
};
|
||||
const mockCheck = { active: true };
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
checkSaveStub = sinon.stub(Check.prototype, "save");
|
||||
checkCountDocumentsStub = sinon.stub(Check, "countDocuments");
|
||||
monitorFindByIdStub = sinon.stub(Monitor, "findById");
|
||||
monitorSaveStub = sinon.stub(Monitor.prototype, "save");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return undefined early if no monitor is found", async () => {
|
||||
it("should return undefined early if no monitor is found", async function() {
|
||||
monitorFindByIdStub.returns(null);
|
||||
const check = await createCheck({ monitorId: "123" });
|
||||
expect(check).to.be.undefined;
|
||||
});
|
||||
|
||||
it("should return a check", async () => {
|
||||
it("should return a check", async function() {
|
||||
monitorFindByIdStub.returns(mockMonitor);
|
||||
checkSaveStub.returns(mockCheck);
|
||||
monitorSaveStub.returns(mockMonitor);
|
||||
const check = await createCheck({ monitorId: "123", status: true });
|
||||
expect(check).to.deep.equal(mockCheck);
|
||||
});
|
||||
it("should return a check if status is down", async () => {
|
||||
|
||||
it("should return a check if status is down", async function() {
|
||||
mockMonitor.status = false;
|
||||
monitorFindByIdStub.returns(mockMonitor);
|
||||
checkSaveStub.returns(mockCheck);
|
||||
@@ -55,7 +57,8 @@ describe("checkModule", () => {
|
||||
const check = await createCheck({ monitorId: "123", status: false });
|
||||
expect(check).to.deep.equal(mockCheck);
|
||||
});
|
||||
it("should return a check if uptimePercentage is undefined", async () => {
|
||||
|
||||
it("should return a check if uptimePercentage is undefined", async function() {
|
||||
mockMonitor.uptimePercentage = undefined;
|
||||
monitorFindByIdStub.returns(mockMonitor);
|
||||
checkSaveStub.returns(mockCheck);
|
||||
@@ -63,7 +66,8 @@ describe("checkModule", () => {
|
||||
const check = await createCheck({ monitorId: "123", status: true });
|
||||
expect(check).to.deep.equal(mockCheck);
|
||||
});
|
||||
it("should return a check if uptimePercentage is undefined and status is down", async () => {
|
||||
|
||||
it("should return a check if uptimePercentage is undefined and status is down", async function() {
|
||||
mockMonitor.uptimePercentage = undefined;
|
||||
monitorFindByIdStub.returns(mockMonitor);
|
||||
checkSaveStub.returns(mockCheck);
|
||||
@@ -71,7 +75,8 @@ describe("checkModule", () => {
|
||||
const check = await createCheck({ monitorId: "123", status: false });
|
||||
expect(check).to.deep.equal(mockCheck);
|
||||
});
|
||||
it("should monitor save error", async () => {
|
||||
|
||||
it("should monitor save error", async function() {
|
||||
const err = new Error("Save Error");
|
||||
monitorSaveStub.throws(err);
|
||||
try {
|
||||
@@ -80,7 +85,8 @@ describe("checkModule", () => {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
it("should handle errors", async () => {
|
||||
|
||||
it("should handle errors", async function() {
|
||||
const err = new Error("DB Error");
|
||||
checkCountDocumentsStub.throws(err);
|
||||
try {
|
||||
@@ -90,18 +96,19 @@ describe("checkModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("getChecksCount", () => {
|
||||
|
||||
describe("getChecksCount", function() {
|
||||
let checkCountDocumentStub;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
checkCountDocumentStub = sinon.stub(Check, "countDocuments");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
checkCountDocumentStub.restore();
|
||||
});
|
||||
|
||||
it("should return count with basic monitorId query", async () => {
|
||||
it("should return count with basic monitorId query", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: {},
|
||||
@@ -117,7 +124,7 @@ describe("checkModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should include dateRange in query when provided", async () => {
|
||||
it("should include dateRange in query when provided", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { dateRange: "day" },
|
||||
@@ -131,7 +138,7 @@ describe("checkModule", () => {
|
||||
expect(checkCountDocumentStub.firstCall.args[0].createdAt).to.have.property("$gte");
|
||||
});
|
||||
|
||||
it('should handle "all" filter correctly', async () => {
|
||||
it('should handle "all" filter correctly', async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "all" },
|
||||
@@ -147,7 +154,7 @@ describe("checkModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle "down" filter correctly', async () => {
|
||||
it('should handle "down" filter correctly', async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "down" },
|
||||
@@ -163,7 +170,7 @@ describe("checkModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle "resolve" filter correctly', async () => {
|
||||
it('should handle "resolve" filter correctly', async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "resolve" },
|
||||
@@ -179,7 +186,8 @@ describe("checkModule", () => {
|
||||
statusCode: 5000,
|
||||
});
|
||||
});
|
||||
it("should handle unknown filter correctly", async () => {
|
||||
|
||||
it("should handle unknown filter correctly", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "unknown" },
|
||||
@@ -195,7 +203,7 @@ describe("checkModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should combine dateRange and filter in query", async () => {
|
||||
it("should combine dateRange and filter in query", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: {
|
||||
@@ -215,10 +223,11 @@ describe("checkModule", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
describe("getChecks", () => {
|
||||
|
||||
describe("getChecks", function() {
|
||||
let checkFindStub, monitorFindStub;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
checkFindStub = sinon.stub(Check, "find").returns({
|
||||
skip: sinon.stub().returns({
|
||||
limit: sinon.stub().returns({
|
||||
@@ -228,11 +237,11 @@ describe("checkModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return checks with basic monitorId query", async () => {
|
||||
it("should return checks with basic monitorId query", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: {},
|
||||
@@ -242,7 +251,8 @@ describe("checkModule", () => {
|
||||
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
it("should return checks with limit query", async () => {
|
||||
|
||||
it("should return checks with limit query", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { limit: 10 },
|
||||
@@ -253,7 +263,7 @@ describe("checkModule", () => {
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it("should handle pagination correctly", async () => {
|
||||
it("should handle pagination correctly", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: {
|
||||
@@ -267,7 +277,7 @@ describe("checkModule", () => {
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it("should handle dateRange filter", async () => {
|
||||
it("should handle dateRange filter", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { dateRange: "week" },
|
||||
@@ -277,7 +287,7 @@ describe("checkModule", () => {
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it('should handle "all" filter', async () => {
|
||||
it('should handle "all" filter', async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "all" },
|
||||
@@ -287,7 +297,8 @@ describe("checkModule", () => {
|
||||
const result = await getChecks(req);
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
it('should handle "down" filter', async () => {
|
||||
|
||||
it('should handle "down" filter', async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "down" },
|
||||
@@ -298,7 +309,7 @@ describe("checkModule", () => {
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it('should handle "resolve" filter', async () => {
|
||||
it('should handle "resolve" filter', async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "resolve" },
|
||||
@@ -308,7 +319,8 @@ describe("checkModule", () => {
|
||||
const result = await getChecks(req);
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
it('should handle "unknown" filter', async () => {
|
||||
|
||||
it('should handle "unknown" filter', async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "unknown" },
|
||||
@@ -319,7 +331,7 @@ describe("checkModule", () => {
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it("should handle ascending sort order", async () => {
|
||||
it("should handle ascending sort order", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { sortOrder: "asc" },
|
||||
@@ -330,7 +342,7 @@ describe("checkModule", () => {
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it("should handle error case", async () => {
|
||||
it("should handle error case", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: {},
|
||||
@@ -347,10 +359,12 @@ describe("checkModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("getTeamChecks", () => {
|
||||
|
||||
describe("getTeamChecks", function() {
|
||||
let checkFindStub, checkCountDocumentsStub, monitorFindStub;
|
||||
const mockMonitors = [{ _id: "123" }];
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
monitorFindStub = sinon.stub(Monitor, "find").returns({
|
||||
select: sinon.stub().returns(mockMonitors),
|
||||
});
|
||||
@@ -366,11 +380,11 @@ describe("checkModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return checks with basic monitorId query", async () => {
|
||||
it("should return checks with basic monitorId query", async function() {
|
||||
const req = {
|
||||
params: { teamId: "test123" },
|
||||
query: {},
|
||||
@@ -380,7 +394,7 @@ describe("checkModule", () => {
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it("should handle pagination correctly", async () => {
|
||||
it("should handle pagination correctly", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { limit: 1, page: 2, rowsPerPage: 10 },
|
||||
@@ -390,7 +404,7 @@ describe("checkModule", () => {
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it("should handle dateRange filter", async () => {
|
||||
it("should handle dateRange filter", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { dateRange: "week" },
|
||||
@@ -399,7 +413,7 @@ describe("checkModule", () => {
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it('should handle "all" filter', async () => {
|
||||
it('should handle "all" filter', async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "all" },
|
||||
@@ -409,7 +423,8 @@ describe("checkModule", () => {
|
||||
const result = await getTeamChecks(req);
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
it('should handle "down" filter', async () => {
|
||||
|
||||
it('should handle "down" filter', async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "down" },
|
||||
@@ -420,7 +435,7 @@ describe("checkModule", () => {
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it('should handle "resolve" filter', async () => {
|
||||
it('should handle "resolve" filter', async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "resolve" },
|
||||
@@ -430,7 +445,8 @@ describe("checkModule", () => {
|
||||
const result = await getTeamChecks(req);
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
it('should handle "unknown" filter', async () => {
|
||||
|
||||
it('should handle "unknown" filter', async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "unknown" },
|
||||
@@ -441,7 +457,7 @@ describe("checkModule", () => {
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it("should handle ascending sort order", async () => {
|
||||
it("should handle ascending sort order", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { sortOrder: "asc" },
|
||||
@@ -452,7 +468,7 @@ describe("checkModule", () => {
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it("should handle error case", async () => {
|
||||
it("should handle error case", async function() {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: {},
|
||||
@@ -469,20 +485,24 @@ describe("checkModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("deleteChecks", () => {
|
||||
|
||||
describe("deleteChecks", function() {
|
||||
let checkDeleteManyStub;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
checkDeleteManyStub = sinon.stub(Check, "deleteMany").resolves({ deletedCount: 1 });
|
||||
});
|
||||
afterEach(() => {
|
||||
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return a value if a check is deleted", async () => {
|
||||
it("should return a value if a check is deleted", async function() {
|
||||
const result = await deleteChecks("123");
|
||||
expect(result).to.equal(1);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
checkDeleteManyStub.throws(new Error("Database error"));
|
||||
try {
|
||||
await deleteChecks("123");
|
||||
@@ -492,22 +512,27 @@ describe("checkModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("deleteChecksByTeamId", () => {
|
||||
|
||||
describe("deleteChecksByTeamId", function() {
|
||||
let mockMonitors = [{ _id: 123, save: () => this }];
|
||||
let monitorFindStub, monitorSaveStub, checkDeleteManyStub;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
monitorSaveStub = sinon.stub(Monitor.prototype, "save");
|
||||
monitorFindStub = sinon.stub(Monitor, "find").returns(mockMonitors);
|
||||
checkDeleteManyStub = sinon.stub(Check, "deleteMany").resolves({ deletedCount: 1 });
|
||||
});
|
||||
afterEach(() => {
|
||||
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
it("should return a deleted count", async () => {
|
||||
|
||||
it("should return a deleted count", async function() {
|
||||
const result = await deleteChecksByTeamId("123");
|
||||
expect(result).to.equal(1);
|
||||
});
|
||||
it("should handle errors", async () => {
|
||||
|
||||
it("should handle errors", async function() {
|
||||
const err = new Error("DB Error");
|
||||
monitorFindStub.throws(err);
|
||||
try {
|
||||
@@ -517,25 +542,27 @@ describe("checkModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("updateChecksTTL", () => {
|
||||
|
||||
describe("updateChecksTTL", function() {
|
||||
let userUpdateManyStub;
|
||||
let loggerStub;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
loggerStub = sinon.stub(logger, "error");
|
||||
userUpdateManyStub = sinon.stub(User, "updateMany");
|
||||
Check.collection = { dropIndex: sinon.stub(), createIndex: sinon.stub() };
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return undefined", async () => {
|
||||
it("should return undefined", async function() {
|
||||
const result = await updateChecksTTL("123", 10);
|
||||
expect(result).to.be.undefined;
|
||||
});
|
||||
|
||||
it("should log an error if dropIndex throws an error", async () => {
|
||||
it("should log an error if dropIndex throws an error", async function() {
|
||||
const err = new Error("Drop Index Error");
|
||||
Check.collection.dropIndex.throws(err);
|
||||
await updateChecksTTL("123", 10);
|
||||
@@ -543,7 +570,7 @@ describe("checkModule", () => {
|
||||
expect(loggerStub.firstCall.args[0].message).to.equal(err.message);
|
||||
});
|
||||
|
||||
it("should throw an error if createIndex throws an error", async () => {
|
||||
it("should throw an error if createIndex throws an error", async function() {
|
||||
const err = new Error("Create Index Error");
|
||||
Check.collection.createIndex.throws(err);
|
||||
try {
|
||||
@@ -552,7 +579,8 @@ describe("checkModule", () => {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
it("should throw an error if User.updateMany throws an error", async () => {
|
||||
|
||||
it("should throw an error if User.updateMany throws an error", async function() {
|
||||
const err = new Error("Update Many Error");
|
||||
userUpdateManyStub.throws(err);
|
||||
try {
|
||||
|
||||
@@ -51,24 +51,25 @@ const mockMonitor = {
|
||||
save: () => this,
|
||||
};
|
||||
|
||||
describe("HardwareCheckModule", () => {
|
||||
describe("HardwareCheckModule", function() {
|
||||
let hardwareCheckSaveStub,
|
||||
hardwareCheckCountDocumentsStub,
|
||||
monitorFindByIdStub,
|
||||
loggerStub;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
loggerStub = sinon.stub(logger, "error");
|
||||
hardwareCheckSaveStub = sinon.stub(HardwareCheck.prototype, "save");
|
||||
monitorFindByIdStub = sinon.stub(Monitor, "findById");
|
||||
hardwareCheckCountDocumentsStub = sinon.stub(HardwareCheck, "countDocuments");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("createHardwareCheck", () => {
|
||||
it("should return a hardware check", async () => {
|
||||
describe("createHardwareCheck", function() {
|
||||
it("should return a hardware check", async function() {
|
||||
hardwareCheckSaveStub.resolves(mockHardwareCheck);
|
||||
monitorFindByIdStub.resolves(mockMonitor);
|
||||
hardwareCheckCountDocumentsStub.resolves(1);
|
||||
@@ -76,7 +77,8 @@ describe("HardwareCheckModule", () => {
|
||||
expect(hardwareCheck).to.exist;
|
||||
expect(hardwareCheck).to.deep.equal(mockHardwareCheck);
|
||||
});
|
||||
it("should return a hardware check for a check with status false", async () => {
|
||||
|
||||
it("should return a hardware check for a check with status false", async function() {
|
||||
hardwareCheckSaveStub.resolves(mockHardwareCheck);
|
||||
monitorFindByIdStub.resolves(mockMonitor);
|
||||
hardwareCheckCountDocumentsStub.resolves(1);
|
||||
@@ -84,7 +86,8 @@ describe("HardwareCheckModule", () => {
|
||||
expect(hardwareCheck).to.exist;
|
||||
expect(hardwareCheck).to.deep.equal(mockHardwareCheck);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
monitorFindByIdStub.resolves(mockMonitor);
|
||||
hardwareCheckSaveStub.rejects(err);
|
||||
@@ -95,20 +98,23 @@ describe("HardwareCheckModule", () => {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
it("should log an error if a monitor is not found", async () => {
|
||||
|
||||
it("should log an error if a monitor is not found", async function() {
|
||||
monitorFindByIdStub.resolves(null);
|
||||
const res = await createHardwareCheck({});
|
||||
expect(loggerStub.calledOnce).to.be.true;
|
||||
expect(res).to.be.null;
|
||||
});
|
||||
it("should handle a monitor with undefined uptimePercentage", async () => {
|
||||
|
||||
it("should handle a monitor with undefined uptimePercentage", async function() {
|
||||
monitorFindByIdStub.resolves({ ...mockMonitor, uptimePercentage: undefined });
|
||||
hardwareCheckSaveStub.resolves(mockHardwareCheck);
|
||||
hardwareCheckCountDocumentsStub.resolves(1);
|
||||
const res = await createHardwareCheck({});
|
||||
expect(res).to.exist;
|
||||
});
|
||||
it("should handle a monitor with undefined uptimePercentage and true status", async () => {
|
||||
|
||||
it("should handle a monitor with undefined uptimePercentage and true status", async function() {
|
||||
monitorFindByIdStub.resolves({
|
||||
...mockMonitor,
|
||||
uptimePercentage: undefined,
|
||||
@@ -118,7 +124,8 @@ describe("HardwareCheckModule", () => {
|
||||
const res = await createHardwareCheck({ status: true });
|
||||
expect(res).to.exist;
|
||||
});
|
||||
it("should handle a monitor with undefined uptimePercentage and false status", async () => {
|
||||
|
||||
it("should handle a monitor with undefined uptimePercentage and false status", async function() {
|
||||
monitorFindByIdStub.resolves({
|
||||
...mockMonitor,
|
||||
uptimePercentage: undefined,
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
} from "../../db/mongo/modules/inviteModule.js";
|
||||
import { errorMessages } from "../../utils/messages.js";
|
||||
|
||||
describe("Invite Module", () => {
|
||||
describe("Invite Module", function() {
|
||||
const mockUserData = {
|
||||
email: "test@test.com",
|
||||
teamId: "123",
|
||||
@@ -19,18 +19,20 @@ describe("Invite Module", () => {
|
||||
inviteTokenSaveStub,
|
||||
inviteTokenFindOneStub,
|
||||
inviteTokenFindOneAndDeleteStub;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
inviteTokenDeleteManyStub = sinon.stub(InviteToken, "deleteMany");
|
||||
inviteTokenSaveStub = sinon.stub(InviteToken.prototype, "save");
|
||||
inviteTokenFindOneStub = sinon.stub(InviteToken, "findOne");
|
||||
inviteTokenFindOneAndDeleteStub = sinon.stub(InviteToken, "findOneAndDelete");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
describe("requestInviteToken", () => {
|
||||
it("should return a new invite token", async () => {
|
||||
|
||||
describe("requestInviteToken", function() {
|
||||
it("should return a new invite token", async function() {
|
||||
inviteTokenDeleteManyStub.resolves();
|
||||
inviteTokenSaveStub.resolves();
|
||||
const inviteToken = await requestInviteToken(mockUserData);
|
||||
@@ -39,7 +41,7 @@ describe("Invite Module", () => {
|
||||
expect(inviteToken.token).to.exist;
|
||||
});
|
||||
|
||||
it("should handle an error", async () => {
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
inviteTokenDeleteManyStub.rejects(err);
|
||||
try {
|
||||
@@ -50,14 +52,14 @@ describe("Invite Module", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getInviteToken", () => {
|
||||
it("should return an invite token", async () => {
|
||||
describe("getInviteToken", function() {
|
||||
it("should return an invite token", async function() {
|
||||
inviteTokenFindOneStub.resolves(mockInviteToken);
|
||||
const inviteToken = await getInviteToken(mockUserData.token);
|
||||
expect(inviteToken).to.deep.equal(mockInviteToken);
|
||||
});
|
||||
|
||||
it("should handle a token not found", async () => {
|
||||
it("should handle a token not found", async function() {
|
||||
inviteTokenFindOneStub.resolves(null);
|
||||
try {
|
||||
await getInviteToken(mockUserData.token);
|
||||
@@ -66,7 +68,7 @@ describe("Invite Module", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle DB errors", async () => {
|
||||
it("should handle DB errors", async function() {
|
||||
const err = new Error("test error");
|
||||
inviteTokenFindOneStub.rejects(err);
|
||||
try {
|
||||
@@ -78,14 +80,14 @@ describe("Invite Module", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getInviteTokenAndDelete", () => {
|
||||
it("should return a deleted invite", async () => {
|
||||
describe("getInviteTokenAndDelete", function() {
|
||||
it("should return a deleted invite", async function() {
|
||||
inviteTokenFindOneAndDeleteStub.resolves(mockInviteToken);
|
||||
const deletedInvite = await getInviteTokenAndDelete(mockUserData.token);
|
||||
expect(deletedInvite).to.deep.equal(mockInviteToken);
|
||||
});
|
||||
|
||||
it("should handle a token not found", async () => {
|
||||
it("should handle a token not found", async function() {
|
||||
inviteTokenFindOneAndDeleteStub.resolves(null);
|
||||
try {
|
||||
await getInviteTokenAndDelete(mockUserData.token);
|
||||
@@ -94,7 +96,7 @@ describe("Invite Module", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle DB errors", async () => {
|
||||
it("should handle DB errors", async function() {
|
||||
const err = new Error("test error");
|
||||
inviteTokenFindOneAndDeleteStub.rejects(err);
|
||||
try {
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
editMaintenanceWindowById,
|
||||
} from "../../db/mongo/modules/maintenanceWindowModule.js";
|
||||
|
||||
describe("MaintenanceWindow Module", () => {
|
||||
describe("MaintenanceWindow Module", function() {
|
||||
const mockMaintenanceWindow = {
|
||||
monitorId: "123",
|
||||
active: true,
|
||||
@@ -29,7 +29,7 @@ describe("MaintenanceWindow Module", () => {
|
||||
maintenanceWindowDeleteManyStub,
|
||||
maintenanceWindowFindByIdAndUpdateStub;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
maintenanceWindowSaveStub = sinon.stub(MaintenanceWindow.prototype, "save");
|
||||
maintenanceWindowFindByIdStub = sinon.stub(MaintenanceWindow, "findById");
|
||||
maintenanceWindowCountDocumentsStub = sinon.stub(MaintenanceWindow, "countDocuments");
|
||||
@@ -51,18 +51,18 @@ describe("MaintenanceWindow Module", () => {
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("createMaintenanceWindow", () => {
|
||||
it("should save a new maintenance window", async () => {
|
||||
describe("createMaintenanceWindow", function() {
|
||||
it("should save a new maintenance window", async function() {
|
||||
maintenanceWindowSaveStub.resolves(mockMaintenanceWindow);
|
||||
const result = await createMaintenanceWindow(mockMaintenanceWindow);
|
||||
expect(result).to.deep.equal(mockMaintenanceWindow);
|
||||
});
|
||||
|
||||
it("should handle an error", async () => {
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowSaveStub.rejects(err);
|
||||
try {
|
||||
@@ -72,13 +72,15 @@ describe("MaintenanceWindow Module", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("getMaintenanceWindowById", () => {
|
||||
it("should return a maintenance window", async () => {
|
||||
|
||||
describe("getMaintenanceWindowById", function() {
|
||||
it("should return a maintenance window", async function() {
|
||||
maintenanceWindowFindByIdStub.resolves(mockMaintenanceWindow);
|
||||
const result = await getMaintenanceWindowById(mockMaintenanceWindow.id);
|
||||
expect(result).to.deep.equal(mockMaintenanceWindow);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowFindByIdStub.rejects(err);
|
||||
try {
|
||||
@@ -89,9 +91,10 @@ describe("MaintenanceWindow Module", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getMaintenanceWindowsByTeamId", () => {
|
||||
describe("getMaintenanceWindowsByTeamId", function() {
|
||||
let query;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
query = {
|
||||
active: true,
|
||||
page: 1,
|
||||
@@ -101,11 +104,11 @@ describe("MaintenanceWindow Module", () => {
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return a list of maintenance windows and count", async () => {
|
||||
it("should return a list of maintenance windows and count", async function() {
|
||||
maintenanceWindowCountDocumentsStub.resolves(1);
|
||||
const result = await getMaintenanceWindowsByTeamId(
|
||||
mockMaintenanceWindow.teamId,
|
||||
@@ -116,7 +119,8 @@ describe("MaintenanceWindow Module", () => {
|
||||
maintenanceWindowCount: 1,
|
||||
});
|
||||
});
|
||||
it("should return a list of maintenance windows and count with empty query", async () => {
|
||||
|
||||
it("should return a list of maintenance windows and count with empty query", async function() {
|
||||
query = undefined;
|
||||
maintenanceWindowCountDocumentsStub.resolves(1);
|
||||
const result = await getMaintenanceWindowsByTeamId(
|
||||
@@ -128,7 +132,8 @@ describe("MaintenanceWindow Module", () => {
|
||||
maintenanceWindowCount: 1,
|
||||
});
|
||||
});
|
||||
it("should return a list of maintenance windows and count with no pagination provided", async () => {
|
||||
|
||||
it("should return a list of maintenance windows and count with no pagination provided", async function() {
|
||||
query.page = undefined;
|
||||
query.rowsPerPage = undefined;
|
||||
maintenanceWindowCountDocumentsStub.resolves(1);
|
||||
@@ -141,7 +146,8 @@ describe("MaintenanceWindow Module", () => {
|
||||
maintenanceWindowCount: 1,
|
||||
});
|
||||
});
|
||||
it("should return a list of maintenance windows and count with field and desc order", async () => {
|
||||
|
||||
it("should return a list of maintenance windows and count with field and desc order", async function() {
|
||||
query.order = "desc";
|
||||
maintenanceWindowCountDocumentsStub.resolves(1);
|
||||
const result = await getMaintenanceWindowsByTeamId(
|
||||
@@ -153,7 +159,8 @@ describe("MaintenanceWindow Module", () => {
|
||||
maintenanceWindowCount: 1,
|
||||
});
|
||||
});
|
||||
it("should return a list of maintenance windows and count no field", async () => {
|
||||
|
||||
it("should return a list of maintenance windows and count no field", async function() {
|
||||
query.field = undefined;
|
||||
maintenanceWindowCountDocumentsStub.resolves(1);
|
||||
const result = await getMaintenanceWindowsByTeamId(
|
||||
@@ -165,7 +172,8 @@ describe("MaintenanceWindow Module", () => {
|
||||
maintenanceWindowCount: 1,
|
||||
});
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowCountDocumentsStub.rejects(err);
|
||||
try {
|
||||
@@ -175,15 +183,17 @@ describe("MaintenanceWindow Module", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("getMaintenanceWindowsByMonitorId", () => {
|
||||
it("should return a list of maintenance windows", async () => {
|
||||
|
||||
describe("getMaintenanceWindowsByMonitorId", function() {
|
||||
it("should return a list of maintenance windows", async function() {
|
||||
maintenanceWindowFindStub.resolves(mockMaintenanceWindows);
|
||||
const result = await getMaintenanceWindowsByMonitorId(
|
||||
mockMaintenanceWindow.monitorId
|
||||
);
|
||||
expect(result).to.deep.equal(mockMaintenanceWindows);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowFindStub.rejects(err);
|
||||
try {
|
||||
@@ -193,13 +203,15 @@ describe("MaintenanceWindow Module", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("deleteMaintenanceWindowById", () => {
|
||||
it("should delete a maintenance window", async () => {
|
||||
|
||||
describe("deleteMaintenanceWindowById", function() {
|
||||
it("should delete a maintenance window", async function() {
|
||||
maintenanceWindowFindByIdAndDeleteStub.resolves(mockMaintenanceWindow);
|
||||
const result = await deleteMaintenanceWindowById(mockMaintenanceWindow.id);
|
||||
expect(result).to.deep.equal(mockMaintenanceWindow);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowFindByIdAndDeleteStub.rejects(err);
|
||||
try {
|
||||
@@ -210,15 +222,16 @@ describe("MaintenanceWindow Module", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteMaintenanceWindowByMonitorId", () => {
|
||||
it("should return the number of documents deleted", async () => {
|
||||
describe("deleteMaintenanceWindowByMonitorId", function() {
|
||||
it("should return the number of documents deleted", async function() {
|
||||
maintenanceWindowDeleteManyStub.resolves({ deletedCount: 1 });
|
||||
const result = await deleteMaintenanceWindowByMonitorId(
|
||||
mockMaintenanceWindow.monitorId
|
||||
);
|
||||
expect(result).to.deep.equal({ deletedCount: 1 });
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowDeleteManyStub.rejects(err);
|
||||
try {
|
||||
@@ -229,13 +242,14 @@ describe("MaintenanceWindow Module", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteMaintenanceWindowByUserId", () => {
|
||||
it("should return the number of documents deleted", async () => {
|
||||
describe("deleteMaintenanceWindowByUserId", function() {
|
||||
it("should return the number of documents deleted", async function() {
|
||||
maintenanceWindowDeleteManyStub.resolves({ deletedCount: 1 });
|
||||
const result = await deleteMaintenanceWindowByUserId(mockMaintenanceWindow.userId);
|
||||
expect(result).to.deep.equal({ deletedCount: 1 });
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowDeleteManyStub.rejects(err);
|
||||
try {
|
||||
@@ -245,8 +259,9 @@ describe("MaintenanceWindow Module", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("editMaintenanceWindowById", () => {
|
||||
it("should return the updated maintenance window", async () => {
|
||||
|
||||
describe("editMaintenanceWindowById", function() {
|
||||
it("should return the updated maintenance window", async function() {
|
||||
maintenanceWindowFindByIdAndUpdateStub.resolves(mockMaintenanceWindow);
|
||||
const result = await editMaintenanceWindowById(
|
||||
mockMaintenanceWindow.id,
|
||||
@@ -254,7 +269,8 @@ describe("MaintenanceWindow Module", () => {
|
||||
);
|
||||
expect(result).to.deep.equal(mockMaintenanceWindow);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowFindByIdAndUpdateStub.rejects(err);
|
||||
try {
|
||||
|
||||
@@ -31,7 +31,7 @@ import {
|
||||
calculateGroupStats,
|
||||
} from "../../db/mongo/modules/monitorModule.js";
|
||||
|
||||
describe("monitorModule", () => {
|
||||
describe("monitorModule", function() {
|
||||
let monitorFindStub,
|
||||
monitorFindByIdStub,
|
||||
monitorFindByIdAndUpdateStub,
|
||||
@@ -42,7 +42,8 @@ describe("monitorModule", () => {
|
||||
checkFindStub,
|
||||
pageSpeedCheckFindStub,
|
||||
hardwareCheckFindStub;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
monitorFindStub = sinon.stub(Monitor, "find");
|
||||
monitorFindByIdStub = sinon.stub(Monitor, "findById");
|
||||
monitorFindByIdAndUpdateStub = sinon.stub(Monitor, "findByIdAndUpdate");
|
||||
@@ -61,12 +62,13 @@ describe("monitorModule", () => {
|
||||
sort: sinon.stub(),
|
||||
});
|
||||
});
|
||||
afterEach(() => {
|
||||
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("getAllMonitors", () => {
|
||||
it("should return all monitors", async () => {
|
||||
describe("getAllMonitors", function() {
|
||||
it("should return all monitors", async function() {
|
||||
const mockMonitors = [
|
||||
{ _id: "1", name: "Monitor 1", url: "test1.com" },
|
||||
{ _id: "2", name: "Monitor 2", url: "test2.com" },
|
||||
@@ -78,12 +80,14 @@ describe("monitorModule", () => {
|
||||
expect(monitorFindStub.calledOnce).to.be.true;
|
||||
expect(monitorFindStub.firstCall.args).to.deep.equal([]);
|
||||
});
|
||||
it("should handle empty results", async () => {
|
||||
|
||||
it("should handle empty results", async function() {
|
||||
monitorFindStub.returns([]);
|
||||
const result = await getAllMonitors();
|
||||
expect(result).to.be.an("array").that.is.empty;
|
||||
});
|
||||
it("should throw error when database fails", async () => {
|
||||
|
||||
it("should throw error when database fails", async function() {
|
||||
// Arrange
|
||||
const error = new Error("Database error");
|
||||
error.service = "MonitorModule";
|
||||
@@ -101,8 +105,8 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllMonitorsWithUptimeStats", () => {
|
||||
it("should return monitors with uptime stats for different time periods", async () => {
|
||||
describe("getAllMonitorsWithUptimeStats", function() {
|
||||
it("should return monitors with uptime stats for different time periods", async function() {
|
||||
// Mock data
|
||||
const mockMonitors = [
|
||||
{
|
||||
@@ -147,7 +151,8 @@ describe("monitorModule", () => {
|
||||
expect(monitor["30"]).to.equal(75);
|
||||
expect(monitor["90"]).to.equal(75);
|
||||
});
|
||||
it("should return monitors with stats for pagespeed type", async () => {
|
||||
|
||||
it("should return monitors with stats for pagespeed type", async function() {
|
||||
// Mock data
|
||||
const mockMonitors = [
|
||||
{
|
||||
@@ -192,7 +197,8 @@ describe("monitorModule", () => {
|
||||
expect(monitor["30"]).to.equal(75);
|
||||
expect(monitor["90"]).to.equal(75);
|
||||
});
|
||||
it("should return monitors with stats for hardware type", async () => {
|
||||
|
||||
it("should return monitors with stats for hardware type", async function() {
|
||||
// Mock data
|
||||
const mockMonitors = [
|
||||
{
|
||||
@@ -238,7 +244,7 @@ describe("monitorModule", () => {
|
||||
expect(monitor["90"]).to.equal(75);
|
||||
});
|
||||
|
||||
it("should handle errors appropriately", async () => {
|
||||
it("should handle errors appropriately", async function() {
|
||||
// Setup stub to throw error
|
||||
monitorFindStub.rejects(new Error("Database error"));
|
||||
|
||||
@@ -251,7 +257,8 @@ describe("monitorModule", () => {
|
||||
expect(error.method).to.equal("getAllMonitorsWithUptimeStats");
|
||||
}
|
||||
});
|
||||
it("should handle empty monitor list", async () => {
|
||||
|
||||
it("should handle empty monitor list", async function() {
|
||||
monitorFindStub.resolves([]);
|
||||
|
||||
const result = await getAllMonitorsWithUptimeStats();
|
||||
@@ -260,7 +267,7 @@ describe("monitorModule", () => {
|
||||
expect(result).to.have.lengthOf(0);
|
||||
});
|
||||
|
||||
it("should handle monitor with no checks", async () => {
|
||||
it("should handle monitor with no checks", async function() {
|
||||
const mockMonitors = [
|
||||
{
|
||||
_id: "monitor1",
|
||||
@@ -285,28 +292,28 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("calculateUptimeDuration", () => {
|
||||
describe("calculateUptimeDuration", function() {
|
||||
let clock;
|
||||
const NOW = new Date("2024-01-01T12:00:00Z").getTime();
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
// Fix the current time
|
||||
clock = sinon.useFakeTimers(NOW);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is empty", () => {
|
||||
it("should return 0 when checks array is empty", function() {
|
||||
expect(calculateUptimeDuration([])).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is null", () => {
|
||||
it("should return 0 when checks array is null", function() {
|
||||
expect(calculateUptimeDuration(null)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should calculate uptime from last down check to most recent check", () => {
|
||||
it("should calculate uptime from last down check to most recent check", function() {
|
||||
const checks = [
|
||||
{ status: true, createdAt: "2024-01-01T11:00:00Z" }, // Most recent
|
||||
{ status: true, createdAt: "2024-01-01T10:00:00Z" },
|
||||
@@ -318,7 +325,7 @@ describe("monitorModule", () => {
|
||||
expect(calculateUptimeDuration(checks)).to.equal(7200000);
|
||||
});
|
||||
|
||||
it("should calculate uptime from first check when no down checks exist", () => {
|
||||
it("should calculate uptime from first check when no down checks exist", function() {
|
||||
const checks = [
|
||||
{ status: true, createdAt: "2024-01-01T11:00:00Z" },
|
||||
{ status: true, createdAt: "2024-01-01T10:00:00Z" },
|
||||
@@ -330,28 +337,28 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getLastChecked", () => {
|
||||
describe("getLastChecked", function() {
|
||||
let clock;
|
||||
const NOW = new Date("2024-01-01T12:00:00Z").getTime();
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
// Fix the current time
|
||||
clock = sinon.useFakeTimers(NOW);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is empty", () => {
|
||||
it("should return 0 when checks array is empty", function() {
|
||||
expect(getLastChecked([])).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is null", () => {
|
||||
it("should return 0 when checks array is null", function() {
|
||||
expect(getLastChecked(null)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return time difference between now and most recent check", () => {
|
||||
it("should return time difference between now and most recent check", function() {
|
||||
const checks = [
|
||||
{ createdAt: "2024-01-01T11:30:00Z" }, // 30 minutes ago
|
||||
{ createdAt: "2024-01-01T11:00:00Z" },
|
||||
@@ -362,7 +369,7 @@ describe("monitorModule", () => {
|
||||
expect(getLastChecked(checks)).to.equal(1800000);
|
||||
});
|
||||
|
||||
it("should handle checks from different days", () => {
|
||||
it("should handle checks from different days", function() {
|
||||
const checks = [
|
||||
{ createdAt: "2023-12-31T12:00:00Z" }, // 24 hours ago
|
||||
{ createdAt: "2023-12-30T12:00:00Z" },
|
||||
@@ -372,16 +379,17 @@ describe("monitorModule", () => {
|
||||
expect(getLastChecked(checks)).to.equal(86400000);
|
||||
});
|
||||
});
|
||||
describe("getLatestResponseTime", () => {
|
||||
it("should return 0 when checks array is empty", () => {
|
||||
|
||||
describe("getLatestResponseTime", function() {
|
||||
it("should return 0 when checks array is empty", function() {
|
||||
expect(getLatestResponseTime([])).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is null", () => {
|
||||
it("should return 0 when checks array is null", function() {
|
||||
expect(getLatestResponseTime(null)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return response time from most recent check", () => {
|
||||
it("should return response time from most recent check", function() {
|
||||
const checks = [
|
||||
{ responseTime: 150, createdAt: "2024-01-01T11:30:00Z" }, // Most recent
|
||||
{ responseTime: 200, createdAt: "2024-01-01T11:00:00Z" },
|
||||
@@ -391,7 +399,7 @@ describe("monitorModule", () => {
|
||||
expect(getLatestResponseTime(checks)).to.equal(150);
|
||||
});
|
||||
|
||||
it("should handle missing responseTime in checks", () => {
|
||||
it("should handle missing responseTime in checks", function() {
|
||||
const checks = [
|
||||
{ createdAt: "2024-01-01T11:30:00Z" },
|
||||
{ responseTime: 200, createdAt: "2024-01-01T11:00:00Z" },
|
||||
@@ -400,16 +408,17 @@ describe("monitorModule", () => {
|
||||
expect(getLatestResponseTime(checks)).to.equal(0);
|
||||
});
|
||||
});
|
||||
describe("getAverageResponseTime", () => {
|
||||
it("should return 0 when checks array is empty", () => {
|
||||
|
||||
describe("getAverageResponseTime", function() {
|
||||
it("should return 0 when checks array is empty", function() {
|
||||
expect(getAverageResponseTime([])).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is null", () => {
|
||||
it("should return 0 when checks array is null", function() {
|
||||
expect(getAverageResponseTime(null)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should calculate average response time from all checks", () => {
|
||||
it("should calculate average response time from all checks", function() {
|
||||
const checks = [
|
||||
{ responseTime: 100, createdAt: "2024-01-01T11:30:00Z" },
|
||||
{ responseTime: 200, createdAt: "2024-01-01T11:00:00Z" },
|
||||
@@ -420,7 +429,7 @@ describe("monitorModule", () => {
|
||||
expect(getAverageResponseTime(checks)).to.equal(200);
|
||||
});
|
||||
|
||||
it("should handle missing responseTime in some checks", () => {
|
||||
it("should handle missing responseTime in some checks", function() {
|
||||
const checks = [
|
||||
{ responseTime: 100, createdAt: "2024-01-01T11:30:00Z" },
|
||||
{ createdAt: "2024-01-01T11:00:00Z" },
|
||||
@@ -431,7 +440,7 @@ describe("monitorModule", () => {
|
||||
expect(getAverageResponseTime(checks)).to.equal(200);
|
||||
});
|
||||
|
||||
it("should return 0 when no checks have responseTime", () => {
|
||||
it("should return 0 when no checks have responseTime", function() {
|
||||
const checks = [
|
||||
{ createdAt: "2024-01-01T11:30:00Z" },
|
||||
{ createdAt: "2024-01-01T11:00:00Z" },
|
||||
@@ -440,26 +449,27 @@ describe("monitorModule", () => {
|
||||
expect(getAverageResponseTime(checks)).to.equal(0);
|
||||
});
|
||||
});
|
||||
describe("getUptimePercentage", () => {
|
||||
it("should return 0 when checks array is empty", () => {
|
||||
|
||||
describe("getUptimePercentage", function() {
|
||||
it("should return 0 when checks array is empty", function() {
|
||||
expect(getUptimePercentage([])).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is null", () => {
|
||||
it("should return 0 when checks array is null", function() {
|
||||
expect(getUptimePercentage(null)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 100 when all checks are up", () => {
|
||||
it("should return 100 when all checks are up", function() {
|
||||
const checks = [{ status: true }, { status: true }, { status: true }];
|
||||
expect(getUptimePercentage(checks)).to.equal(100);
|
||||
});
|
||||
|
||||
it("should return 0 when all checks are down", () => {
|
||||
it("should return 0 when all checks are down", function() {
|
||||
const checks = [{ status: false }, { status: false }, { status: false }];
|
||||
expect(getUptimePercentage(checks)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should calculate correct percentage for mixed status checks", () => {
|
||||
it("should calculate correct percentage for mixed status checks", function() {
|
||||
const checks = [
|
||||
{ status: true },
|
||||
{ status: false },
|
||||
@@ -470,32 +480,33 @@ describe("monitorModule", () => {
|
||||
expect(getUptimePercentage(checks)).to.equal(75);
|
||||
});
|
||||
|
||||
it("should handle undefined status values", () => {
|
||||
it("should handle undefined status values", function() {
|
||||
const checks = [{ status: true }, { status: undefined }, { status: true }];
|
||||
// 2 up out of 3 total ≈ 66.67%
|
||||
expect(getUptimePercentage(checks)).to.equal((2 / 3) * 100);
|
||||
});
|
||||
});
|
||||
describe("getIncidents", () => {
|
||||
it("should return 0 when checks array is empty", () => {
|
||||
|
||||
describe("getIncidents", function() {
|
||||
it("should return 0 when checks array is empty", function() {
|
||||
expect(getIncidents([])).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when checks array is null", () => {
|
||||
it("should return 0 when checks array is null", function() {
|
||||
expect(getIncidents(null)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should return 0 when all checks are up", () => {
|
||||
it("should return 0 when all checks are up", function() {
|
||||
const checks = [{ status: true }, { status: true }, { status: true }];
|
||||
expect(getIncidents(checks)).to.equal(0);
|
||||
});
|
||||
|
||||
it("should count all incidents when all checks are down", () => {
|
||||
it("should count all incidents when all checks are down", function() {
|
||||
const checks = [{ status: false }, { status: false }, { status: false }];
|
||||
expect(getIncidents(checks)).to.equal(3);
|
||||
});
|
||||
|
||||
it("should count correct number of incidents for mixed status checks", () => {
|
||||
it("should count correct number of incidents for mixed status checks", function() {
|
||||
const checks = [
|
||||
{ status: true },
|
||||
{ status: false },
|
||||
@@ -506,7 +517,7 @@ describe("monitorModule", () => {
|
||||
expect(getIncidents(checks)).to.equal(2);
|
||||
});
|
||||
|
||||
it("should handle undefined status values", () => {
|
||||
it("should handle undefined status values", function() {
|
||||
const checks = [
|
||||
{ status: true },
|
||||
{ status: undefined },
|
||||
@@ -517,10 +528,11 @@ describe("monitorModule", () => {
|
||||
expect(getIncidents(checks)).to.equal(2);
|
||||
});
|
||||
});
|
||||
describe("getMonitorChecks", () => {
|
||||
|
||||
describe("getMonitorChecks", function() {
|
||||
let mockModel;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
// Create a mock model with chainable methods
|
||||
const mockChecks = [
|
||||
{ monitorId: "123", createdAt: new Date("2024-01-01") },
|
||||
@@ -534,11 +546,11 @@ describe("monitorModule", () => {
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return all checks and date-ranged checks", async () => {
|
||||
it("should return all checks and date-ranged checks", async function() {
|
||||
// Arrange
|
||||
const monitorId = "123";
|
||||
const dateRange = {
|
||||
@@ -567,7 +579,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle empty results", async () => {
|
||||
it("should handle empty results", async function() {
|
||||
// Arrange
|
||||
const emptyModel = {
|
||||
find: sinon.stub().returns({
|
||||
@@ -591,7 +603,7 @@ describe("monitorModule", () => {
|
||||
expect(result.checksForDateRange).to.be.an("array").that.is.empty;
|
||||
});
|
||||
|
||||
it("should maintain sort order", async () => {
|
||||
it("should maintain sort order", async function() {
|
||||
// Arrange
|
||||
const sortedChecks = [
|
||||
{ monitorId: "123", createdAt: new Date("2024-01-02") },
|
||||
@@ -625,39 +637,39 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("processChecksForDisplay", () => {
|
||||
describe("processChecksForDisplay", function() {
|
||||
let normalizeStub;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
normalizeStub = sinon.stub();
|
||||
});
|
||||
|
||||
it("should return original checks when numToDisplay is not provided", () => {
|
||||
it("should return original checks when numToDisplay is not provided", function() {
|
||||
const checks = [1, 2, 3, 4, 5];
|
||||
const result = processChecksForDisplay(normalizeStub, checks);
|
||||
expect(result).to.deep.equal(checks);
|
||||
});
|
||||
|
||||
it("should return original checks when numToDisplay is greater than checks length", () => {
|
||||
it("should return original checks when numToDisplay is greater than checks length", function() {
|
||||
const checks = [1, 2, 3];
|
||||
const result = processChecksForDisplay(normalizeStub, checks, 5);
|
||||
expect(result).to.deep.equal(checks);
|
||||
});
|
||||
|
||||
it("should filter checks based on numToDisplay", () => {
|
||||
it("should filter checks based on numToDisplay", function() {
|
||||
const checks = [1, 2, 3, 4, 5, 6];
|
||||
const result = processChecksForDisplay(normalizeStub, checks, 3);
|
||||
// Should return [1, 3, 5] as n = ceil(6/3) = 2
|
||||
expect(result).to.deep.equal([1, 3, 5]);
|
||||
});
|
||||
|
||||
it("should handle empty checks array", () => {
|
||||
it("should handle empty checks array", function() {
|
||||
const checks = [];
|
||||
const result = processChecksForDisplay(normalizeStub, checks, 3);
|
||||
expect(result).to.be.an("array").that.is.empty;
|
||||
});
|
||||
|
||||
it("should call normalizeData when normalize is true", () => {
|
||||
it("should call normalizeData when normalize is true", function() {
|
||||
const checks = [1, 2, 3];
|
||||
normalizeStub.returns([10, 20, 30]);
|
||||
|
||||
@@ -667,7 +679,7 @@ describe("monitorModule", () => {
|
||||
expect(result).to.deep.equal([10, 20, 30]);
|
||||
});
|
||||
|
||||
it("should handle both filtering and normalization", () => {
|
||||
it("should handle both filtering and normalization", function() {
|
||||
const checks = [1, 2, 3, 4, 5, 6];
|
||||
normalizeStub.returns([10, 30, 50]);
|
||||
|
||||
@@ -677,7 +689,8 @@ describe("monitorModule", () => {
|
||||
expect(result).to.deep.equal([10, 30, 50]);
|
||||
});
|
||||
});
|
||||
describe("groupChecksByTime", () => {
|
||||
|
||||
describe("groupChecksByTime", function() {
|
||||
const mockChecks = [
|
||||
{ createdAt: "2024-01-15T10:30:45Z" },
|
||||
{ createdAt: "2024-01-15T10:45:15Z" },
|
||||
@@ -685,7 +698,7 @@ describe("monitorModule", () => {
|
||||
{ createdAt: "2024-01-16T10:30:00Z" },
|
||||
];
|
||||
|
||||
it("should group checks by hour when dateRange is 'day'", () => {
|
||||
it("should group checks by hour when dateRange is 'day'", function() {
|
||||
const result = groupChecksByTime(mockChecks, "day");
|
||||
|
||||
// Get timestamps for 10:00 and 11:00 on Jan 15
|
||||
@@ -700,7 +713,7 @@ describe("monitorModule", () => {
|
||||
expect(result[time3].checks).to.have.lengthOf(1);
|
||||
});
|
||||
|
||||
it("should group checks by day when dateRange is not 'day'", () => {
|
||||
it("should group checks by day when dateRange is not 'day'", function() {
|
||||
const result = groupChecksByTime(mockChecks, "week");
|
||||
|
||||
expect(Object.keys(result)).to.have.lengthOf(2);
|
||||
@@ -708,12 +721,12 @@ describe("monitorModule", () => {
|
||||
expect(result["2024-01-16"].checks).to.have.lengthOf(1);
|
||||
});
|
||||
|
||||
it("should handle empty checks array", () => {
|
||||
it("should handle empty checks array", function() {
|
||||
const result = groupChecksByTime([], "day");
|
||||
expect(result).to.deep.equal({});
|
||||
});
|
||||
|
||||
it("should handle single check", () => {
|
||||
it("should handle single check", function() {
|
||||
const singleCheck = [{ createdAt: "2024-01-15T10:30:45Z" }];
|
||||
const result = groupChecksByTime(singleCheck, "day");
|
||||
|
||||
@@ -721,7 +734,8 @@ describe("monitorModule", () => {
|
||||
expect(Object.keys(result)).to.have.lengthOf(1);
|
||||
expect(result[expectedTime].checks).to.have.lengthOf(1);
|
||||
});
|
||||
it("should skip invalid dates and process valid ones", () => {
|
||||
|
||||
it("should skip invalid dates and process valid ones", function() {
|
||||
const checksWithInvalidDate = [
|
||||
{ createdAt: "invalid-date" },
|
||||
{ createdAt: "2024-01-15T10:30:45Z" },
|
||||
@@ -737,7 +751,8 @@ describe("monitorModule", () => {
|
||||
expect(result[expectedTime].checks).to.have.lengthOf(1);
|
||||
expect(result[expectedTime].checks[0].createdAt).to.equal("2024-01-15T10:30:45Z");
|
||||
});
|
||||
it("should handle checks in same time group", () => {
|
||||
|
||||
it("should handle checks in same time group", function() {
|
||||
const checksInSameHour = [
|
||||
{ createdAt: "2024-01-15T10:15:00Z" },
|
||||
{ createdAt: "2024-01-15T10:45:00Z" },
|
||||
@@ -750,16 +765,17 @@ describe("monitorModule", () => {
|
||||
expect(result[expectedTime].checks).to.have.lengthOf(2);
|
||||
});
|
||||
});
|
||||
describe("calculateGroupStats", () => {
|
||||
|
||||
describe("calculateGroupStats", function() {
|
||||
// Mock getUptimePercentage function
|
||||
let uptimePercentageStub;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
uptimePercentageStub = sinon.stub();
|
||||
uptimePercentageStub.returns(95); // Default return value
|
||||
});
|
||||
|
||||
it("should calculate stats correctly for a group of checks", () => {
|
||||
it("should calculate stats correctly for a group of checks", function() {
|
||||
const mockGroup = {
|
||||
time: "2024-01-15",
|
||||
checks: [
|
||||
@@ -780,7 +796,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle empty checks array", () => {
|
||||
it("should handle empty checks array", function() {
|
||||
const mockGroup = {
|
||||
time: "2024-01-15",
|
||||
checks: [],
|
||||
@@ -797,7 +813,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle missing responseTime values", () => {
|
||||
it("should handle missing responseTime values", function() {
|
||||
const mockGroup = {
|
||||
time: "2024-01-15",
|
||||
checks: [
|
||||
@@ -818,7 +834,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle all checks with status false", () => {
|
||||
it("should handle all checks with status false", function() {
|
||||
const mockGroup = {
|
||||
time: "2024-01-15",
|
||||
checks: [
|
||||
@@ -839,7 +855,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle all checks with status true", () => {
|
||||
it("should handle all checks with status true", function() {
|
||||
const mockGroup = {
|
||||
time: "2024-01-15",
|
||||
checks: [
|
||||
@@ -861,7 +877,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getMonitorStatsById", () => {
|
||||
describe("getMonitorStatsById", function() {
|
||||
const now = new Date();
|
||||
const oneHourAgo = new Date(now - 3600000);
|
||||
const twoHoursAgo = new Date(now - 7200000);
|
||||
@@ -958,18 +974,18 @@ describe("monitorModule", () => {
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
checkFindStub.returns({
|
||||
sort: () => checkDocs,
|
||||
});
|
||||
monitorFindByIdStub.returns(mockMonitor);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return monitor stats with calculated values, sort order desc", async () => {
|
||||
it("should return monitor stats with calculated values, sort order desc", async function() {
|
||||
req.query.sortOrder = "desc";
|
||||
const result = await getMonitorStatsById(req);
|
||||
expect(result).to.include.keys([
|
||||
@@ -992,7 +1008,8 @@ describe("monitorModule", () => {
|
||||
expect(result.periodUptime).to.be.a("number");
|
||||
expect(result.aggregateData).to.be.an("array");
|
||||
});
|
||||
it("should return monitor stats with calculated values, ping type", async () => {
|
||||
|
||||
it("should return monitor stats with calculated values, ping type", async function() {
|
||||
monitorFindByIdStub.returns(mockMonitorPing);
|
||||
req.query.sortOrder = "desc";
|
||||
const result = await getMonitorStatsById(req);
|
||||
@@ -1016,7 +1033,8 @@ describe("monitorModule", () => {
|
||||
expect(result.periodUptime).to.be.a("number");
|
||||
expect(result.aggregateData).to.be.an("array");
|
||||
});
|
||||
it("should return monitor stats with calculated values, docker type", async () => {
|
||||
|
||||
it("should return monitor stats with calculated values, docker type", async function() {
|
||||
monitorFindByIdStub.returns(mockMonitorDocker);
|
||||
req.query.sortOrder = "desc";
|
||||
const result = await getMonitorStatsById(req);
|
||||
@@ -1040,7 +1058,8 @@ describe("monitorModule", () => {
|
||||
expect(result.periodUptime).to.be.a("number");
|
||||
expect(result.aggregateData).to.be.an("array");
|
||||
});
|
||||
it("should return monitor stats with calculated values", async () => {
|
||||
|
||||
it("should return monitor stats with calculated values", async function() {
|
||||
req.query.sortOrder = "asc";
|
||||
const result = await getMonitorStatsById(req);
|
||||
expect(result).to.include.keys([
|
||||
@@ -1063,7 +1082,8 @@ describe("monitorModule", () => {
|
||||
expect(result.periodUptime).to.be.a("number");
|
||||
expect(result.aggregateData).to.be.an("array");
|
||||
});
|
||||
it("should throw error when monitor is not found", async () => {
|
||||
|
||||
it("should throw error when monitor is not found", async function() {
|
||||
monitorFindByIdStub.returns(Promise.resolve(null));
|
||||
|
||||
const req = {
|
||||
@@ -1081,21 +1101,21 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getMonitorById", () => {
|
||||
describe("getMonitorById", function() {
|
||||
let notificationFindStub;
|
||||
let monitorSaveStub;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
// Create stubs
|
||||
notificationFindStub = sinon.stub(Notification, "find");
|
||||
monitorSaveStub = sinon.stub().resolves();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return monitor with notifications when found", async () => {
|
||||
it("should return monitor with notifications when found", async function() {
|
||||
// Arrange
|
||||
const monitorId = "123";
|
||||
const mockMonitor = {
|
||||
@@ -1119,7 +1139,7 @@ describe("monitorModule", () => {
|
||||
expect(monitorSaveStub.calledOnce).to.be.true;
|
||||
});
|
||||
|
||||
it("should throw 404 error when monitor not found", async () => {
|
||||
it("should throw 404 error when monitor not found", async function() {
|
||||
// Arrange
|
||||
const monitorId = "nonexistent";
|
||||
monitorFindByIdStub.resolves(null);
|
||||
@@ -1136,7 +1156,7 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle database errors properly", async () => {
|
||||
it("should handle database errors properly", async function() {
|
||||
// Arrange
|
||||
const monitorId = "123";
|
||||
const dbError = new Error("Database connection failed");
|
||||
@@ -1153,7 +1173,7 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle notification fetch errors", async () => {
|
||||
it("should handle notification fetch errors", async function() {
|
||||
// Arrange
|
||||
const monitorId = "123";
|
||||
const mockMonitor = {
|
||||
@@ -1177,7 +1197,7 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle monitor save errors", async () => {
|
||||
it("should handle monitor save errors", async function() {
|
||||
// Arrange
|
||||
const monitorId = "123";
|
||||
const mockMonitor = {
|
||||
@@ -1201,8 +1221,9 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("getMonitorsAndSummaryByTeamId", () => {
|
||||
it("should return monitors and correct summary counts", async () => {
|
||||
|
||||
describe("getMonitorsAndSummaryByTeamId", function() {
|
||||
it("should return monitors and correct summary counts", async function() {
|
||||
// Arrange
|
||||
const teamId = "team123";
|
||||
const type = "http";
|
||||
@@ -1228,7 +1249,7 @@ describe("monitorModule", () => {
|
||||
expect(monitorFindStub.calledOnceWith({ teamId, type })).to.be.true;
|
||||
});
|
||||
|
||||
it("should return empty results for non-existent team", async () => {
|
||||
it("should return empty results for non-existent team", async function() {
|
||||
// Arrange
|
||||
monitorFindStub.resolves([]);
|
||||
|
||||
@@ -1245,7 +1266,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle database errors", async () => {
|
||||
it("should handle database errors", async function() {
|
||||
// Arrange
|
||||
const error = new Error("Database error");
|
||||
error.service = "MonitorModule";
|
||||
@@ -1263,8 +1284,9 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("getMonitorsByTeamId", () => {
|
||||
beforeEach(() => {
|
||||
|
||||
describe("getMonitorsByTeamId", function() {
|
||||
beforeEach(function() {
|
||||
// Chain stubs for Monitor.find().skip().limit().sort()
|
||||
|
||||
// Stub for CHECK_MODEL_LOOKUP model find
|
||||
@@ -1275,11 +1297,11 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return monitors with basic query parameters", async () => {
|
||||
it("should return monitors with basic query parameters", async function() {
|
||||
const mockMonitors = [
|
||||
{ _id: "1", type: "http", toObject: () => ({ _id: "1", type: "http" }) },
|
||||
{ _id: "2", type: "ping", toObject: () => ({ _id: "2", type: "ping" }) },
|
||||
@@ -1312,7 +1334,7 @@ describe("monitorModule", () => {
|
||||
expect(result).to.have.property("monitorCount", 2);
|
||||
});
|
||||
|
||||
it("should return monitors with basic query parameters", async () => {
|
||||
it("should return monitors with basic query parameters", async function() {
|
||||
const mockMonitors = [
|
||||
{ _id: "1", type: "http", toObject: () => ({ _id: "1", type: "http" }) },
|
||||
{ _id: "2", type: "ping", toObject: () => ({ _id: "2", type: "ping" }) },
|
||||
@@ -1345,7 +1367,7 @@ describe("monitorModule", () => {
|
||||
expect(result).to.have.property("monitorCount", 2);
|
||||
});
|
||||
|
||||
it("should handle type filter with array input", async () => {
|
||||
it("should handle type filter with array input", async function() {
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
query: {
|
||||
@@ -1370,7 +1392,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle text search filter", async () => {
|
||||
it("should handle text search filter", async function() {
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
query: {
|
||||
@@ -1398,7 +1420,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle pagination parameters", async () => {
|
||||
it("should handle pagination parameters", async function() {
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
query: {
|
||||
@@ -1423,7 +1445,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle sorting parameters", async () => {
|
||||
it("should handle sorting parameters", async function() {
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
query: {
|
||||
@@ -1450,7 +1472,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should return early when limit is -1", async () => {
|
||||
it("should return early when limit is -1", async function() {
|
||||
// Arrange
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
@@ -1484,7 +1506,7 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should normalize checks when normalize parameter is provided", async () => {
|
||||
it("should normalize checks when normalize parameter is provided", async function() {
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
query: { normalize: "true" },
|
||||
@@ -1508,7 +1530,8 @@ describe("monitorModule", () => {
|
||||
expect(result.monitorCount).to.equal(2);
|
||||
expect(result.monitors).to.have.lengthOf(2);
|
||||
});
|
||||
it("should handle database errors", async () => {
|
||||
|
||||
it("should handle database errors", async function() {
|
||||
const req = {
|
||||
params: { teamId: "team123" },
|
||||
query: {},
|
||||
@@ -1533,8 +1556,9 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("createMonitor", () => {
|
||||
it("should create a monitor without notifications", async () => {
|
||||
|
||||
describe("createMonitor", function() {
|
||||
it("should create a monitor without notifications", async function() {
|
||||
let monitorSaveStub = sinon.stub(Monitor.prototype, "save").resolves();
|
||||
|
||||
const req = {
|
||||
@@ -1558,7 +1582,8 @@ describe("monitorModule", () => {
|
||||
expect(result.name).to.equal(expectedMonitor.name);
|
||||
expect(result.url).to.equal(expectedMonitor.url);
|
||||
});
|
||||
it("should handle database errors", async () => {
|
||||
|
||||
it("should handle database errors", async function() {
|
||||
const req = {
|
||||
body: {
|
||||
name: "Test Monitor",
|
||||
@@ -1575,8 +1600,8 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteMonitor", () => {
|
||||
it("should delete a monitor successfully", async () => {
|
||||
describe("deleteMonitor", function() {
|
||||
it("should delete a monitor successfully", async function() {
|
||||
const monitorId = "123456789";
|
||||
const mockMonitor = {
|
||||
_id: monitorId,
|
||||
@@ -1596,7 +1621,7 @@ describe("monitorModule", () => {
|
||||
sinon.assert.calledWith(monitorFindByIdAndDeleteStub, monitorId);
|
||||
});
|
||||
|
||||
it("should throw error when monitor not found", async () => {
|
||||
it("should throw error when monitor not found", async function() {
|
||||
const monitorId = "nonexistent123";
|
||||
const req = {
|
||||
params: { monitorId },
|
||||
@@ -1614,7 +1639,7 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle database errors", async () => {
|
||||
it("should handle database errors", async function() {
|
||||
const monitorId = "123456789";
|
||||
const req = {
|
||||
params: { monitorId },
|
||||
@@ -1634,8 +1659,8 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteAllMonitors", () => {
|
||||
it("should delete all monitors for a team successfully", async () => {
|
||||
describe("deleteAllMonitors", function() {
|
||||
it("should delete all monitors for a team successfully", async function() {
|
||||
const teamId = "team123";
|
||||
const mockMonitors = [
|
||||
{ _id: "1", name: "Monitor 1", teamId },
|
||||
@@ -1655,7 +1680,7 @@ describe("monitorModule", () => {
|
||||
sinon.assert.calledWith(monitorDeleteManyStub, { teamId });
|
||||
});
|
||||
|
||||
it("should return empty array when no monitors found", async () => {
|
||||
it("should return empty array when no monitors found", async function() {
|
||||
const teamId = "emptyTeam";
|
||||
|
||||
monitorFindStub.resolves([]);
|
||||
@@ -1671,7 +1696,7 @@ describe("monitorModule", () => {
|
||||
sinon.assert.calledWith(monitorDeleteManyStub, { teamId });
|
||||
});
|
||||
|
||||
it("should handle database errors", async () => {
|
||||
it("should handle database errors", async function() {
|
||||
const teamId = "team123";
|
||||
const dbError = new Error("Database connection error");
|
||||
monitorFindStub.rejects(dbError);
|
||||
@@ -1685,7 +1710,7 @@ describe("monitorModule", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle deleteMany errors", async () => {
|
||||
it("should handle deleteMany errors", async function() {
|
||||
const teamId = "team123";
|
||||
monitorFindStub.resolves([{ _id: "1", name: "Monitor 1" }]);
|
||||
monitorDeleteManyStub.rejects(new Error("Delete operation failed"));
|
||||
@@ -1700,14 +1725,14 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteMonitorsByUserId", () => {
|
||||
beforeEach(() => {});
|
||||
describe("deleteMonitorsByUserId", function() {
|
||||
beforeEach(function() {});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should delete all monitors for a user successfully", async () => {
|
||||
it("should delete all monitors for a user successfully", async function() {
|
||||
// Arrange
|
||||
const userId = "user123";
|
||||
const mockResult = {
|
||||
@@ -1725,7 +1750,7 @@ describe("monitorModule", () => {
|
||||
sinon.assert.calledWith(monitorDeleteManyStub, { userId: userId });
|
||||
});
|
||||
|
||||
it("should return zero deletedCount when no monitors found", async () => {
|
||||
it("should return zero deletedCount when no monitors found", async function() {
|
||||
// Arrange
|
||||
const userId = "nonexistentUser";
|
||||
const mockResult = {
|
||||
@@ -1743,7 +1768,7 @@ describe("monitorModule", () => {
|
||||
sinon.assert.calledWith(monitorDeleteManyStub, { userId: userId });
|
||||
});
|
||||
|
||||
it("should handle database errors", async () => {
|
||||
it("should handle database errors", async function() {
|
||||
// Arrange
|
||||
const userId = "user123";
|
||||
const dbError = new Error("Database connection error");
|
||||
@@ -1761,8 +1786,8 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("editMonitor", () => {
|
||||
it("should edit a monitor successfully", async () => {
|
||||
describe("editMonitor", function() {
|
||||
it("should edit a monitor successfully", async function() {
|
||||
// Arrange
|
||||
const candidateId = "monitor123";
|
||||
const candidateMonitor = {
|
||||
@@ -1801,7 +1826,7 @@ describe("monitorModule", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("should return null when monitor not found", async () => {
|
||||
it("should return null when monitor not found", async function() {
|
||||
// Arrange
|
||||
const candidateId = "nonexistent123";
|
||||
const candidateMonitor = {
|
||||
@@ -1823,7 +1848,7 @@ describe("monitorModule", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("should remove notifications from update data", async () => {
|
||||
it("should remove notifications from update data", async function() {
|
||||
// Arrange
|
||||
const candidateId = "monitor123";
|
||||
const candidateMonitor = {
|
||||
@@ -1855,7 +1880,7 @@ describe("monitorModule", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle database errors", async () => {
|
||||
it("should handle database errors", async function() {
|
||||
// Arrange
|
||||
const candidateId = "monitor123";
|
||||
const candidateMonitor = {
|
||||
@@ -1877,8 +1902,8 @@ describe("monitorModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("addDemoMonitors", () => {
|
||||
it("should add demo monitors successfully", async () => {
|
||||
describe("addDemoMonitors", function() {
|
||||
it("should add demo monitors successfully", async function() {
|
||||
// Arrange
|
||||
const userId = "user123";
|
||||
const teamId = "team123";
|
||||
@@ -1887,7 +1912,7 @@ describe("monitorModule", () => {
|
||||
expect(result).to.deep.equal([{ _id: "123" }]);
|
||||
});
|
||||
|
||||
it("should handle database errors", async () => {
|
||||
it("should handle database errors", async function() {
|
||||
const userId = "user123";
|
||||
const teamId = "team123";
|
||||
|
||||
|
||||
@@ -6,31 +6,32 @@ import {
|
||||
deleteNotificationsByMonitorId,
|
||||
} from "../../db/mongo/modules/notificationModule.js";
|
||||
|
||||
describe("notificationModule", () => {
|
||||
describe("notificationModule", function() {
|
||||
const mockNotification = {
|
||||
monitorId: "123",
|
||||
};
|
||||
const mockNotifications = [mockNotification];
|
||||
let notificationSaveStub, notificationFindStub, notificationDeleteManyStub;
|
||||
|
||||
beforeEach(() => {
|
||||
beforeEach(function() {
|
||||
notificationSaveStub = sinon.stub(Notification.prototype, "save").resolves();
|
||||
notificationFindStub = sinon.stub(Notification, "find").resolves();
|
||||
notificationDeleteManyStub = sinon.stub(Notification, "deleteMany").resolves();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("createNotification", () => {
|
||||
it("should create a new notification", async () => {
|
||||
describe("createNotification", function() {
|
||||
it("should create a new notification", async function() {
|
||||
const notificationData = { _id: "123", name: "test" };
|
||||
notificationSaveStub.resolves(notificationData);
|
||||
const res = await createNotification(notificationData);
|
||||
expect(res).to.deep.equal(notificationData);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
notificationSaveStub.rejects(err);
|
||||
try {
|
||||
@@ -41,13 +42,14 @@ describe("notificationModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getNotificationsByMonitorId", () => {
|
||||
it("should return notifications by monitor ID", async () => {
|
||||
describe("getNotificationsByMonitorId", function() {
|
||||
it("should return notifications by monitor ID", async function() {
|
||||
notificationFindStub.resolves(mockNotifications);
|
||||
const res = await getNotificationsByMonitorId(mockNotification.monitorId);
|
||||
expect(res).to.deep.equal(mockNotifications);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
notificationFindStub.rejects(err);
|
||||
try {
|
||||
@@ -58,13 +60,14 @@ describe("notificationModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteNotificationsByMonitorId", () => {
|
||||
it("should delete notifications by monitor ID", async () => {
|
||||
describe("deleteNotificationsByMonitorId", function() {
|
||||
it("should delete notifications by monitor ID", async function() {
|
||||
notificationDeleteManyStub.resolves({ deletedCount: mockNotifications.length });
|
||||
const res = await deleteNotificationsByMonitorId(mockNotification.monitorId);
|
||||
expect(res).to.deep.equal(mockNotifications.length);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
notificationDeleteManyStub.rejects(err);
|
||||
try {
|
||||
|
||||
@@ -14,25 +14,26 @@ const mockPageSpeedCheck = {
|
||||
|
||||
const mockDeletedResult = { deletedCount: 1 };
|
||||
|
||||
describe("pageSpeedCheckModule", () => {
|
||||
describe("pageSpeedCheckModule", function() {
|
||||
let pageSpeedCheckSaveStub, pageSpeedCheckDeleteManyStub;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
pageSpeedCheckSaveStub = sinon.stub(PageSpeedCheck.prototype, "save");
|
||||
pageSpeedCheckDeleteManyStub = sinon.stub(PageSpeedCheck, "deleteMany");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("createPageSpeedCheck", () => {
|
||||
it("should return a page speed check", async () => {
|
||||
describe("createPageSpeedCheck", function() {
|
||||
it("should return a page speed check", async function() {
|
||||
pageSpeedCheckSaveStub.resolves(mockPageSpeedCheck);
|
||||
const pageSpeedCheck = await createPageSpeedCheck(mockPageSpeedCheck);
|
||||
expect(pageSpeedCheck).to.deep.equal(mockPageSpeedCheck);
|
||||
});
|
||||
|
||||
it("should handle an error", async () => {
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
pageSpeedCheckSaveStub.rejects(err);
|
||||
try {
|
||||
@@ -44,14 +45,14 @@ describe("pageSpeedCheckModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deletePageSpeedChecksByMonitorId", () => {
|
||||
it("should return the number of deleted checks", async () => {
|
||||
describe("deletePageSpeedChecksByMonitorId", function() {
|
||||
it("should return the number of deleted checks", async function() {
|
||||
pageSpeedCheckDeleteManyStub.resolves(mockDeletedResult);
|
||||
const result = await deletePageSpeedChecksByMonitorId("monitorId");
|
||||
expect(result).to.deep.equal(mockDeletedResult.deletedCount);
|
||||
});
|
||||
|
||||
it("should handle an error", async () => {
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
pageSpeedCheckDeleteManyStub.rejects(err);
|
||||
try {
|
||||
|
||||
@@ -43,7 +43,7 @@ const createQueryChain = (finalResult, comparePasswordResult = false) => ({
|
||||
save: sinon.stub().resolves(),
|
||||
});
|
||||
|
||||
describe("recoveryModule", () => {
|
||||
describe("recoveryModule", function() {
|
||||
let deleteManyStub,
|
||||
saveStub,
|
||||
findOneStub,
|
||||
@@ -51,7 +51,8 @@ describe("recoveryModule", () => {
|
||||
userSaveStub,
|
||||
userFindOneStub;
|
||||
let req, res;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
req = {
|
||||
body: { email: "test@test.com" },
|
||||
};
|
||||
@@ -63,18 +64,19 @@ describe("recoveryModule", () => {
|
||||
userFindOneStub = sinon.stub().resolves();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("requestRecoveryToken", () => {
|
||||
it("should return a recovery token", async () => {
|
||||
describe("requestRecoveryToken", function() {
|
||||
it("should return a recovery token", async function() {
|
||||
deleteManyStub.resolves();
|
||||
saveStub.resolves(mockRecoveryToken);
|
||||
const result = await requestRecoveryToken(req, res);
|
||||
expect(result.email).to.equal(mockRecoveryToken.email);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("Test error");
|
||||
deleteManyStub.rejects(err);
|
||||
try {
|
||||
@@ -85,13 +87,15 @@ describe("recoveryModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("validateRecoveryToken", () => {
|
||||
it("should return a recovery token if found", async () => {
|
||||
|
||||
describe("validateRecoveryToken", function() {
|
||||
it("should return a recovery token if found", async function() {
|
||||
findOneStub.resolves(mockRecoveryToken);
|
||||
const result = await validateRecoveryToken(req, res);
|
||||
expect(result).to.deep.equal(mockRecoveryToken);
|
||||
});
|
||||
it("should thrown an error if a token is not found", async () => {
|
||||
|
||||
it("should thrown an error if a token is not found", async function() {
|
||||
findOneStub.resolves(null);
|
||||
try {
|
||||
await validateRecoveryToken(req, res);
|
||||
@@ -100,7 +104,8 @@ describe("recoveryModule", () => {
|
||||
expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND);
|
||||
}
|
||||
});
|
||||
it("should handle DB errors", async () => {
|
||||
|
||||
it("should handle DB errors", async function() {
|
||||
const err = new Error("Test error");
|
||||
findOneStub.rejects(err);
|
||||
try {
|
||||
@@ -112,19 +117,19 @@ describe("recoveryModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("resetPassword", () => {
|
||||
beforeEach(() => {
|
||||
describe("resetPassword", function() {
|
||||
beforeEach(function() {
|
||||
req.body = {
|
||||
password: "test",
|
||||
newPassword: "test1",
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should thrown an error if a recovery token is not found", async () => {
|
||||
it("should thrown an error if a recovery token is not found", async function() {
|
||||
findOneStub.resolves(null);
|
||||
try {
|
||||
await resetPassword(req, res);
|
||||
@@ -133,7 +138,8 @@ describe("recoveryModule", () => {
|
||||
expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND);
|
||||
}
|
||||
});
|
||||
it("should throw an error if a user is not found", async () => {
|
||||
|
||||
it("should throw an error if a user is not found", async function() {
|
||||
findOneStub.resolves(mockRecoveryToken);
|
||||
userFindOneStub = sinon.stub(User, "findOne").resolves(null);
|
||||
try {
|
||||
@@ -143,7 +149,8 @@ describe("recoveryModule", () => {
|
||||
expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND);
|
||||
}
|
||||
});
|
||||
it("should throw an error if the passwords match", async () => {
|
||||
|
||||
it("should throw an error if the passwords match", async function() {
|
||||
findOneStub.resolves(mockRecoveryToken);
|
||||
saveStub.resolves();
|
||||
userFindOneStub = sinon
|
||||
@@ -156,7 +163,8 @@ describe("recoveryModule", () => {
|
||||
expect(error.message).to.equal(errorMessages.DB_RESET_PASSWORD_BAD_MATCH);
|
||||
}
|
||||
});
|
||||
it("should return a user without password if successful", async () => {
|
||||
|
||||
it("should return a user without password if successful", async function() {
|
||||
findOneStub.resolves(mockRecoveryToken);
|
||||
saveStub.resolves();
|
||||
userFindOneStub = sinon
|
||||
|
||||
@@ -9,23 +9,26 @@ const mockAppSettings = {
|
||||
appName: "Test App",
|
||||
};
|
||||
|
||||
describe("SettingsModule", () => {
|
||||
describe("SettingsModule", function() {
|
||||
let appSettingsFindOneStub, appSettingsFindOneAndUpdateStub;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
appSettingsFindOneStub = sinon.stub(AppSettings, "findOne");
|
||||
appSettingsFindOneAndUpdateStub = sinon.stub(AppSettings, "findOneAndUpdate");
|
||||
});
|
||||
afterEach(() => {
|
||||
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("getAppSettings", () => {
|
||||
it("should return app settings", async () => {
|
||||
describe("getAppSettings", function() {
|
||||
it("should return app settings", async function() {
|
||||
appSettingsFindOneStub.resolves(mockAppSettings);
|
||||
const result = await getAppSettings();
|
||||
expect(result).to.deep.equal(mockAppSettings);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("Test error");
|
||||
appSettingsFindOneStub.throws(err);
|
||||
try {
|
||||
@@ -35,13 +38,15 @@ describe("SettingsModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("updateAppSettings", () => {
|
||||
it("should update app settings", async () => {
|
||||
|
||||
describe("updateAppSettings", function() {
|
||||
it("should update app settings", async function() {
|
||||
appSettingsFindOneAndUpdateStub.resolves(mockAppSettings);
|
||||
const result = await updateAppSettings(mockAppSettings);
|
||||
expect(result).to.deep.equal(mockAppSettings);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("Test error");
|
||||
appSettingsFindOneAndUpdateStub.throws(err);
|
||||
try {
|
||||
|
||||
@@ -6,18 +6,21 @@ import {
|
||||
import StatusPage from "../../db/models/StatusPage.js";
|
||||
import { errorMessages } from "../../utils/messages.js";
|
||||
|
||||
describe("statusPageModule", () => {
|
||||
describe("statusPageModule", function() {
|
||||
let statusPageFindOneStub, statusPageSaveStub, statusPageFindStub;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
statusPageSaveStub = sinon.stub(StatusPage.prototype, "save");
|
||||
statusPageFindOneStub = sinon.stub(StatusPage, "findOne");
|
||||
statusPageFindStub = sinon.stub(StatusPage, "find");
|
||||
});
|
||||
afterEach(() => {
|
||||
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
describe("createStatusPage", () => {
|
||||
it("should throw an error if a non-unique url is provided", async () => {
|
||||
|
||||
describe("createStatusPage", function() {
|
||||
it("should throw an error if a non-unique url is provided", async function() {
|
||||
statusPageFindOneStub.resolves(true);
|
||||
try {
|
||||
await createStatusPage({ url: "test" });
|
||||
@@ -26,7 +29,8 @@ describe("statusPageModule", () => {
|
||||
expect(error.message).to.equal(errorMessages.STATUS_PAGE_URL_NOT_UNIQUE);
|
||||
}
|
||||
});
|
||||
it("should handle duplicate URL errors", async () => {
|
||||
|
||||
it("should handle duplicate URL errors", async function() {
|
||||
const err = new Error("test");
|
||||
err.code = 11000;
|
||||
statusPageSaveStub.rejects(err);
|
||||
@@ -36,7 +40,8 @@ describe("statusPageModule", () => {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
it("should return a status page if a unique url is provided", async () => {
|
||||
|
||||
it("should return a status page if a unique url is provided", async function() {
|
||||
statusPageFindOneStub.resolves(null);
|
||||
statusPageFindStub.resolves([]);
|
||||
const mockStatusPage = { url: "test" };
|
||||
@@ -45,8 +50,9 @@ describe("statusPageModule", () => {
|
||||
expect(statusPage.url).to.equal(mockStatusPage.url);
|
||||
});
|
||||
});
|
||||
describe("getStatusPageByUrl", () => {
|
||||
it("should throw an error if a status page is not found", async () => {
|
||||
|
||||
describe("getStatusPageByUrl", function() {
|
||||
it("should throw an error if a status page is not found", async function() {
|
||||
statusPageFindOneStub.resolves(null);
|
||||
try {
|
||||
await getStatusPageByUrl("test");
|
||||
@@ -56,7 +62,7 @@ describe("statusPageModule", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("should return a status page if a status page is found", async () => {
|
||||
it("should return a status page if a status page is found", async function() {
|
||||
const mockStatusPage = { url: "test" };
|
||||
statusPageFindOneStub.resolves(mockStatusPage);
|
||||
const statusPage = await getStatusPageByUrl(mockStatusPage.url);
|
||||
|
||||
@@ -27,7 +27,7 @@ const imageFile = {
|
||||
image: 1,
|
||||
};
|
||||
|
||||
describe("userModule", () => {
|
||||
describe("userModule", function() {
|
||||
let teamSaveStub,
|
||||
teamFindByIdAndDeleteStub,
|
||||
userSaveStub,
|
||||
@@ -39,7 +39,8 @@ describe("userModule", () => {
|
||||
userUpdateOneStub,
|
||||
generateAvatarImageStub,
|
||||
parseBooleanStub;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
teamSaveStub = sinon.stub(TeamModel.prototype, "save");
|
||||
teamFindByIdAndDeleteStub = sinon.stub(TeamModel, "findByIdAndDelete");
|
||||
userSaveStub = sinon.stub(UserModel.prototype, "save");
|
||||
@@ -53,12 +54,12 @@ describe("userModule", () => {
|
||||
parseBooleanStub = sinon.stub().returns(true);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
afterEach(function() {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("insertUser", () => {
|
||||
it("should insert a regular user", async () => {
|
||||
describe("insertUser", function() {
|
||||
it("should insert a regular user", async function() {
|
||||
userSaveStub.resolves(mockUser);
|
||||
userFindOneStub.returns({
|
||||
select: sinon.stub().returns({
|
||||
@@ -68,7 +69,8 @@ describe("userModule", () => {
|
||||
const result = await insertUser(mockUser, imageFile, generateAvatarImageStub);
|
||||
expect(result).to.deep.equal(mockUser);
|
||||
});
|
||||
it("should insert a superadmin user", async () => {
|
||||
|
||||
it("should insert a superadmin user", async function() {
|
||||
userSaveStub.resolves(mockSuperUser);
|
||||
userFindOneStub.returns({
|
||||
select: sinon.stub().returns({
|
||||
@@ -78,7 +80,8 @@ describe("userModule", () => {
|
||||
const result = await insertUser(mockSuperUser, imageFile, generateAvatarImageStub);
|
||||
expect(result).to.deep.equal(mockSuperUser);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
userSaveStub.rejects(err);
|
||||
try {
|
||||
@@ -88,7 +91,8 @@ describe("userModule", () => {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
it("should handle a duplicate key error", async () => {
|
||||
|
||||
it("should handle a duplicate key error", async function() {
|
||||
const err = new Error("test error");
|
||||
err.code = 11000;
|
||||
userSaveStub.rejects(err);
|
||||
@@ -100,8 +104,9 @@ describe("userModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("getUserByEmail", () => {
|
||||
it("should return a user", async () => {
|
||||
|
||||
describe("getUserByEmail", function() {
|
||||
it("should return a user", async function() {
|
||||
userFindOneStub.returns({
|
||||
select: sinon.stub().resolves(mockUser),
|
||||
});
|
||||
@@ -109,8 +114,9 @@ describe("userModule", () => {
|
||||
expect(result).to.deep.equal(mockUser);
|
||||
});
|
||||
});
|
||||
describe("getUserByEmail", () => {
|
||||
it("throw an error if a user is not found", async () => {
|
||||
|
||||
describe("getUserByEmail", function() {
|
||||
it("throw an error if a user is not found", async function() {
|
||||
userFindOneStub.returns({
|
||||
select: sinon.stub().resolves(null),
|
||||
});
|
||||
@@ -121,9 +127,11 @@ describe("userModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("updateUser", () => {
|
||||
|
||||
describe("updateUser", function() {
|
||||
let req, res;
|
||||
beforeEach(() => {
|
||||
|
||||
beforeEach(function() {
|
||||
req = {
|
||||
params: {
|
||||
userId: "testId",
|
||||
@@ -140,8 +148,9 @@ describe("userModule", () => {
|
||||
res = {};
|
||||
});
|
||||
|
||||
afterEach(() => {});
|
||||
it("should update a user", async () => {
|
||||
afterEach(function() {});
|
||||
|
||||
it("should update a user", async function() {
|
||||
parseBooleanStub.returns(false);
|
||||
userFindByIdAndUpdateStub.returns({
|
||||
select: sinon.stub().returns({
|
||||
@@ -156,7 +165,8 @@ describe("userModule", () => {
|
||||
);
|
||||
expect(result).to.deep.equal(mockUser);
|
||||
});
|
||||
it("should delete a user profile image", async () => {
|
||||
|
||||
it("should delete a user profile image", async function() {
|
||||
req.body.deleteProfileImage = "true";
|
||||
userFindByIdAndUpdateStub.returns({
|
||||
select: sinon.stub().returns({
|
||||
@@ -171,7 +181,8 @@ describe("userModule", () => {
|
||||
);
|
||||
expect(result).to.deep.equal(mockUser);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
userFindByIdAndUpdateStub.throws(err);
|
||||
try {
|
||||
@@ -182,13 +193,15 @@ describe("userModule", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("deleteUser", async () => {
|
||||
it("should return a deleted user", async () => {
|
||||
|
||||
describe("deleteUser", function() {
|
||||
it("should return a deleted user", async function() {
|
||||
userFindByIdAndDeleteStub.resolves(mockUser);
|
||||
const result = await deleteUser("testId");
|
||||
expect(result).to.deep.equal(mockUser);
|
||||
});
|
||||
it("should throw an error if a user is not found", async () => {
|
||||
|
||||
it("should throw an error if a user is not found", async function() {
|
||||
try {
|
||||
await deleteUser("testId");
|
||||
} catch (error) {
|
||||
@@ -196,7 +209,8 @@ describe("userModule", () => {
|
||||
expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND);
|
||||
}
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
userFindByIdAndDeleteStub.throws(err);
|
||||
try {
|
||||
@@ -208,13 +222,14 @@ describe("userModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteTeam", () => {
|
||||
it("should return true if team deleted", async () => {
|
||||
describe("deleteTeam", function() {
|
||||
it("should return true if team deleted", async function() {
|
||||
teamFindByIdAndDeleteStub.resolves();
|
||||
const result = await deleteTeam("testId");
|
||||
expect(result).to.equal(true);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
teamFindByIdAndDeleteStub.throws(err);
|
||||
try {
|
||||
@@ -226,13 +241,14 @@ describe("userModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteAllOtherUsers", () => {
|
||||
it("should return true if all other users deleted", async () => {
|
||||
describe("deleteAllOtherUsers", function() {
|
||||
it("should return true if all other users deleted", async function() {
|
||||
userDeleteManyStub.resolves(true);
|
||||
const result = await deleteAllOtherUsers();
|
||||
expect(result).to.equal(true);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
userDeleteManyStub.throws(err);
|
||||
try {
|
||||
@@ -244,8 +260,8 @@ describe("userModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllUsers", () => {
|
||||
it("should return all users", async () => {
|
||||
describe("getAllUsers", function() {
|
||||
it("should return all users", async function() {
|
||||
userFindStub.returns({
|
||||
select: sinon.stub().returns({
|
||||
select: sinon.stub().resolves([mockUser]),
|
||||
@@ -254,7 +270,8 @@ describe("userModule", () => {
|
||||
const result = await getAllUsers();
|
||||
expect(result).to.deep.equal([mockUser]);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
userFindStub.throws(err);
|
||||
try {
|
||||
@@ -266,13 +283,14 @@ describe("userModule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("logoutUser", async () => {
|
||||
it("should return true if user logged out", async () => {
|
||||
describe("logoutUser", function() {
|
||||
it("should return true if user logged out", async function() {
|
||||
userUpdateOneStub.resolves(true);
|
||||
const result = await logoutUser("testId");
|
||||
expect(result).to.equal(true);
|
||||
});
|
||||
it("should handle an error", async () => {
|
||||
|
||||
it("should handle an error", async function() {
|
||||
const err = new Error("test error");
|
||||
userUpdateOneStub.throws(err);
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user