Files
cloudron-box/src/test/notifications-test.js
T

114 lines
4.0 KiB
JavaScript
Raw Normal View History

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-06-03 12:20:44 -07:00
const BoxError = require('../boxerror.js'),
common = require('./common.js'),
delay = require('delay'),
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
describe('Notifications', function () {
2021-06-03 12:20:44 -07:00
before(common.setup);
after(common.cleanup);
2019-01-04 17:13:52 +01:00
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);
2021-06-03 12:20:44 -07:00
await delay(1000);
2021-05-28 14:34:18 -07:00
}
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);
2021-06-03 12:20:44 -07:00
expect(result[0].title).to.be('title 2');
2021-05-28 14:34:18 -07:00
expect(result[1].title).to.be('title 1');
2021-06-03 12:20:44 -07:00
expect(result[2].title).to.be('title 0');
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
});
2021-06-23 22:44:30 -07:00
let alertId;
it('can add alert', async function () {
alertId = await notifications.alert(notifications.ALERT_BOX_UPDATE, 'Cloudron xx is available', 'Awesome changelog');
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', 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');
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 delete the alert', async function () {
const id = await notifications.alert(notifications.ALERT_BOX_UPDATE, 'Cloudron xx is available', '');
expect(id).to.be(null);
const result = await notifications.get(alertId);
expect(result).to.be(null);
});
2019-01-04 17:13:52 +01:00
});