| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-17 21:55:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 16:11:44 +03:00
										 |  |  | var assert = require('assert'); | 
					
						
							| 
									
										
										
										
											2016-11-10 17:55:50 -06:00
										 |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | var db = require('./mocks/databasemock'); | 
					
						
							|  |  |  | var user = require('../src/user'); | 
					
						
							|  |  |  | var notifications = require('../src/notifications'); | 
					
						
							| 
									
										
										
										
											2016-12-15 16:08:32 +03:00
										 |  |  | var socketNotifications = require('../src/socket.io/notifications'); | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | describe('Notifications', function () { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 	var uid; | 
					
						
							|  |  |  | 	var notification; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	before(function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		user.create({ username: 'poster' }, function (err, _uid) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return done(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			uid = _uid; | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	it('should create a notification', function (done) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 		notifications.create({ | 
					
						
							|  |  |  | 			bodyShort: 'bodyShort', | 
					
						
							| 
									
										
										
										
											2016-12-15 16:08:32 +03:00
										 |  |  | 			nid: 'notification_id', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			path: '/notification/path', | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		}, function (err, _notification) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 			notification = _notification; | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert(notification); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			db.exists('notifications:' + notification.nid, function (err, exists) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(exists); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				db.isSortedSetMember('notifications', notification.nid, function (err, isMember) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(isMember); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	it('should get notifications', function (done) { | 
					
						
							|  |  |  | 		notifications.getMultiple([notification.nid], function (err, notificationsData) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert(Array.isArray(notificationsData)); | 
					
						
							|  |  |  | 			assert(notificationsData[0]); | 
					
						
							|  |  |  | 			assert.equal(notification.nid, notificationsData[0].nid); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	it('should push a notification to uid', function (done) { | 
					
						
							|  |  |  | 		notifications.push(notification, [uid], function (err) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			setTimeout(function () { | 
					
						
							|  |  |  | 				db.isSortedSetMember('uid:' + uid + ':notifications:unread', notification.nid, function (err, isMember) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(isMember); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, 2000); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 11:15:22 +03:00
										 |  |  | 	it('should push a notification to a group', function (done) { | 
					
						
							|  |  |  | 		notifications.pushGroup(notification, 'registered-users', function (err) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			setTimeout(function () { | 
					
						
							|  |  |  | 				db.isSortedSetMember('uid:' + uid + ':notifications:unread', notification.nid, function (err, isMember) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(isMember); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, 2000); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should push a notification to groups', function (done) { | 
					
						
							| 
									
										
										
										
											2016-11-18 12:10:22 +03:00
										 |  |  | 		notifications.pushGroups(notification, ['registered-users', 'administrators'], function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-18 11:15:22 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			setTimeout(function () { | 
					
						
							|  |  |  | 				db.isSortedSetMember('uid:' + uid + ':notifications:unread', notification.nid, function (err, isMember) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(isMember); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, 2000); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	it('should mark a notification read', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		socketNotifications.markRead({ uid: uid }, notification.nid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			db.isSortedSetMember('uid:' + uid + ':notifications:unread', notification.nid, function (err, isMember) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(isMember, false); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				db.isSortedSetMember('uid:' + uid + ':notifications:read', notification.nid, function (err, isMember) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(isMember, true); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	it('should mark a notification unread', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		socketNotifications.markUnread({ uid: uid }, notification.nid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			db.isSortedSetMember('uid:' + uid + ':notifications:unread', notification.nid, function (err, isMember) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(isMember, true); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				db.isSortedSetMember('uid:' + uid + ':notifications:read', notification.nid, function (err, isMember) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(isMember, false); | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					socketNotifications.getCount({ uid: uid }, null, function (err, count) { | 
					
						
							| 
									
										
										
										
											2016-12-15 16:08:32 +03:00
										 |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert.equal(count, 1); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should mark all notifications read', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		socketNotifications.markAllRead({ uid: uid }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 16:08:32 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			db.isSortedSetMember('uid:' + uid + ':notifications:unread', notification.nid, function (err, isMember) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(isMember, false); | 
					
						
							|  |  |  | 				db.isSortedSetMember('uid:' + uid + ':notifications:read', notification.nid, function (err, isMember) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(isMember, true); | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 17:55:50 -06:00
										 |  |  | 	it('should link to the first unread post in a watched topic', function (done) { | 
					
						
							|  |  |  | 		var categories = require('../src/categories'); | 
					
						
							|  |  |  | 		var topics = require('../src/topics'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var watcherUid; | 
					
						
							|  |  |  | 		var cid; | 
					
						
							|  |  |  | 		var tid; | 
					
						
							|  |  |  | 		var pid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				user.create({ username: 'watcher' }, next); | 
					
						
							| 
									
										
										
										
											2016-11-10 17:55:50 -06:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function (_watcherUid, next) { | 
					
						
							|  |  |  | 				watcherUid = _watcherUid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				categories.create({ | 
					
						
							|  |  |  | 					name: 'Test Category', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					description: 'Test category created by testing script', | 
					
						
							| 
									
										
										
										
											2016-11-10 17:55:50 -06:00
										 |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (category, next) { | 
					
						
							|  |  |  | 				cid = category.cid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				topics.post({ | 
					
						
							|  |  |  | 					uid: watcherUid, | 
					
						
							|  |  |  | 					cid: cid, | 
					
						
							|  |  |  | 					title: 'Test Topic Title', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					content: 'The content of test topic', | 
					
						
							| 
									
										
										
										
											2016-11-10 17:55:50 -06:00
										 |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (topic, next) { | 
					
						
							|  |  |  | 				tid = topic.topicData.tid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				topics.follow(tid, watcherUid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				topics.reply({ | 
					
						
							|  |  |  | 					uid: uid, | 
					
						
							|  |  |  | 					content: 'This is the first reply.', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					tid: tid, | 
					
						
							| 
									
										
										
										
											2016-11-10 17:55:50 -06:00
										 |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (post, next) { | 
					
						
							|  |  |  | 				pid = post.pid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				topics.reply({ | 
					
						
							|  |  |  | 					uid: uid, | 
					
						
							|  |  |  | 					content: 'This is the second reply.', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					tid: tid, | 
					
						
							| 
									
										
										
										
											2016-11-10 17:55:50 -06:00
										 |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (post, next) { | 
					
						
							|  |  |  | 				// notifications are sent asynchronously with a 1 second delay.
 | 
					
						
							|  |  |  | 				setTimeout(next, 3000); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				user.notifications.get(watcherUid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (notifications, next) { | 
					
						
							|  |  |  | 				assert.equal(notifications.unread.length, 1, 'there should be 1 unread notification'); | 
					
						
							|  |  |  | 				assert.equal('/post/' + pid, notifications.unread[0].path, 'the notification should link to the first unread post'); | 
					
						
							|  |  |  | 				next(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-11-10 17:55:50 -06:00
										 |  |  | 		], function (err) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 16:08:32 +03:00
										 |  |  | 	it('should get notification by nid', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		socketNotifications.get({ uid: uid }, { nids: [notification.nid] }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-15 16:08:32 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(data[0].bodyShort, 'bodyShort'); | 
					
						
							|  |  |  | 			assert.equal(data[0].nid, 'notification_id'); | 
					
						
							|  |  |  | 			assert.equal(data[0].path, '/notification/path'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should get user\'s notifications', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		socketNotifications.get({ uid: uid }, {}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-15 16:08:32 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(data.unread.length, 0); | 
					
						
							|  |  |  | 			assert.equal(data.read[0].nid, 'notification_id'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should error if not logged in', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		socketNotifications.deleteAll({ uid: 0 }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 16:08:32 +03:00
										 |  |  | 			assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should delete all user notifications', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		socketNotifications.deleteAll({ uid: uid }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 16:08:32 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketNotifications.get({ uid: uid }, {}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-15 16:08:32 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(data.unread.length, 0); | 
					
						
							|  |  |  | 				assert.equal(data.read.length, 0); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	after(function (done) { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		db.emptydb(done); | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }); |