From 64b8493316045004a2e6e1238a4c1cfc69ff4e9d Mon Sep 17 00:00:00 2001 From: Lloyd Hilaiel <lloyd@hilaiel.com> Date: Tue, 22 Nov 2011 17:42:05 -0700 Subject: [PATCH] (loadgen) add facilities to have browserid create test users at startup time - issue #504 --- bin/browserid | 27 +++++++++++++++++++++++++++ lib/db.js | 6 ++++++ lib/db/json.js | 12 ++++++++++++ lib/db/mysql.js | 24 ++++++++++++++++++++++-- 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/bin/browserid b/bin/browserid index 0e6150977..1ac097f9b 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 b71dcf889..2b5cbbadc 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 79e4bf3e9..f78045b00 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 97070a51c..574c5a72e 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); + }); + }); +}; -- GitLab