From 6a3124becb3fdba6b5a2d92d66accf1216bf9bda Mon Sep 17 00:00:00 2001 From: Avital Oliver Date: Tue, 7 Aug 2012 20:23:05 -0700 Subject: [PATCH] Tests for reset password flow --- packages/accounts-passwords/email_tests.js | 56 +++++++++++++++++++ .../accounts-passwords/email_tests_setup.js | 26 +++++++++ packages/accounts-passwords/package.js | 2 + .../accounts-passwords/passwords_client.js | 2 +- .../accounts-passwords/passwords_common.js | 9 ++- 5 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 packages/accounts-passwords/email_tests.js create mode 100644 packages/accounts-passwords/email_tests_setup.js diff --git a/packages/accounts-passwords/email_tests.js b/packages/accounts-passwords/email_tests.js new file mode 100644 index 0000000000..30f1f2c81c --- /dev/null +++ b/packages/accounts-passwords/email_tests.js @@ -0,0 +1,56 @@ +(function () { + var email = Meteor.uuid() + "-intercept@example.com"; + var resetPasswordToken; + + testAsyncMulti("accounts emails - reset password flow", [ + function (test, expect) { + Meteor.createUser({email: email, password: 'foobar'}, + expect(function (error) { + test.equal(error, undefined); + })); + }, + function (test, expect) { + Meteor.forgotPassword({email: email}, expect(function (error) { + test.equal(error, undefined); + })); + }, + function (test, expect) { + Meteor.call("getInterceptedEmails", email, expect(function (error, result) { + test.notEqual(result, undefined); + test.equal(result.length, 1); + var content = result[0]; + + var match = content.match( + new RegExp(window.location.protocol + "//" + + window.location.host + "/#\\?reset-password/(\\S*)")); + test.isTrue(match); + resetPasswordToken = match[1]; + console.log(resetPasswordToken); + })); + }, + function (test, expect) { + Meteor.resetPassword(resetPasswordToken, "newPassword", expect(function(error) { + test.isFalse(error); + })); + }, + function (test, expect) { + Meteor.logout(expect(function (error) { + test.equal(error, undefined); + test.equal(Meteor.user(), null); + })); + }, + function (test, expect) { + Meteor.loginWithPassword( + {email: email}, "newPassword", + expect(function (error) { + test.isFalse(error); + })); + }, + function (test, expect) { + Meteor.logout(expect(function (error) { + test.equal(error, undefined); + test.equal(Meteor.user(), null); + })); + } + ]); +}) (); \ No newline at end of file diff --git a/packages/accounts-passwords/email_tests_setup.js b/packages/accounts-passwords/email_tests_setup.js new file mode 100644 index 0000000000..793db851a7 --- /dev/null +++ b/packages/accounts-passwords/email_tests_setup.js @@ -0,0 +1,26 @@ +(function () { + // + // a mechanism to intercept emails sent to addressing including + // the string "intercept", storing them in an array that can then + // be retrieved using the getInterceptedEmails method + // + var oldMeteorMailSend = Meteor.mail.send; + var interceptedEmails = {}; // (email address) -> (array of contents) + + Meteor.mail.send = function (email, content) { + if (email.indexOf('intercept') === -1) { + oldMeteorMailSend(email, content); + } else { + if (!interceptedEmails[email]) + interceptedEmails[email] = []; + + interceptedEmails[email].push(content); + } + }; + + Meteor.methods({ + getInterceptedEmails: function (email) { + return interceptedEmails[email]; + } + }); +}) (); \ No newline at end of file diff --git a/packages/accounts-passwords/package.js b/packages/accounts-passwords/package.js index aa58c7d9bb..c84d6cb5b1 100644 --- a/packages/accounts-passwords/package.js +++ b/packages/accounts-passwords/package.js @@ -15,4 +15,6 @@ Package.on_test(function(api) { api.use(['accounts-passwords', 'tinytest', 'test-helpers']); api.add_files('passwords_tests_setup.js', 'server'); api.add_files('passwords_tests.js', 'client'); + api.add_files('email_tests_setup.js', 'server'); + api.add_files('email_tests.js', 'client'); }); diff --git a/packages/accounts-passwords/passwords_client.js b/packages/accounts-passwords/passwords_client.js index 05ac3c6fa6..bf2915e9b9 100644 --- a/packages/accounts-passwords/passwords_client.js +++ b/packages/accounts-passwords/passwords_client.js @@ -131,7 +131,7 @@ // @param options {Object} // - email: (email) // @param callback (optional) {Function(error|undefined)} - Meteor.forgotPassword = function(options) { + Meteor.forgotPassword = function(options, callback) { if (!options.email) throw new Error("Must pass options.email"); options.baseUrl = window.location.protocol + "//" diff --git a/packages/accounts-passwords/passwords_common.js b/packages/accounts-passwords/passwords_common.js index f7f4cce93c..24dc1f2bd6 100644 --- a/packages/accounts-passwords/passwords_common.js +++ b/packages/accounts-passwords/passwords_common.js @@ -1 +1,8 @@ -Meteor.accounts.passwords = {}; \ No newline at end of file +Meteor.accounts.passwords = {}; + +// internal email validation tokens collection. Never published. +Meteor.accounts._emailValidationTokens = new Meteor.Collection( + "accounts._emailValidationTokens", + null /*manager*/, + null /*driver*/, + true /*preventAutopublish*/); \ No newline at end of file