Skip to content

Commit

Permalink
Adding unit test integration for the boiler plate.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramachandrangunasekaran committed Jun 17, 2019
1 parent 50c1d53 commit 1eac7f5
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -4,6 +4,7 @@ package-lock.json

# Credentials
.env
.env.test

# Webstorm/text editor meta
.idea/
Expand All @@ -22,3 +23,8 @@ dist/
.vscode


#coverage
.nyc_output
coverage


17 changes: 14 additions & 3 deletions app/boot/connection-factory.js
Expand Up @@ -12,13 +12,24 @@ db.Promise = global.Promise;

class ConnectionFactory {

static connect(config) {
db.connect(config.mongodbUrl, {
static connect(mongodbUrl) {
db.connect(mongodbUrl, {
useMongoClient: true
});
return new Promise((resolve, reject) => {
db.connection.on('open', () => {
resolve('SUCCESS');
resolve('CONNECTION SUCCESS');
}).on('error', err => {
reject(err);
});
});
}

static disconnect(){
db.connection.close();
return new Promise((resolve, reject) => {
db.connection.on('disconnected', () => {
resolve('DISCONNECT SUCCESS');
}).on('error', err => {
reject(err);
});
Expand Down
2 changes: 1 addition & 1 deletion app/routes/user-routes.js
Expand Up @@ -26,7 +26,7 @@ function signUp(req, res) {

emailController.sendWelcomeEmail(user.email, verificationHash);

return res.status(200).send({
return res.status(201).send({
msg: 'User created successfully!',
token: jwtController.generateToken({
userId: user._id
Expand Down
3 changes: 2 additions & 1 deletion config.js
Expand Up @@ -9,7 +9,8 @@ const _ = require('lodash');
const ENVS = [
'production',
'development',
'local'
'local',
'test'
];

class Config {
Expand Down
11 changes: 9 additions & 2 deletions package.json
Expand Up @@ -4,7 +4,8 @@
"description": "A custom MEAN stack boilerplate for web application ",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"clean:coverage": "rimraf .nyc_output coverage",
"test": "npm run clean:coverage && nyc --reporter=html mocha --recursive --exit --timeout 10000",
"start": "node-inspector & nodemon --inspect server.js"
},
"repository": {
Expand All @@ -22,11 +23,17 @@
},
"homepage": "https://github.com/sridharrajs/nodejs-api-boilerplate#readme",
"devDependencies": {
"chai": "^4.2.0",
"gulp": "^3.9.1",
"gulp-eslint": "^4.0.2",
"gulp-jshint": "^2.1.0",
"mocha": "^6.1.4",
"mongodb-memory-server": "^5.1.4",
"nodemon": "^1.18.3",
"run-sequence": "^1.1.5"
"nyc": "^14.1.1",
"rimraf": "^2.6.3",
"run-sequence": "^1.1.5",
"supertest": "^4.0.2"
},
"dependencies": {
"async": "^1.5.1",
Expand Down
2 changes: 1 addition & 1 deletion server.js
Expand Up @@ -25,7 +25,7 @@ config.isValidEnv(HOST_ENVIRONMENT).then(info => {
}).then(info => {
console.log('Initializing settings ', chalk.blue(info));
let connectionFactory = require('./app/boot/connection-factory');
return connectionFactory.connect(config);
return connectionFactory.connect(config.mongodbUrl);
}).then(info => {
console.log('Connecting DB ', chalk.blue(info));
let models = require('./app/models');
Expand Down
54 changes: 54 additions & 0 deletions test.js
@@ -0,0 +1,54 @@
/**
* Created by sridharrajs.
*/

'use strict';

const chalk = require('chalk');
const config = require('./config');
const CF = require('./app/boot/connection-factory');
const MongoMockServer = require('mongodb-memory-server').MongoMemoryServer;
const dotenv = require('dotenv').config({ path: `${__dirname}/.env.test` });
const mInstance = new MongoMockServer();

class TestFactory extends CF {

static init() {
if (dotenv.error) {
console.trace(chalk.red('.env.test file is missing'));
return Promise.reject('.env.test file is missing')
}

return config.isValidEnv(process.env.NODE_ENV).then(info => {
console.log('Checking Environment ', chalk.blue(info));
return config.isSecretSet(process.env.MY_SECRET);
}).then(info => {
console.log('Checking tokens ', chalk.blue(info));
return config.init(process.env.HOST_ENVIRONMENT);
}).then(info => {
console.log('Initializing settings ', chalk.blue(info));
return mInstance.getConnectionString().then((mongoUri) => {
return super.connect(mongoUri);
});
}).then(info => {
console.log('Connecting DB ', chalk.blue(info));
let models = require('./app/models');
return models.init();
}).then(info => {
console.log('Starting Test Server ', chalk.blue(info));
return require('./app/app');
}).catch(error => {
console.trace(chalk.red(error));
console.trace(chalk.red(error.message));
return Promise.reject(error);
});

}
}

process.on('uncaughtException', error => {
console.trace(error.stack);
});


module.exports = TestFactory;
147 changes: 147 additions & 0 deletions test/api/auth/user.js
@@ -0,0 +1,147 @@
const expect = require('chai').expect;
const request = require('supertest');
const testFactory = require('../../../test');
let app;

describe('*****USER AUTHENTICATION AND AUTHORIZATION*****', () => {
before((done) => {
testFactory.init().then((appObj) => { app = appObj; done() }).catch((err) => { done(err); process.exit(0); });
})

after((done) => {
testFactory.disconnect().then(() => done()).catch((err) => done(err));
})

//Success Case.

it('Happy Case(201), Create a User.', (done) => {
request(app).post('/users/signup')
.send({ email: 'foo@bar.com', password: "foobar123" })
.then((res) => {
const { body, status } = res;
expect(status).to.equal(201);
expect(body).to.contain.property('token');
done();
})
.catch((err) => done(err));
});

it('Happy Case(200), Login to the user.', (done) => {
request(app).post('/users/login')
.send({ email: 'foo@bar.com', password: "foobar123" })
.then((res) => {
const { body, status } = res;
expect(status).to.equal(200);
expect(body).to.contain.property('token');
done();
})
.catch((err) => done(err));
});

//Failure Case.

it('Failure Case(422), Create user with out email.', (done) => {
request(app).post('/users/signup')
.send({})
.then((res) => {
const { body, status } = res;
expect(status).to.equal(422);
expect(body.errors.param).to.equal('email');
done();
})
.catch((err) => done(err));
});

it('Failure Case(422), Create user with out password.', (done) => {
request(app).post('/users/signup')
.send({ email: 'foo@bar.com' })
.then((res) => {
const { body, status } = res;
expect(status).to.equal(422);
expect(body.errors.param).to.equal('password');
done();
})
.catch((err) => done(err));
});

it('Failure Case(422), Create user with invalid email.', (done) => {
request(app).post('/users/signup')
.send({ email: 'foo', password: 'some password' })
.then((res) => {
const { body, status } = res;
expect(status).to.equal(422);
expect(body.errors.param).to.equal('email');
done();
})
.catch((err) => done(err));
});

it('Failure Case(422), Create user with invalid password.', (done) => {
request(app).post('/users/signup')
.send({ email: 'foo@bar.com', password: '121' })
.then((res) => {
const { body, status } = res;
expect(status).to.equal(422);
expect(body.errors.param).to.equal('password');
done();
})
.catch((err) => done(err));
});

it('Failure Case(422), Login with Empty username.', (done) => {
request(app).post('/users/login')
.send({ email: '', password: 'some password' })
.then((res) => {
const { body, status } = res;
expect(status).to.equal(422);
expect(body.errors.param).to.equal('email');
done();
})
.catch((err) => done(err));
});

it('Failure Case(422), Login with Empty password.', (done) => {
request(app).post('/users/login')
.send({ email: 'foo@bar.com', password: '' })
.then((res) => {
const { body, status } = res;
expect(status).to.equal(422);
expect(body.errors.param).to.equal('password');
done();
})
.catch((err) => done(err));
});

it('Failure Case(403), Invalid username and password.', (done) => {
request(app).post('/users/login')
.send({ email: 'foo@bar.com', password: 'some password' })
.then((res) => {
const { body, status } = res;
expect(status).to.equal(403);
done();
})
.catch((err) => done(err));
});

it('Failure Case(200), Unavailable email reset-password.', (done) => {
request(app).post('/users/reset-password')
.send({ email: 'foo@bar.com1' })
.then((res) => {
const { body, status } = res;
expect(status).to.equal(200);
done();
})
.catch((err) => done(err));
});

it('Failure Case(200), Reset password with email.', (done) => {
request(app).post('/users/reset-password')
.send({ email: 'foo@bar.com' })
.then((res) => {
const { body, status } = res;
expect(status).to.equal(200);
done();
})
.catch((err) => done(err));
});
});

0 comments on commit 1eac7f5

Please sign in to comment.