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

Commit

Permalink
Merge pull request #957 from mleanos/seeddb-enhance-testability
Browse files Browse the repository at this point in the history
Seed options - logResults
  • Loading branch information
lirantal committed Oct 15, 2015
2 parents 8a12f76 + 32e0d12 commit e30c3d1
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 92 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
125 changes: 68 additions & 57 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,9 @@ 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,82 +73,86 @@ 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);
.then(saveUser(user))
.then(reportSuccess(password))
.then(function () {
resolve();
})
.catch(function (err) {
reject(err);
});
} else {
removeUser(user)
.then(saveUser(user))
.then(reportSuccess(password))
.then(function () {
resolve();
})
.catch(reportError);
.then(saveUser(user))
.then(reportSuccess(password))
.then(function () {
resolve();
})
.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') {
User.generateRandomPassphrase()
.then(seedTheUser(adminAccount))
.then(function () {
.then(seedTheUser(adminAccount))
.then(function () {
resolve();
})
.catch(reportError);
.catch(reportError(reject));
} else {
// Add both Admin and User account

User.generateRandomPassphrase()
.then(seedTheUser(userAccount))
.then(User.generateRandomPassphrase)
.then(seedTheUser(adminAccount))
.then(function () {
.then(seedTheUser(userAccount))
.then(User.generateRandomPassphrase)
.then(seedTheUser(adminAccount))
.then(function () {
resolve();
})
.catch(reportError);
.catch(reportError(reject));
}
});
};
Loading

0 comments on commit e30c3d1

Please sign in to comment.