Add validation for monitor URL and update test case and api documentation accordingly
This commit is contained in:
1 parent
7f0d5d4255
commit
aee7e02587
4 files changed
+28
-5
No files matched your search
@@ -3,6 +3,7 @@ import {
|
|||||||
getMonitorByIdQueryValidation,
|
getMonitorByIdQueryValidation,
|
||||||
getMonitorsByTeamIdValidation,
|
getMonitorsByTeamIdValidation,
|
||||||
createMonitorBodyValidation,
|
createMonitorBodyValidation,
|
||||||
|
getMonitorURLByQueryValidation,
|
||||||
editMonitorBodyValidation,
|
editMonitorBodyValidation,
|
||||||
getMonitorsAndSummaryByTeamIdParamValidation,
|
getMonitorsAndSummaryByTeamIdParamValidation,
|
||||||
getMonitorsAndSummaryByTeamIdQueryValidation,
|
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).
|
* @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 {
|
||||||
|
await getMonitorURLByQueryValidation.validateAsync(req.query);
|
||||||
|
} catch (error) {
|
||||||
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let { monitorURL } = req.query;
|
let { monitorURL } = req.query;
|
||||||
monitorURL = new URL(monitorURL);
|
monitorURL = new URL(monitorURL);
|
||||||
|
|||||||
@@ -882,6 +882,16 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "Unprocessable Content",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/ErrorResponse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"500": {
|
"500": {
|
||||||
"description": "Internal Server Error",
|
"description": "Internal Server Error",
|
||||||
"content": {
|
"content": {
|
||||||
|
|||||||
@@ -480,7 +480,7 @@ describe("Monitor Controllor - checkEndpointResolution", () => {
|
|||||||
})).to.be.true;
|
})).to.be.true;
|
||||||
expect(next.called).to.be.false;
|
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");
|
const dnsError = new Error("DNS resolution failed");
|
||||||
dnsError.code = 'ENOTFOUND';
|
dnsError.code = 'ENOTFOUND';
|
||||||
dnsResolveStub.callsFake((hostname, callback) => callback(dnsError));
|
dnsResolveStub.callsFake((hostname, callback) => callback(dnsError));
|
||||||
@@ -491,13 +491,13 @@ describe("Monitor Controllor - checkEndpointResolution", () => {
|
|||||||
expect(errorPassedToNext.message).to.include('DNS resolution failed');
|
expect(errorPassedToNext.message).to.include('DNS resolution failed');
|
||||||
expect(errorPassedToNext.code).to.equal('ENOTFOUND');
|
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';
|
req.query.monitorURL = 'invalid-url';
|
||||||
await checkEndpointResolution(req, res, next);
|
await checkEndpointResolution(req, res, next);
|
||||||
expect(next.calledOnce).to.be.true;
|
expect(next.calledOnce).to.be.true;
|
||||||
const error = next.getCall(0).args[0];
|
const error = next.getCall(0).args[0];
|
||||||
expect(error).to.be.an.instanceOf(TypeError);
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
expect(error.message).to.include('Invalid URL');
|
expect(next.firstCall.args[0].status).to.equal(422);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -236,12 +236,16 @@ const pauseMonitorParamValidation = joi.object({
|
|||||||
monitorId: joi.string().required(),
|
monitorId: joi.string().required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const getMonitorURLByQueryValidation = joi.object({
|
||||||
|
monitorURL: joi.string().uri().required(),
|
||||||
|
});
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Alerts
|
// Alerts
|
||||||
//****************************************
|
//****************************************
|
||||||
|
|
||||||
const createAlertParamValidation = joi.object({
|
const createAlertParamValidation = joi.object({
|
||||||
monitorId: joi.string().required(),
|
monitorId: joi.string().uri().required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const createAlertBodyValidation = joi.object({
|
const createAlertBodyValidation = joi.object({
|
||||||
@@ -446,6 +450,7 @@ export {
|
|||||||
getCertificateParamValidation,
|
getCertificateParamValidation,
|
||||||
editMonitorBodyValidation,
|
editMonitorBodyValidation,
|
||||||
pauseMonitorParamValidation,
|
pauseMonitorParamValidation,
|
||||||
|
getMonitorURLByQueryValidation,
|
||||||
editUserParamValidation,
|
editUserParamValidation,
|
||||||
editUserBodyValidation,
|
editUserBodyValidation,
|
||||||
createAlertParamValidation,
|
createAlertParamValidation,
|
||||||
|
|||||||
Reference in new issue
Block a user