diff --git a/bin/browserid b/bin/browserid index 0e6150977398b882f235f5b38007078ea3d6e658..1ac097f9bfa5842e209025685760ed123251db71 100755 --- a/bin/browserid +++ b/bin/browserid @@ -191,5 +191,32 @@ db.open(config.get('database'), function (error) { var bindTo = config.get('bind_to'); app.listen(bindTo.port, bindTo.host, function() { logger.info("running on http://" + app.address().address + ":" + app.address().port); + + // #13 if the CREATE_TEST_USERS env var is defined, we'll try to create + // some test users + if (process.env['CREATE_TEST_USERS']) { + logger.warn("creating test users... this can take a while..."); + bcrypt.gen_salt(config.get('bcrypt_work_factor'), function (err, salt) { + if (err) { + logger.error("error creating test users - bcrypt salt gen: " + err); + process.exit(1); + } + bcrypt.encrypt("THE PASSWORD", salt, function(err, hash) { + if (err) { + logger.error("error creating test users - bcrypt encrypt pass: " + err); + process.exit(1); + } + var want = parseInt(process.env['CREATE_TEST_USERS']); + var have = 0; + for (i = 1; i <= want; i++) { + db.addTestUser(i + "@loadtest.domain", hash, function(err, email) { + if (++have == want) { + logger.warn("created " + want + " test users"); + } + }); + } + }); + }); + } }); }); diff --git a/lib/db.js b/lib/db.js index b71dcf8894660df2447c43bb07ac5e48d40ec21b..2b5cbbadcdd263929542333238c5b9c681800966 100644 --- a/lib/db.js +++ b/lib/db.js @@ -138,3 +138,9 @@ exports.onReady = function(f) { driver[fn].apply(undefined, arguments); }; }); + +exports.addTestUser = function() { + // would we like to check the environment here? + checkReady(); + driver['addTestUser'].apply(undefined, arguments); +}; \ No newline at end of file diff --git a/lib/db/json.js b/lib/db/json.js index 79e4bf3e97ad2e6b87582915add5af27e7e53977..f78045b009d3c14d41447d03be4fc152b2bffd69 100644 --- a/lib/db/json.js +++ b/lib/db/json.js @@ -346,3 +346,15 @@ exports.cancelAccount = function(authenticated_email, cb) { cb(); }); }; + +exports.addTestUser = function(email, hash, cb) { + sync(); + exports.removeEmail(email, email, function() { + db.users.push({ + password: hash, + emails: [ email ] + }); + flush(); + cb(); + }); +}; diff --git a/lib/db/mysql.js b/lib/db/mysql.js index 97070a51cdd84d5bf83aa23a21616761c57ed6a0..574c5a72e13206bb34c2ce4741e1747e9c911623 100644 --- a/lib/db/mysql.js +++ b/lib/db/mysql.js @@ -412,7 +412,7 @@ exports.removeEmail = function(authenticated_email, email, cb) { cb(err ? err : undefined); }); }); -} +}; exports.cancelAccount = function(email, cb) { function reportErr(err) { if (err) logUnexpectedError(err); } @@ -432,4 +432,24 @@ exports.cancelAccount = function(email, cb) { client.query("DELETE LOW_PRIORITY FROM user WHERE id = ?", [ uid ], reportErr); cb(); }); -} +}; + +exports.addTestUser = function(email, hash, cb) { + client.query( + "INSERT INTO user(passwd) VALUES(?)", + [ hash ], + function(err, info) { + if (err) { + logUnexpectedError(err); + cb(err); + return; + } + client.query( + "INSERT INTO email(user, address) VALUES(?, ?)", + [ info.insertId, email ], + function(err, info) { + if (err) logUnexpectedError(err); + cb(err ? err : undefined, email); + }); + }); +};