This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
382 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'] | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'] | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'] | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
|
@@ -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); | ||
} | ||
|
@@ -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)); | ||
} | ||
}); | ||
}); | ||
|
@@ -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(); | ||
}); | ||
}; | ||
|
@@ -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') { | ||
|
@@ -130,7 +139,7 @@ module.exports.start = function start() { | |
.then(function () { | ||
resolve(); | ||
}) | ||
.catch(reportError); | ||
.catch(reportError(reject)); | ||
} else { | ||
// Add both Admin and User account | ||
|
||
|
@@ -141,7 +150,7 @@ module.exports.start = function start() { | |
.then(function () { | ||
resolve(); | ||
}) | ||
.catch(reportError); | ||
.catch(reportError(reject)); | ||
} | ||
}); | ||
}; |
Oops, something went wrong.