Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Seed options - logResults
Browse files Browse the repository at this point in the history
Added an options object to the database seed configuration. Currently,
the only option implemented is `logResults`; set using the seedDB env
config
options (default to "true").

Modified the definition of the env config for seedDB. It's now an
object, with
options.

Setting the logResults option is set to `false` in the core
configuration server test suite.

Also, fixed an issue with how env configs were reading the seedDB
setting from the env variables. Previously, the config was getting set
by
looking for merely the existence of the env variable (MONGO_SEED).
However,
if this setting existed but was set to "false", the seedDB would be
turned on.

Added the SeedDB user details to the env config, and seedDB options.

Added tests to the core server config test suite

should have seedDB configuration set for "regular" user
should have seedDB configuration set for admin user
should seed admin, and "regular" user accounts when NODE_ENV is set to
"test" when they already exist
should ONLY seed admin user account when NODE_ENV is set to "production"
with custom admin
should seed admin, and "regular" user accounts when NODE_ENV is set to
"test" with custom options
should NOT seed admin user account if it already exists when NODE_ENV is
set to "production"
should NOT seed "regular" user account if missing email when NODE_ENV
set to "test"

Added support for environment variables to seedDB env configs; currently
only supporting username & email.

Refactored how the SeedDB rejects were being handled
  • Loading branch information
mleanos committed Oct 9, 2015
1 parent 9c78c63 commit 0560062
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 60 deletions.
25 changes: 24 additions & 1 deletion config/env/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,28 @@ module.exports = {
}
},
livereload: true,
seedDB: process.env.MONGO_SEED || false
seedDB: {
seed: process.env.MONGO_SEED === 'true' ? true : false,
options: {
logResults: process.env.MONGO_SEED_LOG_RESULTS === 'false' ? false : true,
seedUser: {
username: process.env.MONGO_SEED_USER_USERNAME || 'user',
provider: 'local',
email: process.env.MONGO_SEED_USER_EMAIL || '[email protected]',
firstName: 'User',
lastName: 'Local',
displayName: 'User Local',
roles: ['user']
},
seedAdmin: {
username: process.env.MONGO_SEED_ADMIN_USERNAME || 'admin',
provider: 'local',
email: process.env.MONGO_SEED_ADMIN_EMAIL || '[email protected]',
firstName: 'Admin',
lastName: 'Local',
displayName: 'Admin Local',
roles: ['user', 'admin']
}
}
}
};
25 changes: 24 additions & 1 deletion config/env/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,28 @@ module.exports = {
}
}
},
seedDB: process.env.MONGO_SEED || false
seedDB: {
seed: process.env.MONGO_SEED === 'true' ? true : false,
options: {
logResults: process.env.MONGO_SEED_LOG_RESULTS === 'false' ? false : true,
seedUser: {
username: process.env.MONGO_SEED_USER_USERNAME || 'user',
provider: 'local',
email: process.env.MONGO_SEED_USER_EMAIL || '[email protected]',
firstName: 'User',
lastName: 'Local',
displayName: 'User Local',
roles: ['user']
},
seedAdmin: {
username: process.env.MONGO_SEED_ADMIN_USERNAME || 'admin',
provider: 'local',
email: process.env.MONGO_SEED_ADMIN_EMAIL || '[email protected]',
firstName: 'Admin',
lastName: 'Local',
displayName: 'Admin Local',
roles: ['user', 'admin']
}
}
}
};
25 changes: 24 additions & 1 deletion config/env/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,28 @@ module.exports = {
}
}
},
seedDB: process.env.MONGO_SEED || false
seedDB: {
seed: process.env.MONGO_SEED === 'true' ? true : false,
options: {
logResults: process.env.MONGO_SEED_LOG_RESULTS === 'false' ? false : true,
seedUser: {
username: process.env.MONGO_SEED_USER_USERNAME || 'user',
provider: 'local',
email: process.env.MONGO_SEED_USER_EMAIL || '[email protected]',
firstName: 'User',
lastName: 'Local',
displayName: 'User Local',
roles: ['user']
},
seedAdmin: {
username: process.env.MONGO_SEED_ADMIN_USERNAME || 'admin',
provider: 'local',
email: process.env.MONGO_SEED_ADMIN_EMAIL || '[email protected]',
firstName: 'Admin',
lastName: 'Local',
displayName: 'Admin Local',
roles: ['user', 'admin']
}
}
}
};
2 changes: 1 addition & 1 deletion config/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var config = require('../config'),
seed = require('./seed');

function seedDB() {
if (config.seedDB) {
if (config.seedDB.seed) {
console.log(chalk.bold.red('Warning: Database seeding is turned on'));
seed.start();
}
Expand Down
91 changes: 50 additions & 41 deletions config/lib/seed.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
'use strict';

var mongoose = require('mongoose'),
var _ = require('lodash'),
config = require('../config'),
mongoose = require('mongoose'),
chalk = require('chalk'),
crypto = require('crypto');

// global seed options object
var seedOptions = {};

function removeUser (user) {
return new Promise(function (resolve, reject) {
var User = mongoose.model('User');
User.find({username: user.username}).remove(function (err) {
if (err) {
reject(new Error('Database Seeding:\t\t\tFailed to remove local ' + user.username));
reject(new Error('Failed to remove local ' + user.username));
}
resolve();
});
Expand All @@ -22,7 +27,7 @@ function saveUser (user) {
// Then save the user
user.save(function (err, theuser) {
if (err) {
reject(new Error('Database Seeding:\t\t\tFailed to add local ' + user.username));
reject(new Error('Failed to add local ' + user.username));
} else {
resolve(theuser);
}
Expand All @@ -36,13 +41,13 @@ function checkUserNotExists (user) {
var User = mongoose.model('User');
User.find({username: user.username}, function (err, users) {
if (err) {
reject(new Error('Database Seeding:\t\t\tFailed to find local account ' + user.username));
reject(new Error('Failed to find local account ' + user.username));
}

if (users.length === 0) {
resolve();
} else {
reject(new Error('Database Seeding:\t\t\tFailed due to local account already exists: ' + user.username));
reject(new Error('Failed due to local account already exists: ' + user.username));
}
});
});
Expand All @@ -51,7 +56,7 @@ function checkUserNotExists (user) {
function reportSuccess (password) {
return function (user) {
return new Promise(function (resolve, reject) {
console.log(chalk.bold.red('Database Seeding:\t\t\tLocal ' + user.username + ' added with password set to ' + password));
if (seedOptions.logResults) console.log(chalk.bold.red('Database Seeding:\t\t\tLocal ' + user.username + ' added with password set to ' + password));
resolve();
});
};
Expand All @@ -66,62 +71,66 @@ function seedTheUser (user) {
// set the new password
user.password = password;

if (user.username === 'admin' && process.env.NODE_ENV === 'production') {
if (user.username === seedOptions.seedAdmin.username && process.env.NODE_ENV === 'production') {
checkUserNotExists(user)
.then(saveUser(user))
.then(reportSuccess(password))
.then(function () {
resolve();
})
.catch(reportError);
.catch(function (err) {
reject(err);
});
} else {
removeUser(user)
.then(saveUser(user))
.then(reportSuccess(password))
.then(function () {
resolve();
})
.catch(reportError);
.catch(function (err) {
reject(err);
});
}
});
};
}

// report the error
function reportError (err) {
console.log();
console.log(err);
console.log();
function reportError (reject) {
return function (err) {
if (seedOptions.logResults) {
console.log();
console.log('Database Seeding:\t\t\t' + err);
console.log();
}
reject(err);
};
}

module.exports.start = function start() {
module.exports.start = function start(options) {
// Initialize the default seed options
seedOptions = _.clone(config.seedDB.options, true);

// Check for provided options

if (_.has(options, 'logResults')) {
seedOptions.logResults = options.logResults;
}

if (_.has(options, 'seedUser')) {
seedOptions.seedUser = options.seedUser;
}

if (_.has(options, 'seedAdmin')) {
seedOptions.seedAdmin = options.seedAdmin;
}

var User = mongoose.model('User');
return new Promise(function (resolve, reject) {
var seedUser = {
username: 'user',
password: 'User_Password1!',
provider: 'local',
email: '[email protected]',
firstName: 'User',
lastName: 'Local',
displayName: 'User Local',
roles: ['user']
};

var seedAdmin = {
username: 'admin',
password: 'Admin_Password1!',
provider: 'local',
email: '[email protected]',
firstName: 'Admin',
lastName: 'Local',
displayName: 'Admin Local',
roles: ['user', 'admin']
};

var user = null;
var adminAccount = new User(seedAdmin);
var userAccount = new User(seedUser);

var adminAccount = new User(seedOptions.seedAdmin);
var userAccount = new User(seedOptions.seedUser);

//If production only seed admin if it does not exist
if (process.env.NODE_ENV === 'production') {
Expand All @@ -130,7 +139,7 @@ module.exports.start = function start() {
.then(function () {
resolve();
})
.catch(reportError);
.catch(reportError(reject));
} else {
// Add both Admin and User account

Expand All @@ -141,7 +150,7 @@ module.exports.start = function start() {
.then(function () {
resolve();
})
.catch(reportError);
.catch(reportError(reject));
}
});
};
Loading

0 comments on commit 0560062

Please sign in to comment.