diff --git a/api/models/User.js b/api/models/User.js index 4ed5aba..dcdb024 100644 --- a/api/models/User.js +++ b/api/models/User.js @@ -5,6 +5,9 @@ * @docs :: http://sailsjs.org/#!documentation/models */ +var bcrypt = require('bcrypt'); +var assert = require('assert'); + module.exports = { schema: true, @@ -43,28 +46,52 @@ module.exports = { } }, - beforeValidation: function (values, next) { + beforeValidation: function (values, callback) { + if (typeof values.admin !== 'undefined') { if (values.admin === 'unchecked') { values.admin = false; - } else if (values.admin[1] === 'on') { + } else if (values.admin[1] === 'on') { values.admin = true; } } - next(); + + callback(); + }, - beforeCreate: function (values, next) { - if(!values.password || values.password != values.confirmation) { - return next({err: ["Password doesn't match password confirmation."]}); + beforeCreate: function (values, callback) { + + if (!values.password || !values.confirmation) { + return callback('Password required.', null); + } else if (values.password != values.confirmation) { + return callback('Passwords do not match.', null); // Return the error and a null user object } - require('bcrypt').hash(values.password, 10, function passwordEncrypted(err, encryptedPassword) { - if(err) - return next(err); - values.encryptedPassword = encryptedPassword; - next(); + bcrypt.hash(values.password, 10, function passwordEncrypted (error, encrypted) { + if (error) { + return callback(error, null); // Return the error and a null user object + } else { + try { + + // Assert that the password is encrypted + assert.notEqual(values.password, encrypted); + + // Update the value of the password to be encrypted + values.password = encrypted; + + // Return no error + return callback(null, values); + + } catch (error) { + + // If an error is caught, return it and a null user object. + return callback(error, null); + + } + } }); + } }; diff --git a/bin/tests/models/user.spec.js b/bin/tests/models/user.spec.js index 879e212..7ddbf3f 100644 --- a/bin/tests/models/user.spec.js +++ b/bin/tests/models/user.spec.js @@ -1,7 +1,98 @@ var User = require('../../../api/models/User'); -var sinon = require('sinon'); -var assert = require('assert'); -describe('/', function () { +var chai = require('chai'); +var assert = chai.assert; +var expect = chai.expect; +describe('User model —', function () { + describe('Before the user is created', function () { + + /** + * @description Test to make sure an error is thrown if there is + * no password. If an error is thrown, the test passes. + */ + it('should be a password', function (done) { + User.beforeCreate({ + password: null, + confirmation: null + }, function (error, user) { + try { + expect(user).to.be.null; + expect(error).to.be.a('string'); + expect(error).to.equal('Password required.'); + done(); + } catch (error) { + done(error); + } + }) + }); + + /** + * @description Test to make sure that the password will be + * encrypted. If the `encryptedPassword` and `password` are + * different, the test passes. + */ + it('should hash the password', function (done) { + User.beforeCreate({ + password: 'password', + confirmation: 'password' + }, function (error, user) { + + if (!error) { + try { + assert.notEqual(user.password , 'password'); + done(); + } catch (error) { + done(error); + } + } if (error) { + done(error); // test fails + } + + }); + }); + + /** + * @description Test to make sure that an error will be thrown + * if the password and the password confirmation are the not + * same. If an error is thrown, the test passes. + */ + it('should not allow different passwords', function (done) { + User.beforeCreate({ + password: 'password', + confirmation: 'is different' + }, function (error, user) { + + if (error) { + expect(user).to.be.null; + done(); + } else if (!error) { + done('different passwords are allowed'); // test fails + } + + }); + }); + + /** + * @description Test to make sure that no errors will be thrown + * when the password and the password confirmation are the + * same. If no error is thrown, the test passes. + */ + it('should allow identical passwords', function (done) { + User.beforeCreate({ + password: 'password', + confirmation: 'password' + }, function (error, user) { + + if (error) { + expect(user).to.be.null; + done(error); + } else if (!error) { + done(); + } + + }); + }); + + }); }); diff --git a/config/connections.js b/config/connections.js index 33e35ef..b0a6b15 100644 --- a/config/connections.js +++ b/config/connections.js @@ -13,41 +13,30 @@ * For more information on configuration, check out: * http://sailsjs.org/#/documentation/reference/sails.config/sails.config.connections.html */ - + module.exports.connections = { /** * Local disk storage for development. */ - localDiskDb: { + 'local': { adapter: 'sails-disk' }, /** * MongoDB database for production */ - productionMongo: { + 'production': { adapter: 'sails-mongo', url: process.env.MONGOLAB_URI }, /** - * Redis database for production + * MongoDB database for development. */ - productionRedis: { - adapter: 'sails-redis', - port: process.env.REDISTOGO_PORT, - host: process.env.REDISTOGO_HOST, - password: process.env.REDISTOGO_PASSWORD, - database: process.env.REDISTOGO_DATABASE, - options: { - parser: 'hiredis', - return_buffers: false, - detect_buffers: false, - socket_nodelay: true, - no_ready_check: false, - enable_offline_queue: true - } + 'development': { + adapter: 'sails-mongo', + url: process.env.MONGOLAB_URI } }; diff --git a/config/env/development.js b/config/env/development.js index e5fc7d4..14e471d 100644 --- a/config/env/development.js +++ b/config/env/development.js @@ -2,7 +2,9 @@ * Development environment settings */ module.exports = { + port: process.env.PORT || 80, + environment: process.env.NODE_ENV || 'development', models: { - connection: 'localDiskDb' + connection: 'development' } }; diff --git a/config/env/production.js b/config/env/production.js index 28dd210..52327c0 100644 --- a/config/env/production.js +++ b/config/env/production.js @@ -3,9 +3,9 @@ */ module.exports = { port: process.env.PORT || 80, - environment: 'production', + environment: process.env.NODE_ENV || 'production', models: { - connection: 'productionMongoDB' + connection: 'production' }, log: { level: 'silent' diff --git a/config/i18n.js b/config/i18n.js index db66bc4..d0db84f 100644 --- a/config/i18n.js +++ b/config/i18n.js @@ -34,7 +34,7 @@ module.exports.i18n = { * * ****************************************************************************/ - // defaultLocale: 'en', + defaultLocale: 'en', /**************************************************************************** * * diff --git a/config/log.js b/config/log.js index 1c53e60..7e27bf1 100644 --- a/config/log.js +++ b/config/log.js @@ -11,19 +11,5 @@ */ module.exports.log = { - - /*************************************************************************** - * * - * Valid `level` configs: i.e. the minimum log level to capture with * - * sails.log.*() * - * * - * The order of precedence for log levels from lowest to highest is: * - * silly, verbose, info, debug, warn, error * - * * - * You may also set the level to "silent" to suppress all logs. * - * * - ***************************************************************************/ - - // level: 'info' - + level: 'info' }; diff --git a/config/models.js b/config/models.js index e8b4411..c08d64e 100644 --- a/config/models.js +++ b/config/models.js @@ -10,5 +10,5 @@ module.exports.models = { migrate: 'safe', - connection: 'productionMongo' + connection: 'production' }; diff --git a/config/routes.js b/config/routes.js index 5a026b6..7d8c5bf 100644 --- a/config/routes.js +++ b/config/routes.js @@ -22,16 +22,6 @@ module.exports.routes = { - /*************************************************************************** - * * - * Make the view located at `views/homepage.ejs` (or `views/homepage.jade`, * - * etc. depending on your default view engine) your home page. * - * * - * (Alternatively, remove this and add an `index.html` file in your * - * `assets` directory) * - * * - ***************************************************************************/ - '/': { view: 'static/index' },