more tests

v1.18.x
Barış Soner Uşaklı
parent edeb22fcdf
commit 7fb45cc148

@ -12,16 +12,16 @@ var utils = require('../utils');
var server = require('./'); var server = require('./');
var user = require('../user'); var user = require('../user');
var SocketModules = { var SocketModules = module.exports;
chats: {},
sounds: {}, SocketModules.chats = {};
settings: {}, SocketModules.sounds = {};
}; SocketModules.settings = {};
/* Chat */ /* Chat */
SocketModules.chats.getRaw = function (socket, data, callback) { SocketModules.chats.getRaw = function (socket, data, callback) {
if (!data || !data.hasOwnProperty('mid')) { if (!data || !data.hasOwnProperty('mid') || !data.hasOwnProperty('roomId')) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
async.waterfall([ async.waterfall([
@ -57,13 +57,14 @@ SocketModules.chats.newRoom = function (socket, data, callback) {
return callback(new Error('[[error:too-many-messages]]')); return callback(new Error('[[error:too-many-messages]]'));
} }
Messaging.canMessageUser(socket.uid, data.touid, function (err) { async.waterfall([
if (err) { function (next) {
return callback(err); Messaging.canMessageUser(socket.uid, data.touid, next);
} },
function (next) {
Messaging.newRoom(socket.uid, [data.touid], callback); Messaging.newRoom(socket.uid, [data.touid], next);
}); },
], callback);
}; };
SocketModules.chats.send = function (socket, data, callback) { SocketModules.chats.send = function (socket, data, callback) {
@ -223,17 +224,21 @@ SocketModules.chats.leave = function (socket, roomid, callback) {
SocketModules.chats.edit = function (socket, data, callback) { SocketModules.chats.edit = function (socket, data, callback) {
if (!data || !data.roomId) { if (!data || !data.roomId || !data.message) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
Messaging.canEdit(data.mid, socket.uid, function (err, allowed) { async.waterfall([
if (err || !allowed) { function (next) {
return callback(err || new Error('[[error:cant-edit-chat-message]]')); Messaging.canEdit(data.mid, socket.uid, next);
} },
function (allowed, next) {
Messaging.editMessage(socket.uid, data.mid, data.roomId, data.message, callback); if (!allowed) {
}); return next(new Error('[[error:cant-edit-chat-message]]'));
}
Messaging.editMessage(socket.uid, data.mid, data.roomId, data.message, next);
},
], callback);
}; };
SocketModules.chats.delete = function (socket, data, callback) { SocketModules.chats.delete = function (socket, data, callback) {
@ -241,13 +246,18 @@ SocketModules.chats.delete = function (socket, data, callback) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
Messaging.canEdit(data.messageId, socket.uid, function (err, allowed) { async.waterfall([
if (err || !allowed) { function (next) {
return callback(err || new Error('[[error:cant-delete-chat-message]]')); Messaging.canEdit(data.messageId, socket.uid, next);
} },
function (allowed, next) {
if (!allowed) {
return next(new Error('[[error:cant-delete-chat-message]]'));
}
Messaging.deleteMessage(data.messageId, data.roomId, callback); Messaging.deleteMessage(data.messageId, data.roomId, next);
}); },
], callback);
}; };
SocketModules.chats.canMessage = function (socket, roomId, callback) { SocketModules.chats.canMessage = function (socket, roomId, callback) {
@ -255,37 +265,38 @@ SocketModules.chats.canMessage = function (socket, roomId, callback) {
}; };
SocketModules.chats.markRead = function (socket, roomId, callback) { SocketModules.chats.markRead = function (socket, roomId, callback) {
if (!socket.uid) { if (!socket.uid || !roomId) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
async.parallel({ async.waterfall([
uidsInRoom: async.apply(Messaging.getUidsInRoom, roomId, 0, -1), function (next) {
markRead: async.apply(Messaging.markRead, socket.uid, roomId), async.parallel({
}, function (err, results) { uidsInRoom: async.apply(Messaging.getUidsInRoom, roomId, 0, -1),
if (err) { markRead: async.apply(Messaging.markRead, socket.uid, roomId),
return callback(err); }, next);
} },
function (results, next) {
Messaging.pushUnreadCount(socket.uid); Messaging.pushUnreadCount(socket.uid);
server.in('uid_' + socket.uid).emit('event:chats.markedAsRead', { roomId: roomId }); server.in('uid_' + socket.uid).emit('event:chats.markedAsRead', { roomId: roomId });
if (results.uidsInRoom.indexOf(socket.uid.toString()) === -1) { if (results.uidsInRoom.indexOf(socket.uid.toString()) === -1) {
return callback(); return callback();
} }
// Mark notification read // Mark notification read
var nids = results.uidsInRoom.filter(function (uid) { var nids = results.uidsInRoom.filter(function (uid) {
return parseInt(uid, 10) !== socket.uid; return parseInt(uid, 10) !== socket.uid;
}).map(function (uid) { }).map(function (uid) {
return 'chat_' + uid + '_' + roomId; return 'chat_' + uid + '_' + roomId;
}); });
notifications.markReadMultiple(nids, socket.uid, function () { notifications.markReadMultiple(nids, socket.uid, function () {
user.notifications.pushCount(socket.uid); user.notifications.pushCount(socket.uid);
}); });
callback(); next();
}); },
], callback);
}; };
SocketModules.chats.markAllRead = function (socket, data, callback) { SocketModules.chats.markAllRead = function (socket, data, callback) {
@ -301,8 +312,8 @@ SocketModules.chats.markAllRead = function (socket, data, callback) {
}; };
SocketModules.chats.renameRoom = function (socket, data, callback) { SocketModules.chats.renameRoom = function (socket, data, callback) {
if (!data) { if (!data || !data.roomId || !data.newName) {
return callback(new Error('[[error:invalid-name]]')); return callback(new Error('[[error:invalid-data]]'));
} }
async.waterfall([ async.waterfall([
@ -333,13 +344,13 @@ SocketModules.chats.getRecentChats = function (socket, data, callback) {
SocketModules.chats.hasPrivateChat = function (socket, uid, callback) { SocketModules.chats.hasPrivateChat = function (socket, uid, callback) {
if (!socket.uid || !uid) { if (!socket.uid || !uid) {
return callback(null, new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
Messaging.hasPrivateChat(socket.uid, uid, callback); Messaging.hasPrivateChat(socket.uid, uid, callback);
}; };
SocketModules.chats.getMessages = function (socket, data, callback) { SocketModules.chats.getMessages = function (socket, data, callback) {
if (!socket.uid || !data.uid || !data.roomId) { if (!socket.uid || !data || !data.uid || !data.roomId) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
@ -358,5 +369,3 @@ SocketModules.chats.getMessages = function (socket, data, callback) {
SocketModules.sounds.getUserSoundMap = function getUserSoundMap(socket, data, callback) { SocketModules.sounds.getUserSoundMap = function getUserSoundMap(socket, data, callback) {
meta.sounds.getUserSoundMap(socket.uid, callback); meta.sounds.getUserSoundMap(socket.uid, callback);
}; };
module.exports = SocketModules;

@ -11,7 +11,7 @@ var User = require('../src/user');
var Groups = require('../src/groups'); var Groups = require('../src/groups');
var Messaging = require('../src/messaging'); var Messaging = require('../src/messaging');
var helpers = require('./helpers'); var helpers = require('./helpers');
var socketModules = require('../src/socket.io/modules');
describe('Messaging Library', function () { describe('Messaging Library', function () {
var fooUid; var fooUid;
@ -55,7 +55,10 @@ describe('Messaging Library', function () {
assert.ifError(err); assert.ifError(err);
Messaging.canMessageUser(herpUid, bazUid, function (err) { Messaging.canMessageUser(herpUid, bazUid, function (err) {
assert.strictEqual(err.message, '[[error:chat-restricted]]'); assert.strictEqual(err.message, '[[error:chat-restricted]]');
done(); socketModules.chats.addUserToRoom({ uid: herpUid }, { roomId: 1, username: 'baz' }, function (err) {
assert.equal(err.message, '[[error:chat-restricted]]');
done();
});
}); });
}); });
}); });
@ -78,23 +81,93 @@ describe('Messaging Library', function () {
}); });
describe('rooms', function () { describe('rooms', function () {
var socketModules = require('../src/socket.io/modules'); it('should fail to create a new chat room with invalid data', function (done) {
socketModules.chats.newRoom({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
it('should return rate limit error on second try', function (done) {
var socketMock = { uid: fooUid };
socketModules.chats.newRoom(socketMock, { touid: bazUid }, function (err) {
assert.ifError(err);
socketModules.chats.newRoom(socketMock, { touid: bazUid }, function (err) {
assert.equal(err.message, '[[error:too-many-messages]]');
done();
});
});
});
it('should create a new chat room', function (done) { it('should create a new chat room', function (done) {
socketModules.chats.newRoom({ uid: fooUid }, { touid: bazUid }, function (err, _roomId) { socketModules.chats.newRoom({ uid: fooUid }, { touid: bazUid }, function (err, _roomId) {
roomId = _roomId; roomId = _roomId;
assert.ifError(err); assert.ifError(err);
assert(roomId); assert(roomId);
done(); socketModules.chats.canMessage({ uid: fooUid }, _roomId, function (err) {
assert.ifError(err);
done();
});
});
});
it('should fail to add user to room with invalid data', function (done) {
socketModules.chats.addUserToRoom({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
}); });
}); });
it('should add a user to room', function (done) { it('should add a user to room', function (done) {
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'herp' }, function (err) { socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'herp' }, function (err) {
assert.ifError(err); assert.ifError(err);
Messaging.isUserInRoom(herpUid, roomId, function (err, isInRoom) {
assert.ifError(err);
assert(isInRoom);
done();
});
});
});
it('should fail to add users to room if max is reached', function (done) {
meta.config.maximumUsersInChatRoom = 2;
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'test' }, function (err) {
assert.equal(err.message, '[[error:cant-add-more-users-to-chat-room]]');
meta.config.maximumUsersInChatRoom = 0;
done();
});
});
it('should fail to add users to room if user does not exist', function (done) {
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'doesnotexist' }, function (err) {
assert.equal(err.message, '[[error:no-user]]');
done();
});
});
it('should fail to add self to room', function (done) {
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'foo' }, function (err) {
assert.equal(err.message, '[[error:cant-add-self-to-chat-room]]');
done(); done();
}); });
}); });
it('should fail to leave room with invalid data', function (done) {
socketModules.chats.leave({ uid: null }, roomId, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.leave({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
it('should leave the chat room', function (done) { it('should leave the chat room', function (done) {
socketModules.chats.leave({ uid: bazUid }, roomId, function (err) { socketModules.chats.leave({ uid: bazUid }, roomId, function (err) {
assert.ifError(err); assert.ifError(err);
@ -106,6 +179,60 @@ describe('Messaging Library', function () {
}); });
}); });
it('should fail to remove user from room', function (done) {
socketModules.chats.removeUserFromRoom({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.removeUserFromRoom({ uid: fooUid }, {}, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
it('should fail to remove user from room if user does not exist', function (done) {
socketModules.chats.removeUserFromRoom({ uid: fooUid }, { roomId: roomId, username: 'doesnotexist' }, function (err) {
assert.equal(err.message, '[[error:no-user]]');
done();
});
});
it('should remove user from room', function (done) {
socketModules.chats.newRoom({ uid: fooUid }, { touid: herpUid }, function (err, roomId) {
assert.ifError(err);
Messaging.isUserInRoom(herpUid, roomId, function (err, isInRoom) {
assert.ifError(err);
assert(isInRoom);
socketModules.chats.removeUserFromRoom({ uid: fooUid }, { roomId: roomId, username: 'herp' }, function (err) {
assert.equal(err.message, '[[error:cant-remove-last-user]]');
socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'baz' }, function (err) {
assert.ifError(err);
socketModules.chats.removeUserFromRoom({ uid: fooUid }, { roomId: roomId, username: 'herp' }, function (err) {
assert.ifError(err);
Messaging.isUserInRoom(herpUid, roomId, function (err, isInRoom) {
assert.ifError(err);
assert(!isInRoom);
done();
});
});
});
});
});
});
});
it('should fail to send a message to room with invalid data', function (done) {
socketModules.chats.send({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.send({ uid: fooUid }, { roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.send({ uid: null }, { roomId: 1 }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
it('should send a message to a room', function (done) { it('should send a message to a room', function (done) {
socketModules.chats.send({ uid: fooUid }, { roomId: roomId, message: 'first chat message' }, function (err, messageData) { socketModules.chats.send({ uid: fooUid }, { roomId: roomId, message: 'first chat message' }, function (err, messageData) {
assert.ifError(err); assert.ifError(err);
@ -116,11 +243,39 @@ describe('Messaging Library', function () {
socketModules.chats.getRaw({ uid: fooUid }, { roomId: roomId, mid: messageData.mid }, function (err, raw) { socketModules.chats.getRaw({ uid: fooUid }, { roomId: roomId, mid: messageData.mid }, function (err, raw) {
assert.ifError(err); assert.ifError(err);
assert.equal(raw, 'first chat message'); assert.equal(raw, 'first chat message');
setTimeout(done, 300);
});
});
});
it('should fail to send second message due to rate limit', function (done) {
var socketMock = { uid: fooUid };
socketModules.chats.send(socketMock, { roomId: roomId, message: 'first chat message' }, function (err) {
assert.ifError(err);
socketModules.chats.send(socketMock, { roomId: roomId, message: 'first chat message' }, function (err) {
assert.equal(err.message, '[[error:too-many-messages]]');
done();
});
});
});
it('should return invalid-data error', function (done) {
socketModules.chats.getRaw({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getRaw({ uid: fooUid }, { }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done(); done();
}); });
}); });
}); });
it('should return not in room error', function (done) {
socketModules.chats.getRaw({ uid: 0 }, { roomId: roomId, mid: 1 }, function (err) {
assert.equal(err.message, '[[error:not-allowed]]');
done();
});
});
it('should notify offline users of message', function (done) { it('should notify offline users of message', function (done) {
Messaging.notificationSendDelay = 100; Messaging.notificationSendDelay = 100;
@ -143,6 +298,22 @@ describe('Messaging Library', function () {
}); });
}); });
it('should fail to get messages from room with invalid data', function (done) {
socketModules.chats.getMessages({ uid: null }, null, function (err, messages) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getMessages({ uid: fooUid }, null, function (err, messages) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getMessages({ uid: fooUid }, { uid: null }, function (err, messages) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getMessages({ uid: fooUid }, { uid: 1, roomId: null }, function (err, messages) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
});
it('should get messages from room', function (done) { it('should get messages from room', function (done) {
socketModules.chats.getMessages({ uid: fooUid }, { socketModules.chats.getMessages({ uid: fooUid }, {
uid: fooUid, uid: fooUid,
@ -157,6 +328,23 @@ describe('Messaging Library', function () {
}); });
}); });
it('should fail to mark read with invalid data', function (done) {
socketModules.chats.markRead({ uid: null }, roomId, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.markRead({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
it('should not error if user is not in room', function (done) {
socketModules.chats.markRead({ uid: herpUid }, roomId, function (err) {
assert.ifError(err);
done();
});
});
it('should mark room read', function (done) { it('should mark room read', function (done) {
socketModules.chats.markRead({ uid: fooUid }, roomId, function (err) { socketModules.chats.markRead({ uid: fooUid }, roomId, function (err) {
assert.ifError(err); assert.ifError(err);
@ -171,10 +359,39 @@ describe('Messaging Library', function () {
}); });
}); });
it('should fail to rename room with invalid data', function (done) {
socketModules.chats.renameRoom({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.renameRoom({ uid: fooUid }, { roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.renameRoom({ uid: fooUid }, { roomId: roomId, newName: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
it('should rename room', function (done) { it('should rename room', function (done) {
socketModules.chats.renameRoom({ uid: fooUid }, { roomId: roomId, newName: 'new room name' }, function (err) { socketModules.chats.renameRoom({ uid: fooUid }, { roomId: roomId, newName: 'new room name' }, function (err) {
assert.ifError(err); assert.ifError(err);
done();
});
});
it('should fail to load room with invalid-data', function (done) {
socketModules.chats.loadRoom({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.loadRoom({ uid: fooUid }, { roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
it('should fail to load room if user is not in', function (done) {
socketModules.chats.loadRoom({ uid: 0 }, { roomId: roomId }, function (err) {
assert.equal(err.message, '[[error:not-allowed]]');
done(); done();
}); });
}); });
@ -198,6 +415,45 @@ describe('Messaging Library', function () {
}); });
}); });
}); });
it('should fail to load recent chats with invalid data', function (done) {
socketModules.chats.getRecentChats({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getRecentChats({ uid: fooUid }, { after: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.getRecentChats({ uid: fooUid }, { after: 0, uid: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
it('should load recent chats of user', function (done) {
socketModules.chats.getRecentChats({ uid: fooUid }, { after: 0, uid: fooUid }, function (err, data) {
assert.ifError(err);
assert(Array.isArray(data.rooms));
done();
});
});
it('should fail to check if user has private chat with invalid data', function (done) {
socketModules.chats.hasPrivateChat({ uid: null }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.hasPrivateChat({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
it('should check if user has private chat with another uid', function (done) {
socketModules.chats.hasPrivateChat({ uid: fooUid }, herpUid, function (err, roomId) {
assert.ifError(err);
assert(roomId);
done();
});
});
}); });
describe('edit/delete', function () { describe('edit/delete', function () {
@ -211,6 +467,26 @@ describe('Messaging Library', function () {
}); });
}); });
it('should fail to edit message with invalid data', function (done) {
socketModules.chats.edit({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.edit({ uid: fooUid }, { roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.edit({ uid: fooUid }, { roomId: 1, message: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
it('should fail to edit message if not own message', function (done) {
socketModules.chats.edit({ uid: herpUid }, { mid: 5, roomId: roomId, message: 'message edited' }, function (err) {
assert.equal(err.message, '[[error:cant-edit-chat-message]]');
done();
});
});
it('should edit message', function (done) { it('should edit message', function (done) {
socketModules.chats.edit({ uid: fooUid }, { mid: mid, roomId: roomId, message: 'message edited' }, function (err) { socketModules.chats.edit({ uid: fooUid }, { mid: mid, roomId: roomId, message: 'message edited' }, function (err) {
assert.ifError(err); assert.ifError(err);
@ -222,6 +498,20 @@ describe('Messaging Library', function () {
}); });
}); });
it('should fail to delete message with invalid data', function (done) {
socketModules.chats.delete({ uid: fooUid }, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.delete({ uid: fooUid }, { roomId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
socketModules.chats.delete({ uid: fooUid }, { roomId: 1, messageId: null }, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
});
});
it('should delete message', function (done) { it('should delete message', function (done) {
socketModules.chats.delete({ uid: fooUid }, { messageId: mid, roomId: roomId }, function (err) { socketModules.chats.delete({ uid: fooUid }, { messageId: mid, roomId: roomId }, function (err) {
assert.ifError(err); assert.ifError(err);

@ -262,6 +262,21 @@ describe('meta', function () {
}); });
describe('sounds', function () {
var socketModules = require('../src/socket.io/modules');
it('should getUserMap', function (done) {
socketModules.sounds.getUserSoundMap({ uid: 1 }, null, function (err, data) {
assert.ifError(err);
assert(data.hasOwnProperty('chat-incoming'));
assert(data.hasOwnProperty('chat-outgoing'));
assert(data.hasOwnProperty('notification'));
done();
});
});
});
after(function (done) { after(function (done) {
db.emptydb(done); db.emptydb(done);
}); });

@ -5,181 +5,178 @@
* ATTENTION: testing db is flushed before every use! * ATTENTION: testing db is flushed before every use!
*/ */
(function (module) { var async = require('async');
var async = require('async'); var winston = require('winston');
var winston = require('winston'); var path = require('path');
var path = require('path'); var nconf = require('nconf');
var nconf = require('nconf'); var url = require('url');
var url = require('url'); var errorText;
var errorText;
nconf.file({ file: path.join(__dirname, '../../config.json') });
nconf.file({ file: path.join(__dirname, '../../config.json') }); nconf.defaults({
nconf.defaults({ base_dir: path.join(__dirname, '../..'),
base_dir: path.join(__dirname, '../..'), themes_path: path.join(__dirname, '../../node_modules'),
themes_path: path.join(__dirname, '../../node_modules'), upload_path: 'public/uploads',
upload_path: 'public/uploads', views_dir: path.join(__dirname, '../../build/public/templates'),
views_dir: path.join(__dirname, '../../build/public/templates'), relative_path: '',
relative_path: '', });
});
if (!nconf.get('isCluster')) {
if (!nconf.get('isCluster')) { nconf.set('isPrimary', 'true');
nconf.set('isPrimary', 'true'); nconf.set('isCluster', 'false');
nconf.set('isCluster', 'false'); }
}
var dbType = nconf.get('database');
var dbType = nconf.get('database'); var testDbConfig = nconf.get('test_database');
var testDbConfig = nconf.get('test_database'); var productionDbConfig = nconf.get(dbType);
var productionDbConfig = nconf.get(dbType);
if (!testDbConfig) {
if (!testDbConfig) { errorText = 'test_database is not defined';
errorText = 'test_database is not defined'; winston.info(
winston.info( '\n===========================================================\n' +
'\n===========================================================\n' + 'Please, add parameters for test database in config.json\n' +
'Please, add parameters for test database in config.json\n' + 'For example (redis):\n' +
'For example (redis):\n' + '"test_database": {\n' +
'"test_database": {\n' + ' "host": "127.0.0.1",\n' +
' "host": "127.0.0.1",\n' + ' "port": "6379",\n' +
' "port": "6379",\n' + ' "password": "",\n' +
' "password": "",\n' + ' "database": "1"\n' +
' "database": "1"\n' + '}\n' +
'}\n' + ' or (mongo):\n' +
' or (mongo):\n' + '"test_database": {\n' +
'"test_database": {\n' + ' "host": "127.0.0.1",\n' +
' "host": "127.0.0.1",\n' + ' "port": "27017",\n' +
' "port": "27017",\n' + ' "password": "",\n' +
' "password": "",\n' + ' "database": "1\n' +
' "database": "1\n' + '}\n' +
'}\n' + ' or (mongo) in a replicaset\n' +
' or (mongo) in a replicaset\n' + '"test_database": {\n' +
'"test_database": {\n' + ' "host": "127.0.0.1,127.0.0.1,127.0.0.1",\n' +
' "host": "127.0.0.1,127.0.0.1,127.0.0.1",\n' + ' "port": "27017,27018,27019",\n' +
' "port": "27017,27018,27019",\n' + ' "username": "",\n' +
' "username": "",\n' + ' "password": "",\n' +
' "password": "",\n' + ' "database": "nodebb_test"\n' +
' "database": "nodebb_test"\n' + '}\n' +
'}\n' + '==========================================================='
'===========================================================' );
); winston.error(errorText);
winston.error(errorText); throw new Error(errorText);
throw new Error(errorText); }
}
if (testDbConfig.database === productionDbConfig.database &&
if (testDbConfig.database === productionDbConfig.database && testDbConfig.host === productionDbConfig.host &&
testDbConfig.host === productionDbConfig.host && testDbConfig.port === productionDbConfig.port) {
testDbConfig.port === productionDbConfig.port) { errorText = 'test_database has the same config as production db';
errorText = 'test_database has the same config as production db'; winston.error(errorText);
winston.error(errorText); throw new Error(errorText);
throw new Error(errorText); }
}
nconf.set(dbType, testDbConfig);
nconf.set(dbType, testDbConfig);
winston.info('database config');
winston.info('database config'); winston.info(dbType);
winston.info(dbType); winston.info(testDbConfig);
winston.info(testDbConfig);
var db = require('../../src/database');
var db = require('../../src/database'); module.exports = db;
after(function (done) { after(function (done) {
db.close(done); db.close(done);
}); });
before(function (done) { before(function (done) {
this.timeout(30000); this.timeout(30000);
var meta; var meta;
async.waterfall([ async.waterfall([
function (next) { function (next) {
db.init(next); db.init(next);
}, },
function (next) { function (next) {
db.emptydb(next); db.emptydb(next);
}, },
function (next) { function (next) {
db.createIndices(next); db.createIndices(next);
}, },
function (next) { function (next) {
winston.info('test_database flushed'); winston.info('test_database flushed');
meta = require('../../src/meta'); meta = require('../../src/meta');
setupDefaultConfigs(meta, next); setupDefaultConfigs(meta, next);
}, },
function (next) { function (next) {
meta.configs.init(next); meta.configs.init(next);
}, },
function (next) { function (next) {
db.initSessionStore(next); db.initSessionStore(next);
}, },
function (next) { function (next) {
meta.dependencies.check(next); meta.dependencies.check(next);
}, },
function (next) { function (next) {
meta.config.postDelay = 0; meta.config.postDelay = 0;
meta.config.initialPostDelay = 0; meta.config.initialPostDelay = 0;
meta.config.newbiePostDelay = 0; meta.config.newbiePostDelay = 0;
enableDefaultPlugins(next); enableDefaultPlugins(next);
}, },
function (next) { function (next) {
meta.themes.set({ meta.themes.set({
type: 'local', type: 'local',
id: 'nodebb-theme-persona', id: 'nodebb-theme-persona',
}, next); }, next);
}, },
function (next) { function (next) {
// nconf defaults, if not set in config // nconf defaults, if not set in config
if (!nconf.get('sessionKey')) { if (!nconf.get('sessionKey')) {
nconf.set('sessionKey', 'express.sid'); nconf.set('sessionKey', 'express.sid');
} }
// Parse out the relative_url and other goodies from the configured URL // Parse out the relative_url and other goodies from the configured URL
var urlObject = url.parse(nconf.get('url')); var urlObject = url.parse(nconf.get('url'));
var relativePath = urlObject.pathname !== '/' ? urlObject.pathname : ''; var relativePath = urlObject.pathname !== '/' ? urlObject.pathname : '';
nconf.set('base_url', urlObject.protocol + '//' + urlObject.host); nconf.set('base_url', urlObject.protocol + '//' + urlObject.host);
nconf.set('secure', urlObject.protocol === 'https:'); nconf.set('secure', urlObject.protocol === 'https:');
nconf.set('use_port', !!urlObject.port); nconf.set('use_port', !!urlObject.port);
nconf.set('relative_path', relativePath); nconf.set('relative_path', relativePath);
nconf.set('port', urlObject.port || nconf.get('port') || nconf.get('PORT') || (nconf.get('PORT_ENV_VAR') ? nconf.get(nconf.get('PORT_ENV_VAR')) : false) || 4567); nconf.set('port', urlObject.port || nconf.get('port') || nconf.get('PORT') || (nconf.get('PORT_ENV_VAR') ? nconf.get(nconf.get('PORT_ENV_VAR')) : false) || 4567);
nconf.set('upload_path', path.join(nconf.get('base_dir'), nconf.get('upload_path'))); nconf.set('upload_path', path.join(nconf.get('base_dir'), nconf.get('upload_path')));
nconf.set('core_templates_path', path.join(__dirname, '../../src/views')); nconf.set('core_templates_path', path.join(__dirname, '../../src/views'));
nconf.set('base_templates_path', path.join(nconf.get('themes_path'), 'nodebb-theme-persona/templates')); nconf.set('base_templates_path', path.join(nconf.get('themes_path'), 'nodebb-theme-persona/templates'));
nconf.set('theme_templates_path', meta.config['theme:templates'] ? path.join(nconf.get('themes_path'), meta.config['theme:id'], meta.config['theme:templates']) : nconf.get('base_templates_path')); nconf.set('theme_templates_path', meta.config['theme:templates'] ? path.join(nconf.get('themes_path'), meta.config['theme:id'], meta.config['theme:templates']) : nconf.get('base_templates_path'));
nconf.set('theme_config', path.join(nconf.get('themes_path'), 'nodebb-theme-persona', 'theme.json')); nconf.set('theme_config', path.join(nconf.get('themes_path'), 'nodebb-theme-persona', 'theme.json'));
nconf.set('bcrypt_rounds', 1); nconf.set('bcrypt_rounds', 1);
next(); next();
}, },
function (next) { function (next) {
var webserver = require('../../src/webserver'); var webserver = require('../../src/webserver');
var sockets = require('../../src/socket.io'); var sockets = require('../../src/socket.io');
sockets.init(webserver.server); sockets.init(webserver.server);
require('../../src/notifications').startJobs(); require('../../src/notifications').startJobs();
require('../../src/user').startJobs(); require('../../src/user').startJobs();
webserver.listen(next); webserver.listen(next);
}, },
], done); ], done);
}); });
function setupDefaultConfigs(meta, next) { function setupDefaultConfigs(meta, next) {
winston.info('Populating database with default configs, if not already set...\n'); winston.info('Populating database with default configs, if not already set...\n');
var defaults = require(path.join(nconf.get('base_dir'), 'install/data/defaults.json')); var defaults = require(path.join(nconf.get('base_dir'), 'install/data/defaults.json'));
meta.configs.setOnEmpty(defaults, next); meta.configs.setOnEmpty(defaults, next);
} }
function enableDefaultPlugins(callback) { function enableDefaultPlugins(callback) {
winston.info('Enabling default plugins\n'); winston.info('Enabling default plugins\n');
var defaultEnabled = [ var defaultEnabled = [
'nodebb-plugin-dbsearch', 'nodebb-plugin-dbsearch',
]; ];
winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled); winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
db.sortedSetAdd('plugins:active', [0], defaultEnabled, callback); db.sortedSetAdd('plugins:active', [0], defaultEnabled, callback);
} }
module.exports = db;
}(module));

Loading…
Cancel
Save