128 lines
4.7 KiB
JavaScript
128 lines
4.7 KiB
JavaScript
/* jslint node:true */
|
|
/* global it:false */
|
|
/* global describe:false */
|
|
/* global before:false */
|
|
/* global after:false */
|
|
|
|
'use strict';
|
|
|
|
const BoxError = require('../boxerror.js'),
|
|
common = require('./common.js'),
|
|
expect = require('expect.js'),
|
|
notifications = require('../notifications.js'),
|
|
safe = require('safetydance'),
|
|
timers = require('timers/promises');
|
|
|
|
const EVENT_0 = {
|
|
id: 'event_0',
|
|
action: 'action',
|
|
source: {},
|
|
data: {}
|
|
};
|
|
|
|
describe('Notifications', function () {
|
|
const { setup, cleanup } = common;
|
|
|
|
before(setup);
|
|
after(cleanup);
|
|
|
|
let notificationIds = [];
|
|
|
|
it('can add notifications', async function () {
|
|
for (let i = 0; i < 3; i++) {
|
|
const [error, id] = await safe(notifications._add(`title ${i}`, `message ${i}`, { eventId: EVENT_0.id }));
|
|
expect(error).to.equal(null);
|
|
expect(id).to.be.a('string');
|
|
notificationIds.push(id);
|
|
await timers.setTimeout(1000);
|
|
}
|
|
});
|
|
|
|
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);
|
|
});
|
|
|
|
it('cannot get non-existent id', async function () {
|
|
const result = await notifications.get('random');
|
|
expect(result).to.be(null);
|
|
});
|
|
|
|
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 2');
|
|
expect(result[1].title).to.be('title 1');
|
|
expect(result[2].title).to.be('title 0');
|
|
});
|
|
|
|
it('can update notification', async function () {
|
|
await notifications.update({ id: notificationIds[0] }, { title: 'updated title 0', message: 'updated message 0', acknowledged: true });
|
|
|
|
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);
|
|
});
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
it('can delete', async function () {
|
|
await notifications.del(notificationIds[0]);
|
|
});
|
|
|
|
it('cannot delete non-existent notification', async function () {
|
|
const [error] = await safe(notifications.del('random'));
|
|
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
|
});
|
|
|
|
let alertId;
|
|
it('can add alert', async function () {
|
|
alertId = await notifications.alert(notifications.ALERT_BOX_UPDATE, 'Cloudron xx is available', 'Awesome changelog', { persist: true });
|
|
|
|
const result = await notifications.get(alertId);
|
|
expect(result.title).to.be('Cloudron xx is available');
|
|
expect(result.message).to.be('Awesome changelog');
|
|
expect(result.acknowledged).to.be(false);
|
|
});
|
|
|
|
it('can update the alert (persist)', async function () {
|
|
await notifications.update({ id: alertId }, { acknowledged: true }); // ack the alert
|
|
|
|
const id = await notifications.alert(notifications.ALERT_BOX_UPDATE, 'Cloudron xx is available', 'Awesome new changelog', { persist: true });
|
|
expect(id).to.be(alertId);
|
|
|
|
const result = await notifications.get(alertId);
|
|
expect(result.title).to.be('Cloudron xx is available');
|
|
expect(result.message).to.be('Awesome new changelog');
|
|
expect(result.acknowledged).to.be(false); // notification resurfaces
|
|
});
|
|
|
|
it('can update the alert (non-persist)', async function () {
|
|
await notifications.update({ id: alertId }, { acknowledged: true }); // ack the alert
|
|
|
|
const id = await notifications.alert(notifications.ALERT_BOX_UPDATE, 'Cloudron xx is available', 'Awesome new changelog', { persist: false });
|
|
expect(id).to.be(alertId);
|
|
|
|
const result = await notifications.get(alertId);
|
|
expect(result.title).to.be('Cloudron xx is available');
|
|
expect(result.message).to.be('Awesome new changelog');
|
|
expect(result.acknowledged).to.be(true); // notification does not resurface
|
|
});
|
|
|
|
it('can clear the alert', async function () {
|
|
const id = await notifications.clearAlert(notifications.ALERT_BOX_UPDATE, 'Cloudron xx is available');
|
|
expect(id).to.be(null);
|
|
|
|
const result = await notifications.get(alertId);
|
|
expect(result).to.be(null);
|
|
});
|
|
});
|