Skip to content

Commit

Permalink
bug(user-signup): fix duplicate email error
Browse files Browse the repository at this point in the history
- create helper functions to create a user & find a user by phone no
- update signup route and handle errors as expected
- move all interactions with user model to user service
- add more test cases for signup route

[Fixes #168055189]
  • Loading branch information
jsbuddy committed Aug 22, 2019
1 parent 0b0cd3a commit a05d1c3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
39 changes: 19 additions & 20 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import Redis from 'ioredis';
import models from '../database/models';
import authHelper from '../utils/authHelper';
import response from '../utils/response';
import messages from '../utils/messages';
import create from '../services/dbServices';
import { findByEmail, comparePasswords } from '../services/userServices';

const { User } = models;
import {
create, findByEmail, findByPhone, comparePasswords
} from '../services/userServices';

/**
* user signup controller
* @param {Object} req - server request
* @param {Object} res - server response
* @returns {Object} - custom response
*/

* user signup controller
* @param {Object} req - server request
* @param {Object} res - server response
* @returns {Object} - custom response
*/
const signUp = async (req, res) => {
const {
firstName, lastName, email, password, phoneNo
} = req.body;
try {
const {
firstName, lastName, email, password, phoneNo
} = req.body;
const user = {
firstName, lastName, email, phoneNo, password
};

const emailExists = await findByEmail(email);
if (emailExists) return response(res, 400, 'error', { message: messages.emailExists });
const createdUser = await create(User, user);
const errors = [];
if (await findByEmail(email)) errors.push(messages.emailExists);
if (await findByPhone(phoneNo)) errors.push(messages.phoneExists);
if (errors.length) return response(res, 400, 'error', { message: errors.join(', ') });

const createdUser = await create(user);
const userData = {
user: {
id: createdUser.id,
Expand All @@ -36,8 +36,8 @@ const signUp = async (req, res) => {
};

return response(res, 200, 'success', userData);
} catch (e) {
return response(res, 500, 'error', { errors: e });
} catch (error) {
return response(res, 500, 'error', { errors: error.message });
}
};

Expand All @@ -47,7 +47,6 @@ const signUp = async (req, res) => {
* @param {Object} res - server response
* @returns {Object} - custom response
*/

const signIn = async (req, res) => {
try {
const { email, password } = req.body;
Expand Down
14 changes: 14 additions & 0 deletions src/services/userServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import models from '../database/models';

const { User } = models;

/**
* Helper function to create a new user
* @param {Object} user - user's object
* @returns {Promise} - sequelize response
*/
export const create = (user) => User.create(user);

/**
* Helper function to find a user by email
* @param {String} email - user's email
Expand All @@ -17,6 +24,13 @@ export const findByEmail = (email) => User.findOne({ where: { email } });
*/
export const findById = (id) => User.findOne({ where: { id } });

/**
* Helper function to find a user by phone
* @param {String} phoneNo - user's phone number
* @returns {Promise} - sequelize response
*/
export const findByPhone = (phoneNo) => User.findOne({ where: { phoneNo } });

/**
* Helper function to compare the password provided with the user's hashed password
* @param {String} password - provided password
Expand Down
8 changes: 8 additions & 0 deletions src/test/mockData/userMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ export default {
password: 'Password'
},

validUser2: {
firstName: 'John',
lastName: 'Doe',
email: 'jd@gmail.com',
phoneNo: '08077777772',
password: 'Password2',
},

inValidUser: {
firstName: 'Annie',
lastName: 'Skywalker',
Expand Down
16 changes: 15 additions & 1 deletion src/test/routes/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('AUTH', () => {
});
});

it('should not allow duplicate email address when creating a user', (done) => {
it('should not allow duplicate email address & phone number when creating a user', (done) => {
chai
.request(app)
.post(signupEndpoint)
Expand All @@ -61,6 +61,20 @@ describe('AUTH', () => {
done(err);
});
});

it('should return an internal server error', (done) => {
const stub = sinon.stub(User, 'create').callsFake(() => Promise.reject(new Error('Internal server error')));
chai
.request(app)
.post(signupEndpoint)
.send(userMock.validUser2)
.end((err, res) => {
expect(res.status).to.equal(500);
expect(res.body).to.have.property('status').that.equal('error');
done(err);
stub.restore();
});
});
});

describe('POST /user/signin', () => {
Expand Down
1 change: 1 addition & 0 deletions src/utils/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const messages = {
apiV1Welcome: 'Welcome to Barefoot Nomad API (version 1)',
notFound: 'Sorry, we cannot find this endpoint',
emailExists: 'Email address already in use',
phoneExists: 'Phone number already in use',
userNotFound: 'User not found, please check your email address',
incorrectPassword: 'Incorrect password',
validEmail: 'Enter a valid email address',
Expand Down

0 comments on commit a05d1c3

Please sign in to comment.