From 2f2ff377eb0024dbfe13149d2a4f0652b60c643a Mon Sep 17 00:00:00 2001 From: Tyler Brock Date: Tue, 5 Jul 2016 12:08:46 -0700 Subject: [PATCH] Better e-mail adapter testing (#2208) --- spec/ValidationAndPasswordsReset.spec.js | 88 ++++++++++++++++++++++++ src/Config.js | 15 ++-- src/Routers/UsersRouter.js | 2 +- 3 files changed, 97 insertions(+), 8 deletions(-) diff --git a/spec/ValidationAndPasswordsReset.spec.js b/spec/ValidationAndPasswordsReset.spec.js index 7781d486..afd9ae7f 100644 --- a/spec/ValidationAndPasswordsReset.spec.js +++ b/spec/ValidationAndPasswordsReset.spec.js @@ -393,6 +393,94 @@ describe("Custom Pages, Email Verification, Password Reset", () => { }); }); + it_exclude_dbs(['postgres'])('fails if you include an emailAdapter, have an appName, but have no publicServerURL and send a password reset email', done => { + reconfigureServer({ + appName: undefined, + emailAdapter: MockEmailAdapterWithOptions({ + fromAddress: 'parse@example.com', + apiKey: 'k', + domain: 'd', + }), + }) + .then(() => { + let user = new Parse.User(); + user.setPassword("asdf"); + user.setUsername("zxcv"); + user.set("email", "testInvalidConfig@parse.com"); + user.signUp(null) + .then(user => Parse.User.requestPasswordReset("testInvalidConfig@parse.com")) + .then(result => { + console.log(result); + fail('sending password reset email should not have succeeded'); + done(); + }, error => { + expect(error.message).toEqual('An appName, publicServerURL, and emailAdapter are required for password reset functionality.') + done(); + }); + }) + .catch(error => { + fail(JSON.stringify(error)); + done(); + }); + }); + + it_exclude_dbs(['postgres'])('fails if you set a publicServerURL, have an appName, but no emailAdapter and send a password reset email', done => { + reconfigureServer({ + appName: 'unused', + publicServerURL: 'http://localhost:1337/1', + emailAdapter: undefined, + }) + .then(() => { + let user = new Parse.User(); + user.setPassword("asdf"); + user.setUsername("zxcv"); + user.set("email", "testInvalidConfig@parse.com"); + user.signUp(null) + .then(user => Parse.User.requestPasswordReset("testInvalidConfig@parse.com")) + .then(result => { + console.log(result); + fail('sending password reset email should not have succeeded'); + done(); + }, error => { + expect(error.message).toEqual('An appName, publicServerURL, and emailAdapter are required for password reset functionality.') + done(); + }); + }) + .catch(error => { + fail(JSON.stringify(error)); + done(); + }); + }); + + it_exclude_dbs(['postgres'])('succeeds sending a password reset email if appName, publicServerURL, and email adapter are prodvided', done => { + reconfigureServer({ + appName: 'coolapp', + publicServerURL: 'http://localhost:1337/1', + emailAdapter: MockEmailAdapterWithOptions({ + fromAddress: 'parse@example.com', + apiKey: 'k', + domain: 'd', + }), + }) + .then(() => { + let user = new Parse.User(); + user.setPassword("asdf"); + user.setUsername("zxcv"); + user.set("email", "testInvalidConfig@parse.com"); + user.signUp(null) + .then(user => Parse.User.requestPasswordReset("testInvalidConfig@parse.com")) + .then(result => { + done(); + }, error => { + done(error); + }); + }) + .catch(error => { + fail(JSON.stringify(error)); + done(); + }); + }); + it('does not send verification email if email verification is disabled', done => { var emailAdapter = { sendVerificationEmail: () => Promise.resolve(), diff --git a/src/Config.js b/src/Config.js index 731cc7aa..927319ab 100644 --- a/src/Config.js +++ b/src/Config.js @@ -83,13 +83,14 @@ export class Config { } static validateEmailConfiguration({emailAdapter, appName, publicServerURL}) { - if (emailAdapter) { - if (typeof appName !== 'string') { - throw 'An app name is required for e-mail verification and password resets.'; - } - if (typeof publicServerURL !== 'string') { - throw 'A public server url is required for e-mail verification and password resets.'; - } + if (!emailAdapter) { + throw 'An emailAdapter is required for e-mail verification and password resets.'; + } + if (typeof appName !== 'string') { + throw 'An app name is required for e-mail verification and password resets.'; + } + if (typeof publicServerURL !== 'string') { + throw 'A public server url is required for e-mail verification and password resets.'; } } diff --git a/src/Routers/UsersRouter.js b/src/Routers/UsersRouter.js index dfddd0c1..f036a37b 100644 --- a/src/Routers/UsersRouter.js +++ b/src/Routers/UsersRouter.js @@ -163,7 +163,7 @@ export class UsersRouter extends ClassesRouter { handleResetRequest(req) { try { Config.validateEmailConfiguration({ - emailAdapter: req.config.userController, + emailAdapter: req.config.userController.adapter, appName: req.config.appName, publicServerURL: req.config.publicServerURL, });