test: extract helpers for logging HTTP errors

Extract two helpers into a shared file:

 - logAllServerErrors(app)
 - logServerErrorsOtherThan(statusCode, app)
This commit is contained in:
Miroslav Bajtoš 2017-10-19 13:08:54 +02:00
parent 083fb5d668
commit 4ebc517a78
No known key found for this signature in database
GPG Key ID: 6F2304BA9361C7E3
2 changed files with 30 additions and 20 deletions

View File

@ -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);
});
}
});

View File

@ -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);
});
};