Skip to content

Commit

Permalink
feature-[167573194]: build out signup endpoint
Browse files Browse the repository at this point in the history
- Enable user sign up feature
- Write unit and integration tests for feature
[Deivers #167573194]
  • Loading branch information
chialuka committed Jul 30, 2019
1 parent 14322ad commit 6bf2454
Show file tree
Hide file tree
Showing 20 changed files with 433 additions and 8 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ PORT=8080
DB_URL_DEV=postgres://rambo:rambo@localhost:2800/haven
DB_URL_PRODUCTION=postgres://authors:haven@localhost:2800/haven
NODE_ENV=development
BASE_URL=/api/v5
2 changes: 1 addition & 1 deletion .sequelizerc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const path = require('path');
module.exports = {
'config': path.resolve('server/database/config', 'config.js'),
'models-path': path.resolve('server/database/models'),
'seeds-path': path.resolve('server/database/seeds'),
'seeders-path': path.resolve('server/database/seeds'),
'migrations-path': path.resolve('server/database/migrations')
}
110 changes: 110 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"coverage": "nyc report --reporter=text-lcov | coveralls",
"db:migrate": "sequelize db:migrate",
"db:migrate-undo": "sequelize db:migrate:undo:all",
"db:rollback": "npm run db:back && npm run db:roll && npm run db:seed",
"db:rollback": "npm run db:migrate-undo && npm run db:migrate && npm run db:seed",
"db:seed": "sequelize db:seed:all",
"start": "node dist/index.js",
"start:dev": "cross-env NODE_ENV=development DEBUG=dev nodemon --exec babel-node server",
"test": "nyc mocha --timeout 1000 -r esm --exit"
"test": "cross-env NODE_ENV=test npm run db:rollback && nyc mocha ./test/index.test.js --timeout 1000 -r esm --exit"
},
"husky": {
"hooks": {
Expand All @@ -26,18 +26,19 @@
"author": "Andela Simulations Programme",
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"coveralls": "^3.0.5",
"debug": "^4.1.1",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"morgan": "^1.9.1",
"swagger-ui-express": "^4.0.7",
"yamljs": "^0.3.0",
"pg": "^7.11.0",
"pg-hstore": "^2.3.3",
"sequelize": "^5.10.2",
"sequelize-cli": "^5.5.0",
"sqlite3": "^4.0.9"
"sqlite3": "^4.0.9",
"swagger-ui-express": "^4.0.7",
"yamljs": "^0.3.0"
},
"devDependencies": {
"@babel/cli": "^7.5.5",
Expand All @@ -54,12 +55,14 @@
"eslint-config-airbnb-base": "^13.2.0",
"eslint-plugin-import": "^2.18.2",
"esm": "^3.2.25",
"faker": "^4.1.0",
"husky": "^3.0.1",
"lint-staged": "^9.2.1",
"mocha": "^6.1.4",
"mocha-lcov-reporter": "^1.3.0",
"nodemon": "^1.19.1",
"nyc": "^14.1.1",
"prettier-eslint-cli": "^5.0.0"
"prettier-eslint-cli": "^5.0.0",
"sinon": "^7.3.2"
}
}
47 changes: 47 additions & 0 deletions server/controllers/Users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import models from '../database/models';
// import { generateToken, serverResponse, serverError } from '../helpers';
import { serverResponse, serverError } from '../helpers';
import hashPassword from '../helpers/passwords';

const { User } = models;

/**
* @export
* @class User
*/
class Users {
/**
* @name signUp
* @async
* @static
* @memberof User
* @param {Object} req express request object
* @param {Object} res express response object
* @returns {JSON} JSON object with details of new user
*/
static async signUp(req, res) {
try {
const { email, userName, password } = req.body;
const existingEmail = await User.findOne({ where: { email } });
const existingUserName = await User.findOne({ where: { userName } });
if (existingEmail) {
return serverResponse(res, 409, { error: 'Email already exists' });
}
if (existingUserName) {
return serverResponse(res, 409, { error: 'Username already exists' });
}
const hashedPassword = await hashPassword(password);
const user = await User.create({
...req.body,
password: hashedPassword
});
delete user.dataValues.password;
// const token = await generateToken(user.id);
return serverResponse(res, 201, { user });
} catch (error) {
return serverError(res);
}
}
}

export default Users;
34 changes: 34 additions & 0 deletions server/database/migrations/20190730111558-create-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
userName: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: (queryInterface, Sequelize) => queryInterface.dropTable('Users')
};
3 changes: 2 additions & 1 deletion server/database/models/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* istanbul ignore file */
import fs from 'fs';
import path from 'path';
import Sequelize from 'sequelize';
Expand Down Expand Up @@ -36,4 +37,4 @@ Object.keys(db).forEach((modelName) => {
db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
export default db;
17 changes: 17 additions & 0 deletions server/database/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define(
'User',
{
firstName: DataTypes.STRING,
lastName: DataTypes.STRING,
userName: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING
},
{}
);
User.associate = function (models) {
// associations can be defined here
};
return User;
};
19 changes: 19 additions & 0 deletions server/database/seeds/20190730180408-my-seed-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.bulkInsert(
'Users',
[
{
firstName: 'Abiola',
lastName: 'JayZ',
userName: 'JhayX',
email: 'abiola.jz@andela.com',
password: 'incorrect',
createdAt: new Date(),
updatedAt: new Date()
}
],
{}
),

down: (queryInterface, _) => queryInterface.bulkDelete('Users', null, {})
};
Loading

0 comments on commit 6bf2454

Please sign in to comment.