Skip to content
This repository has been archived by the owner on Apr 24, 2018. It is now read-only.

Commit

Permalink
Merge branch user.spec.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Burtchaell committed Jan 21, 2015
2 parents d3fa5eb + 1073f1f commit f1c3568
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 62 deletions.
49 changes: 38 additions & 11 deletions api/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* @docs :: http://sailsjs.org/#!documentation/models
*/

var bcrypt = require('bcrypt');
var assert = require('assert');

module.exports = {

schema: true,
Expand Down Expand Up @@ -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);

}
}
});

}

};
97 changes: 94 additions & 3 deletions bin/tests/models/user.spec.js
Original file line number Diff line number Diff line change
@@ -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();
}

});
});

});
});
25 changes: 7 additions & 18 deletions config/connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

};
4 changes: 3 additions & 1 deletion config/env/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
};
4 changes: 2 additions & 2 deletions config/env/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion config/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports.i18n = {
* *
****************************************************************************/

// defaultLocale: 'en',
defaultLocale: 'en',

/****************************************************************************
* *
Expand Down
16 changes: 1 addition & 15 deletions config/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};
2 changes: 1 addition & 1 deletion config/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@

module.exports.models = {
migrate: 'safe',
connection: 'productionMongo'
connection: 'production'
};
10 changes: 0 additions & 10 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
},
Expand Down

0 comments on commit f1c3568

Please sign in to comment.