Skip to content

Commit

Permalink
Keep safeLog safe
Browse files Browse the repository at this point in the history
Ensure that the logger is checked for existence *before* calling `.trace()` or `.info()` on it.

Add tests that actually check to see that the correct logger method is being called.

Add handler 'TypeError' test to bring coverage up to 100%.
  • Loading branch information
JaredReisinger committed Apr 17, 2020
1 parent 747b797 commit 3e8fbaf
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 25 deletions.
11 changes: 7 additions & 4 deletions lib/handler.js
Expand Up @@ -15,10 +15,13 @@ function normalize(method) {
function safeLog(ctx, message) {
// Helper function to log validation and request info
// Checks for an available swatch logger before logging
if (ctx.swatchCtx.swatchLogLevel === 'trace') {
ctx.swatchCtx.logger.trace(message);
} else if (ctx.swatchCtx.logger) {
ctx.swatchCtx.logger.info(message);
if (ctx.swatchCtx.logger) {
// TODO: what about supporting the other log levels, like 'debug'?
if (ctx.swatchCtx.swatchLogLevel === 'trace') {
ctx.swatchCtx.logger.trace(message);
} else {
ctx.swatchCtx.logger.info(message);
}
}
}

Expand Down
125 changes: 125 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -38,7 +38,8 @@
"eslint-plugin-import": "^2.14.0",
"mocha": "^5.2.0",
"mocha-eslint": "^4.1.0",
"nyc": "^13.0.1"
"nyc": "^13.0.1",
"sinon": "^9.0.2"
},
"directories": {
"lib": "lib",
Expand Down
76 changes: 56 additions & 20 deletions test/handler.test.js
@@ -1,16 +1,24 @@
const { expect } = require('chai');
const sinon = require('sinon');
const handler = require('../lib/handler');

const mockLogger = {
info: () => {},
trace: () => {},
info: sinon.spy(),
trace: sinon.spy(),

reset: () => {
mockLogger.info.resetHistory();
mockLogger.trace.resetHistory();
},
};

beforeEach(mockLogger.reset);

function execHandler(handle, args) {
const mockCtx = {
swatchCtx: {
logger: mockLogger,
swatchLogLevel: 'trace',
// logger: mockLogger,
// swatchLogLevel: 'trace',
testVal: 100,
},
};
Expand All @@ -36,6 +44,10 @@ describe('handler', () => {
expect(() => handler(zeroMethod)).to.throw('invalid_arg_list');
});

it('should throw a TypeError if passed a non-function, non-schema', () => {
expect(() => handler('BOGUS')).to.throw('TypeError');
});

describe('defaults', () => {
it('should throw if arg.default and arg.validate are specified and the default does not validate', () => {
const oneFn = a => (a);
Expand Down Expand Up @@ -424,28 +436,52 @@ describe('handler', () => {
const handle = handler(method);
expect(execHandler(handle, { a: '25', b: '50' })).to.equal(175);
});
it('should respect the swatchLogLevel parameter on swatchCtx', () => {

describe('logger', () => {
const method = {
handler: a => (a),
args: [
{
name: 'a',
parse: Number,
},
],
handler: () => {},
};
const execHandlerWithLogLevel = (handle, args) => {

const handle = handler(method);

const execHandlerWithSwatchContext = (swatchCtx) => {
const mockCtx = {
swatchCtx: {
logger: mockLogger,
swatchLogLevel: 'trace',
},
swatchCtx,
};
handle.validate(mockCtx, args);
handle.validate(mockCtx, {});
return handle.handle(mockCtx);
};
const handle = handler(method);
expect(execHandlerWithLogLevel(handle, { a: '25' })).to.equal(25);

it('should work without a logger', () => {
execHandlerWithSwatchContext({});
expect(mockLogger.info.callCount).to.equal(0);
expect(mockLogger.trace.callCount).to.equal(0);
});

it('should work with a logger', () => {
execHandlerWithSwatchContext({
logger: mockLogger,
});
expect(mockLogger.info.callCount).to.equal(2);
expect(mockLogger.trace.callCount).to.equal(0);
});

it('should work with a logger and logLevel "trace"', () => {
execHandlerWithSwatchContext({
logger: mockLogger,
swatchLogLevel: 'trace',
});
expect(mockLogger.info.callCount).to.equal(0);
expect(mockLogger.trace.callCount).to.equal(2);
});

it('should work with no logger and logLevel "trace"', () => {
execHandlerWithSwatchContext({
swatchLogLevel: 'trace',
});
expect(mockLogger.info.callCount).to.equal(0);
expect(mockLogger.trace.callCount).to.equal(0);
});
});
});

Expand Down

0 comments on commit 3e8fbaf

Please sign in to comment.