test: extract helpers for logging HTTP errors
Extract two helpers into a shared file: - logAllServerErrors(app) - logServerErrorsOtherThan(statusCode, app)
This commit is contained in:
parent
083fb5d668
commit
4ebc517a78
|
@ -3,6 +3,10 @@
|
|||
const loopback = require('../');
|
||||
const supertest = require('supertest');
|
||||
const strongErrorHandler = require('strong-error-handler');
|
||||
const loggers = require('./helpers/error-loggers');
|
||||
|
||||
const logAllServerErrors = loggers.logAllServerErrors;
|
||||
const logServerErrorsOtherThan = loggers.logServerErrorsOtherThan;
|
||||
|
||||
describe('Authorization scopes', () => {
|
||||
const CUSTOM_SCOPE = 'read:custom';
|
||||
|
@ -15,28 +19,28 @@ describe('Authorization scopes', () => {
|
|||
beforeEach(givenScopedToken);
|
||||
|
||||
it('denies regular token to invoke custom-scoped method', () => {
|
||||
logServerErrorsOtherThan(401);
|
||||
logServerErrorsOtherThan(401, app);
|
||||
return request.get('/users/scoped')
|
||||
.set('Authorization', regularToken.id)
|
||||
.expect(401);
|
||||
});
|
||||
|
||||
it('allows regular tokens to invoke default-scoped method', () => {
|
||||
logAllServerErrors();
|
||||
logAllServerErrors(app);
|
||||
return request.get('/users/' + testUser.id)
|
||||
.set('Authorization', regularToken.id)
|
||||
.expect(200);
|
||||
});
|
||||
|
||||
it('allows scoped token to invoke custom-scoped method', () => {
|
||||
logAllServerErrors();
|
||||
logAllServerErrors(app);
|
||||
return request.get('/users/scoped')
|
||||
.set('Authorization', scopedToken.id)
|
||||
.expect(204);
|
||||
});
|
||||
|
||||
it('denies scoped token to invoke default-scoped method', () => {
|
||||
logServerErrorsOtherThan(401);
|
||||
logServerErrorsOtherThan(401, app);
|
||||
return request.get('/users/' + testUser.id)
|
||||
.set('Authorization', scopedToken.id)
|
||||
.expect(401);
|
||||
|
@ -45,7 +49,7 @@ describe('Authorization scopes', () => {
|
|||
describe('token granted both default and custom scope', () => {
|
||||
beforeEach('given token with default and custom scope',
|
||||
() => givenScopedToken(['DEFAULT', CUSTOM_SCOPE]));
|
||||
beforeEach(logAllServerErrors);
|
||||
beforeEach(() => logAllServerErrors(app));
|
||||
|
||||
it('allows invocation of default-scoped method', () => {
|
||||
return request.get('/users/' + testUser.id)
|
||||
|
@ -116,19 +120,4 @@ describe('Authorization scopes', () => {
|
|||
return testUser.accessTokens.create({ttl: 60, scopes})
|
||||
.then(t => scopedToken = t);
|
||||
}
|
||||
|
||||
function logAllServerErrors() {
|
||||
logServerErrorsOtherThan(-1);
|
||||
}
|
||||
|
||||
function logServerErrorsOtherThan(statusCode) {
|
||||
app.use((err, req, res, next) => {
|
||||
if ((err.statusCode || 500) !== statusCode) {
|
||||
console.log('Unhandled error for request %s %s: %s',
|
||||
req.method, req.url, err.stack || err);
|
||||
}
|
||||
res.statusCode = err.statusCode || 500;
|
||||
res.json(err);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
|
||||
// Node module: loopback
|
||||
// This file is licensed under the MIT License.
|
||||
// License text available at https://opensource.org/licenses/MIT
|
||||
|
||||
'use strict';
|
||||
|
||||
exports.logAllServerErrors = function(app) {
|
||||
exports.logServerErrorsOtherThan(-1, app);
|
||||
};
|
||||
|
||||
exports.logServerErrorsOtherThan = function(statusCode, app) {
|
||||
app.use((err, req, res, next) => {
|
||||
if ((err.statusCode || 500) !== statusCode) {
|
||||
console.log('Unhandled error for request %s %s: %s',
|
||||
req.method, req.url, err.stack || err);
|
||||
}
|
||||
res.statusCode = err.statusCode || 500;
|
||||
res.json(err);
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue