diff --git a/src/models/User.js b/src/models/User.js index 5c32fc37a38893abc1fa2d10654fa7d2dac57429..bcec03cc7ce8555e869a0e4ddbb675dd21a620bf 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -51,7 +51,7 @@ class User extends Model { throw new ConflictError('User already exists') } - result = await User.findByUsername(user.email).catch(swallowNotFound) + result = await User.findByUsername(user.username).catch(swallowNotFound) if (result) { throw new ConflictError('User already exists') diff --git a/test/user_test.js b/test/user_test.js index 6dd45f25bc28fab4e1ece1136e585a61a468b4ad..2359c76daff271c27ddba8dcda52bbea5ddc11cf 100644 --- a/test/user_test.js +++ b/test/user_test.js @@ -21,9 +21,28 @@ describe('User', function () { expect(shouldBeInvalid).toEqual(false) }) - it('raises an error if trying to save a non-unique user', async () => { + it('raises an error if trying to save a user with a non-unique username', async () => { const user = new User(userFixture) - const duplicateUser = new User(userFixture) + const otherUserFixture = fixtures.otherUser + otherUserFixture.username = userFixture.username + const duplicateUser = new User(otherUserFixture) + + await user.save() + + try { + await duplicateUser.save() + } catch (err) { + expect(err.name).toEqual('ConflictError') + } + + expect.hasAssertions() + }) + + it('raises an error if trying to save a user with a non-unique email', async () => { + const user = new User(userFixture) + const otherUserFixture = fixtures.otherUser + otherUserFixture.email = userFixture.email + const duplicateUser = new User(otherUserFixture) await user.save()