-
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 #3719 from airqo-platform/staging
move to production
- Loading branch information
Showing
16 changed files
with
1,373 additions
and
1,926 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
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
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
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
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
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,31 @@ | ||
const validatePagination = (req, res, next) => { | ||
let limit = parseInt(req.query.limit, 10); | ||
const skip = parseInt(req.query.skip, 10); | ||
if (isNaN(limit) || limit < 1) { | ||
limit = 1000; | ||
} | ||
if (limit > 2000) { | ||
limit = 2000; | ||
} | ||
if (isNaN(skip) || skip < 0) { | ||
req.query.skip = 0; | ||
} | ||
req.query.limit = limit; | ||
|
||
next(); | ||
}; | ||
|
||
const headers = (req, res, next) => { | ||
res.setHeader("Access-Control-Allow-Origin", "*"); | ||
res.header( | ||
"Access-Control-Allow-Headers", | ||
"Origin, X-Requested-With, Content-Type, Accept, Authorization" | ||
); | ||
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); | ||
next(); | ||
}; | ||
|
||
module.exports = { | ||
validatePagination, | ||
headers, | ||
}; |
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,82 @@ | ||
require("module-alias/register"); | ||
const { expect } = require("chai"); | ||
const sinon = require("sinon"); | ||
const middleware = require("@middleware/common"); | ||
|
||
describe("Middleware", () => { | ||
describe("validatePagination", () => { | ||
it("should set default limit if not provided", () => { | ||
const req = { query: {} }; | ||
const res = {}; | ||
const next = sinon.spy(); | ||
|
||
middleware.validatePagination(req, res, next); | ||
|
||
expect(req.query.limit).to.equal(1000); | ||
expect(next.calledOnce).to.be.true; | ||
}); | ||
|
||
it("should set limit to 2000 if provided limit is greater", () => { | ||
const req = { query: { limit: "3000" } }; | ||
const res = {}; | ||
const next = sinon.spy(); | ||
|
||
middleware.validatePagination(req, res, next); | ||
|
||
expect(req.query.limit).to.equal(2000); | ||
expect(next.calledOnce).to.be.true; | ||
}); | ||
|
||
it("should set skip to 0 if not provided or invalid", () => { | ||
const req = { query: { skip: "invalid" } }; | ||
const res = {}; | ||
const next = sinon.spy(); | ||
|
||
middleware.validatePagination(req, res, next); | ||
|
||
expect(req.query.skip).to.equal(0); | ||
expect(next.calledOnce).to.be.true; | ||
}); | ||
|
||
it("should not modify valid limit and skip values", () => { | ||
const req = { query: { limit: "500", skip: "100" } }; | ||
const res = {}; | ||
const next = sinon.spy(); | ||
|
||
middleware.validatePagination(req, res, next); | ||
|
||
expect(req.query.limit).to.equal(500); | ||
expect(req.query.skip).to.equal(100); | ||
expect(next.calledOnce).to.be.true; | ||
}); | ||
}); | ||
|
||
describe("headers", () => { | ||
it("should set the correct headers", () => { | ||
const req = {}; | ||
const res = { | ||
setHeader: sinon.spy(), | ||
header: sinon.spy(), | ||
}; | ||
const next = sinon.spy(); | ||
|
||
middleware.headers(req, res, next); | ||
|
||
expect(res.setHeader.calledWith("Access-Control-Allow-Origin", "*")).to.be | ||
.true; | ||
expect( | ||
res.header.calledWith( | ||
"Access-Control-Allow-Headers", | ||
"Origin, X-Requested-With, Content-Type, Accept, Authorization" | ||
) | ||
).to.be.true; | ||
expect( | ||
res.header.calledWith( | ||
"Access-Control-Allow-Methods", | ||
"GET, POST, PUT, DELETE" | ||
) | ||
).to.be.true; | ||
expect(next.calledOnce).to.be.true; | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.