Merge branch 'develop' into feat/fe/infrastructure-details-connect-to-BE

This commit is contained in:
Shemy Gan committed 2024-11-19 21:50:09 -05:00
commit d12420e7ce
4 files changed
+29 -29

No files matched your search

+8 -10
View File
@@ -19,7 +19,7 @@ import jwt from "jsonwebtoken";
import { getTokenFromHeaders } from "../utils/utils.js"; import { getTokenFromHeaders } from "../utils/utils.js";
import logger from "../utils/logger.js"; import logger from "../utils/logger.js";
import { handleError, handleValidationError } from "./controllerUtils.js"; import { handleError, handleValidationError } from "./controllerUtils.js";
import dns from "dns"; import axios from "axios";
const SERVICE_NAME = "monitorController"; const SERVICE_NAME = "monitorController";
@@ -288,18 +288,16 @@ const checkEndpointResolution = async (req, res, next) => {
} }
try { try {
let { monitorURL } = req.query; const { monitorURL } = req.query;
monitorURL = new URL(monitorURL); const parsedUrl = new URL(monitorURL);
await new Promise((resolve, reject) => { const response = await axios.get(parsedUrl, {
dns.resolve(monitorURL.hostname, (error) => { timeout: 5000,
if (error) { validateStatus: () => true,
reject(error);
}
resolve();
});
}); });
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
code: response.status,
statusText: response.statusText,
msg: `URL resolved successfully`, msg: `URL resolved successfully`,
}); });
} catch (error) { } catch (error) {
+7 -7
View File
@@ -11,7 +11,7 @@
"dependencies": { "dependencies": {
"axios": "^1.7.2", "axios": "^1.7.2",
"bcrypt": "^5.1.1", "bcrypt": "^5.1.1",
"bullmq": "5.26.2", "bullmq": "5.28.0",
"cors": "^2.8.5", "cors": "^2.8.5",
"dockerode": "4.0.2", "dockerode": "4.0.2",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
@@ -1217,9 +1217,9 @@
} }
}, },
"node_modules/bullmq": { "node_modules/bullmq": {
"version": "5.26.2", "version": "5.28.0",
"resolved": "https://registry.npmjs.org/bullmq/-/bullmq-5.26.2.tgz", "resolved": "https://registry.npmjs.org/bullmq/-/bullmq-5.28.0.tgz",
"integrity": "sha512-UdHBrJoRkpXoF8b/FVEkuRBnaUZoA7+qHQNyTx1n2oNVZ4iWxqGqss+M9xAwXOpBmSNvOSlaBdHpf+5QJTU8GQ==", "integrity": "sha512-HyYr6bfbBuzfyUvRkP6Ux1Zrv2eZcM4zXMl90Q7oneORp3jKnXYyqjCewJGAoo4PqKyE4TWCcdUnFkdxAzysng==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"cron-parser": "^4.6.0", "cron-parser": "^4.6.0",
@@ -4522,9 +4522,9 @@
} }
}, },
"node_modules/mongoose": { "node_modules/mongoose": {
"version": "8.8.1", "version": "8.8.2",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.8.1.tgz", "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.8.2.tgz",
"integrity": "sha512-l7DgeY1szT98+EKU8GYnga5WnyatAu+kOQ2VlVX1Mxif6A0Umt0YkSiksCiyGxzx8SPhGe9a53ND1GD4yVDrPA==", "integrity": "sha512-jCTSqDANfRzk909v4YoZQi7jlGRB2MTvgG+spVBc/BA4tOs1oWJr//V6yYujqNq9UybpOtsSfBqxI0dSOEFJHQ==",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"bson": "^6.7.0", "bson": "^6.7.0",
+1 -1
View File
@@ -14,7 +14,7 @@
"dependencies": { "dependencies": {
"axios": "^1.7.2", "axios": "^1.7.2",
"bcrypt": "^5.1.1", "bcrypt": "^5.1.1",
"bullmq": "5.26.2", "bullmq": "5.28.0",
"cors": "^2.8.5", "cors": "^2.8.5",
"dockerode": "4.0.2", "dockerode": "4.0.2",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
@@ -18,7 +18,7 @@ import jwt from "jsonwebtoken";
import sinon from "sinon"; import sinon from "sinon";
import { successMessages } from "../../utils/messages.js"; import { successMessages } from "../../utils/messages.js";
import logger from "../../utils/logger.js"; import logger from "../../utils/logger.js";
import dns from "dns"; import axios from "axios";
const SERVICE_NAME = "monitorController"; const SERVICE_NAME = "monitorController";
describe("Monitor Controller - getAllMonitors", () => { describe("Monitor Controller - getAllMonitors", () => {
@@ -476,38 +476,40 @@ describe("Monitor Controller - createMonitor", () => {
}); });
}); });
describe("Monitor Controllor - checkEndpointResolution", () => { describe("Monitor Controller - checkEndpointResolution", () => {
let req, res, next, dnsResolveStub; let req, res, next, axiosGetStub;
beforeEach(() => { beforeEach(() => {
req = { query: { monitorURL: "https://example.com" } }; req = { query: { monitorURL: "https://example.com" } };
res = { status: sinon.stub().returnsThis(), json: sinon.stub() }; res = { status: sinon.stub().returnsThis(), json: sinon.stub() };
next = sinon.stub(); next = sinon.stub();
dnsResolveStub = sinon.stub(dns, "resolve"); axiosGetStub = sinon.stub(axios, "get");
}); });
afterEach(() => { afterEach(() => {
dnsResolveStub.restore(); sinon.restore();
}); });
it("should resolve the URL successfully", async () => { it("should resolve the URL successfully", async () => {
dnsResolveStub.callsFake((hostname, callback) => callback(null)); axiosGetStub.resolves({ status: 200, statusText: "OK" });
await checkEndpointResolution(req, res, next); await checkEndpointResolution(req, res, next);
expect(res.status.calledWith(200)).to.be.true; expect(res.status.calledWith(200)).to.be.true;
expect( expect(
res.json.calledWith({ res.json.calledWith({
success: true, success: true,
code: 200,
statusText: "OK",
msg: "URL resolved successfully", msg: "URL resolved successfully",
}) })
).to.be.true; ).to.be.true;
expect(next.called).to.be.false; expect(next.called).to.be.false;
}); });
it("should return an error if DNS resolution fails", async () => { it("should return an error if endpoint resolution fails", async () => {
const dnsError = new Error("DNS resolution failed"); const axiosError = new Error("resolution failed");
dnsError.code = "ENOTFOUND"; axiosError.code = "ENOTFOUND";
dnsResolveStub.callsFake((hostname, callback) => callback(dnsError)); axiosGetStub.rejects(axiosError);
await checkEndpointResolution(req, res, next); await checkEndpointResolution(req, res, next);
expect(next.calledOnce).to.be.true; expect(next.calledOnce).to.be.true;
const errorPassedToNext = next.getCall(0).args[0]; const errorPassedToNext = next.getCall(0).args[0];
expect(errorPassedToNext).to.be.an.instanceOf(Error); expect(errorPassedToNext).to.be.an.instanceOf(Error);
expect(errorPassedToNext.message).to.include("DNS resolution failed"); expect(errorPassedToNext.message).to.include("resolution failed");
expect(errorPassedToNext.code).to.equal("ENOTFOUND"); expect(errorPassedToNext.code).to.equal("ENOTFOUND");
expect(errorPassedToNext.status).to.equal(500); expect(errorPassedToNext.status).to.equal(500);
}); });