-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
61d78f1
commit 2aae9c0
Showing
5 changed files
with
58 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,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) | ||
} | ||
} |
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,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) | ||
}) | ||
|
||
}) |