2019-01-04 17:13:52 +01:00
|
|
|
/* jslint node:true */
|
|
|
|
|
/* global it:false */
|
|
|
|
|
/* global describe:false */
|
|
|
|
|
/* global before:false */
|
|
|
|
|
/* global after:false */
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
const async = require('async'),
|
2019-10-22 12:59:26 -07:00
|
|
|
BoxError = require('../boxerror.js'),
|
2019-01-04 17:13:52 +01:00
|
|
|
database = require('../database.js'),
|
2021-05-28 14:34:18 -07:00
|
|
|
expect = require('expect.js'),
|
2019-01-04 17:13:52 +01:00
|
|
|
notifications = require('../notifications.js'),
|
2021-05-28 14:34:18 -07:00
|
|
|
safe = require('safetydance');
|
2019-01-04 17:13:52 +01:00
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
const EVENT_0 = {
|
2019-01-21 08:51:04 +01:00
|
|
|
id: 'event_0',
|
2021-05-28 14:34:18 -07:00
|
|
|
action: 'action',
|
2019-01-21 08:51:04 +01:00
|
|
|
source: {},
|
|
|
|
|
data: {}
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-04 17:13:52 +01:00
|
|
|
function setup(done) {
|
|
|
|
|
async.series([
|
|
|
|
|
database.initialize,
|
|
|
|
|
database._clear,
|
|
|
|
|
], done);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cleanup(done) {
|
|
|
|
|
async.series([
|
|
|
|
|
database._clear,
|
|
|
|
|
database.uninitialize
|
|
|
|
|
], done);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('Notifications', function () {
|
|
|
|
|
before(setup);
|
|
|
|
|
after(cleanup);
|
|
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
let notificationIds = [];
|
2019-01-04 17:13:52 +01:00
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
it('can add notifications', async function () {
|
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
|
|
const [error, id] = await safe(notifications._add(EVENT_0.id, `title ${i}`, `message ${i}`));
|
|
|
|
|
expect(error).to.equal(null);
|
|
|
|
|
expect(id).to.be.a('string');
|
|
|
|
|
notificationIds.push(id);
|
|
|
|
|
}
|
2019-01-04 17:13:52 +01:00
|
|
|
});
|
|
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
it('can get by id', async function () {
|
|
|
|
|
const [error, result] = await safe(notifications.get(notificationIds[0]));
|
|
|
|
|
expect(error).to.be(null);
|
|
|
|
|
expect(result.title).to.be('title 0');
|
|
|
|
|
expect(result.message).to.be('message 0');
|
|
|
|
|
expect(result.acknowledged).to.be(false);
|
2019-01-04 17:13:52 +01:00
|
|
|
});
|
|
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
it('cannot get non-existent id', async function () {
|
|
|
|
|
const result = await notifications.get('random');
|
|
|
|
|
expect(result).to.be(null);
|
2019-01-04 17:13:52 +01:00
|
|
|
});
|
|
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
it('can list notifications', async function () {
|
|
|
|
|
const result = await notifications.list({}, 1, 10);
|
|
|
|
|
expect(result.length).to.be(3);
|
|
|
|
|
expect(result[0].title).to.be('title 0');
|
|
|
|
|
expect(result[1].title).to.be('title 1');
|
|
|
|
|
expect(result[2].title).to.be('title 2');
|
2019-01-04 17:13:52 +01:00
|
|
|
});
|
|
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
it('can update notification', async function () {
|
|
|
|
|
await notifications.update({ id: notificationIds[0] }, { title: 'updated title 0', message: 'updated message 0', acknowledged: true });
|
2019-01-04 17:13:52 +01:00
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
const result = await notifications.get(notificationIds[0]);
|
|
|
|
|
expect(result.title).to.be('updated title 0');
|
|
|
|
|
expect(result.message).to.be('updated message 0');
|
|
|
|
|
expect(result.acknowledged).to.be(true);
|
2019-01-04 17:13:52 +01:00
|
|
|
});
|
|
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
it('cannot update non-existent notification', async function () {
|
|
|
|
|
const [error] = await safe(notifications.update({ id: 'random' }, { title: 'updated title 0', message: 'updated message 0', acknowledged: true }));
|
|
|
|
|
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
2019-01-04 17:13:52 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
it('can delete', async function () {
|
|
|
|
|
await notifications.del(notificationIds[0]);
|
2019-01-04 17:13:52 +01:00
|
|
|
});
|
|
|
|
|
|
2021-05-28 14:34:18 -07:00
|
|
|
it('cannot delete non-existent notification', async function () {
|
|
|
|
|
const [error] = await safe(notifications.del('random'));
|
|
|
|
|
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
2019-01-04 17:13:52 +01:00
|
|
|
});
|
|
|
|
|
});
|