Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding unit test integration for the boiler plate.
- Loading branch information
1 parent
50c1d53
commit 1eac7f5
Showing
8 changed files
with
234 additions
and
8 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
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,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; |
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,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)); | ||
}); | ||
}); |