Skip to content

Node.js based forum software built for the modern web

Merge branch 'master' into styleguide

Julian Lame71bd7a27f78
diff --cc public/src/modules/helpers.js
index ea8c29d339,b1ac2e183e..b7cdfaa950
--- a/public/src/modules/helpers.js
+++ b/public/src/modules/helpers.js
@@@ -1,7 -1,13 +1,14 @@@
- 'use strict';
-
-;(function (exports) {
- "use strict";
+(function (exports) {
- var helpers = {};
++ 'use strict';
++
+ /* globals define, utils, config */
+
+ // export the class if we are in a Node-like system.
+ if (typeof module === 'object' && module.exports === exports) {
+ exports = module.exports/* = SemVer*/;
+ }
+
+ var helpers = exports;
helpers.displayMenuItem = function (data, index) {
var item = data.navigation[index];
diff --cc src/controllers/groups.js
index 5fdc140826,25a6d928db..fb780c1b97
--- a/src/controllers/groups.js
+++ b/src/controllers/groups.js
@@@ -32,17 -32,18 +32,18 @@@ groupsController.getGroupsFromSet = fun
set = 'groups:visible:createtime';
}
- groups.getGroupsFromSet(set, uid, start, stop, function (err, groups) {
- if (err) {
- return callback(err);
- }
-
- callback(null, {
- groups: groups,
- allowGroupCreation: parseInt(meta.config.allowGroupCreation, 10) === 1,
- nextStart: stop + 1,
- });
- });
+ async.waterfall([
+ function (next) {
+ groups.getGroupsFromSet(set, uid, start, stop, next);
+ },
+ function (groupsData, next) {
+ next(null, {
+ groups: groupsData,
+ allowGroupCreation: parseInt(meta.config.allowGroupCreation, 10) === 1,
- nextStart: stop + 1
++ nextStart: stop + 1,
+ });
- }
++ },
+ ], callback);
};
groupsController.details = function (req, res, callback) {
diff --cc src/controllers/uploads.js
index 03538faee7,22e533a33d..25375e91a9
--- a/src/controllers/uploads.js
+++ b/src/controllers/uploads.js
@@@ -30,7 -30,7 +30,7 @@@ uploadsController.upload = function (re
deleteTempFiles(files);
if (err) {
- return res.status(500).send(err.message);
- return res.status(500).json({path: req.path, error: err.message});
++ return res.status(500).json({ path: req.path, error: err.message });
}
res.status(200).send(images);
@@@ -136,26 -137,24 +136,24 @@@ uploadsController.uploadThumb = functio
}
uploadsController.upload(req, res, function (uploadedFile, next) {
- file.isFileTypeAllowed(uploadedFile.path, function (err) {
- if (err) {
- return next(err);
- }
-
- if (!uploadedFile.type.match(/image./)) {
- return next(new Error('[[error:invalid-file]]'));
- }
-
- var size = parseInt(meta.config.topicThumbSize, 10) || 120;
- image.resizeImage({
- path: uploadedFile.path,
- extension: path.extname(uploadedFile.name),
- width: size,
- height: size,
- }, function (err) {
- if (err) {
- return next(err);
+ async.waterfall([
+ function (next) {
+ if (!uploadedFile.type.match(/image./)) {
+ return next(new Error('[[error:invalid-file]]'));
}
+ file.isFileTypeAllowed(uploadedFile.path, next);
+ },
+ function (next) {
+ var size = parseInt(meta.config.topicThumbSize, 10) || 120;
+ image.resizeImage({
+ path: uploadedFile.path,
+ extension: path.extname(uploadedFile.name),
+ width: size,
- height: size
++ height: size,
+ }, next);
+ },
+ function (next) {
if (plugins.hasListeners('filter:uploadImage')) {
return plugins.fireHook('filter:uploadImage', {
image: uploadedFile,
@@@ -164,8 -163,8 +162,8 @@@
}
uploadFile(req.uid, uploadedFile, next);
- });
- });
- }
++ },
+ ], next);
}, next);
};
@@@ -184,12 -183,14 +182,14 @@@ uploadsController.uploadGroupCover = fu
}, callback);
}
- file.isFileTypeAllowed(uploadedFile.path, function (err) {
- if (err) {
- return callback(err);
- }
- saveFileToLocal(uploadedFile, callback);
- });
+ async.waterfall([
+ function (next) {
+ file.isFileTypeAllowed(uploadedFile.path, next);
+ },
+ function (next) {
+ saveFileToLocal(uploadedFile, next);
- }
++ },
+ ], callback);
};
function uploadFile(uid, uploadedFile, callback) {
@@@ -228,17 -229,18 +228,18 @@@ function saveFileToLocal(uploadedFile,
filename = Date.now() + '-' + validator.escape(filename.replace(path.extname(uploadedFile.name) || '', '')).substr(0, 255) + extension;
- file.saveFileToLocal(filename, 'files', uploadedFile.path, function (err, upload) {
- if (err) {
- return callback(err);
- }
-
- callback(null, {
- url: nconf.get('relative_path') + upload.url,
- path: upload.path,
- name: uploadedFile.name,
- });
- });
+ async.waterfall([
+ function (next) {
+ file.saveFileToLocal(filename, 'files', uploadedFile.path, next);
+ },
+ function (upload, next) {
+ next(null, {
+ url: nconf.get('relative_path') + upload.url,
+ path: upload.path,
- name: uploadedFile.name
++ name: uploadedFile.name,
+ });
- }
++ },
+ ], callback);
}
function deleteTempFiles(files) {
diff --cc src/groups/membership.js
index 567c5b7dcc,68a9724c9c..31a32da17c
--- a/src/groups/membership.js
+++ b/src/groups/membership.js
@@@ -146,10 -146,9 +145,9 @@@ module.exports = function (Groups)
};
Groups.rejectMembership = function (groupName, uid, callback) {
- // Note: For simplicity, this method intentially doesn't check the caller uid for ownership!
async.parallel([
async.apply(db.setRemove, 'group:' + groupName + ':pending', uid),
- async.apply(db.setRemove, 'group:' + groupName + ':invited', uid)
+ async.apply(db.setRemove, 'group:' + groupName + ':invited', uid),
], callback);
};
diff --cc src/socket.io/groups.js
index 16240a506b,47453b6e0a..ed6cb00180
--- a/src/socket.io/groups.js
+++ b/src/socket.io/groups.js
@@@ -1,6 -1,6 +1,6 @@@
-"use strict";
+'use strict';
- var async = require('async');
+ var async = require('async');
var groups = require('../groups');
var meta = require('../meta');
@@@ -75,9 -75,9 +75,9 @@@ function isOwner(next)
return function (socket, data, callback) {
async.parallel({
isAdmin: async.apply(user.isAdministrator, socket.uid),
- isOwner: async.apply(groups.ownership.isOwner, socket.uid, data.groupName)
+ isOwner: async.apply(groups.ownership.isOwner, socket.uid, data.groupName),
}, function (err, results) {
- if (err || (!isOwner && !results.isAdmin)) {
+ if (err || (!results.isOwner && !results.isAdmin)) {
return callback(err || new Error('[[error:no-privileges]]'));
}
next(socket, data, callback);
@@@ -145,18 -145,21 +145,21 @@@ SocketGroups.issueMassInvite = isOwner(
usernames = usernames.map(function (username) {
return username && username.trim();
});
- user.getUidsByUsernames(usernames, function (err, uids) {
- if (err) {
- return callback(err);
- }
- uids = uids.filter(function (uid) {
- return !!uid && parseInt(uid, 10);
- });
- async.eachSeries(uids, function (uid, next) {
- groups.invite(data.groupName, uid, next);
- }, callback);
- });
+ async.waterfall([
+ function (next) {
+ user.getUidsByUsernames(usernames, next);
+ },
+ function (uids, next) {
+ uids = uids.filter(function (uid) {
+ return !!uid && parseInt(uid, 10);
+ });
+
+ async.eachSeries(uids, function (uid, next) {
+ groups.invite(data.groupName, uid, next);
+ }, next);
- }
++ },
+ ], callback);
});
SocketGroups.rescindInvite = isOwner(function (socket, data, callback) {
@@@ -181,12 -184,14 +184,14 @@@ SocketGroups.kick = isOwner(function (s
return callback(new Error('[[error:cant-kick-self]]'));
}
- groups.ownership.isOwner(data.uid, data.groupName, function (err, isOwner) {
- if (err) {
- return callback(err);
- }
- groups.kick(data.uid, data.groupName, isOwner, callback);
- });
+ async.waterfall([
+ function (next) {
+ groups.ownership.isOwner(data.uid, data.groupName, next);
+ },
+ function (isOwner, next) {
+ groups.kick(data.uid, data.groupName, isOwner, next);
- }
++ },
+ ], callback);
});
SocketGroups.create = function (socket, data, callback) {
@@@ -240,8 -232,8 +232,8 @@@ SocketGroups.search = function (socket
};
SocketGroups.loadMore = function (socket, data, callback) {
- if (!data.sort || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0) {
+ if (!data.sort || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0) {
- return callback();
+ return callback(new Error('[[error:invalid-data]]'));
}
var groupsPerPage = 9;
@@@ -260,13 -252,17 +252,17 @@@ SocketGroups.loadMoreMembers = functio
return callback(new Error('[[error:invalid-data]]'));
}
data.after = parseInt(data.after, 10);
- user.getUsersFromSet('group:' + data.groupName + ':members', socket.uid, data.after, data.after + 9, function (err, users) {
- if (err) {
- return callback(err);
- }
-
- callback(null, { users: users, nextStart: data.after + 10 });
- });
+ async.waterfall([
+ function (next) {
+ user.getUsersFromSet('group:' + data.groupName + ':members', socket.uid, data.after, data.after + 9, next);
+ },
+ function (users, next) {
+ next(null, {
+ users: users,
- nextStart: data.after + 10
++ nextStart: data.after + 10,
+ });
- }
++ },
+ ], callback);
};
SocketGroups.cover = {};
diff --cc test/groups.js
index 71a7bbca5a,6e8bf6e0b7..4d587b3aad
--- a/test/groups.js
+++ b/test/groups.js
@@@ -432,9 -433,8 +432,8 @@@ describe('Groups', function ()
var socketGroups = require('../src/socket.io/groups');
var meta = require('../src/meta');
-
it('should error if data is null', function (done) {
- socketGroups.before({uid: 0}, 'groups.join', null, function (err) {
+ socketGroups.before({ uid: 0 }, 'groups.join', null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
@@@ -535,19 -535,188 +534,188 @@@
});
});
- it('should accept membership of user', function (done) {
- socketGroups.accept({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: testUid }, function (err) {
+ it('should reject membership of user', function (done) {
- socketGroups.reject({uid: adminUid}, {groupName: 'PrivateCanJoin', toUid: testUid}, function (err) {
++ socketGroups.reject({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: testUid }, function (err) {
assert.ifError(err);
- Groups.isMember(testUid, 'PrivateCanJoin', function (err, isMember) {
+ Groups.isInvited(testUid, 'PrivateCanJoin', function (err, invited) {
assert.ifError(err);
- assert(isMember);
+ assert.equal(invited, false);
done();
});
});
});
+ it('should error if not owner or admin', function (done) {
- socketGroups.accept({uid: 0}, {groupName: 'PrivateCanJoin', toUid: testUid}, function (err) {
++ socketGroups.accept({ uid: 0 }, { groupName: 'PrivateCanJoin', toUid: testUid }, function (err) {
+ assert.equal(err.message, '[[error:no-privileges]]');
+ done();
+ });
+ });
+
+ it('should accept membership of user', function (done) {
- socketGroups.join({uid: testUid}, {groupName: 'PrivateCanJoin'}, function (err) {
++ socketGroups.join({ uid: testUid }, { groupName: 'PrivateCanJoin' }, function (err) {
+ assert.ifError(err);
- socketGroups.accept({uid: adminUid}, {groupName: 'PrivateCanJoin', toUid: testUid}, function (err) {
++ socketGroups.accept({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: testUid }, function (err) {
+ assert.ifError(err);
+ Groups.isMember(testUid, 'PrivateCanJoin', function (err, isMember) {
+ assert.ifError(err);
+ assert(isMember);
+ done();
+ });
+ });
+ });
+ });
+
+ it('should reject/accept all memberships requests', function (done) {
+ function requestMembership(uids, callback) {
+ async.series([
+ function (next) {
- socketGroups.join({uid: uids.uid1}, {groupName: 'PrivateCanJoin'}, next);
++ socketGroups.join({ uid: uids.uid1 }, { groupName: 'PrivateCanJoin' }, next);
+ },
+ function (next) {
- socketGroups.join({uid: uids.uid2}, {groupName: 'PrivateCanJoin'}, next);
- }
++ socketGroups.join({ uid: uids.uid2 }, { groupName: 'PrivateCanJoin' }, next);
++ },
+ ], function (err) {
+ callback(err);
+ });
+ }
+ var uids;
+ async.waterfall([
+ function (next) {
+ async.parallel({
+ uid1: function (next) {
- User.create({username: 'groupuser1'}, next);
++ User.create({ username: 'groupuser1' }, next);
+ },
+ uid2: function (next) {
- User.create({username: 'groupuser2'}, next);
- }
++ User.create({ username: 'groupuser2' }, next);
++ },
+ }, next);
+ },
+ function (results, next) {
+ uids = results;
+ requestMembership(results, next);
+ },
+ function (next) {
- socketGroups.rejectAll({uid: adminUid}, {groupName: 'PrivateCanJoin'}, next);
++ socketGroups.rejectAll({ uid: adminUid }, { groupName: 'PrivateCanJoin' }, next);
+ },
+ function (next) {
+ Groups.getPending('PrivateCanJoin', next);
+ },
+ function (pending, next) {
+ assert.equal(pending.length, 0);
+ requestMembership(uids, next);
+ },
+ function (next) {
- socketGroups.acceptAll({uid: adminUid}, {groupName: 'PrivateCanJoin'}, next);
++ socketGroups.acceptAll({ uid: adminUid }, { groupName: 'PrivateCanJoin' }, next);
+ },
+ function (next) {
+ Groups.isMembers([uids.uid1, uids.uid2], 'PrivateCanJoin', next);
+ },
+ function (isMembers, next) {
+ assert(isMembers[0]);
+ assert(isMembers[1]);
+ next();
- }
++ },
+ ], function (err) {
+ done(err);
+ });
+ });
+
+ it('should issue invite to user', function (done) {
- User.create({username: 'invite1'}, function (err, uid) {
++ User.create({ username: 'invite1' }, function (err, uid) {
+ assert.ifError(err);
- socketGroups.issueInvite({uid: adminUid}, {groupName: 'PrivateCanJoin', toUid: uid}, function (err) {
++ socketGroups.issueInvite({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: uid }, function (err) {
+ assert.ifError(err);
+ Groups.isInvited(uid, 'PrivateCanJoin', function (err, isInvited) {
+ assert.ifError(err);
+ assert(isInvited);
+ done();
+ });
+ });
+ });
+ });
+
+ it('should fail with invalid data', function (done) {
- socketGroups.issueMassInvite({uid: adminUid}, {groupName: 'PrivateCanJoin', usernames: null}, function (err) {
++ socketGroups.issueMassInvite({ uid: adminUid }, { groupName: 'PrivateCanJoin', usernames: null }, function (err) {
+ assert.equal(err.message, '[[error:invalid-data]]');
+ done();
+ });
+ });
+
+ it('should issue mass invite to users', function (done) {
- User.create({username: 'invite2'}, function (err, uid) {
++ User.create({ username: 'invite2' }, function (err, uid) {
+ assert.ifError(err);
- socketGroups.issueMassInvite({uid: adminUid}, {groupName: 'PrivateCanJoin', usernames: 'invite1, invite2'}, function (err) {
++ socketGroups.issueMassInvite({ uid: adminUid }, { groupName: 'PrivateCanJoin', usernames: 'invite1, invite2' }, function (err) {
+ assert.ifError(err);
+ Groups.isInvited(uid, 'PrivateCanJoin', function (err, isInvited) {
+ assert.ifError(err);
+ assert(isInvited);
+ done();
+ });
+ });
+ });
+ });
+
+ it('should rescind invite', function (done) {
- User.create({username: 'invite3'}, function (err, uid) {
++ User.create({ username: 'invite3' }, function (err, uid) {
+ assert.ifError(err);
- socketGroups.issueInvite({uid: adminUid}, {groupName: 'PrivateCanJoin', toUid: uid}, function (err) {
++ socketGroups.issueInvite({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: uid }, function (err) {
+ assert.ifError(err);
- socketGroups.rescindInvite({uid: adminUid}, {groupName: 'PrivateCanJoin', toUid: uid}, function (err) {
++ socketGroups.rescindInvite({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: uid }, function (err) {
+ assert.ifError(err);
+ Groups.isInvited(uid, 'PrivateCanJoin', function (err, isInvited) {
+ assert.ifError(err);
+ assert(!isInvited);
+ done();
+ });
+ });
+ });
+ });
+ });
+
+ it('should error if user is not invited', function (done) {
- socketGroups.acceptInvite({uid: adminUid}, {groupName: 'PrivateCanJoin'}, function (err) {
++ socketGroups.acceptInvite({ uid: adminUid }, { groupName: 'PrivateCanJoin' }, function (err) {
+ assert.equal(err.message, '[[error:not-invited]]');
+ done();
+ });
+ });
+
+ it('should accept invite', function (done) {
- User.create({username: 'invite4'}, function (err, uid) {
++ User.create({ username: 'invite4' }, function (err, uid) {
+ assert.ifError(err);
- socketGroups.issueInvite({uid: adminUid}, {groupName: 'PrivateCanJoin', toUid: uid}, function (err) {
++ socketGroups.issueInvite({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: uid }, function (err) {
+ assert.ifError(err);
- socketGroups.acceptInvite({uid: uid}, {groupName: 'PrivateCanJoin'}, function (err) {
++ socketGroups.acceptInvite({ uid: uid }, { groupName: 'PrivateCanJoin' }, function (err) {
+ assert.ifError(err);
+ Groups.isMember(uid, 'PrivateCanJoin', function (err, isMember) {
+ assert.ifError(err);
+ assert(isMember);
+ done();
+ });
+ });
+ });
+ });
+ });
+
+ it('should reject invite', function (done) {
- User.create({username: 'invite5'}, function (err, uid) {
++ User.create({ username: 'invite5' }, function (err, uid) {
+ assert.ifError(err);
- socketGroups.issueInvite({uid: adminUid}, {groupName: 'PrivateCanJoin', toUid: uid}, function (err) {
++ socketGroups.issueInvite({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: uid }, function (err) {
+ assert.ifError(err);
- socketGroups.rejectInvite({uid: uid}, {groupName: 'PrivateCanJoin'}, function (err) {
++ socketGroups.rejectInvite({ uid: uid }, { groupName: 'PrivateCanJoin' }, function (err) {
+ assert.ifError(err);
+ Groups.isInvited(uid, 'PrivateCanJoin', function (err, isInvited) {
+ assert.ifError(err);
+ assert(!isInvited);
+ done();
+ });
+ });
+ });
+ });
+ });
+
it('should grant ownership to user', function (done) {
- socketGroups.grant({uid: adminUid}, {groupName: 'PrivateCanJoin', toUid: testUid}, function (err) {
+ socketGroups.grant({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: testUid }, function (err) {
assert.ifError(err);
Groups.ownership.isOwner(testUid, 'PrivateCanJoin', function (err, isOwner) {
assert.ifError(err);
@@@ -568,8 -737,15 +736,15 @@@
});
});
+ it('should fail to kick user with invalid data', function (done) {
- socketGroups.kick({uid: adminUid}, {groupName: 'PrivateCanJoin', uid: adminUid}, function (err) {
++ socketGroups.kick({ uid: adminUid }, { groupName: 'PrivateCanJoin', uid: adminUid }, function (err) {
+ assert.equal(err.message, '[[error:cant-kick-self]]');
+ done();
+ });
+ });
+
it('should kick user from group', function (done) {
- socketGroups.kick({uid: adminUid}, {groupName: 'PrivateCanJoin', uid: testUid}, function (err) {
+ socketGroups.kick({ uid: adminUid }, { groupName: 'PrivateCanJoin', uid: testUid }, function (err) {
assert.ifError(err);
Groups.isMember(testUid, 'PrivateCanJoin', function (err, isMember) {
assert.ifError(err);
@@@ -578,6 -754,131 +753,131 @@@
});
});
});
+
+ it('should fail to create group with invalid data', function (done) {
- socketGroups.create({uid: 0}, {}, function (err) {
++ socketGroups.create({ uid: 0 }, {}, function (err) {
+ assert.equal(err.message, '[[error:no-privileges]]');
+ done();
+ });
+ });
+
+ it('should fail to create group if group creation is disabled', function (done) {
+ var oldValue = meta.config.allowGroupCreation;
+ meta.config.allowGroupCreation = 0;
- socketGroups.create({uid: 1}, {}, function (err) {
++ socketGroups.create({ uid: 1 }, {}, function (err) {
+ assert.equal(err.message, '[[error:group-creation-disabled]]');
+ meta.config.allowGroupCreation = oldValue;
+ done();
+ });
+ });
+
+ it('should fail to create group if name is privilege group', function (done) {
+ var oldValue = meta.config.allowGroupCreation;
+ meta.config.allowGroupCreation = 1;
- socketGroups.create({uid: 1}, {name: 'cid:1:privileges:groups:find'}, function (err) {
++ socketGroups.create({ uid: 1 }, { name: 'cid:1:privileges:groups:find' }, function (err) {
+ assert.equal(err.message, '[[error:invalid-group-name]]');
+ meta.config.allowGroupCreation = oldValue;
+ done();
+ });
+ });
+
+
+ it('should create/update group', function (done) {
+ var oldValue = meta.config.allowGroupCreation;
+ meta.config.allowGroupCreation = 1;
- socketGroups.create({uid: adminUid}, {name: 'createupdategroup'}, function (err, groupData) {
++ socketGroups.create({ uid: adminUid }, { name: 'createupdategroup' }, function (err, groupData) {
+ meta.config.allowGroupCreation = oldValue;
+ assert.ifError(err);
+ assert(groupData);
+ var data = {
+ groupName: 'createupdategroup',
+ values: {
+ name: 'renamedupdategroup',
+ description: 'cat group',
+ userTitle: 'cats',
+ userTitleEnabled: 1,
+ disableJoinRequests: 1,
+ hidden: 1,
- private: 0
- }
++ private: 0,
++ },
+ };
- socketGroups.update({uid: adminUid}, data, function (err) {
++ socketGroups.update({ uid: adminUid }, data, function (err) {
+ assert.ifError(err);
+ Groups.get('renamedupdategroup', {}, function (err, groupData) {
+ assert.ifError(err);
+ assert.equal(groupData.name, 'renamedupdategroup');
+ assert.equal(groupData.userTitle, 'cats');
+ assert.equal(groupData.description, 'cat group');
+ assert.equal(groupData.hidden, true);
+ assert.equal(groupData.disableJoinRequests, true);
+ assert.equal(groupData.private, false);
+ done();
+ });
+ });
+ });
+ });
+
+ it('should delete group', function (done) {
- socketGroups.delete({uid: adminUid}, {groupName: 'renamedupdategroup'}, function (err) {
++ socketGroups.delete({ uid: adminUid }, { groupName: 'renamedupdategroup' }, function (err) {
+ assert.ifError(err);
+ Groups.exists('renamedupdategroup', function (err, exists) {
+ assert.ifError(err);
+ assert(!exists);
+ done();
+ });
+ });
+ });
+
+ it('should fail to delete group if name is special', function (done) {
- socketGroups.delete({uid: adminUid}, {groupName: 'administrators'}, function (err) {
++ socketGroups.delete({ uid: adminUid }, { groupName: 'administrators' }, function (err) {
+ assert.equal(err.message, '[[error:not-allowed]]');
+ done();
+ });
+ });
+
+ it('should fail to delete group if name is special', function (done) {
- socketGroups.delete({uid: adminUid}, {groupName: 'registered-users'}, function (err) {
++ socketGroups.delete({ uid: adminUid }, { groupName: 'registered-users' }, function (err) {
+ assert.equal(err.message, '[[error:not-allowed]]');
+ done();
+ });
+ });
+
+ it('should fail to delete group if name is special', function (done) {
- socketGroups.delete({uid: adminUid}, {groupName: 'Global Moderators'}, function (err) {
++ socketGroups.delete({ uid: adminUid }, { groupName: 'Global Moderators' }, function (err) {
+ assert.equal(err.message, '[[error:not-allowed]]');
+ done();
+ });
+ });
+
+ it('should fail to load more groups with invalid data', function (done) {
- socketGroups.loadMore({uid: adminUid}, {}, function (err) {
++ socketGroups.loadMore({ uid: adminUid }, {}, function (err) {
+ assert.equal(err.message, '[[error:invalid-data]]');
+ done();
+ });
+ });
+
+ it('should load more groups', function (done) {
- socketGroups.loadMore({uid: adminUid}, {after: 0, sort: 'count'}, function (err, data) {
++ socketGroups.loadMore({ uid: adminUid }, { after: 0, sort: 'count' }, function (err, data) {
+ assert.ifError(err);
+ assert(Array.isArray(data.groups));
+ done();
+ });
+ });
+
+ it('should fail to load more members with invalid data', function (done) {
- socketGroups.loadMoreMembers({uid: adminUid}, {}, function (err) {
++ socketGroups.loadMoreMembers({ uid: adminUid }, {}, function (err) {
+ assert.equal(err.message, '[[error:invalid-data]]');
+ done();
+ });
+ });
+
+ it('should load more members', function (done) {
- socketGroups.loadMoreMembers({uid: adminUid}, {after: 0, groupName: 'PrivateCanJoin'}, function (err, data) {
++ socketGroups.loadMoreMembers({ uid: adminUid }, { after: 0, groupName: 'PrivateCanJoin' }, function (err, data) {
+ assert.ifError(err);
+ assert(Array.isArray(data.users));
+ done();
+ });
+ });
});
describe('admin socket methods', function () {
@@@ -815,8 -1116,15 +1115,15 @@@
});
});
+ it('should fail to remove cover if not logged in', function (done) {
- socketGroups.cover.remove({uid: 0}, {groupName: 'Test'}, function (err) {
++ socketGroups.cover.remove({ uid: 0 }, { groupName: 'Test' }, function (err) {
+ assert.equal(err.message, '[[error:no-privileges]]');
+ done();
+ });
+ });
+
it('should fail to remove cover if not owner', function (done) {
- socketGroups.cover.remove({uid: regularUid}, {groupName: 'Test'}, function (err) {
+ socketGroups.cover.remove({ uid: regularUid }, { groupName: 'Test' }, function (err) {
assert.equal(err.message, '[[error:no-privileges]]');
done();
});
diff --cc test/topics.js
index 911d8bb216,cfb77ed581..2573225511
--- a/test/topics.js
+++ b/test/topics.js
@@@ -19,7 -20,7 +20,7 @@@ describe('Topic\'s', function ()
before(function (done) {
groups.resetCache();
- User.create({ username: 'admin' }, function (err, uid) {
- User.create({username: 'admin', password: '123456'}, function (err, uid) {
++ User.create({ username: 'admin', password: '123456' }, function (err, uid) {
if (err) {
return done(err);
}
@@@ -669,26 -673,177 +670,176 @@@
});
});
- it('should load topic', function (done) {
- topics.post({
- uid: topic.userId,
- title: 'topic for controller test',
- content: 'topic content',
- cid: topic.categoryId,
- thumb: 'http://i.imgur.com/64iBdBD.jpg',
- }, function (err, result) {
- assert.ifError(err);
- assert.ok(result);
- var request = require('request');
- request(nconf.get('url') + '/topic/' + result.topicData.slug, function (err, response, body) {
+ describe('controller', function () {
+ var request = require('request');
+ var topicData;
+
+ before(function (done) {
+ topics.post({
+ uid: topic.userId,
+ title: 'topic for controller test',
+ content: 'topic content',
+ cid: topic.categoryId,
- thumb: 'http://i.imgur.com/64iBdBD.jpg'
++ thumb: 'http://i.imgur.com/64iBdBD.jpg',
+ }, function (err, result) {
+ assert.ifError(err);
+ assert.ok(result);
+ topicData = result.topicData;
+ done();
+ });
+ });
+
+ it('should load topic', function (done) {
+ request(nconf.get('url') + '/topic/' + topicData.slug, function (err, response, body) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 200);
+ assert(body);
+ done();
+ });
+ });
+
+ it('should 404 if post index is invalid', function (done) {
+ request(nconf.get('url') + '/topic/' + topicData.slug + '/derp', function (err, response) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 404);
+ done();
+ });
+ });
+
+ it('should 404 if topic does not exist', function (done) {
+ request(nconf.get('url') + '/topic/123123/does-not-exist', function (err, response) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 404);
+ done();
+ });
+ });
+
+ it('should 401 if not allowed to read as guest', function (done) {
+ var privileges = require('../src/privileges');
+ privileges.categories.rescind(['read'], topicData.cid, 'guests', function (err) {
+ assert.ifError(err);
+ request(nconf.get('url') + '/api/topic/' + topicData.slug, function (err, response, body) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 401);
+ assert(body);
+ privileges.categories.give(['read'], topicData.cid, 'guests', done);
+ });
+ });
+ });
+
+ it('should redirect to correct topic if slug is missing', function (done) {
+ request(nconf.get('url') + '/topic/' + topicData.tid + '/herpderp/1?page=2', function (err, response, body) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 200);
+ assert(body);
+ done();
+ });
+ });
+
+ it('should redirect if post index is out of range', function (done) {
+ request(nconf.get('url') + '/api/topic/' + topicData.slug + '/-1', function (err, response, body) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 308);
+ assert.equal(body, '"/topic/13/topic-for-controller-test"');
+ done();
+ });
+ });
+
+ it('should 404 if page is out of bounds', function (done) {
+ var meta = require('../src/meta');
+ meta.config.usePagination = 1;
+ request(nconf.get('url') + '/topic/' + topicData.slug + '?page=100', function (err, response) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 404);
+ done();
+ });
+ });
+
+ it('should mark topic read', function (done) {
+ helpers.loginUser('admin', '123456', function (err, jar) {
+ assert.ifError(err);
+ request(nconf.get('url') + '/topic/' + topicData.slug, {
- jar: jar
++ jar: jar,
+ }, function (err, res) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ topics.hasReadTopics([topicData.tid], adminUid, function (err, hasRead) {
+ assert.ifError(err);
+ assert.equal(hasRead[0], true);
+ done();
+ });
+ });
+ });
+ });
+
+ it('should 404 if tid is not a number', function (done) {
- request(nconf.get('url') + '/api/topic/teaser/nan', {json: true}, function (err, response, body) {
++ request(nconf.get('url') + '/api/topic/teaser/nan', { json: true }, function (err, response, body) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 404);
+ done();
+ });
+ });
+
+ it('should 403 if cant read', function (done) {
- request(nconf.get('url') + '/api/topic/teaser/' + 123123, {json: true}, function (err, response, body) {
++ request(nconf.get('url') + '/api/topic/teaser/' + 123123, { json: true }, function (err, response, body) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 403);
+ assert.equal(body, '[[error:no-privileges]]');
+
+ done();
+ });
+ });
+
+ it('should load topic teaser', function (done) {
- request(nconf.get('url') + '/api/topic/teaser/' + topicData.tid, {json: true}, function (err, response, body) {
++ request(nconf.get('url') + '/api/topic/teaser/' + topicData.tid, { json: true }, function (err, response, body) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 200);
+ assert(body);
+ assert.equal(body.tid, topicData.tid);
+ assert.equal(body.content, 'topic content');
+ assert(body.user);
+ assert(body.topic);
+ assert(body.category);
+ done();
+ });
+ });
+
+
+ it('should 404 if tid is not a number', function (done) {
- request(nconf.get('url') + '/api/topic/pagination/nan', {json: true}, function (err, response, body) {
++ request(nconf.get('url') + '/api/topic/pagination/nan', { json: true }, function (err, response, body) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 404);
+ done();
+ });
+ });
+
+ it('should 404 if tid does not exist', function (done) {
- request(nconf.get('url') + '/api/topic/pagination/1231231', {json: true}, function (err, response, body) {
++ request(nconf.get('url') + '/api/topic/pagination/1231231', { json: true }, function (err, response, body) {
+ assert.ifError(err);
+ assert.equal(response.statusCode, 404);
+ done();
+ });
+ });
+
+ it('should load pagination', function (done) {
- request(nconf.get('url') + '/api/topic/pagination/' + topicData.tid, {json: true}, function (err, response, body) {
++ request(nconf.get('url') + '/api/topic/pagination/' + topicData.tid, { json: true }, function (err, response, body) {
assert.ifError(err);
assert.equal(response.statusCode, 200);
assert(body);
+ assert.deepEqual(body, {
+ prev: { page: 1, active: false },
+ next: { page: 1, active: false },
+ rel: [],
+ pages: [],
+ currentPage: 1,
- pageCount: 1
++ pageCount: 1,
+ });
done();
});
});
-
});
+
describe('infinitescroll', function () {
var socketTopics = require('../src/socket.io/topics');
var tid;
diff --cc test/uploads.js
index 513f333884,7ac1b595a2..0c4ca8d1d6
--- a/test/uploads.js
+++ b/test/uploads.js
@@@ -87,6 -88,20 +87,20 @@@ describe('Upload Controllers', functio
});
});
+ it('should resize and upload an image to a post', function (done) {
+ var oldValue = meta.config.maximumImageWidth;
+ meta.config.maximumImageWidth = 10;
- helpers.uploadFile(nconf.get('url') + '/api/post/upload', path.join(__dirname, '../test/files/test.png'), {cid: cid}, jar, csrf_token, function (err, res, body) {
++ helpers.uploadFile(nconf.get('url') + '/api/post/upload', path.join(__dirname, '../test/files/test.png'), { cid: cid }, jar, csrf_token, function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert(Array.isArray(body));
+ assert(body[0].path);
+ assert(body[0].url);
+ meta.config.maximumImageWidth = oldValue;
+ done();
+ });
+ });
+
it('should upload a file to a post', function (done) {
meta.config.allowFileUploads = 1;
@@@ -99,6 -114,38 +113,37 @@@
done();
});
});
+
+ it('should fail if topic thumbs are disabled', function (done) {
+ helpers.uploadFile(nconf.get('url') + '/api/topic/thumb/upload', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 500);
+ assert.equal(body.error, '[[error:topic-thumbnails-are-disabled]]');
+ done();
+ });
+ });
+
+ it('should fail if file is not image', function (done) {
+ meta.config.allowTopicsThumbnail = 1;
+ helpers.uploadFile(nconf.get('url') + '/api/topic/thumb/upload', path.join(__dirname, '../test/files/503.html'), {}, jar, csrf_token, function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 500);
+ assert.equal(body.error, '[[error:invalid-file]]');
+ done();
+ });
+ });
+
+ it('should upload topic thumb', function (done) {
+ meta.config.allowTopicsThumbnail = 1;
+ helpers.uploadFile(nconf.get('url') + '/api/topic/thumb/upload', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, function (err, res, body) {
+ assert.ifError(err);
+ assert.equal(res.statusCode, 200);
+ assert(Array.isArray(body));
+ assert(body[0].path);
+ assert(body[0].url);
+ done();
+ });
+ });
-
});