eventlog: async'ify
This commit is contained in:
@@ -14,7 +14,6 @@ const appdb = require('../appdb.js'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
database = require('../database'),
|
||||
domaindb = require('../domaindb'),
|
||||
eventlogdb = require('../eventlogdb.js'),
|
||||
expect = require('expect.js'),
|
||||
groupdb = require('../groupdb.js'),
|
||||
hat = require('../hat.js'),
|
||||
@@ -1306,185 +1305,6 @@ describe('database', function () {
|
||||
|
||||
});
|
||||
|
||||
describe('eventlog', function () {
|
||||
|
||||
it('add succeeds', function (done) {
|
||||
eventlogdb.add('someid', 'some.event', { ip: '1.2.3.4' }, { appId: 'thatapp' }, function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result).to.equal('someid');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('get succeeds', function (done) {
|
||||
eventlogdb.get('someid', function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result.id).to.be('someid');
|
||||
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) {
|
||||
eventlogdb.get('notfoundid', function (error, result) {
|
||||
expect(error).to.be.a(BoxError);
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
expect(result).to.not.be.ok();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('getAllPaged succeeds', function (done) {
|
||||
eventlogdb.getAllPaged([], null, 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('someid');
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
it('getAllPaged succeeds with source search', function (done) {
|
||||
eventlogdb.getAllPaged([], '1.2.3.4', 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('someid');
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
it('getAllPaged succeeds with data search', function (done) {
|
||||
eventlogdb.getAllPaged([], 'thatapp', 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('someid');
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
it('upsert with no existing entry succeeds', function (done) {
|
||||
eventlogdb.upsert('logineventid', 'user.login', { ip: '1.2.3.4' }, { appId: 'thatapp' }, function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result).to.equal('logineventid');
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('upsert with existing entry succeeds', function (done) {
|
||||
eventlogdb.get('logineventid', function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
|
||||
var oldCreationTime = result.creationTime;
|
||||
|
||||
// now wait 2sec
|
||||
setTimeout(function () {
|
||||
eventlogdb.upsert('logineventid_notused', 'user.login', { ip: '1.2.3.4' }, { appId: 'thatapp' }, function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result).to.equal('logineventid');
|
||||
|
||||
eventlogdb.get('logineventid', function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
// should have changed
|
||||
expect(oldCreationTime).to.not.equal(result.creationTime);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
}, 2000);
|
||||
});
|
||||
});
|
||||
|
||||
it('upsert with existing old entry succeeds', function (done) {
|
||||
var yesterday = new Date();
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
|
||||
database.query('INSERT INTO eventlog (id, action, source, data, creationTime) VALUES (?, ?, ?, ?, ?)', [ 'anotherid', 'user.login2', JSON.stringify({ ip: '1.2.3.4' }), JSON.stringify({ appId: 'thatapp' }), yesterday ], function (error) {
|
||||
expect(error).to.equal(null);
|
||||
|
||||
eventlogdb.upsert('anotherid_new', 'user.login2', { ip: '1.2.3.4' }, { appId: 'thatapp' }, function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result).to.equal('anotherid_new');
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('delByCreationTime succeeds', function (done) {
|
||||
async.each([ 'persistent.event', 'transient.event', 'anothertransient.event', 'anotherpersistent.event' ], function (e, callback) {
|
||||
eventlogdb.add('someid' + Math.random(), e, { ip: '1.2.3.4' }, { appId: 'thatapp' }, callback);
|
||||
}, function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
eventlogdb.delByCreationTime(new Date(Date.now() + 1000), function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
eventlogdb.getAllPaged([], null, 1, 100, function (error, results) {
|
||||
expect(error).to.be(null);
|
||||
expect(results.length).to.be(0);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('delByCreationTime succeeds with notifications referencing it', function (done) {
|
||||
async.each([ 'persistent.event', 'transient.event', 'anothertransient.event', 'anotherpersistent.event' ], function (e, callback) {
|
||||
var eventId = 'someid' + Math.random();
|
||||
|
||||
eventlogdb.add(eventId, e, { ip: '1.2.3.4' }, { appId: 'thatapp' }, function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
var notification = {
|
||||
userId: USER_0.id,
|
||||
eventId: eventId,
|
||||
title: 'first one',
|
||||
message: 'some message there',
|
||||
};
|
||||
|
||||
notificationdb.add(notification, callback);
|
||||
});
|
||||
}, function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
eventlogdb.delByCreationTime(new Date(), function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
eventlogdb.getAllPaged([], null, 1, 100, function (error, results) {
|
||||
expect(error).to.be(null);
|
||||
expect(results.length).to.be(0);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('groups', function () {
|
||||
before(function (done) {
|
||||
async.series([
|
||||
|
||||
Reference in New Issue
Block a user