Skip to content

Commit

Permalink
fix(initDB): provide more accurate error messages
Browse files Browse the repository at this point in the history
Fix error message in initDB if database doesn't exist on mongolab
Rewrite initDB.js so everything is wrapped in an initDB namespace (not a module because only one instance is needed at a time)
Add unit tests for initDB
  • Loading branch information
Ken Yee authored and petebacondarwin committed Aug 29, 2013
1 parent 1f1950d commit 4b1c6b8
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 40 deletions.
112 changes: 72 additions & 40 deletions server/initDB.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,79 @@
// code for initializing the DB w/ an admin user

var config = require('./config.js');
var rest = require('request');
var apiKey = config.mongo.apiKey;
var baseUrl = config.mongo.dbUrl + '/databases/' + config.security.dbName + '/collections/';

console.log('Configuration: \n', config);

var checkDocument = function(collection, query, done) {
var url = baseUrl + collection + '/';
console.log(url);
var params = { apiKey:apiKey, q: JSON.stringify(query) };
var request = rest.get(url, { qs: params, json: {} }, function(err, response, data) {
if ( err ) {
console.log('There was an error checking the documents', err);
}
done(err, data);
});
};

var createDocument = function(collection, doc, done) {
var url = baseUrl + collection + '/';
console.log(url);
var request = rest.post(url, { qs: { apiKey:apiKey }, json: doc }, function(err, response, data) {
if ( !err ) {
console.log('Document created', data);
}
done(err, data);
});
};

var adminUser = { email: '[email protected]', password: 'changeme', admin: true, firstName: 'Admin', lastName: 'User' };
console.log('Generating admin user...', adminUser);
var initDB = {
adminUser: { email: '[email protected]', password: 'changeme', admin: true, firstName: 'Admin', lastName: 'User' },

console.log('Checking it is not already created...');
checkDocument(config.security.usersCollection, adminUser, function(err, data) {
if ( !err && data.length === 0 ) {
console.log('Creating new admin user...', err, data);
createDocument(config.security.usersCollection, adminUser, function(err, data) {
console.log('Created new admin user...');
console.log(err);
console.log(data);
initialize: function(cfg) {
initDB.apiKey = cfg.mongo.apiKey
initDB.baseUrl = cfg.mongo.dbUrl + '/databases/' + cfg.security.dbName + '/collections/';
initDB.usersCollection = cfg.security.usersCollection;
},

checkDocument: function(collection, query, done) {
var url = initDB.baseUrl + collection + '/';
console.log("rest.get - " + url);
var params = { apiKey:initDB.apiKey, q: JSON.stringify(query) };
var request = rest.get(url, { qs: params, json: {} }, function(err, response, data) {
if ( err ) {
console.log('There was an error checking the documents', err);
}
done(err, data);
});
},

createDocument: function(collection, doc, done) {
var url = initDB.baseUrl + collection + '/';
console.log("rest.post - " + url);
var request = rest.post(url, { qs: { apiKey:initDB.apiKey }, json: doc }, function(err, response, data) {
if ( !err ) {
console.log('Document created', data);
}
done(err, data);
});
},

deleteDocument: function(collection, docId, done) {
var url = initDB.baseUrl + collection + '/' + docId;
console.log("rest.del - " + url);
var request = rest.del(url, { qs: { apiKey:initDB.apiKey }, json: {} }, function(err, response, data) {
if ( !err ) {
console.log('Document deleted', data);
}
done(err, data);
});
},

addAdminUser: function(done) {
console.log('*** Admin user properties:', initDB.adminUser);
console.log('Checking that admin user does not exist...');
initDB.checkDocument(initDB.usersCollection, initDB.adminUser, function(err, data) {
if ( !err && data.length === 0 ) {
console.log('Creating new admin user...', err, data);
initDB.createDocument(initDB.usersCollection, initDB.adminUser, function(err, data) {
console.log('Created new admin user...');
console.log(err);
console.log(data);
done(err, data);
});
} else {
if (data.message) {
console.log('Error: ' + data.message);
} else {
console.log('User already created.');
}
done(err, data);
}
});
} else {
console.log('User already created.');
}
});
};

module.exports = initDB;

/*
console.log('*** Configuration: \n', config);
initDB.initialize(config);
initDB.addAdminUser();
*/
95 changes: 95 additions & 0 deletions server/test/mongo-initdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// NodeUnit tests for the initDB.js module that initializes the DB

var rewire = require('rewire');
var initDB = require('../initDB.js');
var config = require('../config.js');

module.exports = {
setUp: function (callback) {
callback();
},
tearDown: function (callback) {
callback();
},

testInvalidDB: function(test) {
// override the normal config and change the DB name so it's invalid
var cfgbad = JSON.parse(JSON.stringify(config));
cfgbad.security.dbName = "invalidDB";
initDB.initialize(cfgbad);
initDB.checkDocument(initDB.usersCollection, initDB.adminUser, function(err, data) {
test.equals(err, null);
test.equals(data.message, 'Database not found.');
test.done();
});
},

testCreateAdminUser: function(test) {
// checks for and creates admin user if user doesn't exist
initDB.initialize(config);
initDB.checkDocument(initDB.usersCollection, initDB.adminUser, function(err, data) {
test.equals(err, null);
// validate fields, but ignore password in case users changed it
if (data.length > 0) {
test.equals(data[0].email, initDB.adminUser['email']);
test.equals(data[0].admin, initDB.adminUser['admin']);
test.equals(data[0].firstName, initDB.adminUser['firstName']);
test.equals(data[0].lastName, initDB.adminUser['lastName']);
test.done();
} else {
// create user
initDB.addAdminUser(function(err, data) {
if (data.message) {
test.ok(false, "Error " + data.message);
}
test.done();
});
}
});
},

testCreateDeleteUser: function(test) {
// deletes users, creates user, and then deletes it again
var someuser = JSON.parse(JSON.stringify(initDB.adminUser));
someuser['email'] = "[email protected]";
someuser['firstName'] = "unit";
someuser['lastName'] = "test";
initDB.initialize(config);
initDB.checkDocument(initDB.usersCollection, someuser, function(err, data) {
// first, delete any existing test user docs
var handleDeleteResult = function(err, data) {
//console.log('err is ' + JSON.stringify(err));
//console.log('data is ' + JSON.stringify(data));
console.log('Deleted doc ID ' + data._id.$oid);
};
for (var i = 0; i < data.length; i++) {
var doc = data[i];
console.log('doc is ' + JSON.stringify(doc));
initDB.deleteDocument(initDB.usersCollection, doc._id.$oid, handleDeleteResult);
}
initDB.checkDocument(initDB.usersCollection, someuser, function(err, data) {
// verify that there are no test user docs
test.equals(err, null);
test.equals(data.length, 0);
initDB.createDocument(initDB.usersCollection, someuser, function(err, data) {
// add a new test user doc
test.equals(err, null);
// validate fields of newly created user
test.equals(data.email, someuser['email']);
test.equals(data.password, someuser['password']);
test.equals(data.admin, someuser['admin']);
test.equals(data.firstName, someuser['firstName']);
test.equals(data.lastName, someuser['lastName']);
var docId = data._id.$oid;
initDB.deleteDocument(initDB.usersCollection, docId, function(err, data) {
// and delete the document we created
test.equals(err, null);
test.equals(data._id.$oid, docId);
test.done();
});
});
});
});
}

};

0 comments on commit 4b1c6b8

Please sign in to comment.