-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validation): implement signup validation
[Starts #167891575]
- Loading branch information
1 parent
1b6e517
commit febcfd3
Showing
15 changed files
with
228 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,66 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => { | ||
queryInterface.createTable('Users', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.DataTypes.UUID, | ||
defaultValue: Sequelize.literal('uuid_generate_v4()'), | ||
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => queryInterface.createTable('Users', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.DataTypes.UUID, | ||
defaultValue: Sequelize.literal('uuid_generate_v4()'), | ||
}, | ||
companyId: { | ||
allowNull: false, | ||
type: Sequelize.DataTypes.UUID, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'Companies', | ||
key: 'id', | ||
}, | ||
companyId: { | ||
allowNull: false, | ||
type: Sequelize.DataTypes.UUID, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'Companies', | ||
key: 'id', | ||
}, | ||
}, | ||
firstName: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
lastName: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
email: { | ||
allowNull: false, | ||
unique: true, | ||
type: Sequelize.STRING | ||
}, | ||
password: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
gender: { | ||
allowNull: false, | ||
type: Sequelize.ENUM('male', 'female') | ||
}, | ||
dob: { | ||
allowNull: false, | ||
type: Sequelize.DATEONLY, | ||
}, | ||
role: { | ||
allowNull: false, | ||
type: Sequelize.ENUM('owner', 'admin', 'staff'), | ||
defaultValue: 'staff' | ||
}, | ||
status: { | ||
allowNull: false, | ||
type: Sequelize.ENUM('active', 'inactive'), | ||
defaultValue: 'active' | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
} | ||
}); | ||
}), | ||
down: queryInterface => queryInterface.dropTable('Users') | ||
}, | ||
firstName: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
lastName: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
email: { | ||
allowNull: false, | ||
unique: true, | ||
type: Sequelize.STRING | ||
}, | ||
password: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
gender: { | ||
allowNull: false, | ||
type: Sequelize.ENUM('male', 'female') | ||
}, | ||
dob: { | ||
allowNull: false, | ||
type: Sequelize.DATEONLY, | ||
}, | ||
role: { | ||
allowNull: false, | ||
type: Sequelize.ENUM('owner', 'admin', 'staff'), | ||
defaultValue: 'staff' | ||
}, | ||
status: { | ||
allowNull: false, | ||
type: Sequelize.ENUM('active', 'inactive'), | ||
defaultValue: 'active' | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
} | ||
})), | ||
down: (queryInterface) => queryInterface.dropTable('Users') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* eslint-disable require-jsdoc */ | ||
import { validationResult } from 'express-validator'; | ||
import Response from '../utils/response'; | ||
|
||
export default class SignupValidator { | ||
static validateSignup(req, res, next) { | ||
const errors = validationResult(req); | ||
const validationErrors = []; | ||
|
||
if (errors) { | ||
errors.map(error => validationErrors.push(error.msg)); | ||
const response = new Response( | ||
'Bad request', | ||
400, | ||
'Invalid credentials', | ||
validationErrors | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* eslint-disable require-jsdoc */ | ||
import models from '../database/models'; | ||
import Response from '../utils/response'; | ||
|
||
const { User } = models; | ||
|
||
export default class UserMiddleware { | ||
static async verifyExistingUser(req, res, next) { | ||
try { | ||
const { email } = req.body; | ||
|
||
const user = await User.findOne({ where: { email } }); | ||
if (user) { | ||
const response = new Response( | ||
'Conflict', | ||
409, | ||
'User with email already exist' | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
next(); | ||
} catch (error) { | ||
const response = new Response( | ||
'Internal server error', | ||
500, | ||
`${error}`, | ||
); | ||
return res.status(response.code).json(response); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @function capitalize | ||
* @description | ||
* @param {string} word | ||
* @return {function} call | ||
*/ | ||
const capitalize = (word) => { | ||
const formatWord = word | ||
.toLowerCase() | ||
.split(' ') | ||
.map((f) => f.charAt(0).toUpperCase() + f.substring(1)) | ||
.join(' '); | ||
|
||
return formatWord; | ||
}; | ||
|
||
export default capitalize; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* eslint-disable require-jsdoc */ | ||
class Response { | ||
constructor(status, code, messages, data) { | ||
this.status = status; | ||
this.code = code; | ||
this.messages = messages; | ||
this.data = data; | ||
} | ||
} | ||
|
||
export default Response; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import signup from './signup'; | ||
|
||
export default { | ||
signup, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { check } from 'express-validator/check'; | ||
import capitalize from '../utils/capitalize'; | ||
|
||
export default { | ||
signupSchema: [ | ||
check('firstName') | ||
.trim() | ||
.exists().withMessage('First name is required') | ||
.isLength({ min: 2, max: 15 }) | ||
.withMessage('First name should be between 2 to 15 characters') | ||
.isAlpha() | ||
.withMessage('First name should only be alphabets') | ||
.customSanitizer(value => capitalize(value)), | ||
|
||
check('lastName') | ||
.trim() | ||
.exists().withMessage('Last name is required') | ||
.isLength({ min: 2, max: 15 }) | ||
.withMessage('Last name should be between 2 to 15 characters') | ||
.isAlpha() | ||
.withMessage('Last name should only be alphabets') | ||
.customSanitizer(value => capitalize(value)), | ||
|
||
check('email') | ||
.trim() | ||
.exists().withMessage('Email address is required') | ||
.isEmail() | ||
.withMessage('Email address is invalid'), | ||
|
||
check('password') | ||
.trim() | ||
.exists().withMessage('Password is required') | ||
.isLength({ min: 8, }) | ||
.withMessage('Password must be alphanumeric and not less than 8 characters') | ||
.isAlpha() | ||
.withMessage('Password must be alphanumeric and not less than 8 characters') | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import chai, { should } from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import app from '../../src'; | ||
|
||
const baseURI = '/api/v1/auth'; | ||
|
||
describe('Auth Routes', () => { | ||
describe('Signup Route', () => { | ||
specify('error if user is already registered', async () => { | ||
const response = await chai | ||
.request(app) | ||
.post(`${baseURI}/signup`) | ||
.send(user); | ||
response.should.have.status(409); | ||
response.body.status.should.eql('error'); | ||
response.body.should.have.property('error'); | ||
}); | ||
|
||
specify('error if FirstName is undefined'); | ||
specify('error if FirstName contains an invalid character'); | ||
specify('error if LastName is undefined'); | ||
specify('error if LastName contains an invalid character'); | ||
specify('error if email is not provided'); | ||
specify('error if email is invalid'); | ||
specify('error if password is not provided'); | ||
specify('error if password is invalid'); | ||
specify('error if password is less than minimum length'); | ||
}); | ||
}); |