diff --git a/src/notificationdb.js b/src/notificationdb.js index 2b338d4d7..94d01e56f 100644 --- a/src/notificationdb.js +++ b/src/notificationdb.js @@ -30,6 +30,7 @@ function add(notification, callback) { const args = [ notification.userId, notification.eventId, notification.title, notification.message, notification.action ]; database.query(query, args, function (error, result) { + if (error && error.code === 'ER_NO_REFERENCED_ROW_2') return callback(new DatabaseError(DatabaseError.NOT_FOUND, 'no such eventlog entry')); if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error)); callback(null, String(result.insertId)); diff --git a/src/notifications.js b/src/notifications.js index 4c490250d..d7f74f140 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -68,6 +68,7 @@ function add(userId, eventId, title, message, action, callback) { message: message, action: action }, function (error, result) { + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new NotificationsError(NotificationsError.NOT_FOUND, error.message)); if (error) return callback(new NotificationsError(NotificationsError.INTERNAL_ERROR, error)); callback(null, { id: result }); diff --git a/src/test/notifications-test.js b/src/test/notifications-test.js index a84af9b0b..cec41e200 100644 --- a/src/test/notifications-test.js +++ b/src/test/notifications-test.js @@ -10,6 +10,7 @@ var async = require('async'), database = require('../database.js'), users = require('../users.js'), userdb = require('../userdb.js'), + eventlogdb = require('../eventlogdb.js'), notifications = require('../notifications.js'), NotificationsError = notifications.NotificationsError, expect = require('expect.js'); @@ -23,6 +24,13 @@ var USER_0 = { displayName: 'User 0' }; +var EVENT_0 = { + id: 'event_0', + action: '', + source: {}, + data: {} +}; + var AUDIT_SOURCE = { ip: '1.2.3.4' }; @@ -40,7 +48,8 @@ function setup(done) { callback(); }); - } + }, + eventlogdb.add.bind(null, EVENT_0.id, EVENT_0.action, EVENT_0.source, EVENT_0.data), ], done); } @@ -57,8 +66,18 @@ describe('Notifications', function () { var notificationId; + it('add fails with unknown event', function (done) { + notifications.add(USER_0.id, 'eventunknown', 'title', 'message text', '/actionurl', function (error, result) { + expect(error).to.be.an('object'); + expect(error.reason).to.equal(NotificationsError.NOT_FOUND); + expect(result).not.to.be.ok(); + + done(); + }); + }); + it('add succeeds', function (done) { - notifications.add(USER_0.id, 'title', 'message text', '/actionurl', function (error, result) { + notifications.add(USER_0.id, EVENT_0.id, 'title', 'message text', '/actionurl', function (error, result) { expect(error).to.eql(null); expect(result.id).to.be.ok(); @@ -146,7 +165,7 @@ describe('Notifications', function () { 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); + notifications.add(USER_0.id, EVENT_0.id, 'title' + n, 'some message', 'some action', callback); }, function (error) { expect(error).to.eql(null);