Add notification tests for business logic

This commit is contained in:
Johannes Zellner
2019-01-04 17:13:52 +01:00
parent 808be96de3
commit b9daa62ece
3 changed files with 167 additions and 3 deletions
+2 -2
View File
@@ -6,7 +6,7 @@ exports = module.exports = {
add: add,
get: get,
ack: ack,
listPaged: listPaged,
getAllPaged: getAllPaged,
// specialized notifications
userAdded: userAdded
@@ -95,7 +95,7 @@ function ack(id, callback) {
}
// if acknowledged === null we return all, otherwise yes or no based on acknowledged as a boolean
function listPaged(userId, acknowledged, page, perPage, callback) {
function getAllPaged(userId, acknowledged, page, perPage, callback) {
assert.strictEqual(typeof userId, 'string');
assert(acknowledged === null || typeof acknowledged === 'boolean');
assert.strictEqual(typeof page, 'number');
+1 -1
View File
@@ -43,7 +43,7 @@ function list(req, res, next) {
if (req.query.acknowledged && typeof req.query.acknowledged !== 'boolean') return next(new HttpError(400, 'acknowledged must be a boolean'));
notifications.listPaged(req.user.id, typeof req.query.acknowledged === 'undefined' ? null : !!req.query.acknowledged, page, perPage, function (error, result) {
notifications.getAllPaged(req.user.id, typeof req.query.acknowledged === 'undefined' ? null : !!req.query.acknowledged, page, perPage, function (error, result) {
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, { notifications: result }));
+164
View File
@@ -0,0 +1,164 @@
/* jslint node:true */
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
var async = require('async'),
database = require('../database.js'),
users = require('../users.js'),
userdb = require('../userdb.js'),
notifications = require('../notifications.js'),
NotificationsError = notifications.NotificationsError,
expect = require('expect.js');
// owner
var USER_0 = {
username: 'username0',
password: 'Username0pass?1234',
email: 'user0@email.com',
fallbackEmail: 'user0fallback@email.com',
displayName: 'User 0'
};
var AUDIT_SOURCE = {
ip: '1.2.3.4'
};
function setup(done) {
async.series([
database.initialize,
database._clear,
users.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
function (callback) {
userdb.getByUsername(USER_0.username, function (error, result) {
if (error) return callback(error);
USER_0.id = result.id;
callback();
});
}
], done);
}
function cleanup(done) {
async.series([
database._clear,
database.uninitialize
], done);
}
describe('Notifications', function () {
before(setup);
after(cleanup);
var notificationId;
it('add succeeds', function (done) {
notifications.add(USER_0.id, 'title', 'message text', '/actionurl', function (error, result) {
expect(error).to.eql(null);
expect(result.id).to.be.ok();
notificationId = result.id;
done();
});
});
it('get succeeds', function (done) {
notifications.get(notificationId, function (error, result) {
expect(error).to.eql(null);
expect(result.id).to.equal(notificationId);
expect(result.title).to.equal('title');
expect(result.message).to.equal('message text');
expect(result.action).to.equal('/actionurl');
expect(result.acknowledged).to.equal(false);
expect(result.creationTime).to.be.a(Date);
done();
});
});
it('get of unknown id fails', function (done) {
notifications.get('notfoundid', function (error, result) {
expect(error).to.be.a(NotificationsError);
expect(error.reason).to.be(NotificationsError.NOT_FOUND);
expect(result).to.not.be.ok();
done();
});
});
it('ack succeeds', function (done) {
notifications.ack(notificationId, function (error) {
expect(error).to.eql(null);
notifications.get(notificationId, function (error, result) {
expect(error).to.eql(null);
expect(result.acknowledged).to.equal(true);
done();
});
});
});
it('ack succeeds twice', function (done) {
notifications.ack(notificationId, function (error) {
expect(error).to.eql(null);
notifications.get(notificationId, function (error, result) {
expect(error).to.eql(null);
expect(result.acknowledged).to.equal(true);
done();
});
});
});
it('ack fails for nonexisting id', function (done) {
notifications.ack('id does not exist', function (error) {
expect(error).to.be.a(NotificationsError);
expect(error.reason).to.be(NotificationsError.NOT_FOUND);
done();
});
});
it('getAllPaged succeeds', function (done) {
notifications.getAllPaged(USER_0.id, null, 1, 1, function (error, results) {
expect(error).to.eql(null);
expect(results).to.be.an(Array);
expect(results.length).to.be(1);
expect(results[0].id).to.be(notificationId);
expect(results[0].title).to.equal('title');
expect(results[0].message).to.equal('message text');
expect(results[0].action).to.equal('/actionurl');
expect(results[0].acknowledged).to.equal(true);
expect(results[0].creationTime).to.be.a(Date);
done();
});
});
it('getAllPaged succeeds for second page', function (done) {
async.timesSeries(20, function (n, callback) {
notifications.add(USER_0.id, 'title' + n, 'some message', 'some action', callback);
}, function (error) {
expect(error).to.eql(null);
notifications.getAllPaged(USER_0.id, null, 2, 10, function (error, results) {
expect(error).to.eql(null);
expect(results).to.be.an(Array);
expect(results.length).to.be(10);
expect(results[0].title).to.equal('title9');
done();
});
});
});
});