Skip to content

Commit

Permalink
Missing scopes error handler (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
annalesniak authored and kiebzak committed Mar 5, 2019
1 parent 61d78f1 commit 2aae9c0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ Currently following middlewares are available:
- removing unspecified query parameters,
- content type validation,
- routing to appropriate controller,
- oauth scopes authorization.
- oauth scopes authorization,
- error handlers (`MissingRequiredScopes`).

### Validation middlewares

Expand Down
2 changes: 1 addition & 1 deletion docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ Before submitting pull request, you can `$ npm run preversion`. It will:

## Fixing README.md

Project's readme is generated using [docs/README.hbs](docs/README.hbs), so please modify this file.
Project's readme is generated using [docs/docs.hbs](docs/docs.hbs), so please modify this file.
3 changes: 2 additions & 1 deletion docs/docs.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ Currently following middlewares are available:
- removing unspecified query parameters,
- content type validation,
- routing to appropriate controller,
- oauth scopes authorization.
- oauth scopes authorization,
- error handlers (`MissingRequiredScopes`).

### Validation middlewares

Expand Down
15 changes: 15 additions & 0 deletions middlewares/error-handlers/missing-scopes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const MissingRequiredScopesError = require('../../errors/MissingRequiredScopes')

/**
* Sends response with `403` status code and message with required scopes
* when `err` is an instance of `MissingRequiredScopes`
*
* @returns {function(err, req, res, next)} error handler
*/
module.exports = () => (err, req, res, next) => {
if (err instanceof MissingRequiredScopesError) {
res.status(403).json({message: `Missing required scopes: ${err.requiredScopes}`})
} else {
return next(err)
}
}
38 changes: 38 additions & 0 deletions test/middlewares/error-handlers/missing-scopes.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const missingScopesHandler = require('../../../middlewares/error-handlers/missing-scopes')()
const MissingRequiredScopes = require('../../../errors/MissingRequiredScopes')

describe('missing scopes error handler', () => {

it('should catch MissingRequiredScopes', () => {
// given
const res = {
json(obj) {
this.message = obj.message
return this
},
status(status) {
this.statusCode = status
return this
}
}

// when
missingScopesHandler(new MissingRequiredScopes(['write']), undefined, res, undefined)

// then
expect(res.statusCode).to.eql(403)
expect(res.message).to.eql('Missing required scopes: write')
})

it('should not catch different errors', () => {
// given
const next = sinon.spy()

// when
missingScopesHandler(new Error(), undefined, undefined, next)

// then
sinon.assert.called(next)
})

})

0 comments on commit 2aae9c0

Please sign in to comment.