Skip to content

Commit

Permalink
Merge 98e959a into 8150687
Browse files Browse the repository at this point in the history
  • Loading branch information
rwajon committed May 2, 2019
2 parents 8150687 + 98e959a commit 3c6d6fc
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 32 deletions.
16 changes: 8 additions & 8 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"reporter": [
"lcov",
"text"
],
"exclude": [
"src/migartions",
"src/helpers"
]
"reporter": [
"lcov",
"text"
],
"exclude": [
"src/migrations",
"src/tests"
]
}
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
env:
global:
- CC_TEST_REPORTER_ID=d570e2625aaffa20e7828eaddfbec748c525e866b6d413472bc26e188cb2bc6a
- SECRET_KEY=12345
- NODE_ENV=test
- CC_TEST_REPORTER_ID=d570e2625aaffa20e7828eaddfbec748c525e866b6d413472bc26e188cb2bc6a
- DATABASE_URL_TEST=postgres://postgres@localhost:5432/authorhavens_test
language: node_js
node_js:
- "stable"
cache:
directories:
- "node_modules"
- "node_modules"
services:
- postgresql
before_script:
- psql -c 'CREATE DATABASE authorhavens_test;' -U postgres
- npm i sequelize-cli -g
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
Expand Down
28 changes: 28 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A Social platform for the creative at heart",
"main": "src/index.js",
"scripts": {
"test": "NODE_ENV=test nyc mocha --require @babel/register src/tests/index.js",
"test": "NODE_ENV=test npm run db:migrate && nyc mocha --require @babel/register src/tests/index.js",
"cover": "npm test && nyc report --reporter=text-lcov | coveralls",
"coveralls": "cat coverage/lcov.info | coveralls",
"start": "babel-node src/index.js",
Expand All @@ -20,6 +20,7 @@
"@babel/node": "^7.2.2",
"@babel/preset-env": "^7.4.3",
"@babel/register": "^7.4.0",
"@hapi/joi": "^15.0.0",
"body-parser": "^1.18.3",
"cheke": "^1.0.2",
"cors": "^2.8.4",
Expand Down
18 changes: 9 additions & 9 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ const app = express();

dotenv.config();

app.use(session({
secret: process.env.SECRET_KEY || 'authorshaven',
cookie: { maxAge: 60000 },
resave: true,
saveUninitialized: true,
}));
app.use(
session({
secret: process.env.SECRET_KEY || 'authorshaven',
cookie: { maxAge: 60000 },
resave: true,
saveUninitialized: true
})
);

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());

// api version 1

// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
Expand All @@ -38,7 +38,7 @@ app.use((err, req, res, next) => {
res.status(err.status || 500);
res.send({
message: err.message,
error: err.status,
error: err.status
});
next();
});
Expand Down
6 changes: 3 additions & 3 deletions src/config/db-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ const config = {
password: process.env.DB_PASSWORD_DEV,
database: process.env.DB_NAME_DEV,
host: process.env.DB_HOST_DEV,
dialect: 'postgres',
dialect: 'postgres'
},
test: {
use_env_variable: 'DATABASE_URL_TEST',
username: process.env.DB_USER_TEST,
password: process.env.DB_PASSWORD_TEST,
database: process.env.DB_NAME_TEST,
host: process.env.DB_HOST_TEST,
dialect: 'postgres',
dialect: 'postgres'
},
production: {
use_env_variable: 'DATABASE_URL',
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
dialect: 'postgres',
dialect: 'postgres'
}
};

Expand Down
File renamed without changes.
33 changes: 33 additions & 0 deletions src/helpers/validation/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param {string} input
* @param {string} required
* @returns {array} an array of errors or an empty array if no error
*/
export default (input, required = '') => {
const errors = [];
const re = /[A-Z]+@[A-Z-]+\.[A-Z]{2,4}/gim;
const reUsername = /[\\ ,;:"!#$%&'*+/=?^`{|}~]/g;
const reDomainSLD = /[\d+ \\,;:"!#$%&'*+/=?^`{|}~]/g;
const reDomainTLD = /[\d+ \\,;:"!#$%&'*+-/=?^_`{|}~]/g;

if (!input && !required) {
return [];
}

const emailUsername = input.substring(0, input.lastIndexOf('@'));
const emailDomainSLD = input.substring(input.lastIndexOf('@') + 1, input.lastIndexOf('.'));
const emailDomainTLD = input.substring(input.lastIndexOf('.') + 1);

if (
re.test(input)
&& !reUsername.test(emailUsername)
&& !reDomainSLD.test(emailDomainSLD)
&& !reDomainTLD.test(emailDomainTLD)
) {
return [];
}

errors.push('Please provide a valid email address');

return errors;
};
4 changes: 4 additions & 0 deletions src/helpers/validation/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import email from './email';
import password from './password';

export { email, password };
24 changes: 24 additions & 0 deletions src/helpers/validation/password.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @param {string} input
* @param {string} required
* @returns {array} an array of errors or an empty array if no error
*/
export default (input, required = '') => {
const errors = [];

if (!input && !required) {
return [];
}
if (
input.match(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,25}$/)
&& input.match(/[ \\,;:"!#$%&'+-/=?^_`{|}~]/)
) {
return [];
}

errors.push(
'Your password should have a minimum 8 and maximum of 25 characters, it must include at least one upper case letter, one lower case letter, one numeric digit and one special character (*&^!%$@#)'
);

return errors;
};
31 changes: 31 additions & 0 deletions src/middlewares/validate-new-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import db from '../models';

export default async (req, res, next) => {
const errors = {};
if (Object.keys(req.body).length <= 0) {
return res.status(400).json({
errors: {
body: ['body can not be empty']
}
});
}

const checkUser = await db.User.findAll({
where: {
email: req.body.email
},
logging: false
});

if (checkUser.length > 0) {
errors.email = ['sorry, this email is already used'];
}

if (Object.keys(errors).length > 0) {
return res.status(400).json({
errors
});
}

next();
};
14 changes: 5 additions & 9 deletions src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';

const db = {};
let sequelize;
const config = dbConfig[env];

if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
const sequelize = config.use_env_variable
? new Sequelize(process.env[config.use_env_variable], config)
: new Sequelize(config.database, config.username, config.password, config);

fs
.readdirSync(__dirname)
.filter(file => (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'))
fs.readdirSync(__dirname)
.filter(file => file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js')
.forEach((file) => {
const model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
Expand Down
Empty file removed src/routes/api/index.js
Empty file.
File renamed without changes.
19 changes: 19 additions & 0 deletions src/tests/404.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable import/no-extraneous-dependencies */
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../app';

const { expect } = chai;

chai.use(chaiHttp);

describe('POST /apiii', () => {
it('should return an error message', (done) => {
chai.request(app)
.get('/apiii')
.end((err, res) => {
expect(res.status).to.equal(404);
done();
});
});
});
4 changes: 4 additions & 0 deletions src/tests/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import './user-model';
import './article-model';
import './comment-model';
import './404';
import './validation/email';
import './validation/password';
import './validation/validate-new-user';
16 changes: 16 additions & 0 deletions src/tests/validation/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable import/no-extraneous-dependencies */
import chai from 'chai';
import validateEmail from '../../helpers/validation/email';

const { expect } = chai;

describe('Email validation', () => {
it('should return an array of error messages', () => {
expect(validateEmail('abc@gmail.co/m', 'required').length).to.be.above(0);
});

it('should return an empty array if there is no error', () => {
expect(validateEmail('abc@gmail.com', 'required').length).to.be.equals(0);
expect(validateEmail('', false).length).to.be.equals(0);
});
});
16 changes: 16 additions & 0 deletions src/tests/validation/password.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable import/no-extraneous-dependencies */
import chai from 'chai';
import validatePassword from '../../helpers/validation/password';

const { expect } = chai;

describe('Password validation', () => {
it('should return an array of error messages', () => {
expect(validatePassword('abcde12345', 'required').length).to.be.above(0);
});

it('should return an empty array if there is no error', () => {
expect(validatePassword('Abcde12345!', 'required').length).to.be.equals(0);
expect(validatePassword('', false).length).to.be.equals(0);
});
});
Loading

0 comments on commit 3c6d6fc

Please sign in to comment.