From bdd9e7e4b409268443796c1961ce7f7031b090b2 Mon Sep 17 00:00:00 2001 From: Emily Stark Date: Mon, 17 Feb 2014 10:52:42 -0800 Subject: [PATCH] Add test for deferred registration with API token URL --- tools/tests/registration.js | 86 ++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 20 deletions(-) diff --git a/tools/tests/registration.js b/tools/tests/registration.js index 8dc1d858e1..21bbf084d2 100644 --- a/tools/tests/registration.js +++ b/tools/tests/registration.js @@ -21,6 +21,30 @@ var ddpConnect = function (url) { return DDP.connect(url); }; +var registrationUrlRegexp = + /https:\/\/www\.meteor\.com\/setPassword\?([a-zA-Z0-9\+\/]+)/; + +// Given a registration token created by doing a deferred registration +// with `email`, makes a DDP connection to the accounts server and +// finishes the registration process. +var registerWithToken = function (token, username, password, email) { + // XXX It might make more sense to hard-code the DDP url to + // https://www.meteor.com, since that's who the sandboxes are talking + // to. + var accountsConn = ddpConnect(config.getAuthDDPUrl()); + var registrationTokenInfo = accountsConn.call('registrationTokenInfo', + token); + var registrationCode = registrationTokenInfo.code; + accountsConn.call('register', { + username: username, + password: password, + emails: [email], + token: token, + code: registrationCode + }); + accountsConn.close(); +}; + // Polls a guerrillamail.com inbox every 3 seconds looking for an email // that matches the given subject and body regexes. This could fail if // there is someone else polling this same inbox, so use a random email @@ -96,7 +120,7 @@ var waitForEmail = selftest.markStack(function (inbox, subjectRegExp, return match; }); -selftest.define('deferred registration', ['net'], function () { +selftest.define('deferred registration - email registration token', ['net', 'slow'], function () { var s = new Sandbox; s.createApp('deployapp', 'empty'); s.cd('deployapp'); @@ -112,7 +136,10 @@ selftest.define('deferred registration', ['net'], function () { run.waitSecs(90); // Check that we got a prompt to set a password on meteor.com. run.matchErr('set a password'); - run.matchErr('https://www.meteor.com'); + var urlMatch = run.matchErr(registrationUrlRegexp); + if (! urlMatch || ! urlMatch[1]) { + throw new Error("Missing registration URL"); + } run.expectExit(0); // Check that we got a registration email in our inbox. @@ -121,28 +148,13 @@ selftest.define('deferred registration', ['net'], function () { // Fish out the registration token and use to it to complete // registration. - var token = /\/setPassword\?([a-zA-Z0-9\+\/]+)/. - exec(registrationEmail.bodyPage); + var token = registrationUrlRegexp.exec(registrationEmail.bodyPage); if (! token || ! token[1]) { throw new Error("No registration token in email"); } token = token[1]; - // XXX It might make more sense to hard-code the DDP url to - // https://www.meteor.com, since that's who the sandboxes are talking - // to. - var accountsConn = ddpConnect(config.getAuthDDPUrl()); - var registrationTokenInfo = accountsConn.call('registrationTokenInfo', - token); - var registrationCode = registrationTokenInfo.code; - accountsConn.call('register', { - username: username, - password: 'testtest', - emails: [email], - token: token, - code: registrationCode - }); - accountsConn.close(); + registerWithToken(token, username, 'testtest', email); // Success! We should be able to log out and log back in with our new // password. @@ -153,9 +165,43 @@ selftest.define('deferred registration', ['net'], function () { // authorization to delete our app. testUtils.cleanUpApp(s, appName); - // XXX Test the api registration URLs + // XXX Test that registration can only be done once (after using email + // url, api url fails and vice versa, and after using each of them + // using it again fails) // XXX Test that registration URLs get printed when they should // XXX Test registration while the tool is waiting on a DDP method to // return (e.g. deploy and login with an existing username that // doesn't have a password set yet) }); + +selftest.define( + 'deferred registration - api registration token', + ['net', 'slow'], + function () { + var s = new Sandbox; + s.createApp('deployapp', 'empty'); + s.cd('deployapp'); + + // Deploy an app with a new email address. + var email = testUtils.randomUserEmail(); + var username = testUtils.randomString(10); + var appName = testUtils.randomAppName(); + var run = s.run('deploy', appName); + run.waitSecs(testUtils.accountsCommandTimeoutSecs); + run.matchErr('Email:'); + run.write(email + '\n'); + run.waitSecs(90); + // Check that we got a prompt to set a password on meteor.com. + run.matchErr('set a password'); + var urlMatch = run.matchErr(registrationUrlRegexp); + if (! urlMatch || ! urlMatch.length || ! urlMatch[1]) { + throw new Error("Missing registration token"); + } + var token = urlMatch[1]; + registerWithToken(token, username, 'testtest', email); + + testUtils.logout(s); + testUtils.login(s, username, 'testtest'); + testUtils.cleanUpApp(s, appName); + } +);