| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-16 21:51:42 +03:00
										 |  |  | var assert = require('assert'); | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2016-10-25 17:56:32 +03:00
										 |  |  | var request = require('request'); | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var db = require('./mocks/databasemock'); | 
					
						
							| 
									
										
										
										
											2016-10-26 12:06:53 +03:00
										 |  |  | var meta = require('../src/meta'); | 
					
						
							| 
									
										
										
										
											2016-10-16 21:51:42 +03:00
										 |  |  | var User = require('../src/user'); | 
					
						
							|  |  |  | var Groups = require('../src/groups'); | 
					
						
							|  |  |  | var Messaging = require('../src/messaging'); | 
					
						
							| 
									
										
										
										
											2016-10-26 12:06:53 +03:00
										 |  |  | var helpers = require('./helpers'); | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 19:59:19 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | describe('Messaging Library', function () { | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 	var fooUid; | 
					
						
							|  |  |  | 	var bazUid; | 
					
						
							|  |  |  | 	var herpUid; | 
					
						
							| 
									
										
										
										
											2016-10-26 12:06:53 +03:00
										 |  |  | 	var roomId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	before(function (done) { | 
					
						
							| 
									
										
										
										
											2016-11-23 17:15:31 +03:00
										 |  |  | 		Groups.resetCache(); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 		// Create 3 users: 1 admin, 2 regular
 | 
					
						
							| 
									
										
										
										
											2016-11-23 17:15:31 +03:00
										 |  |  | 		async.series([ | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 			async.apply(User.create, { username: 'foo', password: 'barbar' }),	// admin
 | 
					
						
							|  |  |  | 			async.apply(User.create, { username: 'baz', password: 'quuxquux' }),	// restricted user
 | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			async.apply(User.create, { username: 'herp', password: 'derpderp' }),	// regular user
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		], function (err, uids) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return done(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 			fooUid = uids[0]; | 
					
						
							|  |  |  | 			bazUid = uids[1]; | 
					
						
							|  |  |  | 			herpUid = uids[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 			async.parallel([ | 
					
						
							| 
									
										
										
										
											2016-11-23 17:15:31 +03:00
										 |  |  | 				async.apply(Groups.join, 'administrators', fooUid), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				async.apply(User.setSetting, bazUid, 'restrictChat', '1'), | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('.canMessage()', function () { | 
					
						
							|  |  |  | 		it('should allow messages to be sent to an unrestricted user', function (done) { | 
					
						
							| 
									
										
										
										
											2016-11-23 17:15:31 +03:00
										 |  |  | 			Messaging.canMessageUser(bazUid, herpUid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should NOT allow messages to be sent to a restricted user', function (done) { | 
					
						
							| 
									
										
										
										
											2016-11-23 17:15:31 +03:00
										 |  |  | 			User.setSetting(bazUid, 'restrictChat', '1', function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				Messaging.canMessageUser(herpUid, bazUid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 					assert.strictEqual(err.message, '[[error:chat-restricted]]'); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should always allow admins through', function (done) { | 
					
						
							| 
									
										
										
										
											2016-11-23 17:15:31 +03:00
										 |  |  | 			Messaging.canMessageUser(fooUid, bazUid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should allow messages to be sent to a restricted user if restricted user follows sender', function (done) { | 
					
						
							| 
									
										
										
										
											2016-11-23 17:15:31 +03:00
										 |  |  | 			User.follow(bazUid, herpUid, function () { | 
					
						
							|  |  |  | 				Messaging.canMessageUser(herpUid, bazUid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 	describe('rooms', function () { | 
					
						
							| 
									
										
										
										
											2016-11-08 19:59:19 +03:00
										 |  |  | 		var socketModules = require('../src/socket.io/modules'); | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 		it('should create a new chat room', function (done) { | 
					
						
							| 
									
										
										
										
											2016-11-08 19:59:19 +03:00
										 |  |  | 			socketModules.chats.newRoom({uid: fooUid}, {touid: bazUid}, function (err, _roomId) { | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 				roomId = _roomId; | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(roomId); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 19:59:19 +03:00
										 |  |  | 		it('should add a user to room', function (done) { | 
					
						
							|  |  |  | 			socketModules.chats.addUserToRoom({uid: fooUid}, {roomId: roomId, username: 'herp'}, function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 		it('should leave the chat room', function (done) { | 
					
						
							| 
									
										
										
										
											2016-11-08 19:59:19 +03:00
										 |  |  | 			socketModules.chats.leave({uid: bazUid}, roomId, function (err) { | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				Messaging.isUserInRoom(bazUid, roomId, function (err, isUserInRoom) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(isUserInRoom, false); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should send a message to a room', function (done) { | 
					
						
							| 
									
										
										
										
											2016-11-08 19:59:19 +03:00
										 |  |  | 			socketModules.chats.send({uid: fooUid}, {roomId: roomId, message: 'first chat message'}, function (err, messageData) { | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(messageData); | 
					
						
							|  |  |  | 				assert.equal(messageData.content, 'first chat message'); | 
					
						
							|  |  |  | 				assert(messageData.fromUser); | 
					
						
							|  |  |  | 				assert(messageData.roomId, roomId); | 
					
						
							| 
									
										
										
										
											2016-11-08 19:59:19 +03:00
										 |  |  | 				socketModules.chats.getRaw({uid: fooUid}, {roomId: roomId, mid: messageData.mid}, function (err, raw) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(raw, 'first chat message'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get messages from room', function (done) { | 
					
						
							| 
									
										
										
										
											2016-11-08 19:59:19 +03:00
										 |  |  | 			socketModules.chats.getMessages({uid: fooUid}, { | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 				uid: fooUid, | 
					
						
							|  |  |  | 				roomId: roomId, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				start: 0, | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 			}, function (err, messages) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(messages)); | 
					
						
							|  |  |  | 				assert.equal(messages[0].roomId, roomId); | 
					
						
							|  |  |  | 				assert.equal(messages[0].fromuid, fooUid); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-08 19:59:19 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 20:42:56 +03:00
										 |  |  | 		it('should mark room read', function (done) { | 
					
						
							|  |  |  | 			socketModules.chats.markRead({uid: fooUid}, roomId, function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should mark all rooms read', function (done) { | 
					
						
							|  |  |  | 			socketModules.chats.markAllRead({uid: fooUid}, {}, function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should rename room', function (done) { | 
					
						
							|  |  |  | 			socketModules.chats.renameRoom({uid: fooUid}, {roomId: roomId, newName: 'new room name'}, function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 19:59:19 +03:00
										 |  |  | 		it('should load chat room', function (done) { | 
					
						
							|  |  |  | 			socketModules.chats.loadRoom({uid: fooUid}, {roomId: roomId}, function (err, data) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(data); | 
					
						
							| 
									
										
										
										
											2016-11-08 20:42:56 +03:00
										 |  |  | 				assert.equal(data.roomName, 'new room name'); | 
					
						
							| 
									
										
										
										
											2016-11-08 19:59:19 +03:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-10-18 20:45:25 +03:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 20:42:56 +03:00
										 |  |  | 	describe('edit/delete', function () { | 
					
						
							|  |  |  | 		var socketModules = require('../src/socket.io/modules'); | 
					
						
							|  |  |  | 		var mid; | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			socketModules.chats.send({uid: fooUid}, {roomId: roomId, message: 'first chat message'}, function (err, messageData) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				mid = messageData.mid; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should edit message', function (done) { | 
					
						
							|  |  |  | 			socketModules.chats.edit({uid: fooUid}, {mid: mid, roomId: roomId, message: 'message edited'}, function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				socketModules.chats.getRaw({uid: fooUid}, {roomId: roomId, mid: mid}, function (err, raw) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(raw, 'message edited'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should delete message', function (done) { | 
					
						
							|  |  |  | 			socketModules.chats.delete({uid: fooUid}, {messageId: mid, roomId: roomId}, function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				db.exists('message:' + mid, function (err, exists) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(!exists); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 17:56:32 +03:00
										 |  |  | 	describe('controller', function () { | 
					
						
							| 
									
										
										
										
											2016-10-26 12:06:53 +03:00
										 |  |  | 		it('should 404 if chat is disabled', function (done) { | 
					
						
							|  |  |  | 			meta.config.disableChat = 1; | 
					
						
							|  |  |  | 			request(nconf.get('url') + '/user/baz/chats', function (err, response) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 17:56:32 +03:00
										 |  |  | 		it('should 404 for guest', function (done) { | 
					
						
							| 
									
										
										
										
											2016-10-26 12:06:53 +03:00
										 |  |  | 			meta.config.disableChat = 0; | 
					
						
							| 
									
										
										
										
											2016-10-25 17:56:32 +03:00
										 |  |  | 			request(nconf.get('url') + '/user/baz/chats', function (err, response) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-10-26 12:06:53 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should 404 for non-existent user', function (done) { | 
					
						
							|  |  |  | 			request(nconf.get('url') + '/user/doesntexist/chats', function (err, response) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe('logged in chat controller', function () { | 
					
						
							|  |  |  | 		var jar; | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			helpers.loginUser('herp', 'derpderp', function (err, _jar) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				jar = _jar; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return chats page data', function (done) { | 
					
						
							|  |  |  | 			request(nconf.get('url') + '/api/user/herp/chats', {json: true, jar: jar}, function (err, response, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 				assert(Array.isArray(body.rooms)); | 
					
						
							|  |  |  | 				assert.equal(body.rooms.length, 1); | 
					
						
							|  |  |  | 				assert.equal(body.title, '[[pages:chats]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return room data', function (done) { | 
					
						
							|  |  |  | 			request(nconf.get('url') + '/api/user/herp/chats/' + roomId, {json: true, jar: jar}, function (err, response, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 				assert.equal(body.roomId, roomId); | 
					
						
							|  |  |  | 				assert.equal(body.isOwner, false); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should redirect to chats page', function (done) { | 
					
						
							|  |  |  | 			request(nconf.get('url') + '/api/chats', {jar: jar}, function (err, response, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(body, '"/user/herp/chats"'); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 308); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return 404 if user is not in room', function (done) { | 
					
						
							|  |  |  | 			helpers.loginUser('baz', 'quuxquux', function (err, jar) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				request(nconf.get('url') + '/api/user/baz/chats/' + roomId, {json: true, jar: jar}, function (err, response, body) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-10-25 17:56:32 +03:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	after(function (done) { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		db.emptydb(done); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }); |