Add tests for addDemoMonitors
This commit is contained in:
1 parent
516212b1b0
commit
607b16d1cd
2 files changed
+83
-1
No files matched your search
@@ -469,7 +469,7 @@ const addDemoMonitors = async (req, res, next) => {
|
|||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
message: successMessages.MONITOR_DEMO_ADDED,
|
msg: successMessages.MONITOR_DEMO_ADDED,
|
||||||
data: demoMonitors.length,
|
data: demoMonitors.length,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -874,3 +874,85 @@ describe("Monitor Controller - pauseMonitor", () => {
|
|||||||
).to.be.true;
|
).to.be.true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("Monitor Controller - addDemoMonitors", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
stub = sinon.stub(jwt, "verify").callsFake(() => {
|
||||||
|
return { _id: "123", teamId: "123" };
|
||||||
|
});
|
||||||
|
req = {
|
||||||
|
headers: {
|
||||||
|
authorization: "Bearer token",
|
||||||
|
},
|
||||||
|
params: {},
|
||||||
|
query: {},
|
||||||
|
body: {},
|
||||||
|
db: {
|
||||||
|
addDemoMonitors: sinon.stub(),
|
||||||
|
},
|
||||||
|
settingsService: {
|
||||||
|
getSettings: sinon.stub(),
|
||||||
|
},
|
||||||
|
jobQueue: {
|
||||||
|
addJob: sinon.stub(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
res = {
|
||||||
|
status: sinon.stub().returnsThis(),
|
||||||
|
json: sinon.stub(),
|
||||||
|
};
|
||||||
|
next = sinon.stub();
|
||||||
|
});
|
||||||
|
afterEach(() => {
|
||||||
|
sinon.restore();
|
||||||
|
stub.restore();
|
||||||
|
});
|
||||||
|
it("should reject with an error if getTokenFromHeaders fails", async () => {
|
||||||
|
req.headers = {};
|
||||||
|
await addDemoMonitors(req, res, next);
|
||||||
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
|
expect(next.firstCall.args[0].message).to.equal("No auth headers");
|
||||||
|
expect(next.firstCall.args[0].status).to.equal(500);
|
||||||
|
});
|
||||||
|
it("should reject with an error if getting settings fails", async () => {
|
||||||
|
req.settingsService.getSettings.throws(new Error("Settings error"));
|
||||||
|
await addDemoMonitors(req, res, next);
|
||||||
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
|
expect(next.firstCall.args[0].message).to.equal("Settings error");
|
||||||
|
});
|
||||||
|
it("should reject with an error if JWT validation fails", async () => {
|
||||||
|
stub.restore();
|
||||||
|
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
|
||||||
|
await addDemoMonitors(req, res, next);
|
||||||
|
expect(next.firstCall.args[0]).to.be.instanceOf(jwt.JsonWebTokenError);
|
||||||
|
});
|
||||||
|
it("should reject with an error if addDemoMonitors operation fails", async () => {
|
||||||
|
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
|
||||||
|
req.db.addDemoMonitors.throws(new Error("DB error"));
|
||||||
|
await addDemoMonitors(req, res, next);
|
||||||
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
|
expect(next.firstCall.args[0].message).to.equal("DB error");
|
||||||
|
});
|
||||||
|
it("should reject with an error if addJob operation fails", async () => {
|
||||||
|
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
|
||||||
|
req.db.addDemoMonitors.returns([{ _id: "123" }]);
|
||||||
|
req.jobQueue.addJob.throws(new Error("Add Job error"));
|
||||||
|
await addDemoMonitors(req, res, next);
|
||||||
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
|
expect(next.firstCall.args[0].message).to.equal("Add Job error");
|
||||||
|
});
|
||||||
|
it("should return success message with data if all operations succeed", async () => {
|
||||||
|
const monitors = [{ _id: "123" }];
|
||||||
|
req.settingsService.getSettings.returns({ jwtSecret: "my_secret" });
|
||||||
|
req.db.addDemoMonitors.returns(monitors);
|
||||||
|
await addDemoMonitors(req, res, next);
|
||||||
|
expect(res.status.firstCall.args[0]).to.equal(200);
|
||||||
|
expect(
|
||||||
|
res.json.calledOnceWith({
|
||||||
|
success: true,
|
||||||
|
msg: successMessages.MONITOR_DEMO_ADDED,
|
||||||
|
data: monitors.length,
|
||||||
|
})
|
||||||
|
).to.be.true;
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user