Skip to content

Commit

Permalink
Add tests for new registration code and remove commented out stuff / …
Browse files Browse the repository at this point in the history
…console.logs
  • Loading branch information
alarner committed May 5, 2016
1 parent 17bc6a1 commit ed5eef3
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 103 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
5 changes: 4 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
let config = require('./lib/config');

global.knex = require('knex')(config.database);
// Defer to unit test version of knex if it's already defined
if(!global.knex) {
global.knex = require('knex')(config.database);
}
global.bookshelf = require('bookshelf')(global.knex);
bookshelf.plugin('registry');

Expand Down
3 changes: 0 additions & 3 deletions lib/auth/create-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ module.exports = function(user, password, t) {
return new Promise(function(resolve, reject) {
bcrypt.genSalt(config.auth.local.saltRounds, function(err, salt) {
if(err) {
console.log('ERR 4');
return rejectUnknown(err, reject);
}

bcrypt.hash(password, salt, function(err, hash) {
if(err) {
console.log('ERR 5');
return rejectUnknown(err, reject);
}
AuthModel.forge({
Expand All @@ -33,7 +31,6 @@ module.exports = function(user, password, t) {
.save(null, {transacting: t})
.then(() => resolve(user))
.catch(err => {
console.log('ERR 6');
return rejectUnknown(err, reject);
});
});
Expand Down
1 change: 0 additions & 1 deletion lib/auth/create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module.exports = function(userData, t) {
let newUser = new UserModel(userData);
return newUser.save(null, {transacting: t})
.catch(function(err) {
console.log('ERR 3');
throw new Howhap(
config.errors.auth.UNKNOWN,
{message: err.toString()}
Expand Down
2 changes: 0 additions & 2 deletions lib/auth/no-dupe.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = function(email, t) {
.fetch()
.then(function(u) {
if(u) {
console.log('ERR 1');
reject(new Howhap(
config.errors.auth.EMAIL_EXISTS
));
Expand All @@ -18,7 +17,6 @@ module.exports = function(email, t) {
}
})
.catch(err => {
console.log('ERR 2');
reject(new Howhap(
config.errors.auth.UNKNOWN,
{message: err.toString()}
Expand Down
2 changes: 1 addition & 1 deletion models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module.exports = bookshelf.model('User', {
tableName: 'users',
hasTimestamps: ['createdAt', 'updatedAt', 'deletedAt'],
authentication: function() {
this.hasMany('Authentication', 'userId');
return this.hasMany('Authentication', 'userId');
}
});
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"start": "node --use_strict ./bin/www",
"test": "",
"test": "mocha --compilers js:babel-register --recursive --timeout 5000",
"dev": "./node_modules/.bin/gulp dev",
"config": "./node_modules/.bin/gulp config",
"build": "./node_modules/.bin/gulp build",
Expand All @@ -15,8 +15,10 @@
"babel-core": "^6.7.7",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-register": "^6.8.0",
"babelify": "^7.3.0",
"browserify": "^13.0.0",
"chai": "^3.5.0",
"config-loader": "github:alarner/config-loader",
"config-template": "^1.0.12",
"copy-paste": "^1.2.0",
Expand All @@ -30,10 +32,13 @@
"gulp-uglify": "^1.5.3",
"gulp-util": "^3.0.7",
"inquirer": "^1.0.2",
"mocha": "^2.4.5",
"request": "^2.72.0",
"sequest": "^0.9.0",
"sinon": "^1.17.4",
"ssh-fingerprint": "0.0.1",
"strictify": "^0.2.0",
"supertest": "^1.2.0",
"tosource": "^1.0.0",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
Expand All @@ -46,7 +51,6 @@
"bookshelf": "^0.9.4",
"bunyan": "^1.8.1",
"bunyan-prettystream": "^0.1.3",
"chai": "^3.5.0",
"config-loader": "github:alarner/config-loader",
"connect-redis": "^3.0.2",
"cookie-parser": "~1.4.1",
Expand All @@ -59,7 +63,6 @@
"include-all": "^0.1.6",
"knex": "^0.10.0",
"lodash": "^4.11.2",
"mocha": "^2.4.5",
"morgan": "~1.7.0",
"node-gcm": "^0.14.0",
"nodemailer": "^2.3.2",
Expand All @@ -76,7 +79,6 @@
"react-dom": "^15.0.2",
"request": "^2.72.0",
"serve-favicon": "~2.3.0",
"sinon": "^1.17.4",
"validator": "^5.2.0"
}
}
94 changes: 3 additions & 91 deletions routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let noDupe = require('../lib/auth/no-dupe');
let createUser = require('../lib/auth/create-user');
let createAuth = require('../lib/auth/create-auth');
let Howhap = require('howhap');
let _ = require('lodash');


router.use('/:type/login', validateAuthType, function(req, res, next) {
Expand Down Expand Up @@ -96,12 +97,8 @@ router.use('/logout', function(req, res, next) {
});

router.post('/register', validateLocalCredentials, function(req, res, next) {
// let userData = _.extend({}, req.body);
let userData = {
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email
};
let userData = _.extend({}, req.body);
delete userData.password;

bookshelf.transaction(function(t) {
return noDupe(req.body.email, t)
Expand All @@ -127,91 +124,6 @@ router.post('/register', validateLocalCredentials, function(req, res, next) {
}
res.error.send();
});


// let savedUser = null;
// checkDuplicate()
// .then()
// UserModel
// .forge({email: req.body.email})
// .fetch()
// .then(function(u) {
// // The account already exists, return an error.
// if(u) {
// res.error.add('auth.EMAIL_EXISTS', 'email');
// res.error.send('/auth/register');
// }
// // The account doesn't exists. Create it.
// else {
// return bookshelf.transaction(function(t) {
// let userData = _.extend({}, req.body);
// // Don't try to store the password in the users table
// // that's what authentication is for
// delete userData.password;
// // let newUser = new UserModel(userData);
// let newUser = new UserModel({
// firstName: req.body.firstName,
// lastName: req.body.lastName,
// email: req.body.email
// });
// newUser.save(null, {transacting: t})
// .then(function(user) {
// savedUser = user;
// return new Promise(function(resolve, reject) {
// bcrypt.genSalt(config.auth.local.saltRounds, function(err, salt) {
// bcrypt.hash(req.body.password, salt, function(err, hash) {
// if(err) {
// reject(err);
// }
// else {
// AuthenticationModel.forge({
// type: 'local',
// identifier: req.body.email,
// password: hash,
// userId: user.id
// })
// .save(null, {transacting: t})
// .then(resolve)
// .catch(reject);
// }
// });
// });
// });
// })
// .then(t.commit)
// .then(function() {
// req.logIn(savedUser, err => {
// if(err) {
// res.error.add('auth.UNKNOWN', {message: err.toString()});
// res.error.send('/auth/login');
// }
// else {
// if(req.accepts('html')) {
// res.redirect(config.auth.local.registerRedirect || '/auth/finish');
// }
// else {
// res.json(savedUser.toJSON());
// }
// }
// });
// return true;
// })
// .catch(function(err) {
// console.log(err.toString());
// t.rollback();
// console.log('AAAAAA');
// res.error.add('auth.UNKNOWN', {message: err.toString()});
// console.log('BBBBBB');
// res.error.send('/auth/register');
// console.log('CCCCCC');
// });
// });
// }
// })
// .catch(function(err) {
// console.log(err);
// console.log('catch all 2', err.toString());
// });
});

router.post('/login', validateLocalCredentials, function(req, res, next) {
Expand Down
28 changes: 28 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
let config = require('../lib/config');
before(done => {
global.knex = require('knex')(config.testDatabase);
global.app = require('../app');

knex.migrate.latest()
.then(() => {
return knex.seed.run();
})
.then(() => {
done();
})
.catch(err => {
console.log('MIGRATION / SEED ERROR:');
console.log(err);
});
});

beforeEach(done => {
knex.seed.run()
.then(() => {
done();
})
.catch(err => {
console.log('MIGRATION / SEED ERROR:');
console.log(err);
});
});
106 changes: 106 additions & 0 deletions test/routes/auth-register.post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
let request = require('supertest');
let expect = require('chai').expect;
let config = require('../../lib/config');
let _ = require('lodash');

describe('POST /auth/register -> JSON', function() {

it('should throw an error if the user is already registered', function(done) {
request(app)
.post('/auth/register')
.set('Accept', 'application/json')
.send('firstName=Aaron')
.send('lastName=Larner')
.send('email=test@test.com')
.send('password=password')
.expect('Content-Type', /json/)
.expect(function(res) {
expect(
res.body,
'EMAIL_EXISTS error should be returned'
).to.deep.equal({
default: _.assign({params:{}}, config.errors.auth.EMAIL_EXISTS)
});
})
.expect(409, done);
});

it('should throw an error if no email is supplied', function(done) {
request(app)
.post('/auth/register')
.set('Accept', 'application/json')
.send('password=password')
.expect('Content-Type', /json/)
.expect(function(res) {
expect(
res.body,
'MISSING_EMAIL error should be returned'
).to.deep.equal({
email: _.assign({params:{}}, config.errors.auth.MISSING_EMAIL)
});
})
.expect(400, done);
});

it('should throw an error if no password is supplied', function(done) {
request(app)
.post('/auth/register')
.set('Accept', 'application/json')
.send('email=foo@test.com')
.expect('Content-Type', /json/)
.expect(function(res) {
expect(
res.body,
'MISSING_PASSWORD error should be returned'
).to.deep.equal({
password: _.assign({params:{}}, config.errors.auth.MISSING_PASSWORD)
});
})
.expect(400, done);
});

it('should throw an error if no email and no password are supplied', function(done) {
request(app)
.post('/auth/register')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(function(res) {
expect(
res.body,
'MISSING_EMAIL and MISSING_PASSWORD error should be returned'
).to.deep.equal({
email: _.assign({params:{}}, config.errors.auth.MISSING_EMAIL),
password: _.assign({params:{}}, config.errors.auth.MISSING_PASSWORD)
});
})
.expect(400, done);
});

it('should throw an error if a bad field is supplied', function(done) {
request(app)
.post('/auth/register')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.send('email=foo@test.com')
.send('password=password')
.send('username=bananas')
.expect(function(res) {
expect(res.body.default).not.to.be.undefined;
expect(res.body.default.message).to.equal('An unknown error occurred: {{message}}');
})
.expect(500, done);
});

it('should create a user if all necessary information is provided', function(done) {
request(app)
.post('/auth/register')
.set('Accept', 'application/json')
.send('email=foo@test.com')
.send('password=password')
.expect('Content-Type', /json/)
.expect(function(res) {
expect(res.body.email).to.equal('foo@test.com');
})
.expect(200, done);
});
});
Loading

0 comments on commit ed5eef3

Please sign in to comment.