Add test for event serializer and fix cause of error

This commit is contained in:
Brandon Keepers 2017-08-09 15:03:40 -05:00
parent 9707c0ba21
commit e9964cb7f8
No known key found for this signature in database
GPG Key ID: F9533396D5FACBF6
3 changed files with 45 additions and 10 deletions

View File

@ -8,6 +8,7 @@ const Raven = require('raven');
const createRobot = require('./lib/robot');
const createServer = require('./lib/server');
const serializers = require('./lib/serializers');
module.exports = (options = {}) => {
const cache = cacheManager.caching({
@ -19,16 +20,7 @@ module.exports = (options = {}) => {
name: 'PRobot',
level: process.env.LOG_LEVEL || 'debug',
stream: bunyanFormat({outputMode: process.env.LOG_FORMAT || 'short'}),
serializers: {
repository: repository => repository.full_name,
event: ({id, event, payload}) => {
return {
id, event,
action: payload.action,
repository: payload.repository && payload.repository.full_name
};
}
}
serializers
});
const webhook = createWebhook({path: '/', secret: options.secret || 'development'});

16
lib/serializers.js Normal file
View File

@ -0,0 +1,16 @@
module.exports = {
repository: repository => repository.full_name,
event: event => {
if (typeof event !== 'object' || !event.payload) {
return event;
} else {
return {
id: event.id,
event: event.event,
action: event.payload.action,
repository: event.payload.repository && event.payload.repository.full_name
};
}
},
installation: installation => installation.account.login
};

27
test/serializers.js Normal file
View File

@ -0,0 +1,27 @@
const expect = require('expect');
const serializers = require('../lib/serializers');
describe('serializers', () => {
describe('event', () => {
it('works with a legit event', () => {
const event = {id: 1, event: 'test', payload: {
action: 'test',
repository: {full_name: 'probot/test'}
}};
expect(serializers.event(event)).toEqual({
id: 1,
event: 'test',
action: 'test',
repository: 'probot/test'
});
});
it('works with boolean', () => {
expect(serializers.event(false)).toBe(false);
});
it('works empty object', () => {
expect(serializers.event({})).toEqual({});
});
});
});