test for system group rename

v1.18.x
barisusakli 8 years ago
parent 9f2e5e9c4b
commit 57691d9ad8

@ -91,16 +91,18 @@ module.exports = function (Groups) {
async.apply(db.sortedSetRemove, 'groups:visible:name', groupName.toLowerCase() + ':' + groupName), async.apply(db.sortedSetRemove, 'groups:visible:name', groupName.toLowerCase() + ':' + groupName),
], callback); ], callback);
} else { } else {
db.getObjectFields('group:' + groupName, ['createtime', 'memberCount'], function (err, groupData) { async.waterfall([
if (err) { function (next) {
return callback(err); db.getObjectFields('group:' + groupName, ['createtime', 'memberCount'], next);
} },
async.parallel([ function (groupData, next) {
async.apply(db.sortedSetAdd, 'groups:visible:createtime', groupData.createtime, groupName), async.parallel([
async.apply(db.sortedSetAdd, 'groups:visible:memberCount', groupData.memberCount, groupName), async.apply(db.sortedSetAdd, 'groups:visible:createtime', groupData.createtime, groupName),
async.apply(db.sortedSetAdd, 'groups:visible:name', 0, groupName.toLowerCase() + ':' + groupName), async.apply(db.sortedSetAdd, 'groups:visible:memberCount', groupData.memberCount, groupName),
], callback); async.apply(db.sortedSetAdd, 'groups:visible:name', 0, groupName.toLowerCase() + ':' + groupName),
}); ], next);
},
], callback);
} }
} }
@ -155,40 +157,48 @@ module.exports = function (Groups) {
function checkNameChange(currentName, newName, callback) { function checkNameChange(currentName, newName, callback) {
if (currentName === newName) { if (currentName === newName) {
return callback(); return setImmediate(callback);
} }
var currentSlug = utils.slugify(currentName); var currentSlug = utils.slugify(currentName);
var newSlug = utils.slugify(newName); var newSlug = utils.slugify(newName);
if (currentSlug === newSlug) { if (currentSlug === newSlug) {
return callback(); return setImmediate(callback);
} }
Groups.existsBySlug(newSlug, function (err, exists) { async.waterfall([
if (err || exists) { function (next) {
return callback(err || new Error('[[error:group-already-exists]]')); Groups.existsBySlug(newSlug, next);
} },
callback(); function (exists, next) {
}); next(!exists ? new Error('[[error:group-already-exists]]') : null);
},
], callback);
} }
function renameGroup(oldName, newName, callback) { function renameGroup(oldName, newName, callback) {
if (oldName === newName || !newName || newName.length === 0) { if (oldName === newName || !newName || newName.length === 0) {
return callback(); return setImmediate(callback);
} }
var group;
async.waterfall([
function (next) {
db.getObject('group:' + oldName, next);
},
function (_group, next) {
group = _group;
if (!group) {
return callback();
}
db.getObject('group:' + oldName, function (err, group) { if (parseInt(group.system, 10) === 1) {
if (err || !group) { return callback(new Error('[[error:not-allowed-to-rename-system-group]]'));
return callback(err);
}
if (parseInt(group.system, 10) === 1) {
return callback();
}
Groups.exists(newName, function (err, exists) {
if (err || exists) {
return callback(err || new Error('[[error:group-already-exists]]'));
} }
Groups.exists(newName, next);
},
function (exists, next) {
if (exists) {
return callback(new Error('[[error:group-already-exists]]'));
}
async.series([ async.series([
async.apply(db.setObjectField, 'group:' + oldName, 'name', newName), async.apply(db.setObjectField, 'group:' + oldName, 'name', newName),
async.apply(db.setObjectField, 'group:' + oldName, 'slug', utils.slugify(newName)), async.apply(db.setObjectField, 'group:' + oldName, 'slug', utils.slugify(newName)),
@ -222,29 +232,33 @@ module.exports = function (Groups) {
next(); next();
}, },
], callback); ], next);
}); },
], function (err) {
callback(err);
}); });
} }
function renameGroupMember(group, oldName, newName, callback) { function renameGroupMember(group, oldName, newName, callback) {
db.isSortedSetMember(group, oldName, function (err, isMember) { var score;
if (err || !isMember) { async.waterfall([
return callback(err); function (next) {
} db.isSortedSetMember(group, oldName, next);
var score; },
async.waterfall([ function (isMember, next) {
function (next) { if (!isMember) {
db.sortedSetScore(group, oldName, next); return callback();
}, }
function (_score, next) {
score = _score; db.sortedSetScore(group, oldName, next);
db.sortedSetRemove(group, oldName, next); },
}, function (_score, next) {
function (next) { score = _score;
db.sortedSetAdd(group, score, newName, next); db.sortedSetRemove(group, oldName, next);
}, },
], callback); function (next) {
}); db.sortedSetAdd(group, score, newName, next);
},
], callback);
} }
}; };

@ -315,6 +315,15 @@ describe('Groups', function () {
}); });
}); });
}); });
it('should fail if system groups is being renamed', function (done) {
Groups.update('administrators', {
name: 'administrators_fail',
}, function (err) {
assert.equal(err.message, '[[error:not-allowed-to-rename-system-group]]');
done();
});
});
}); });
describe('.destroy()', function () { describe('.destroy()', function () {

Loading…
Cancel
Save