-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3677 from airqo-platform/en-validation
Refactor and enhance validateOptionalObjectId middleware with comprehensive unit tests
- Loading branch information
Showing
5 changed files
with
155 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/device-registry/middleware/test/ut_validateOptionalObjectId.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
require("module-alias/register"); | ||
const { expect } = require("chai"); | ||
const sinon = require("sinon"); | ||
const mongoose = require("mongoose"); | ||
const { BadRequestError } = require("@utils/errors"); | ||
const validateOptionalObjectId = require("@middleware/validateOptionalObjectId"); | ||
|
||
describe("validateOptionalObjectId", () => { | ||
let req, res, next; | ||
|
||
beforeEach(() => { | ||
req = { | ||
query: {}, | ||
}; | ||
res = {}; | ||
next = sinon.spy(); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it("should call next() if the field is not present in the query", () => { | ||
const middleware = validateOptionalObjectId("testField"); | ||
middleware(req, res, next); | ||
expect(next.calledOnce).to.be.true; | ||
expect(next.args[0]).to.be.empty; | ||
}); | ||
|
||
it("should validate a single valid ObjectId", () => { | ||
const validObjectId = new mongoose.Types.ObjectId().toString(); | ||
req.query.testField = validObjectId; | ||
const middleware = validateOptionalObjectId("testField"); | ||
middleware(req, res, next); | ||
expect(next.calledOnce).to.be.true; | ||
expect(next.args[0]).to.be.empty; | ||
}); | ||
|
||
it("should validate multiple valid ObjectIds", () => { | ||
const validObjectIds = [ | ||
new mongoose.Types.ObjectId().toString(), | ||
new mongoose.Types.ObjectId().toString(), | ||
]; | ||
req.query.testField = validObjectIds.join(","); | ||
const middleware = validateOptionalObjectId("testField"); | ||
middleware(req, res, next); | ||
expect(next.calledOnce).to.be.true; | ||
expect(next.args[0]).to.be.empty; | ||
}); | ||
|
||
it("should throw BadRequestError for a single invalid ObjectId", () => { | ||
req.query.testField = "invalidObjectId"; | ||
const middleware = validateOptionalObjectId("testField"); | ||
expect(() => middleware(req, res, next)).to.throw(BadRequestError); | ||
expect(next.called).to.be.false; | ||
try { | ||
middleware(req, res, next); | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(BadRequestError); | ||
expect(error.message).to.equal("Validation failed for testField"); | ||
expect(error.errors).to.deep.equal([ | ||
"Invalid testField format: invalidObjectId", | ||
]); | ||
} | ||
}); | ||
|
||
it("should throw BadRequestError for multiple ObjectIds with some invalid", () => { | ||
const validObjectId = new mongoose.Types.ObjectId().toString(); | ||
req.query.testField = `${validObjectId},invalidObjectId`; | ||
const middleware = validateOptionalObjectId("testField"); | ||
expect(() => middleware(req, res, next)).to.throw(BadRequestError); | ||
expect(next.called).to.be.false; | ||
try { | ||
middleware(req, res, next); | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(BadRequestError); | ||
expect(error.message).to.equal("Validation failed for testField"); | ||
expect(error.errors).to.deep.equal([ | ||
"Invalid testField format: invalidObjectId", | ||
]); | ||
} | ||
}); | ||
|
||
it("should handle an array of ObjectIds in the query", () => { | ||
const validObjectId = new mongoose.Types.ObjectId().toString(); | ||
req.query.testField = [validObjectId, "invalidObjectId"]; | ||
const middleware = validateOptionalObjectId("testField"); | ||
expect(() => middleware(req, res, next)).to.throw(BadRequestError); | ||
expect(next.called).to.be.false; | ||
try { | ||
middleware(req, res, next); | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(BadRequestError); | ||
expect(error.message).to.equal("Validation failed for testField"); | ||
expect(error.errors).to.deep.equal([ | ||
"Invalid testField format: invalidObjectId", | ||
]); | ||
} | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
src/device-registry/middleware/validateOptionalObjectId.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const { isValidObjectId } = require("mongoose"); | ||
const { BadRequestError } = require("@utils/errors"); | ||
|
||
const validateOptionalObjectId = (field) => { | ||
return (req, res, next) => { | ||
if (req.query[field]) { | ||
let values; | ||
if (Array.isArray(req.query[field])) { | ||
values = req.query[field]; | ||
} else { | ||
values = req.query[field].toString().split(","); | ||
} | ||
|
||
const errors = []; | ||
for (const value of values) { | ||
if (!isValidObjectId(value)) { | ||
errors.push(`Invalid ${field} format: ${value}`); | ||
} | ||
} | ||
|
||
if (errors.length > 0) { | ||
throw new BadRequestError({ | ||
message: `Validation failed for ${field}`, | ||
errors: errors, | ||
}); | ||
} | ||
} | ||
next(); | ||
}; | ||
}; | ||
|
||
module.exports = validateOptionalObjectId; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters