diff --git a/tests/cert-emails-test.js b/tests/cert-emails-test.js index b8e353fe3057b5829c60bfb901199f23ed9257a3..59d0521f12b0e99ae14d1ae32e36b435c7408129 100755 --- a/tests/cert-emails-test.js +++ b/tests/cert-emails-test.js @@ -50,18 +50,13 @@ jwt = require('jwcrypto/jwt'); var suite = vows.describe('cert-emails'); +var token = undefined; + // disable vows (often flakey?) async error behavior suite.options.error = false; start_stop.addStartupBatches(suite); -// ever time a new token is sent out, let's update the global -// var 'token' -var token = undefined; -start_stop.browserid.on('token', function(secret) { - token = secret; -}); - // INFO: some of these tests are repeat of sync-emails... to set // things up properly for key certification @@ -83,11 +78,11 @@ suite.addBatch({ suite.addBatch({ "a token": { topic: function() { - if (token) return token; - else start_stop.browserid.once('token', this.callback); + start_stop.waitForToken(this.callback); }, "is obtained": function (t) { assert.strictEqual(typeof t, 'string'); + token = t; } } }); diff --git a/tests/email-throttling-test.js b/tests/email-throttling-test.js index 52e86a126631e8c13183ca86c2fd032ab7aa5707..bc3a1805932587e0518823611519c47a7dd4ec1b 100755 --- a/tests/email-throttling-test.js +++ b/tests/email-throttling-test.js @@ -67,8 +67,7 @@ suite.addBatch({ suite.addBatch({ "a token": { topic: function() { - if (token) return token; - else start_stop.browserid.once('token', this.callback); + start_stop.waitForToken(this.callback); }, "is obtained": function (t) { assert.strictEqual(typeof t, 'string'); @@ -118,8 +117,7 @@ suite.addBatch({ suite.addBatch({ "a token": { topic: function() { - if (token) return token; - else start_stop.browserid.once('token', this.callback); + start_stop.waitForToken(this.callback); }, "is obtained": function (t) { assert.strictEqual(typeof t, 'string'); diff --git a/tests/forgotten-email-test.js b/tests/forgotten-email-test.js index 87774f4a812059525096d799d5b843976cf491de..25ee6763055d47b6a5f042b9a0f83af2dbc6d647 100755 --- a/tests/forgotten-email-test.js +++ b/tests/forgotten-email-test.js @@ -68,8 +68,7 @@ suite.addBatch({ suite.addBatch({ "a token": { topic: function() { - if (token) return token; - else start_stop.browserid.once('token', this.callback); + start_stop.waitForToken(this.callback); }, "is obtained": function (t) { assert.strictEqual(typeof t, 'string'); @@ -118,8 +117,7 @@ suite.addBatch({ suite.addBatch({ "a token": { topic: function() { - if (token) return token; - else start_stop.browserid.once('token', this.callback); + start_stop.waitForToken(this.callback); }, "is obtained": function (t) { assert.strictEqual(typeof t, 'string'); @@ -182,8 +180,7 @@ suite.addBatch({ suite.addBatch({ "a token": { topic: function() { - if (token) return token; - else start_stop.browserid.once('token', this.callback); + start_stop.waitForToken(this.callback); }, "is obtained": function (t) { assert.strictEqual(typeof t, 'string'); diff --git a/tests/lib/start-stop.js b/tests/lib/start-stop.js index a54da66595db2cb396c2a7b7ef04c1bfcdb266c3..1f289391050ae7979b193e2c615f3328fdad5adb 100644 --- a/tests/lib/start-stop.js +++ b/tests/lib/start-stop.js @@ -49,6 +49,19 @@ process.on('exit', function () { if (proc) { proc.kill(); } }); +var nextTokenFunction = undefined; +var tokenStack = []; + +exports.waitForToken = function(cb) { + if (tokenStack.length) { + cb(tokenStack.shift()); + } + else { + if (nextTokenFunction) throw "can't wait for a verification token when someone else is!"; + nextTokenFunction = cb; + } +}; + exports.browserid = new events.EventEmitter; function setupProc(proc) { @@ -62,7 +75,11 @@ function setupProc(proc) { exports.browserid.emit('ready'); sentReady = true; } else if (m = tokenRegex.exec(x)) { - exports.browserid.emit('token', m[1]); + tokenStack.push(m[1]); + if (nextTokenFunction) { + nextTokenFunction(tokenStack.shift()); + nextTokenFunction = undefined; + } } }); proc.stderr.on('data', function(x) { diff --git a/tests/list-emails-wsapi-test.js b/tests/list-emails-wsapi-test.js index 1cc60423148f69d2cdaf8cc3b1d90fb26f80f5ac..ac9581ba60e0bf9b388f14277c3fd73200e31545 100755 --- a/tests/list-emails-wsapi-test.js +++ b/tests/list-emails-wsapi-test.js @@ -70,8 +70,7 @@ suite.addBatch({ suite.addBatch({ "a token": { topic: function() { - if (token) return token; - else start_stop.browserid.once('token', this.callback); + start_stop.waitForToken(this.callback); }, "is obtained": function (t) { assert.strictEqual(typeof t, 'string'); diff --git a/tests/password-bcrypt-update-test.js b/tests/password-bcrypt-update-test.js index ad0483652b4db4414215e6a7489fe062f0a7d065..15f684112eb4e909141e03c61c48c5ee9c4e4c3a 100755 --- a/tests/password-bcrypt-update-test.js +++ b/tests/password-bcrypt-update-test.js @@ -89,8 +89,7 @@ suite.addBatch({ suite.addBatch({ "a token": { topic: function() { - if (token) return token; - else start_stop.browserid.once('token', this.callback); + start_stop.waitForToken(this.callback); }, "is obtained": function (t) { assert.strictEqual(typeof t, 'string'); diff --git a/tests/registration-status-wsapi-test.js b/tests/registration-status-wsapi-test.js index e0d40aa7762a1062482d8954cd200bd3fa03e6b4..171974e91a38990d97ea36b922ebee7c630522ce 100755 --- a/tests/registration-status-wsapi-test.js +++ b/tests/registration-status-wsapi-test.js @@ -90,8 +90,7 @@ suite.addBatch({ suite.addBatch({ "a token": { topic: function() { - if (token) return token; - else start_stop.browserid.once('token', this.callback); + start_stop.waitForToken(this.callback); }, "is obtained": function (t) { assert.strictEqual(typeof t, 'string'); @@ -210,8 +209,7 @@ suite.addBatch({ suite.addBatch({ "a token": { topic: function() { - if (token) return token; - else start_stop.browserid.once('token', this.callback); + start_stop.waitForToken(this.callback); }, "is obtained": function (t) { assert.strictEqual(typeof t, 'string');