allow updating other profile fields when username isnt being changed
v1.18.x
Barış Soner Uşaklı 4 years ago
parent 4be693f2e7
commit 38d3982bf1

@ -86,7 +86,9 @@ module.exports = function (User) {
} }
data.username = data.username.trim(); data.username = data.username.trim();
const userData = await User.getUserFields(uid, ['username', 'userslug']); const userData = await User.getUserFields(uid, ['username', 'userslug']);
var userslug = utils.slugify(data.username); if (userData.username === data.username) {
return;
}
if (data.username.length < meta.config.minimumUsernameLength) { if (data.username.length < meta.config.minimumUsernameLength) {
throw new Error('[[error:username-too-short]]'); throw new Error('[[error:username-too-short]]');
@ -96,6 +98,7 @@ module.exports = function (User) {
throw new Error('[[error:username-too-long]]'); throw new Error('[[error:username-too-long]]');
} }
const userslug = utils.slugify(data.username);
if (!utils.isUserNameValid(data.username) || !userslug) { if (!utils.isUserNameValid(data.username) || !userslug) {
throw new Error('[[error:invalid-username]]'); throw new Error('[[error:invalid-username]]');
} }

@ -900,6 +900,22 @@ describe('User', function () {
}); });
}); });
it('should not let setting an empty username', async function () {
await socketUser.changeUsernameEmail({ uid: uid }, { uid: uid, username: '', password: '123456' });
const username = await db.getObjectField('user:' + uid, 'username');
assert.strictEqual(username, 'updatedAgain');
});
it('should let updating profile if current username is above max length and it is not being changed', async function () {
const maxLength = meta.config.maximumUsernameLength + 1;
const longName = new Array(maxLength).fill('a').join('');
const uid = await User.create({ username: longName });
await socketUser.changeUsernameEmail({ uid: uid }, { uid: uid, username: longName, email: 'verylong@name.com' });
const userData = await db.getObject('user:' + uid);
assert.strictEqual(userData.username, longName);
assert.strictEqual(userData.email, 'verylong@name.com');
});
it('should not update a user\'s username if it did not change', function (done) { it('should not update a user\'s username if it did not change', function (done) {
socketUser.changeUsernameEmail({ uid: uid }, { uid: uid, username: 'updatedAgain', password: '123456' }, function (err) { socketUser.changeUsernameEmail({ uid: uid }, { uid: uid, username: 'updatedAgain', password: '123456' }, function (err) {
assert.ifError(err); assert.ifError(err);

Loading…
Cancel
Save