it('should report error when email already exists', () => { return request(app) .post('/v1/auth/register') .send(dbUser) .expect(httpStatus.CONFLICT) .then((res) => { const { field } = res.body.errors[0]; const { location } = res.body.errors[0]; const { messages } = res.body.errors[0]; expect(field).to.be.equal('email'); expect(location).to.be.equal('body'); expect(messages).to.include('"email" already exists'); }); });
it('should report error when email already exists', () => { user.email = dbUsers.branStark.email; return request(app) .post('/v1/users') .set('Authorization', `Bearer ${adminAccessToken}`) .send(user) .expect(httpStatus.CONFLICT) .then((res) => { const { field } = res.body.errors[0]; const { location } = res.body.errors[0]; const { messages } = res.body.errors[0]; expect(field).to.be.equal('email'); expect(location).to.be.equal('body'); expect(messages).to.include('"email" already exists'); }); });
/** * Return new validation error * if error is a mongoose duplicate key error * * @param {Error} error * @returns {Error|APIError} */ checkDuplicateEmail(error) { if (error.name === 'MongoError' && error.code === 11000) { return new APIError({ message: 'Validation Error', errors: [{ field: 'email', location: 'body', messages: ['"email" already exists'], }], status: httpStatus.CONFLICT, isPublic: true, stack: error.stack, }); } return error; }
checkDuplicateEmailError (err) { if (err.code === 11000) { let error = new Error('Email already taken') error.errors = [{ field: 'email', location: 'body', messages: ['Email already taken'] }] error.status = httpStatus.CONFLICT return error } return err }
checkDuplicateEmailError (err) { if (err.code === 11000) { var error = new Error('Email already taken') error.errors = [{ field: 'email', location: 'body', messages: ['Email already taken'] }] error.status = httpStatus.CONFLICT return error } return err }