eventlog: async'ify
This commit is contained in:
@@ -6,11 +6,12 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var async = require('async'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
const async = require('async'),
|
||||
database = require('../database.js'),
|
||||
delay = require('delay'),
|
||||
eventlog = require('../eventlog.js'),
|
||||
expect = require('expect.js');
|
||||
expect = require('expect.js'),
|
||||
notifications = require('../notifications.js');
|
||||
|
||||
function setup(done) {
|
||||
// ensure data/config/mount paths
|
||||
@@ -33,68 +34,111 @@ describe('Eventlog', function () {
|
||||
|
||||
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();
|
||||
it('add succeeds', async function () {
|
||||
const id = await eventlog.add('some.event', { ip: '1.2.3.4' }, { appId: 'thatapp' });
|
||||
expect(id).to.be.a('string');
|
||||
|
||||
eventId = result.id;
|
||||
|
||||
done();
|
||||
});
|
||||
eventId = id;
|
||||
});
|
||||
|
||||
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);
|
||||
it('get succeeds', async function () {
|
||||
const result = await eventlog.get(eventId);
|
||||
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();
|
||||
});
|
||||
expect(result.source).to.be.eql({ ip: '1.2.3.4' });
|
||||
expect(result.data).to.be.eql({ appId: 'thatapp' });
|
||||
});
|
||||
|
||||
it('get of unknown id fails', function (done) {
|
||||
eventlog.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('get of unknown id fails', async function () {
|
||||
const result = await eventlog.get('notfoundid');
|
||||
expect(result).to.be(null);
|
||||
});
|
||||
|
||||
it('getAllPaged succeeds', function (done) {
|
||||
eventlog.getAllPaged([], null, 1, 1, function (error, results) {
|
||||
expect(error).to.be(null);
|
||||
expect(results).to.be.an(Array);
|
||||
expect(results.length).to.be(1);
|
||||
it('getAllPaged succeeds', async function () {
|
||||
const results = await eventlog.getAllPaged([], null, 1, 1);
|
||||
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();
|
||||
});
|
||||
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' });
|
||||
});
|
||||
|
||||
it('cleans up token', function (done) {
|
||||
eventlog.cleanup(function (error) {
|
||||
expect(error).to.be(null);
|
||||
it('getAllPaged succeeds with source search', async function () {
|
||||
const results = await eventlog.getAllPaged([], '1.2.3.4', 1, 1);
|
||||
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' });
|
||||
});
|
||||
|
||||
eventlog.get(eventId, function (error, result) { // should not have deleted it
|
||||
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);
|
||||
it('getAllPaged succeeds with data search', async function () {
|
||||
const results = await eventlog.getAllPaged([], 'thatapp', 1, 1);
|
||||
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();
|
||||
});
|
||||
});
|
||||
let loginEventId;
|
||||
it('upsert with no existing entry succeeds', async function () {
|
||||
const result = await eventlog.upsertLoginEvent('user.login', { ip: '1.2.3.4' }, { appId: 'thatapp' });
|
||||
expect(result).to.be.a('string');
|
||||
loginEventId = result;
|
||||
});
|
||||
|
||||
it('upsert with existing entry succeeds', async function () {
|
||||
let result = await eventlog.get(loginEventId);
|
||||
const oldCreationTime = result.creationTime;
|
||||
|
||||
await delay(2000);
|
||||
result = await eventlog.upsertLoginEvent('user.login', { ip: '1.2.3.4' }, { appId: 'thatapp' });
|
||||
expect(result).to.equal(loginEventId);
|
||||
|
||||
result = await eventlog.get(loginEventId);
|
||||
// should have changed
|
||||
expect(oldCreationTime).to.not.equal(result.creationTime);
|
||||
});
|
||||
|
||||
it('upsert with existing old entry succeeds', async function () {
|
||||
let yesterday = new Date();
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
|
||||
await 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 ]);
|
||||
|
||||
const result = await eventlog.upsertLoginEvent('user.login2', { ip: '1.2.3.4' }, { appId: 'thatapp' });
|
||||
expect(result).to.not.equal('anotherid');
|
||||
});
|
||||
|
||||
it('cleans up', async function () {
|
||||
await eventlog._clear();
|
||||
|
||||
for (const e of [ 'persistent.event', 'transient.event', 'anothertransient.event', 'anotherpersistent.event' ]) {
|
||||
const eventId = await eventlog.add(e, { ip: '1.2.3.4' }, { appId: 'thatapp' });
|
||||
|
||||
await notifications._add(eventId, 'title', 'some message');
|
||||
}
|
||||
|
||||
await delay(2000);
|
||||
|
||||
const id = await eventlog.add('some.event', { ip: '1.2.3.4' }, { appId: 'thatapp' });
|
||||
|
||||
await eventlog.cleanup({ creationTime: new Date(Date.now() - 1000) }); // 1 second ago
|
||||
let results = await eventlog.getAllPaged([], null, 1, 100);
|
||||
expect(results.length).to.be(1);
|
||||
|
||||
expect(results[0].id).to.be(id);
|
||||
expect(results[0].action).to.be('some.event');
|
||||
expect(results[0].creationTime).to.be.a(Date);
|
||||
|
||||
results = await notifications.list({}, 1, 10);
|
||||
expect(results.length).to.be(0);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user