diff --git a/src/eventlog.js b/src/eventlog.js index f5b534f13..0a271e215 100644 --- a/src/eventlog.js +++ b/src/eventlog.js @@ -49,6 +49,7 @@ function EventLogError(reason, errorOrMessage) { } util.inherits(EventLogError, Error); EventLogError.INTERNAL_ERROR = 'Internal error'; +EventLogError.NOT_FOUND = 'Not Found'; function add(action, source, data, callback) { assert.strictEqual(typeof action, 'string'); @@ -58,10 +59,11 @@ function add(action, source, data, callback) { callback = callback || NOOP_CALLBACK; - eventlogdb.add(uuid.v4(), action, source, data, function (error) { + var id = uuid.v4(); + eventlogdb.add(id, action, source, data, function (error) { if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error)); - callback(); + callback(null, { id: id }); }); } @@ -70,7 +72,7 @@ function get(id, callback) { assert.strictEqual(typeof callback, 'function'); eventlogdb.get(id, function (error, result) { - if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new EventLogError(EventLogError.NOT_FOUND, 'No such box')); + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new EventLogError(EventLogError.NOT_FOUND, 'No such event')); if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error)); callback(null, result); diff --git a/src/test/eventlog-test.js b/src/test/eventlog-test.js new file mode 100644 index 000000000..f25adf23f --- /dev/null +++ b/src/test/eventlog-test.js @@ -0,0 +1,81 @@ +/* jslint node:true */ +/* global it:false */ +/* global describe:false */ +/* global before:false */ +/* global after:false */ + +'use strict'; + +var database = require('../database.js'), + expect = require('expect.js'), + eventlog = require('../eventlog.js'), + EventLogError = eventlog.EventLogError; + +function setup(done) { + // ensure data/config/mount paths + database.initialize(function (error) { + expect(error).to.be(null); + done(); + }); +} + +function cleanup(done) { + database._clear(done); +} + +describe('Eventlog', function () { + before(setup); + after(cleanup); + + var eventId; + + it('add succeeds', function (done) { + eventlog.add('some.event', { ip: '1.2.3.4' }, { appId: 'thatapp' }, function (error, result) { + expect(error).to.be(null); + expect(result.id).to.be.ok(); + + eventId = result.id; + + done(); + }); + }); + + it('get succeeds', function (done) { + eventlog.get(eventId, function (error, result) { + expect(error).to.be(null); + expect(result.id).to.be(eventId); + expect(result.action).to.be('some.event'); + expect(result.creationTime).to.be.a(Date); + + expect(result.source).to.be.eql({ ip: '1.2.3.4' }); + expect(result.data).to.be.eql({ appId: 'thatapp' }); + + done(); + }); + }); + + it('get of unknown id fails', function (done) { + eventlog.get('notfoundid', function (error, result) { + expect(error).to.be.a(EventLogError); + expect(error.reason).to.be(EventLogError.NOT_FOUND); + expect(result).to.not.be.ok(); + + done(); + }); + }); + + it('getAllPaged succeeds', function (done) { + eventlog.getAllPaged(1, 1, function (error, results) { + expect(error).to.be(null); + expect(results).to.be.an(Array); + expect(results.length).to.be(1); + + expect(results[0].id).to.be(eventId); + expect(results[0].action).to.be('some.event'); + expect(results[0].source).to.be.eql({ ip: '1.2.3.4' }); + expect(results[0].data).to.be.eql({ appId: 'thatapp' }); + + done(); + }); + }); +});