-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
110 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { serverResponse } from '../helpers'; | ||
|
||
/** | ||
* @name authorizeUser | ||
* @param {Integer} allowedLevel the level given access | ||
* @param {Object} response express response object | ||
* @param {Object} next express next function that calls the next middleware | ||
* @returns {Void} it calls the next middleware | ||
*/ | ||
export default allowedLevel => async (request, response, next) => { | ||
const { level } = request.user; | ||
if (level < allowedLevel) { | ||
return serverResponse(response, 403, { error: 'unauthorized user' }); | ||
} | ||
next(); | ||
}; |
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,43 @@ | ||
import chai, { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import middlewares from '../../server/middlewares'; | ||
|
||
const { authorizeUser } = middlewares; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('authorizeUser middleware', () => { | ||
context( | ||
'when an unauthorized user makes tries to access a protected route', | ||
() => { | ||
it('returns an unauthorized error', async () => { | ||
const request = { user: { level: 2 } }; | ||
const response = { status() {}, json() {} }; | ||
const next = sinon.spy(); | ||
const status = sinon.stub(response, 'status').returnsThis(); | ||
sinon.stub(response, 'json').returns({}); | ||
const authMiddleware = authorizeUser(5); | ||
await authMiddleware(request, response, next); | ||
expect(status).to.calledWith(403); | ||
response.json.restore(); | ||
}); | ||
} | ||
); | ||
|
||
context( | ||
'when an authorized user makes tries to access a protected route', | ||
() => { | ||
it('goes to the next middleware', async () => { | ||
const request = { user: { level: 9 } }; | ||
const response = { status() {}, json() {} }; | ||
const next = sinon.spy(); | ||
sinon.stub(response, 'json').returns({}); | ||
const authMiddleware = authorizeUser(5); | ||
await authMiddleware(request, response, next); | ||
expect(next).to.calledOnce; | ||
response.json.restore(); | ||
}); | ||
} | ||
); | ||
}); |
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