Add validation for monitor URL and update test case and api documentation accordingly

This commit is contained in:
om-3004
2024-10-17 12:35:15 +05:30
parent 7f0d5d4255
commit aee7e02587
4 changed files with 28 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ import {
getMonitorByIdQueryValidation,
getMonitorsByTeamIdValidation,
createMonitorBodyValidation,
getMonitorURLByQueryValidation,
editMonitorBodyValidation,
getMonitorsAndSummaryByTeamIdParamValidation,
getMonitorsAndSummaryByTeamIdQueryValidation,
@@ -273,6 +274,13 @@ const createMonitor = async (req, res, next) => {
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/
const checkEndpointResolution = async (req, res, next) => {
try {
await getMonitorURLByQueryValidation.validateAsync(req.query);
} catch (error) {
next(handleValidationError(error, SERVICE_NAME));
return;
}
try {
let { monitorURL } = req.query;
monitorURL = new URL(monitorURL);

View File

@@ -882,6 +882,16 @@
}
}
},
"422": {
"description": "Unprocessable Content",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorResponse"
}
}
}
},
"500": {
"description": "Internal Server Error",
"content": {

View File

@@ -480,7 +480,7 @@ describe("Monitor Controllor - checkEndpointResolution", () => {
})).to.be.true;
expect(next.called).to.be.false;
});
it("should return a 400 error message if DNS resolution fails", async () => {
it("should return an error if DNS resolution fails", async () => {
const dnsError = new Error("DNS resolution failed");
dnsError.code = 'ENOTFOUND';
dnsResolveStub.callsFake((hostname, callback) => callback(dnsError));
@@ -491,13 +491,13 @@ describe("Monitor Controllor - checkEndpointResolution", () => {
expect(errorPassedToNext.message).to.include('DNS resolution failed');
expect(errorPassedToNext.code).to.equal('ENOTFOUND');
});
it('should call next with an error for invalid monitorURL', async () => {
it('should reject with an error if query validation fails', async () => {
req.query.monitorURL = 'invalid-url';
await checkEndpointResolution(req, res, next);
expect(next.calledOnce).to.be.true;
const error = next.getCall(0).args[0];
expect(error).to.be.an.instanceOf(TypeError);
expect(error.message).to.include('Invalid URL');
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].status).to.equal(422);
});
});

View File

@@ -236,12 +236,16 @@ const pauseMonitorParamValidation = joi.object({
monitorId: joi.string().required(),
});
const getMonitorURLByQueryValidation = joi.object({
monitorURL: joi.string().uri().required(),
});
//****************************************
// Alerts
//****************************************
const createAlertParamValidation = joi.object({
monitorId: joi.string().required(),
monitorId: joi.string().uri().required(),
});
const createAlertBodyValidation = joi.object({
@@ -446,6 +450,7 @@ export {
getCertificateParamValidation,
editMonitorBodyValidation,
pauseMonitorParamValidation,
getMonitorURLByQueryValidation,
editUserParamValidation,
editUserBodyValidation,
createAlertParamValidation,