restore repo structure
This commit is contained in:
1 parent
6df099a5b6
commit
8b7e3c650b
592 files changed
+37663
-127
No files matched your search
Executable
+593
@@ -0,0 +1,593 @@
|
||||
import sinon from "sinon";
|
||||
import {
|
||||
createCheck,
|
||||
getChecksCount,
|
||||
getChecks,
|
||||
getTeamChecks,
|
||||
deleteChecks,
|
||||
deleteChecksByTeamId,
|
||||
updateChecksTTL,
|
||||
} from "../../db/mongo/modules/checkModule.js";
|
||||
import Check from "../../db/models/Check.js";
|
||||
import Monitor from "../../db/models/Monitor.js";
|
||||
import User from "../../db/models/User.js";
|
||||
import logger from "../../utils/logger.js";
|
||||
|
||||
describe("checkModule", function () {
|
||||
describe("createCheck", function () {
|
||||
let checkCountDocumentsStub, checkSaveStub, monitorFindByIdStub, monitorSaveStub;
|
||||
const mockMonitor = {
|
||||
_id: "123",
|
||||
uptimePercentage: 0.5,
|
||||
status: true,
|
||||
save: () => this,
|
||||
};
|
||||
const mockCheck = { active: true };
|
||||
|
||||
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(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
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 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 function () {
|
||||
mockMonitor.status = false;
|
||||
monitorFindByIdStub.returns(mockMonitor);
|
||||
checkSaveStub.returns(mockCheck);
|
||||
monitorSaveStub.returns(mockMonitor);
|
||||
const check = await createCheck({ monitorId: "123", status: false });
|
||||
expect(check).to.deep.equal(mockCheck);
|
||||
});
|
||||
|
||||
it("should return a check if uptimePercentage is undefined", async function () {
|
||||
mockMonitor.uptimePercentage = undefined;
|
||||
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 uptimePercentage is undefined and status is down", async function () {
|
||||
mockMonitor.uptimePercentage = undefined;
|
||||
monitorFindByIdStub.returns(mockMonitor);
|
||||
checkSaveStub.returns(mockCheck);
|
||||
monitorSaveStub.returns(mockMonitor);
|
||||
const check = await createCheck({ monitorId: "123", status: false });
|
||||
expect(check).to.deep.equal(mockCheck);
|
||||
});
|
||||
|
||||
it("should monitor save error", async function () {
|
||||
const err = new Error("Save Error");
|
||||
monitorSaveStub.throws(err);
|
||||
try {
|
||||
await createCheck({ monitorId: "123" });
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle errors", async function () {
|
||||
const err = new Error("DB Error");
|
||||
checkCountDocumentsStub.throws(err);
|
||||
try {
|
||||
await createCheck({ monitorId: "123" });
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("getChecksCount", function () {
|
||||
let checkCountDocumentStub;
|
||||
|
||||
beforeEach(function () {
|
||||
checkCountDocumentStub = sinon.stub(Check, "countDocuments");
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
checkCountDocumentStub.restore();
|
||||
});
|
||||
|
||||
it("should return count with basic monitorId query", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: {},
|
||||
};
|
||||
checkCountDocumentStub.resolves(5);
|
||||
|
||||
const result = await getChecksCount(req);
|
||||
|
||||
expect(result).to.equal(5);
|
||||
expect(checkCountDocumentStub.calledOnce).to.be.true;
|
||||
expect(checkCountDocumentStub.firstCall.args[0]).to.deep.equal({
|
||||
monitorId: "test123",
|
||||
});
|
||||
});
|
||||
|
||||
it("should include dateRange in query when provided", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { dateRange: "day" },
|
||||
};
|
||||
checkCountDocumentStub.resolves(3);
|
||||
|
||||
const result = await getChecksCount(req);
|
||||
|
||||
expect(result).to.equal(3);
|
||||
expect(checkCountDocumentStub.firstCall.args[0]).to.have.property("createdAt");
|
||||
expect(checkCountDocumentStub.firstCall.args[0].createdAt).to.have.property("$gte");
|
||||
});
|
||||
|
||||
it('should handle "all" filter correctly', async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "all" },
|
||||
};
|
||||
checkCountDocumentStub.resolves(2);
|
||||
|
||||
const result = await getChecksCount(req);
|
||||
|
||||
expect(result).to.equal(2);
|
||||
expect(checkCountDocumentStub.firstCall.args[0]).to.deep.equal({
|
||||
monitorId: "test123",
|
||||
status: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle "down" filter correctly', async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "down" },
|
||||
};
|
||||
checkCountDocumentStub.resolves(2);
|
||||
|
||||
const result = await getChecksCount(req);
|
||||
|
||||
expect(result).to.equal(2);
|
||||
expect(checkCountDocumentStub.firstCall.args[0]).to.deep.equal({
|
||||
monitorId: "test123",
|
||||
status: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle "resolve" filter correctly', async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "resolve" },
|
||||
};
|
||||
checkCountDocumentStub.resolves(1);
|
||||
|
||||
const result = await getChecksCount(req);
|
||||
|
||||
expect(result).to.equal(1);
|
||||
expect(checkCountDocumentStub.firstCall.args[0]).to.deep.equal({
|
||||
monitorId: "test123",
|
||||
status: false,
|
||||
statusCode: 5000,
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle unknown filter correctly", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "unknown" },
|
||||
};
|
||||
checkCountDocumentStub.resolves(1);
|
||||
|
||||
const result = await getChecksCount(req);
|
||||
|
||||
expect(result).to.equal(1);
|
||||
expect(checkCountDocumentStub.firstCall.args[0]).to.deep.equal({
|
||||
monitorId: "test123",
|
||||
status: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("should combine dateRange and filter in query", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: {
|
||||
dateRange: "week",
|
||||
filter: "down",
|
||||
},
|
||||
};
|
||||
checkCountDocumentStub.resolves(4);
|
||||
|
||||
const result = await getChecksCount(req);
|
||||
|
||||
expect(result).to.equal(4);
|
||||
expect(checkCountDocumentStub.firstCall.args[0]).to.have.all.keys(
|
||||
"monitorId",
|
||||
"createdAt",
|
||||
"status"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getChecks", function () {
|
||||
let checkFindStub, monitorFindStub;
|
||||
|
||||
beforeEach(function () {
|
||||
checkFindStub = sinon.stub(Check, "find").returns({
|
||||
skip: sinon.stub().returns({
|
||||
limit: sinon.stub().returns({
|
||||
sort: sinon.stub().returns([{ id: 1 }, { id: 2 }]),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return checks with basic monitorId query", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: {},
|
||||
};
|
||||
|
||||
const result = await getChecks(req);
|
||||
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it("should return checks with limit query", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { limit: 10 },
|
||||
};
|
||||
|
||||
const result = await getChecks(req);
|
||||
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it("should handle pagination correctly", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: {
|
||||
page: 2,
|
||||
rowsPerPage: 10,
|
||||
},
|
||||
};
|
||||
|
||||
const result = await getChecks(req);
|
||||
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it("should handle dateRange filter", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { dateRange: "week" },
|
||||
};
|
||||
const result = await getChecks(req);
|
||||
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it('should handle "all" filter', async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "all" },
|
||||
};
|
||||
|
||||
await getChecks(req);
|
||||
const result = await getChecks(req);
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it('should handle "down" filter', async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "down" },
|
||||
};
|
||||
|
||||
await getChecks(req);
|
||||
const result = await getChecks(req);
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it('should handle "resolve" filter', async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "resolve" },
|
||||
};
|
||||
|
||||
await getChecks(req);
|
||||
const result = await getChecks(req);
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it('should handle "unknown" filter', async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "unknown" },
|
||||
};
|
||||
|
||||
await getChecks(req);
|
||||
const result = await getChecks(req);
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it("should handle ascending sort order", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { sortOrder: "asc" },
|
||||
};
|
||||
|
||||
await getChecks(req);
|
||||
const result = await getChecks(req);
|
||||
expect(result).to.deep.equal([{ id: 1 }, { id: 2 }]);
|
||||
});
|
||||
|
||||
it("should handle error case", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: {},
|
||||
};
|
||||
|
||||
checkFindStub.throws(new Error("Database error"));
|
||||
|
||||
try {
|
||||
await getChecks(req);
|
||||
} catch (error) {
|
||||
expect(error.message).to.equal("Database error");
|
||||
expect(error.service).to.equal("checkModule");
|
||||
expect(error.method).to.equal("getChecks");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("getTeamChecks", function () {
|
||||
let checkFindStub, checkCountDocumentsStub, monitorFindStub;
|
||||
const mockMonitors = [{ _id: "123" }];
|
||||
|
||||
beforeEach(function () {
|
||||
monitorFindStub = sinon.stub(Monitor, "find").returns({
|
||||
select: sinon.stub().returns(mockMonitors),
|
||||
});
|
||||
checkCountDocumentsStub = sinon.stub(Check, "countDocuments").returns(2);
|
||||
checkFindStub = sinon.stub(Check, "find").returns({
|
||||
skip: sinon.stub().returns({
|
||||
limit: sinon.stub().returns({
|
||||
sort: sinon.stub().returns({
|
||||
select: sinon.stub().returns([{ id: 1 }, { id: 2 }]),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return checks with basic monitorId query", async function () {
|
||||
const req = {
|
||||
params: { teamId: "test123" },
|
||||
query: {},
|
||||
};
|
||||
|
||||
const result = await getTeamChecks(req);
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it("should handle pagination correctly", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { limit: 1, page: 2, rowsPerPage: 10 },
|
||||
};
|
||||
|
||||
const result = await getTeamChecks(req);
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it("should handle dateRange filter", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { dateRange: "week" },
|
||||
};
|
||||
const result = await getTeamChecks(req);
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it('should handle "all" filter', async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "all" },
|
||||
};
|
||||
|
||||
await getChecks(req);
|
||||
const result = await getTeamChecks(req);
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it('should handle "down" filter', async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "down" },
|
||||
};
|
||||
|
||||
await getChecks(req);
|
||||
const result = await getTeamChecks(req);
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it('should handle "resolve" filter', async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "resolve" },
|
||||
};
|
||||
|
||||
await getChecks(req);
|
||||
const result = await getTeamChecks(req);
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it('should handle "unknown" filter', async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { filter: "unknown" },
|
||||
};
|
||||
|
||||
await getChecks(req);
|
||||
const result = await getTeamChecks(req);
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it("should handle ascending sort order", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: { sortOrder: "asc" },
|
||||
};
|
||||
|
||||
await getChecks(req);
|
||||
const result = await getTeamChecks(req);
|
||||
expect(result).to.deep.equal({ checksCount: 2, checks: [{ id: 1 }, { id: 2 }] });
|
||||
});
|
||||
|
||||
it("should handle error case", async function () {
|
||||
const req = {
|
||||
params: { monitorId: "test123" },
|
||||
query: {},
|
||||
};
|
||||
|
||||
checkFindStub.throws(new Error("Database error"));
|
||||
|
||||
try {
|
||||
await getTeamChecks(req);
|
||||
} catch (error) {
|
||||
expect(error.message).to.equal("Database error");
|
||||
expect(error.service).to.equal("checkModule");
|
||||
expect(error.method).to.equal("getTeamChecks");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteChecks", function () {
|
||||
let checkDeleteManyStub;
|
||||
|
||||
beforeEach(function () {
|
||||
checkDeleteManyStub = sinon.stub(Check, "deleteMany").resolves({ deletedCount: 1 });
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
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 function () {
|
||||
checkDeleteManyStub.throws(new Error("Database error"));
|
||||
try {
|
||||
await deleteChecks("123");
|
||||
} catch (error) {
|
||||
expect(error.message).to.equal("Database error");
|
||||
expect(error.method).to.equal("deleteChecks");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteChecksByTeamId", function () {
|
||||
let mockMonitors = [{ _id: 123, save: () => this }];
|
||||
let monitorFindStub, monitorSaveStub, checkDeleteManyStub;
|
||||
|
||||
beforeEach(function () {
|
||||
monitorSaveStub = sinon.stub(Monitor.prototype, "save");
|
||||
monitorFindStub = sinon.stub(Monitor, "find").returns(mockMonitors);
|
||||
checkDeleteManyStub = sinon.stub(Check, "deleteMany").resolves({ deletedCount: 1 });
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return a deleted count", async function () {
|
||||
const result = await deleteChecksByTeamId("123");
|
||||
expect(result).to.equal(1);
|
||||
});
|
||||
|
||||
it("should handle errors", async function () {
|
||||
const err = new Error("DB Error");
|
||||
monitorFindStub.throws(err);
|
||||
try {
|
||||
const result = await deleteChecksByTeamId("123");
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateChecksTTL", function () {
|
||||
let userUpdateManyStub;
|
||||
let loggerStub;
|
||||
|
||||
beforeEach(function () {
|
||||
loggerStub = sinon.stub(logger, "error");
|
||||
userUpdateManyStub = sinon.stub(User, "updateMany");
|
||||
Check.collection = { dropIndex: sinon.stub(), createIndex: sinon.stub() };
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("Drop Index Error");
|
||||
Check.collection.dropIndex.throws(err);
|
||||
await updateChecksTTL("123", 10);
|
||||
expect(loggerStub.calledOnce).to.be.true;
|
||||
expect(loggerStub.firstCall.args[0].message).to.equal(err.message);
|
||||
});
|
||||
|
||||
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 {
|
||||
await updateChecksTTL("123", 10);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
|
||||
it("should throw an error if User.updateMany throws an error", async function () {
|
||||
const err = new Error("Update Many Error");
|
||||
userUpdateManyStub.throws(err);
|
||||
try {
|
||||
await updateChecksTTL("123", 10);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Executable
+139
@@ -0,0 +1,139 @@
|
||||
import sinon from "sinon";
|
||||
import HardwareCheck from "../../db/models/HardwareCheck.js";
|
||||
import { createHardwareCheck } from "../../db/mongo/modules/hardwareCheckModule.js";
|
||||
import Monitor from "../../db/models/Monitor.js";
|
||||
import logger from "../../utils/logger.js";
|
||||
|
||||
const mockHardwareCheck = {
|
||||
data: {
|
||||
cpu: {
|
||||
physical_core: 4,
|
||||
logical_core: 8,
|
||||
frequency: 4800,
|
||||
current_frequency: 1411,
|
||||
temperature: [45, 50, 46, 47, 45, 50, 46, 47],
|
||||
free_percent: 0.8552990910595134,
|
||||
usage_percent: 0.14470090894048657,
|
||||
},
|
||||
memory: {
|
||||
total_bytes: 16467628032,
|
||||
available_bytes: 7895044096,
|
||||
used_bytes: 6599561216,
|
||||
usage_percent: 0.4008,
|
||||
},
|
||||
disk: [
|
||||
{
|
||||
read_speed_bytes: null,
|
||||
write_speed_bytes: null,
|
||||
total_bytes: 931258499072,
|
||||
free_bytes: 737097256960,
|
||||
usage_percent: 0.1661,
|
||||
},
|
||||
],
|
||||
host: {
|
||||
os: "linux",
|
||||
platform: "ubuntu",
|
||||
kernel_version: "6.8.0-48-generic",
|
||||
},
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
metric: ["cpu.temperature"],
|
||||
err: "unable to read CPU temperature",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const mockMonitor = {
|
||||
_id: "123",
|
||||
uptimePercentage: 1,
|
||||
status: true,
|
||||
save: () => this,
|
||||
};
|
||||
|
||||
describe("HardwareCheckModule", function () {
|
||||
let hardwareCheckSaveStub,
|
||||
hardwareCheckCountDocumentsStub,
|
||||
monitorFindByIdStub,
|
||||
loggerStub;
|
||||
|
||||
beforeEach(function () {
|
||||
loggerStub = sinon.stub(logger, "error");
|
||||
hardwareCheckSaveStub = sinon.stub(HardwareCheck.prototype, "save");
|
||||
monitorFindByIdStub = sinon.stub(Monitor, "findById");
|
||||
hardwareCheckCountDocumentsStub = sinon.stub(HardwareCheck, "countDocuments");
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("createHardwareCheck", function () {
|
||||
it("should return a hardware check", async function () {
|
||||
hardwareCheckSaveStub.resolves(mockHardwareCheck);
|
||||
monitorFindByIdStub.resolves(mockMonitor);
|
||||
hardwareCheckCountDocumentsStub.resolves(1);
|
||||
const hardwareCheck = await createHardwareCheck({ status: true });
|
||||
expect(hardwareCheck).to.exist;
|
||||
expect(hardwareCheck).to.deep.equal(mockHardwareCheck);
|
||||
});
|
||||
|
||||
it("should return a hardware check for a check with status false", async function () {
|
||||
hardwareCheckSaveStub.resolves(mockHardwareCheck);
|
||||
monitorFindByIdStub.resolves(mockMonitor);
|
||||
hardwareCheckCountDocumentsStub.resolves(1);
|
||||
const hardwareCheck = await createHardwareCheck({ status: false });
|
||||
expect(hardwareCheck).to.exist;
|
||||
expect(hardwareCheck).to.deep.equal(mockHardwareCheck);
|
||||
});
|
||||
|
||||
it("should handle an error", async function () {
|
||||
const err = new Error("test error");
|
||||
monitorFindByIdStub.resolves(mockMonitor);
|
||||
hardwareCheckSaveStub.rejects(err);
|
||||
try {
|
||||
await createHardwareCheck({});
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
|
||||
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 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 function () {
|
||||
monitorFindByIdStub.resolves({
|
||||
...mockMonitor,
|
||||
uptimePercentage: undefined,
|
||||
});
|
||||
hardwareCheckSaveStub.resolves(mockHardwareCheck);
|
||||
hardwareCheckCountDocumentsStub.resolves(1);
|
||||
const res = await createHardwareCheck({ status: true });
|
||||
expect(res).to.exist;
|
||||
});
|
||||
|
||||
it("should handle a monitor with undefined uptimePercentage and false status", async function () {
|
||||
monitorFindByIdStub.resolves({
|
||||
...mockMonitor,
|
||||
uptimePercentage: undefined,
|
||||
});
|
||||
hardwareCheckSaveStub.resolves(mockHardwareCheck);
|
||||
hardwareCheckCountDocumentsStub.resolves(1);
|
||||
const res = await createHardwareCheck({ status: false });
|
||||
expect(res).to.exist;
|
||||
});
|
||||
});
|
||||
});
|
||||
Executable
+110
@@ -0,0 +1,110 @@
|
||||
import sinon from "sinon";
|
||||
import InviteToken from "../../db/models/InviteToken.js";
|
||||
import {
|
||||
requestInviteToken,
|
||||
getInviteToken,
|
||||
getInviteTokenAndDelete,
|
||||
} from "../../db/mongo/modules/inviteModule.js";
|
||||
import { errorMessages } from "../../utils/messages.js";
|
||||
|
||||
describe("Invite Module", function () {
|
||||
const mockUserData = {
|
||||
email: "test@test.com",
|
||||
teamId: "123",
|
||||
role: ["admin"],
|
||||
token: "123",
|
||||
};
|
||||
const mockInviteToken = { _id: 123, time: 123 };
|
||||
let inviteTokenDeleteManyStub,
|
||||
inviteTokenSaveStub,
|
||||
inviteTokenFindOneStub,
|
||||
inviteTokenFindOneAndDeleteStub;
|
||||
|
||||
beforeEach(function () {
|
||||
inviteTokenDeleteManyStub = sinon.stub(InviteToken, "deleteMany");
|
||||
inviteTokenSaveStub = sinon.stub(InviteToken.prototype, "save");
|
||||
inviteTokenFindOneStub = sinon.stub(InviteToken, "findOne");
|
||||
inviteTokenFindOneAndDeleteStub = sinon.stub(InviteToken, "findOneAndDelete");
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("requestInviteToken", function () {
|
||||
it("should return a new invite token", async function () {
|
||||
inviteTokenDeleteManyStub.resolves();
|
||||
inviteTokenSaveStub.resolves();
|
||||
const inviteToken = await requestInviteToken(mockUserData);
|
||||
expect(inviteToken.email).to.equal(mockUserData.email);
|
||||
expect(inviteToken.role).to.deep.equal(mockUserData.role);
|
||||
expect(inviteToken.token).to.exist;
|
||||
});
|
||||
|
||||
it("should handle an error", async function () {
|
||||
const err = new Error("test error");
|
||||
inviteTokenDeleteManyStub.rejects(err);
|
||||
try {
|
||||
await requestInviteToken(mockUserData);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
inviteTokenFindOneStub.resolves(null);
|
||||
try {
|
||||
await getInviteToken(mockUserData.token);
|
||||
} catch (error) {
|
||||
expect(error.message).to.equal(errorMessages.AUTH_INVITE_NOT_FOUND);
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle DB errors", async function () {
|
||||
const err = new Error("test error");
|
||||
inviteTokenFindOneStub.rejects(err);
|
||||
try {
|
||||
await getInviteToken(mockUserData.token);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
expect(error.method).to.equal("getInviteToken");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
inviteTokenFindOneAndDeleteStub.resolves(null);
|
||||
try {
|
||||
await getInviteTokenAndDelete(mockUserData.token);
|
||||
} catch (error) {
|
||||
expect(error.message).to.equal(errorMessages.AUTH_INVITE_NOT_FOUND);
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle DB errors", async function () {
|
||||
const err = new Error("test error");
|
||||
inviteTokenFindOneAndDeleteStub.rejects(err);
|
||||
try {
|
||||
await getInviteTokenAndDelete(mockUserData.token);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
expect(error.method).to.equal("getInviteTokenAndDelete");
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
+283
@@ -0,0 +1,283 @@
|
||||
import sinon from "sinon";
|
||||
import MaintenanceWindow from "../../db/models/MaintenanceWindow.js";
|
||||
import {
|
||||
createMaintenanceWindow,
|
||||
getMaintenanceWindowById,
|
||||
getMaintenanceWindowsByTeamId,
|
||||
getMaintenanceWindowsByMonitorId,
|
||||
deleteMaintenanceWindowById,
|
||||
deleteMaintenanceWindowByMonitorId,
|
||||
deleteMaintenanceWindowByUserId,
|
||||
editMaintenanceWindowById,
|
||||
} from "../../db/mongo/modules/maintenanceWindowModule.js";
|
||||
|
||||
describe("MaintenanceWindow Module", function () {
|
||||
const mockMaintenanceWindow = {
|
||||
monitorId: "123",
|
||||
active: true,
|
||||
oneTime: true,
|
||||
start: 1,
|
||||
end: 20000,
|
||||
};
|
||||
|
||||
let mockMaintenanceWindows = [mockMaintenanceWindow];
|
||||
let maintenanceWindowSaveStub,
|
||||
maintenanceWindowFindByIdStub,
|
||||
maintenanceWindowCountDocumentsStub,
|
||||
maintenanceWindowFindStub,
|
||||
maintenanceWindowFindByIdAndDeleteStub,
|
||||
maintenanceWindowDeleteManyStub,
|
||||
maintenanceWindowFindByIdAndUpdateStub;
|
||||
|
||||
beforeEach(function () {
|
||||
maintenanceWindowSaveStub = sinon.stub(MaintenanceWindow.prototype, "save");
|
||||
maintenanceWindowFindByIdStub = sinon.stub(MaintenanceWindow, "findById");
|
||||
maintenanceWindowCountDocumentsStub = sinon.stub(MaintenanceWindow, "countDocuments");
|
||||
maintenanceWindowFindStub = sinon.stub(MaintenanceWindow, "find").returns({
|
||||
skip: sinon.stub().returns({
|
||||
limit: sinon.stub().returns({
|
||||
sort: sinon.stub().returns(mockMaintenanceWindows),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
maintenanceWindowFindByIdAndDeleteStub = sinon.stub(
|
||||
MaintenanceWindow,
|
||||
"findByIdAndDelete"
|
||||
);
|
||||
maintenanceWindowDeleteManyStub = sinon.stub(MaintenanceWindow, "deleteMany");
|
||||
maintenanceWindowFindByIdAndUpdateStub = sinon.stub(
|
||||
MaintenanceWindow,
|
||||
"findByIdAndUpdate"
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowSaveStub.rejects(err);
|
||||
try {
|
||||
await createMaintenanceWindow(mockMaintenanceWindow);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowFindByIdStub.rejects(err);
|
||||
try {
|
||||
await getMaintenanceWindowById(mockMaintenanceWindow.id);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("getMaintenanceWindowsByTeamId", function () {
|
||||
let query;
|
||||
|
||||
beforeEach(function () {
|
||||
query = {
|
||||
active: true,
|
||||
page: 1,
|
||||
rowsPerPage: 10,
|
||||
field: "name",
|
||||
order: "asc",
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should return a list of maintenance windows and count", async function () {
|
||||
maintenanceWindowCountDocumentsStub.resolves(1);
|
||||
const result = await getMaintenanceWindowsByTeamId(
|
||||
mockMaintenanceWindow.teamId,
|
||||
query
|
||||
);
|
||||
expect(result).to.deep.equal({
|
||||
maintenanceWindows: mockMaintenanceWindows,
|
||||
maintenanceWindowCount: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("should return a list of maintenance windows and count with empty query", async function () {
|
||||
query = undefined;
|
||||
maintenanceWindowCountDocumentsStub.resolves(1);
|
||||
const result = await getMaintenanceWindowsByTeamId(
|
||||
mockMaintenanceWindow.teamId,
|
||||
query
|
||||
);
|
||||
expect(result).to.deep.equal({
|
||||
maintenanceWindows: mockMaintenanceWindows,
|
||||
maintenanceWindowCount: 1,
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
const result = await getMaintenanceWindowsByTeamId(
|
||||
mockMaintenanceWindow.teamId,
|
||||
query
|
||||
);
|
||||
expect(result).to.deep.equal({
|
||||
maintenanceWindows: mockMaintenanceWindows,
|
||||
maintenanceWindowCount: 1,
|
||||
});
|
||||
});
|
||||
|
||||
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(
|
||||
mockMaintenanceWindow.teamId,
|
||||
query
|
||||
);
|
||||
expect(result).to.deep.equal({
|
||||
maintenanceWindows: mockMaintenanceWindows,
|
||||
maintenanceWindowCount: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("should return a list of maintenance windows and count no field", async function () {
|
||||
query.field = undefined;
|
||||
maintenanceWindowCountDocumentsStub.resolves(1);
|
||||
const result = await getMaintenanceWindowsByTeamId(
|
||||
mockMaintenanceWindow.teamId,
|
||||
query
|
||||
);
|
||||
expect(result).to.deep.equal({
|
||||
maintenanceWindows: mockMaintenanceWindows,
|
||||
maintenanceWindowCount: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle an error", async function () {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowCountDocumentsStub.rejects(err);
|
||||
try {
|
||||
await getMaintenanceWindowsByTeamId(mockMaintenanceWindow.teamId, query);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowFindStub.rejects(err);
|
||||
try {
|
||||
await getMaintenanceWindowsByMonitorId(mockMaintenanceWindow.monitorId);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowFindByIdAndDeleteStub.rejects(err);
|
||||
try {
|
||||
await deleteMaintenanceWindowById(mockMaintenanceWindow.id);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowDeleteManyStub.rejects(err);
|
||||
try {
|
||||
await deleteMaintenanceWindowByMonitorId(mockMaintenanceWindow.monitorId);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowDeleteManyStub.rejects(err);
|
||||
try {
|
||||
await deleteMaintenanceWindowByUserId(mockMaintenanceWindow.userId);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("editMaintenanceWindowById", function () {
|
||||
it("should return the updated maintenance window", async function () {
|
||||
maintenanceWindowFindByIdAndUpdateStub.resolves(mockMaintenanceWindow);
|
||||
const result = await editMaintenanceWindowById(
|
||||
mockMaintenanceWindow.id,
|
||||
mockMaintenanceWindow
|
||||
);
|
||||
expect(result).to.deep.equal(mockMaintenanceWindow);
|
||||
});
|
||||
|
||||
it("should handle an error", async function () {
|
||||
const err = new Error("test error");
|
||||
maintenanceWindowFindByIdAndUpdateStub.rejects(err);
|
||||
try {
|
||||
await editMaintenanceWindowById(mockMaintenanceWindow.id, mockMaintenanceWindow);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Executable
+1931
File diff suppressed because it is too large.
Load diff
Executable
+80
@@ -0,0 +1,80 @@
|
||||
import sinon from "sinon";
|
||||
import Notification from "../../db/models/Notification.js";
|
||||
import {
|
||||
createNotification,
|
||||
getNotificationsByMonitorId,
|
||||
deleteNotificationsByMonitorId,
|
||||
} from "../../db/mongo/modules/notificationModule.js";
|
||||
|
||||
describe("notificationModule", function () {
|
||||
const mockNotification = {
|
||||
monitorId: "123",
|
||||
};
|
||||
const mockNotifications = [mockNotification];
|
||||
let notificationSaveStub, notificationFindStub, notificationDeleteManyStub;
|
||||
|
||||
beforeEach(function () {
|
||||
notificationSaveStub = sinon.stub(Notification.prototype, "save").resolves();
|
||||
notificationFindStub = sinon.stub(Notification, "find").resolves();
|
||||
notificationDeleteManyStub = sinon.stub(Notification, "deleteMany").resolves();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
notificationSaveStub.rejects(err);
|
||||
try {
|
||||
await createNotification(mockNotification);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
notificationFindStub.rejects(err);
|
||||
try {
|
||||
await getNotificationsByMonitorId(mockNotification.monitorId);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
notificationDeleteManyStub.rejects(err);
|
||||
try {
|
||||
await deleteNotificationsByMonitorId(mockNotification.monitorId);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Executable
+66
@@ -0,0 +1,66 @@
|
||||
import sinon from "sinon";
|
||||
import PageSpeedCheck from "../../db/models/PageSpeedCheck.js";
|
||||
import {
|
||||
createPageSpeedCheck,
|
||||
deletePageSpeedChecksByMonitorId,
|
||||
} from "../../db/mongo/modules/pageSpeedCheckModule.js";
|
||||
|
||||
const mockPageSpeedCheck = {
|
||||
monitorId: "monitorId",
|
||||
bestPractices: 1,
|
||||
seo: 1,
|
||||
performance: 1,
|
||||
};
|
||||
|
||||
const mockDeletedResult = { deletedCount: 1 };
|
||||
|
||||
describe("pageSpeedCheckModule", function () {
|
||||
let pageSpeedCheckSaveStub, pageSpeedCheckDeleteManyStub;
|
||||
|
||||
beforeEach(function () {
|
||||
pageSpeedCheckSaveStub = sinon.stub(PageSpeedCheck.prototype, "save");
|
||||
pageSpeedCheckDeleteManyStub = sinon.stub(PageSpeedCheck, "deleteMany");
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
pageSpeedCheckSaveStub.rejects(err);
|
||||
try {
|
||||
await expect(createPageSpeedCheck(mockPageSpeedCheck));
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
pageSpeedCheckDeleteManyStub.rejects(err);
|
||||
try {
|
||||
await expect(deletePageSpeedChecksByMonitorId("monitorId"));
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Executable
+179
@@ -0,0 +1,179 @@
|
||||
import sinon from "sinon";
|
||||
import RecoveryToken from "../../db/models/RecoveryToken.js";
|
||||
import User from "../../db/models/User.js";
|
||||
import {
|
||||
requestRecoveryToken,
|
||||
validateRecoveryToken,
|
||||
resetPassword,
|
||||
} from "../../db/mongo/modules/recoveryModule.js";
|
||||
import { errorMessages } from "../../utils/messages.js";
|
||||
|
||||
const mockRecoveryToken = {
|
||||
email: "test@test.com",
|
||||
token: "1234567890",
|
||||
};
|
||||
|
||||
const mockUser = {
|
||||
email: "test@test.com",
|
||||
password: "oldPassword",
|
||||
};
|
||||
|
||||
const mockUserWithoutPassword = {
|
||||
email: "test@test.com",
|
||||
};
|
||||
|
||||
// Create a query builder that logs
|
||||
const createQueryChain = (finalResult, comparePasswordResult = false) => ({
|
||||
select: () => ({
|
||||
select: async () => {
|
||||
if (finalResult === mockUser) {
|
||||
// Return a new object with all required methods
|
||||
return {
|
||||
email: "test@test.com",
|
||||
password: "oldPassword",
|
||||
comparePassword: sinon.stub().resolves(comparePasswordResult),
|
||||
save: sinon.stub().resolves(),
|
||||
};
|
||||
}
|
||||
return finalResult;
|
||||
},
|
||||
}),
|
||||
// Add methods to the top level too
|
||||
comparePassword: sinon.stub().resolves(comparePasswordResult),
|
||||
save: sinon.stub().resolves(),
|
||||
});
|
||||
|
||||
describe("recoveryModule", function () {
|
||||
let deleteManyStub,
|
||||
saveStub,
|
||||
findOneStub,
|
||||
userCompareStub,
|
||||
userSaveStub,
|
||||
userFindOneStub;
|
||||
let req, res;
|
||||
|
||||
beforeEach(function () {
|
||||
req = {
|
||||
body: { email: "test@test.com" },
|
||||
};
|
||||
deleteManyStub = sinon.stub(RecoveryToken, "deleteMany");
|
||||
saveStub = sinon.stub(RecoveryToken.prototype, "save");
|
||||
findOneStub = sinon.stub(RecoveryToken, "findOne");
|
||||
userCompareStub = sinon.stub(User.prototype, "comparePassword");
|
||||
userSaveStub = sinon.stub(User.prototype, "save");
|
||||
userFindOneStub = sinon.stub().resolves();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("Test error");
|
||||
deleteManyStub.rejects(err);
|
||||
try {
|
||||
await requestRecoveryToken(req, res);
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
findOneStub.resolves(null);
|
||||
try {
|
||||
await validateRecoveryToken(req, res);
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND);
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle DB errors", async function () {
|
||||
const err = new Error("Test error");
|
||||
findOneStub.rejects(err);
|
||||
try {
|
||||
await validateRecoveryToken(req, res);
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("resetPassword", function () {
|
||||
beforeEach(function () {
|
||||
req.body = {
|
||||
password: "test",
|
||||
newPassword: "test1",
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it("should thrown an error if a recovery token is not found", async function () {
|
||||
findOneStub.resolves(null);
|
||||
try {
|
||||
await resetPassword(req, res);
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error.message).to.equal(errorMessages.DB_TOKEN_NOT_FOUND);
|
||||
}
|
||||
});
|
||||
|
||||
it("should throw an error if a user is not found", async function () {
|
||||
findOneStub.resolves(mockRecoveryToken);
|
||||
userFindOneStub = sinon.stub(User, "findOne").resolves(null);
|
||||
try {
|
||||
await resetPassword(req, res);
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND);
|
||||
}
|
||||
});
|
||||
|
||||
it("should throw an error if the passwords match", async function () {
|
||||
findOneStub.resolves(mockRecoveryToken);
|
||||
saveStub.resolves();
|
||||
userFindOneStub = sinon
|
||||
.stub(User, "findOne")
|
||||
.returns(createQueryChain(mockUser, true));
|
||||
try {
|
||||
await resetPassword(req, res);
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error.message).to.equal(errorMessages.DB_RESET_PASSWORD_BAD_MATCH);
|
||||
}
|
||||
});
|
||||
|
||||
it("should return a user without password if successful", async function () {
|
||||
findOneStub.resolves(mockRecoveryToken);
|
||||
saveStub.resolves();
|
||||
userFindOneStub = sinon
|
||||
.stub(User, "findOne")
|
||||
.returns(createQueryChain(mockUser)) // First call will resolve to mockUser
|
||||
.onSecondCall()
|
||||
.returns(createQueryChain(mockUserWithoutPassword));
|
||||
const result = await resetPassword(req, res);
|
||||
expect(result).to.deep.equal(mockUserWithoutPassword);
|
||||
});
|
||||
});
|
||||
});
|
||||
Executable
+59
@@ -0,0 +1,59 @@
|
||||
import sinon from "sinon";
|
||||
import {
|
||||
getAppSettings,
|
||||
updateAppSettings,
|
||||
} from "../../db/mongo/modules/settingsModule.js";
|
||||
import AppSettings from "../../db/models/AppSettings.js";
|
||||
|
||||
const mockAppSettings = {
|
||||
appName: "Test App",
|
||||
};
|
||||
|
||||
describe("SettingsModule", function () {
|
||||
let appSettingsFindOneStub, appSettingsFindOneAndUpdateStub;
|
||||
|
||||
beforeEach(function () {
|
||||
appSettingsFindOneStub = sinon.stub(AppSettings, "findOne");
|
||||
appSettingsFindOneAndUpdateStub = sinon.stub(AppSettings, "findOneAndUpdate");
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("Test error");
|
||||
appSettingsFindOneStub.throws(err);
|
||||
try {
|
||||
await getAppSettings();
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("Test error");
|
||||
appSettingsFindOneAndUpdateStub.throws(err);
|
||||
try {
|
||||
await updateAppSettings(mockAppSettings);
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Executable
+73
@@ -0,0 +1,73 @@
|
||||
import sinon from "sinon";
|
||||
import {
|
||||
createStatusPage,
|
||||
getStatusPageByUrl,
|
||||
} from "../../db/mongo/modules/statusPageModule.js";
|
||||
import StatusPage from "../../db/models/StatusPage.js";
|
||||
import { errorMessages } from "../../utils/messages.js";
|
||||
|
||||
describe("statusPageModule", function () {
|
||||
let statusPageFindOneStub, statusPageSaveStub, statusPageFindStub;
|
||||
|
||||
beforeEach(function () {
|
||||
statusPageSaveStub = sinon.stub(StatusPage.prototype, "save");
|
||||
statusPageFindOneStub = sinon.stub(StatusPage, "findOne");
|
||||
statusPageFindStub = sinon.stub(StatusPage, "find");
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
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" });
|
||||
} catch (error) {
|
||||
expect(error.status).to.equal(400);
|
||||
expect(error.message).to.equal(errorMessages.STATUS_PAGE_URL_NOT_UNIQUE);
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle duplicate URL errors", async function () {
|
||||
const err = new Error("test");
|
||||
err.code = 11000;
|
||||
statusPageSaveStub.rejects(err);
|
||||
try {
|
||||
await createStatusPage({ url: "test" });
|
||||
} catch (error) {
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
|
||||
it("should return a status page if a unique url is provided", async function () {
|
||||
statusPageFindOneStub.resolves(null);
|
||||
statusPageFindStub.resolves([]);
|
||||
const mockStatusPage = { url: "test" };
|
||||
const statusPage = await createStatusPage(mockStatusPage);
|
||||
expect(statusPage).to.exist;
|
||||
expect(statusPage.url).to.equal(mockStatusPage.url);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getStatusPageByUrl", function () {
|
||||
it("should throw an error if a status page is not found", async function () {
|
||||
statusPageFindOneStub.resolves(null);
|
||||
try {
|
||||
await getStatusPageByUrl("test");
|
||||
} catch (error) {
|
||||
expect(error.status).to.equal(404);
|
||||
expect(error.message).to.equal(errorMessages.STATUS_PAGE_NOT_FOUND);
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
expect(statusPage).to.exist;
|
||||
expect(statusPage).to.deep.equal(mockStatusPage);
|
||||
});
|
||||
});
|
||||
});
|
||||
Executable
+304
@@ -0,0 +1,304 @@
|
||||
import sinon from "sinon";
|
||||
import UserModel from "../../db/models/User.js";
|
||||
import TeamModel from "../../db/models/Team.js";
|
||||
import {
|
||||
insertUser,
|
||||
getUserByEmail,
|
||||
updateUser,
|
||||
deleteUser,
|
||||
deleteTeam,
|
||||
deleteAllOtherUsers,
|
||||
getAllUsers,
|
||||
logoutUser,
|
||||
} from "../../db/mongo/modules/userModule.js";
|
||||
import { errorMessages } from "../../utils/messages.js";
|
||||
|
||||
const mockUser = {
|
||||
email: "test@test.com",
|
||||
password: "password",
|
||||
role: ["user"],
|
||||
};
|
||||
const mockSuperUser = {
|
||||
email: "test@test.com",
|
||||
password: "password",
|
||||
role: ["superadmin"],
|
||||
};
|
||||
const imageFile = {
|
||||
image: 1,
|
||||
};
|
||||
|
||||
describe("userModule", function () {
|
||||
let teamSaveStub,
|
||||
teamFindByIdAndDeleteStub,
|
||||
userSaveStub,
|
||||
userFindStub,
|
||||
userFindOneStub,
|
||||
userFindByIdAndUpdateStub,
|
||||
userFindByIdAndDeleteStub,
|
||||
userDeleteManyStub,
|
||||
userUpdateOneStub,
|
||||
generateAvatarImageStub,
|
||||
parseBooleanStub;
|
||||
|
||||
beforeEach(function () {
|
||||
teamSaveStub = sinon.stub(TeamModel.prototype, "save");
|
||||
teamFindByIdAndDeleteStub = sinon.stub(TeamModel, "findByIdAndDelete");
|
||||
userSaveStub = sinon.stub(UserModel.prototype, "save");
|
||||
userFindStub = sinon.stub(UserModel, "find");
|
||||
userFindOneStub = sinon.stub(UserModel, "findOne");
|
||||
userFindByIdAndUpdateStub = sinon.stub(UserModel, "findByIdAndUpdate");
|
||||
userFindByIdAndDeleteStub = sinon.stub(UserModel, "findByIdAndDelete");
|
||||
userDeleteManyStub = sinon.stub(UserModel, "deleteMany");
|
||||
userUpdateOneStub = sinon.stub(UserModel, "updateOne");
|
||||
generateAvatarImageStub = sinon.stub().resolves({ image: 2 });
|
||||
parseBooleanStub = sinon.stub().returns(true);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
describe("insertUser", function () {
|
||||
it("should insert a regular user", async function () {
|
||||
userSaveStub.resolves(mockUser);
|
||||
userFindOneStub.returns({
|
||||
select: sinon.stub().returns({
|
||||
select: sinon.stub().resolves(mockUser),
|
||||
}),
|
||||
});
|
||||
const result = await insertUser(mockUser, imageFile, generateAvatarImageStub);
|
||||
expect(result).to.deep.equal(mockUser);
|
||||
});
|
||||
|
||||
it("should insert a superadmin user", async function () {
|
||||
userSaveStub.resolves(mockSuperUser);
|
||||
userFindOneStub.returns({
|
||||
select: sinon.stub().returns({
|
||||
select: sinon.stub().resolves(mockSuperUser),
|
||||
}),
|
||||
});
|
||||
const result = await insertUser(mockSuperUser, imageFile, generateAvatarImageStub);
|
||||
expect(result).to.deep.equal(mockSuperUser);
|
||||
});
|
||||
|
||||
it("should handle an error", async function () {
|
||||
const err = new Error("test error");
|
||||
userSaveStub.rejects(err);
|
||||
try {
|
||||
await insertUser(mockUser, imageFile, generateAvatarImageStub);
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle a duplicate key error", async function () {
|
||||
const err = new Error("test error");
|
||||
err.code = 11000;
|
||||
userSaveStub.rejects(err);
|
||||
try {
|
||||
await insertUser(mockUser, imageFile, generateAvatarImageStub);
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("getUserByEmail", function () {
|
||||
it("should return a user", async function () {
|
||||
userFindOneStub.returns({
|
||||
select: sinon.stub().resolves(mockUser),
|
||||
});
|
||||
const result = await getUserByEmail(mockUser.email);
|
||||
expect(result).to.deep.equal(mockUser);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getUserByEmail", function () {
|
||||
it("throw an error if a user is not found", async function () {
|
||||
userFindOneStub.returns({
|
||||
select: sinon.stub().resolves(null),
|
||||
});
|
||||
try {
|
||||
await getUserByEmail(mockUser.email);
|
||||
} catch (error) {
|
||||
expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateUser", function () {
|
||||
let req, res;
|
||||
|
||||
beforeEach(function () {
|
||||
req = {
|
||||
params: {
|
||||
userId: "testId",
|
||||
},
|
||||
body: {
|
||||
deleteProfileImage: "false",
|
||||
email: "test@test.com",
|
||||
},
|
||||
file: {
|
||||
buffer: "test",
|
||||
mimetype: "test",
|
||||
},
|
||||
};
|
||||
res = {};
|
||||
});
|
||||
|
||||
afterEach(function () {});
|
||||
|
||||
it("should update a user", async function () {
|
||||
parseBooleanStub.returns(false);
|
||||
userFindByIdAndUpdateStub.returns({
|
||||
select: sinon.stub().returns({
|
||||
select: sinon.stub().resolves(mockUser),
|
||||
}),
|
||||
});
|
||||
const result = await updateUser(
|
||||
req,
|
||||
res,
|
||||
parseBooleanStub,
|
||||
generateAvatarImageStub
|
||||
);
|
||||
expect(result).to.deep.equal(mockUser);
|
||||
});
|
||||
|
||||
it("should delete a user profile image", async function () {
|
||||
req.body.deleteProfileImage = "true";
|
||||
userFindByIdAndUpdateStub.returns({
|
||||
select: sinon.stub().returns({
|
||||
select: sinon.stub().resolves(mockUser),
|
||||
}),
|
||||
});
|
||||
const result = await updateUser(
|
||||
req,
|
||||
res,
|
||||
parseBooleanStub,
|
||||
generateAvatarImageStub
|
||||
);
|
||||
expect(result).to.deep.equal(mockUser);
|
||||
});
|
||||
|
||||
it("should handle an error", async function () {
|
||||
const err = new Error("test error");
|
||||
userFindByIdAndUpdateStub.throws(err);
|
||||
try {
|
||||
await updateUser(req, res, parseBooleanStub, generateAvatarImageStub);
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
try {
|
||||
await deleteUser("testId");
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error.message).to.equal(errorMessages.DB_USER_NOT_FOUND);
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle an error", async function () {
|
||||
const err = new Error("test error");
|
||||
userFindByIdAndDeleteStub.throws(err);
|
||||
try {
|
||||
await deleteUser("testId");
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
teamFindByIdAndDeleteStub.throws(err);
|
||||
try {
|
||||
await deleteTeam("testId");
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
userDeleteManyStub.throws(err);
|
||||
try {
|
||||
await deleteAllOtherUsers();
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllUsers", function () {
|
||||
it("should return all users", async function () {
|
||||
userFindStub.returns({
|
||||
select: sinon.stub().returns({
|
||||
select: sinon.stub().resolves([mockUser]),
|
||||
}),
|
||||
});
|
||||
const result = await getAllUsers();
|
||||
expect(result).to.deep.equal([mockUser]);
|
||||
});
|
||||
|
||||
it("should handle an error", async function () {
|
||||
const err = new Error("test error");
|
||||
userFindStub.throws(err);
|
||||
try {
|
||||
await getAllUsers();
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
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 function () {
|
||||
const err = new Error("test error");
|
||||
userUpdateOneStub.throws(err);
|
||||
try {
|
||||
await logoutUser("testId");
|
||||
} catch (error) {
|
||||
expect(error).to.exist;
|
||||
expect(error).to.deep.equal(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user