2016-04-30 13:00:44 -07:00
|
|
|
/* jslint node:true */
|
|
|
|
|
/* global it:false */
|
|
|
|
|
/* global describe:false */
|
|
|
|
|
/* global before:false */
|
|
|
|
|
/* global after:false */
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2021-08-13 10:41:10 -07:00
|
|
|
const common = require('./common.js'),
|
2017-11-27 11:48:36 -08:00
|
|
|
database = require('../database.js'),
|
2022-04-15 07:52:35 -05:00
|
|
|
delay = require('../delay.js'),
|
2016-04-30 13:00:44 -07:00
|
|
|
eventlog = require('../eventlog.js'),
|
2021-06-01 09:35:20 -07:00
|
|
|
expect = require('expect.js'),
|
|
|
|
|
notifications = require('../notifications.js');
|
2016-04-30 13:00:44 -07:00
|
|
|
|
2021-06-03 12:20:44 -07:00
|
|
|
describe('Eventlog', function () {
|
2021-08-13 10:41:10 -07:00
|
|
|
const { setup, cleanup } = common;
|
|
|
|
|
|
|
|
|
|
before(setup);
|
|
|
|
|
after(cleanup);
|
2016-04-30 13:00:44 -07:00
|
|
|
|
2021-08-13 10:41:10 -07:00
|
|
|
let eventId;
|
2016-04-30 13:00:44 -07:00
|
|
|
|
2021-08-13 10:41:10 -07:00
|
|
|
it('clear', async function () {
|
|
|
|
|
await eventlog._clear();
|
|
|
|
|
});
|
2016-04-30 13:00:44 -07:00
|
|
|
|
2021-06-01 09:35:20 -07:00
|
|
|
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');
|
2016-04-30 13:00:44 -07:00
|
|
|
|
2021-06-01 09:35:20 -07:00
|
|
|
eventId = id;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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' });
|
|
|
|
|
});
|
2016-04-30 13:00:44 -07:00
|
|
|
|
2021-06-01 09:35:20 -07:00
|
|
|
it('get of unknown id fails', async function () {
|
|
|
|
|
const result = await eventlog.get('notfoundid');
|
|
|
|
|
expect(result).to.be(null);
|
2016-04-30 13:00:44 -07:00
|
|
|
});
|
|
|
|
|
|
2021-08-20 11:27:35 -07:00
|
|
|
it('listPaged succeeds', async function () {
|
|
|
|
|
const results = await eventlog.listPaged([], null, 1, 1);
|
2021-06-01 09:35:20 -07:00
|
|
|
expect(results).to.be.an(Array);
|
|
|
|
|
expect(results.length).to.be(1);
|
2016-04-30 13:00:44 -07:00
|
|
|
|
2021-06-01 09:35:20 -07:00
|
|
|
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' });
|
|
|
|
|
});
|
2016-04-30 13:00:44 -07:00
|
|
|
|
2021-08-20 11:27:35 -07:00
|
|
|
it('listPaged succeeds with source search', async function () {
|
|
|
|
|
const results = await eventlog.listPaged([], '1.2.3.4', 1, 1);
|
2021-06-01 09:35:20 -07:00
|
|
|
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' });
|
2016-04-30 13:00:44 -07:00
|
|
|
});
|
|
|
|
|
|
2021-08-20 11:27:35 -07:00
|
|
|
it('listPaged succeeds with data search', async function () {
|
|
|
|
|
const results = await eventlog.listPaged([], 'thatapp', 1, 1);
|
2021-06-01 09:35:20 -07:00
|
|
|
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' });
|
|
|
|
|
});
|
2016-04-30 13:00:44 -07:00
|
|
|
|
2021-06-01 09:35:20 -07:00
|
|
|
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;
|
2016-04-30 13:00:44 -07:00
|
|
|
});
|
|
|
|
|
|
2021-06-01 09:35:20 -07:00
|
|
|
it('upsert with existing entry succeeds', async function () {
|
|
|
|
|
let result = await eventlog.get(loginEventId);
|
|
|
|
|
const oldCreationTime = result.creationTime;
|
2016-04-30 13:00:44 -07:00
|
|
|
|
2021-06-01 09:35:20 -07:00
|
|
|
await delay(2000);
|
|
|
|
|
result = await eventlog.upsertLoginEvent('user.login', { ip: '1.2.3.4' }, { appId: 'thatapp' });
|
|
|
|
|
expect(result).to.equal(loginEventId);
|
2016-04-30 13:00:44 -07:00
|
|
|
|
2021-06-01 09:35:20 -07:00
|
|
|
result = await eventlog.get(loginEventId);
|
|
|
|
|
// should have changed
|
|
|
|
|
expect(oldCreationTime).to.not.equal(result.creationTime);
|
2016-04-30 13:00:44 -07:00
|
|
|
});
|
2016-07-25 12:36:43 -07:00
|
|
|
|
2021-06-01 09:35:20 -07:00
|
|
|
it('upsert with existing old entry succeeds', async function () {
|
|
|
|
|
let yesterday = new Date();
|
|
|
|
|
yesterday.setDate(yesterday.getDate() - 1);
|
|
|
|
|
|
2021-11-17 12:21:46 -08:00
|
|
|
await database.query('INSERT INTO eventlog (id, action, sourceJson, dataJson, creationTime) VALUES (?, ?, ?, ?, ?)', [ 'anotherid', 'user.login2', JSON.stringify({ ip: '1.2.3.4' }), JSON.stringify({ appId: 'thatapp' }), yesterday ]);
|
2021-06-01 09:35:20 -07:00
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 12:20:44 -07:00
|
|
|
await delay(3000);
|
2021-06-01 09:35:20 -07:00
|
|
|
|
|
|
|
|
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
|
2021-08-20 11:27:35 -07:00
|
|
|
let results = await eventlog.listPaged([], null, 1, 100);
|
2021-06-01 09:35:20 -07:00
|
|
|
expect(results.length).to.be(1);
|
2016-07-25 12:36:43 -07:00
|
|
|
|
2021-06-01 09:35:20 -07:00
|
|
|
expect(results[0].id).to.be(id);
|
|
|
|
|
expect(results[0].action).to.be('some.event');
|
|
|
|
|
expect(results[0].creationTime).to.be.a(Date);
|
2016-07-25 12:36:43 -07:00
|
|
|
|
2021-06-01 09:35:20 -07:00
|
|
|
results = await notifications.list({}, 1, 10);
|
|
|
|
|
expect(results.length).to.be(0);
|
2016-07-25 12:36:43 -07:00
|
|
|
});
|
2016-04-30 13:00:44 -07:00
|
|
|
});
|