Add test cases for checking monitor endpoint resolution
This commit is contained in:
1 parent
2c85828144
commit
d1e65f53ba
2 files changed
+58
-5
No files matched your search
@@ -274,7 +274,6 @@ const createMonitor = async (req, res, next) => {
|
|||||||
* @returns {Object} The response object with a success status, a message, and the resolution result.
|
* @returns {Object} The response object with a success status, a message, and the resolution result.
|
||||||
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
|
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const checkEndpointResolution = async (req, res, next) => {
|
const checkEndpointResolution = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
let { monitorURL } = req.query;
|
let { monitorURL } = req.query;
|
||||||
@@ -284,12 +283,12 @@ const checkEndpointResolution = async (req, res, next) => {
|
|||||||
// Remove the trailing slash if it exists
|
// Remove the trailing slash if it exists
|
||||||
monitorURL = monitorURL.endsWith('/') ? monitorURL.slice(0, -1) : monitorURL;
|
monitorURL = monitorURL.endsWith('/') ? monitorURL.slice(0, -1) : monitorURL;
|
||||||
|
|
||||||
dns.setServers(['8.8.8.8', '1.1.1.1']);// Google, Cloudfare
|
|
||||||
dns.resolve(monitorURL, (error) => {
|
dns.resolve(monitorURL, (error) => {
|
||||||
console.log("Inside .resolve");
|
|
||||||
if (error) {
|
if (error) {
|
||||||
next(handleError(error, SERVICE_NAME, "checkEndpointResolution"));
|
return res.status(400).json({
|
||||||
return ;
|
success: false,
|
||||||
|
msg: `DNS resolution failed`,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const {
|
|||||||
getMonitorsAndSummaryByTeamId,
|
getMonitorsAndSummaryByTeamId,
|
||||||
getMonitorsByTeamId,
|
getMonitorsByTeamId,
|
||||||
createMonitor,
|
createMonitor,
|
||||||
|
checkEndpointResolution,
|
||||||
deleteMonitor,
|
deleteMonitor,
|
||||||
deleteAllMonitors,
|
deleteAllMonitors,
|
||||||
editMonitor,
|
editMonitor,
|
||||||
@@ -21,6 +22,7 @@ const monitorController = proxyquire("../../controllers/monitorController", {
|
|||||||
"ssl-checker": sslCheckerStub,
|
"ssl-checker": sslCheckerStub,
|
||||||
});
|
});
|
||||||
const logger = require("../../utils/logger");
|
const logger = require("../../utils/logger");
|
||||||
|
const dns = require("dns");
|
||||||
const SERVICE_NAME = "monitorController";
|
const SERVICE_NAME = "monitorController";
|
||||||
|
|
||||||
describe("Monitor Controller - getAllMonitors", () => {
|
describe("Monitor Controller - getAllMonitors", () => {
|
||||||
@@ -473,6 +475,58 @@ describe("Monitor Controller - createMonitor", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('checkEndpointResolution', () => {
|
||||||
|
let dnsResolveStub;
|
||||||
|
beforeEach(() => {
|
||||||
|
req = { query: { monitorURL: 'example.com' } };
|
||||||
|
res = { status: sinon.stub().returnsThis(), json: sinon.stub() };
|
||||||
|
next = sinon.stub();
|
||||||
|
dnsResolveStub = sinon.stub(dns, 'resolve');
|
||||||
|
});
|
||||||
|
afterEach(() => {
|
||||||
|
dnsResolveStub.restore();
|
||||||
|
});
|
||||||
|
it('should resolve the URL successfully', async () => {
|
||||||
|
dnsResolveStub.callsFake((monitorURL, callback) => callback(null));
|
||||||
|
await checkEndpointResolution(req, res, next);
|
||||||
|
expect(res.status.calledWith(200)).to.be.true;
|
||||||
|
expect(res.json.calledWith({
|
||||||
|
success: true,
|
||||||
|
msg: 'URL resolved successfully',
|
||||||
|
})).to.be.true;
|
||||||
|
expect(next.called).to.be.false;
|
||||||
|
});
|
||||||
|
it("should return a 400 error message if DNS resolution fails", async () => {
|
||||||
|
const dnsError = new Error("DNS resolution failed");
|
||||||
|
dnsResolveStub.callsFake((monitorURL, callback) => callback(dnsError));
|
||||||
|
await checkEndpointResolution(req, res, next);
|
||||||
|
expect(res.status.calledOnceWith(400)).to.be.true;
|
||||||
|
expect(res.json.calledOnceWith({
|
||||||
|
success: false,
|
||||||
|
msg: "DNS resolution failed",
|
||||||
|
})).to.be.true;
|
||||||
|
expect(next.notCalled).to.be.true;
|
||||||
|
});
|
||||||
|
it("should remove the trailing slash and resolve the URL successfully", async () => {
|
||||||
|
req.query.monitorURL = 'http://example.com/'; // URL with a trailing slash
|
||||||
|
dnsResolveStub.callsFake((monitorURL, callback) => callback(null));
|
||||||
|
await checkEndpointResolution(req, res, next);
|
||||||
|
expect(res.status.calledWith(200)).to.be.true;
|
||||||
|
expect(res.json.calledWith({
|
||||||
|
success: true,
|
||||||
|
msg: 'URL resolved successfully',
|
||||||
|
})).to.be.true;
|
||||||
|
expect(next.called).to.be.false;
|
||||||
|
});
|
||||||
|
it("should call next with an error if an exception is thrown", async () => {
|
||||||
|
req.query = {}; // Missing monitorURL will cause an error
|
||||||
|
await checkEndpointResolution(req, res, next);
|
||||||
|
expect(next.calledOnce).to.be.true;
|
||||||
|
const error = next.getCall(0).args[0];
|
||||||
|
expect(error).to.be.an.instanceOf(Error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("Monitor Controller - deleteMonitor", () => {
|
describe("Monitor Controller - deleteMonitor", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
req = {
|
req = {
|
||||||
|
|||||||
Reference in new issue
Block a user