Allow the userdb code to distinguish between username or email duplicates

This commit is contained in:
Johannes Zellner
2016-06-02 15:34:27 +02:00
parent a52e2ffc23
commit 594be7dbbd
2 changed files with 58 additions and 4 deletions

View File

@@ -86,10 +86,28 @@ describe('database', function () {
userdb.add(USER_2.id, USER_2, done);
});
it('cannot add same user again', function (done) {
userdb.add(USER_0.id, USER_0, function (error) {
it('cannot add user width same email again', function (done) {
var tmp = JSON.parse(JSON.stringify(USER_0));
tmp.id = 'somethingelse';
tmp.username = 'somethingelse';
userdb.add(tmp.id, tmp, function (error) {
expect(error).to.be.ok();
expect(error.reason).to.be(DatabaseError.ALREADY_EXISTS);
expect(error.message).to.equal('email already exists');
done();
});
});
it('cannot add user width same username again', function (done) {
var tmp = JSON.parse(JSON.stringify(USER_0));
tmp.id = 'somethingelse';
tmp.email = 'somethingelse@not.taken';
userdb.add(tmp.id, tmp, function (error) {
expect(error).to.be.ok();
expect(error.reason).to.be(DatabaseError.ALREADY_EXISTS);
expect(error.message).to.equal('username already exists');
done();
});
});
@@ -185,6 +203,24 @@ describe('database', function () {
});
});
it('can update the user with already existing email', function (done) {
userdb.update(USER_0.id, { email: USER_2.email }, function (error) {
expect(error).to.be.ok();
expect(error.reason).to.be(DatabaseError.ALREADY_EXISTS);
expect(error.message).to.equal('email already exists');
done();
});
});
it('can update the user with already existing username', function (done) {
userdb.update(USER_0.id, { username: USER_2.username }, function (error) {
expect(error).to.be.ok();
expect(error.reason).to.be(DatabaseError.ALREADY_EXISTS);
expect(error.message).to.equal('username already exists');
done();
});
});
it('cannot update with null field', function () {
expect(function () {
userdb.update(USER_0.id, { email: null }, function () {});

View File

@@ -143,7 +143,16 @@ function add(userId, user, callback) {
var data = [ userId, user.username || null, user.password, user.email, user.salt, user.createdAt, user.modifiedAt, user.resetToken, user.displayName, user.showTutorial ];
database.query('INSERT INTO users (id, username, password, email, salt, createdAt, modifiedAt, resetToken, displayName, showTutorial) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', data, function (error, result) {
if (error && error.code === 'ER_DUP_ENTRY') return callback(new DatabaseError(DatabaseError.ALREADY_EXISTS, error));
if (error && error.code === 'ER_DUP_ENTRY') {
var msg = error.message;
if (error.message.indexOf('users_email') !== -1) {
msg = 'email already exists';
} else if (error.message.indexOf('users_username') !== -1) {
msg = 'username already exists';
}
return callback(new DatabaseError(DatabaseError.ALREADY_EXISTS, msg));
}
if (error || result.affectedRows !== 1) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error));
callback(null);
@@ -216,7 +225,16 @@ function update(userId, user, callback) {
args.push(userId);
database.query('UPDATE users SET ' + fields.join(', ') + ' WHERE id = ?', args, function (error, result) {
if (error && error.code === 'ER_DUP_ENTRY') return callback(new DatabaseError(DatabaseError.ALREADY_EXISTS, error));
if (error && error.code === 'ER_DUP_ENTRY') {
var msg = error.message;
if (error.message.indexOf('users_email') !== -1) {
msg = 'email already exists';
} else if (error.message.indexOf('users_username') !== -1) {
msg = 'username already exists';
}
return callback(new DatabaseError(DatabaseError.ALREADY_EXISTS, msg));
}
if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error));
if (result.affectedRows !== 1) return callback(new DatabaseError(DatabaseError.NOT_FOUND));