From fbd9b73d40edfb70dbde35839d064bb464a2fea0 Mon Sep 17 00:00:00 2001 From: Lloyd Hilaiel <lloyd@hilaiel.com> Date: Wed, 22 Jun 2011 22:16:48 -0600 Subject: [PATCH] refactor common bits out of forgotten-email-test --- browserid/lib/wsapi.js | 2 +- browserid/tests/forgotten-email-test.js | 129 +----------------------- browserid/tests/lib/start-stop.js | 88 ++++++++++++++++ browserid/tests/lib/wsapi.js | 46 +++++++++ 4 files changed, 140 insertions(+), 125 deletions(-) create mode 100644 browserid/tests/lib/start-stop.js create mode 100644 browserid/tests/lib/wsapi.js diff --git a/browserid/lib/wsapi.js b/browserid/lib/wsapi.js index bae2b79b7..1d3f2bc39 100644 --- a/browserid/lib/wsapi.js +++ b/browserid/lib/wsapi.js @@ -62,7 +62,7 @@ exports.stage_user = function(req, resp) { if (!checkParams(getArgs, resp, [ "email", "pass", "pubkey", "site" ])) { return; } - + getArgs.pass = obfuscatePassword(getArgs.pass); try { diff --git a/browserid/tests/forgotten-email-test.js b/browserid/tests/forgotten-email-test.js index ebecf4ae6..badd4ccd4 100755 --- a/browserid/tests/forgotten-email-test.js +++ b/browserid/tests/forgotten-email-test.js @@ -5,108 +5,15 @@ const assert = require('assert'), fs = require('fs'), path = require('path'), http = require('http'), - querystring = require('querystring'); + querystring = require('querystring'), + start_stop = require('./lib/start-stop.js'), + wsapi = require('./lib/wsapi.js'); const amMain = (process.argv[1] === __filename); -const varPath = path.join(path.dirname(__dirname), "var"); var suite = vows.describe('forgotten-email'); -function removeVarDir() { - try { - fs.readdirSync(varPath).forEach(function(f) { - fs.unlinkSync(path.join(varPath, f)); - }); - fs.rmdirSync(varPath); - } catch(e) {} -} - -// wsapi abstractions trivial cookie jar -var cookieJar = {}; - -// A macro for wsapi requests -var wsapi = { - get: function (path, getArgs) { - return function () { - var cb = this.callback; - if (typeof getArgs === 'object') - path += "?" + querystring.stringify(getArgs); - - var headers = {}; - if (Object.keys(cookieJar).length) { - headers['Cookie'] = ""; - for (var k in cookieJar) { - headers['Cookie'] += k + "=" + cookieJar[k]; - } - } - http.get({ - host: '127.0.0.1', - port: '62700', - path: path, - headers: headers - }, function(res) { - // see if there are any set-cookies that we should honor - if (res.headers['set-cookie']) { - res.headers['set-cookie'].forEach(function(cookie) { - var m = /^([^;]+)(?:;.*)$/.exec(cookie); - if (m) { - var x = m[1].split('='); - cookieJar[x[0]] = x[1]; - } - }); - } - var body = ''; - res.on('data', function(chunk) { body += chunk; }) - .on('end', function() { - cb({code: res.statusCode, headers: res.headers, body: body}); - }); - }).on('error', function (e) { - cb(); - }); - }; - } -}; - -suite.addBatch({ - "remove the user database": { - topic: function() { - removeVarDir(); - fs.mkdirSync(varPath, 0755); - return true; - }, - "directory should exist": function(x) { - assert.ok(fs.statSync(varPath).isDirectory()); - } - } -}); - -suite.addBatch({ - "run the server": { - topic: function() { - const server = require("../run.js"); - server.runServer(); - return true; - }, - "server should be running": { - topic: wsapi.get('/ping.txt'), - "server is running": function (r, err) { - assert.equal(r.code, 200); - } - } - } -}); - -suite.addBatch({ - "wait for readiness": { - topic: function() { - var cb = this.callback; - require("../lib/db.js").onReady(function() { cb(true) }); - }, - "readiness has arrived": function(v) { - assert.ok(v); - } - } -}); +start_stop.addStartupBatches(suite); // let's kludge our way into nodemailer to intercept outbound emails var lastEmailBody = undefined; @@ -292,32 +199,6 @@ suite.addBatch({ }, }); -// stop the server -suite.addBatch({ - "stop the server": { - topic: function() { - const server = require("../run.js"); - var cb = this.callback; - server.stopServer(function() { cb(true); }); - }, - "stopped": function(x) { - assert.strictEqual(x, true); - } - } -}); - - -// clean up -suite.addBatch({ - "clean up": { - topic: function() { - removeVarDir(); - return true; - }, - "directory should not exist": function(x) { - assert.throws(function(){ fs.statSync(varPath) }); - } - } -}); +start_stop.addShutdownBatches(suite); suite.run(); diff --git a/browserid/tests/lib/start-stop.js b/browserid/tests/lib/start-stop.js new file mode 100644 index 000000000..915dabaed --- /dev/null +++ b/browserid/tests/lib/start-stop.js @@ -0,0 +1,88 @@ +const assert = require('assert'), + fs = require('fs'), + path = require('path'), + wsapi = require('./wsapi.js'); + +const varPath = path.join(path.dirname(__dirname), "var"); + +function removeVarDir() { + try { + fs.readdirSync(varPath).forEach(function(f) { + fs.unlinkSync(path.join(varPath, f)); + }); + fs.rmdirSync(varPath); + } catch(e) {} +} + +exports.addStartupBatches = function(suite) { + suite.addBatch({ + "remove the user database": { + topic: function() { + removeVarDir(); + fs.mkdirSync(varPath, 0755); + return true; + }, + "directory should exist": function(x) { + assert.ok(fs.statSync(varPath).isDirectory()); + } + } + }); + + suite.addBatch({ + "run the server": { + topic: function() { + const server = require("../../run.js"); + server.runServer(); + return true; + }, + "server should be running": { + topic: wsapi.get('/ping.txt'), + "server is running": function (r, err) { + assert.equal(r.code, 200); + } + } + } + }); + + suite.addBatch({ + "wait for readiness": { + topic: function() { + var cb = this.callback; + require("../../lib/db.js").onReady(function() { cb(true) }); + }, + "readiness has arrived": function(v) { + assert.ok(v); + } + } + }); +}; + +exports.addShutdownBatches = function(suite) { + // stop the server + suite.addBatch({ + "stop the server": { + topic: function() { + const server = require("../../run.js"); + var cb = this.callback; + server.stopServer(function() { cb(true); }); + }, + "stopped": function(x) { + assert.strictEqual(x, true); + } + } + }); + + + // clean up + suite.addBatch({ + "clean up": { + topic: function() { + removeVarDir(); + return true; + }, + "directory should not exist": function(x) { + assert.throws(function(){ fs.statSync(varPath) }); + } + } + }); +} \ No newline at end of file diff --git a/browserid/tests/lib/wsapi.js b/browserid/tests/lib/wsapi.js new file mode 100644 index 000000000..80c4e0b62 --- /dev/null +++ b/browserid/tests/lib/wsapi.js @@ -0,0 +1,46 @@ +const http = require('http'), + querystring = require('querystring'); + +// wsapi abstractions trivial cookie jar +var cookieJar = {}; + +// A macro for wsapi requests +exports.get = function (path, getArgs) { + return function () { + var cb = this.callback; + if (typeof getArgs === 'object') + path += "?" + querystring.stringify(getArgs); + + var headers = {}; + if (Object.keys(cookieJar).length) { + headers['Cookie'] = ""; + for (var k in cookieJar) { + headers['Cookie'] += k + "=" + cookieJar[k]; + } + } + http.get({ + host: '127.0.0.1', + port: '62700', + path: path, + headers: headers + }, function(res) { + // see if there are any set-cookies that we should honor + if (res.headers['set-cookie']) { + res.headers['set-cookie'].forEach(function(cookie) { + var m = /^([^;]+)(?:;.*)$/.exec(cookie); + if (m) { + var x = m[1].split('='); + cookieJar[x[0]] = x[1]; + } + }); + } + var body = ''; + res.on('data', function(chunk) { body += chunk; }) + .on('end', function() { + cb({code: res.statusCode, headers: res.headers, body: body}); + }); + }).on('error', function (e) { + cb(); + }); + }; +}; -- GitLab