Skip to content

Commit

Permalink
Merge 8b0a9c7 into 2b89f81
Browse files Browse the repository at this point in the history
  • Loading branch information
alainburindi committed Aug 5, 2019
2 parents 2b89f81 + 8b0a9c7 commit 1310e5e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/api/routes/authRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Router } from 'express';
import ValidateUser from '../../middlewares/ValidateUser';
import authController from '../controllers/authController';
import validateToken from '../../middlewares/checkValidToken';
import checkUsername from '../../middlewares/checkUsername';

import socialAuthController from '../controllers/socialAuth';
import passport from '../../configs/passport';
Expand All @@ -10,7 +11,7 @@ import EmailVerifier from '../controllers/verifyEmail';

const authRouter = new Router();

authRouter.post('/signup', ValidateUser.validateSignup, authController.signup);
authRouter.post('/signup', ValidateUser.validateSignup, checkUsername, authController.signup);
authRouter.post('/login', authController.login);
authRouter.get('/signout', validateToken, authController.signout);

Expand Down
4 changes: 3 additions & 1 deletion src/helpers/constants/error.messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@ export default {
notificationNotFound: 'No notifications found for now, Thanks',
noUserComment: 'You have no comment history on this article.',
notModifiedComment: 'The comment is not modifying what is already stored',
notAValidReason: 'Sorry, but that reason does not exist, Thanks'
notAValidReason: 'Sorry, but that reason does not exist, Thanks',
emailExists: 'The user with the same email already exists',
usernameExists: 'The username has already been used'
};
3 changes: 2 additions & 1 deletion src/middlewares/ValidateUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import models from '../api/models';
import signupValidator from '../helpers/validators/signupValidator';
import statusCode from '../helpers/constants/status.codes';
import sendError from '../helpers/error.sender';
import errorMessage from '../helpers/constants/error.messages';

const { User } = models;
/**
Expand All @@ -19,7 +20,7 @@ export default class {
try {
await signupValidator(req.body);
const emailExists = await User.findByEmail(email);
if (emailExists) sendError(statusCode.BAD_REQUEST, res, 'email', ' The user already exists');
if (emailExists) sendError(statusCode.BAD_REQUEST, res, 'email', errorMessage.emailExists);
else return next();
} catch (error) {
if (error.isJoi) {
Expand Down
12 changes: 12 additions & 0 deletions src/middlewares/checkUsername.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import models from '../api/models';
import statusCode from '../helpers/constants/status.codes';
import sendError from '../helpers/error.sender';
import errorMessage from '../helpers/constants/error.messages';

const { User } = models;
export default async (req, res, next) => {
const { username } = req.body;
const usedUsername = await User.findOne({ where: { username } });
if (usedUsername) sendError(statusCode.BAD_REQUEST, res, 'username', errorMessage.usernameExists);
else return next();
};
2 changes: 1 addition & 1 deletion tests/admin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Users', () => {
})
.end((err, res) => {
res.should.have.status(status.BAD_REQUEST);
res.body.errors.should.have.property('email').eql(error.exist);
res.body.errors.should.have.property('email').eql(error.emailExists);
done();
});
});
Expand Down
20 changes: 16 additions & 4 deletions tests/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ describe('Signup', () => {
.request(server)
.post('/api/users/signup')
.send({
username: data.username,
email: data.email,
password: data.password,
confirmPassword: data.password
...data
})
.end((err, res) => {
res.should.have.status(status.CREATED);
Expand Down Expand Up @@ -254,6 +251,21 @@ describe('Routes', () => {
done();
});
});

it('should not register with a used username', (done) => {
data.email = 'data.email@em.com';
chai
.request(server)
.post('/api/users/signup')
.send({
...data
})
.end((err, res) => {
res.should.have.status(status.BAD_REQUEST);
res.body.errors.should.have.property('username', errors.usernameExists);
done();
});
});
});

describe('Mailer', async () => {
Expand Down

0 comments on commit 1310e5e

Please sign in to comment.