Skip to content

Commit 2f2ff37

Browse files
authored
Better e-mail adapter testing (#2208)
1 parent 48b33d3 commit 2f2ff37

File tree

3 files changed

+97
-8
lines changed

3 files changed

+97
-8
lines changed

spec/ValidationAndPasswordsReset.spec.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,94 @@ describe("Custom Pages, Email Verification, Password Reset", () => {
393393
});
394394
});
395395

396+
it_exclude_dbs(['postgres'])('fails if you include an emailAdapter, have an appName, but have no publicServerURL and send a password reset email', done => {
397+
reconfigureServer({
398+
appName: undefined,
399+
emailAdapter: MockEmailAdapterWithOptions({
400+
fromAddress: '[email protected]',
401+
apiKey: 'k',
402+
domain: 'd',
403+
}),
404+
})
405+
.then(() => {
406+
let user = new Parse.User();
407+
user.setPassword("asdf");
408+
user.setUsername("zxcv");
409+
user.set("email", "[email protected]");
410+
user.signUp(null)
411+
.then(user => Parse.User.requestPasswordReset("[email protected]"))
412+
.then(result => {
413+
console.log(result);
414+
fail('sending password reset email should not have succeeded');
415+
done();
416+
}, error => {
417+
expect(error.message).toEqual('An appName, publicServerURL, and emailAdapter are required for password reset functionality.')
418+
done();
419+
});
420+
})
421+
.catch(error => {
422+
fail(JSON.stringify(error));
423+
done();
424+
});
425+
});
426+
427+
it_exclude_dbs(['postgres'])('fails if you set a publicServerURL, have an appName, but no emailAdapter and send a password reset email', done => {
428+
reconfigureServer({
429+
appName: 'unused',
430+
publicServerURL: 'http://localhost:1337/1',
431+
emailAdapter: undefined,
432+
})
433+
.then(() => {
434+
let user = new Parse.User();
435+
user.setPassword("asdf");
436+
user.setUsername("zxcv");
437+
user.set("email", "[email protected]");
438+
user.signUp(null)
439+
.then(user => Parse.User.requestPasswordReset("[email protected]"))
440+
.then(result => {
441+
console.log(result);
442+
fail('sending password reset email should not have succeeded');
443+
done();
444+
}, error => {
445+
expect(error.message).toEqual('An appName, publicServerURL, and emailAdapter are required for password reset functionality.')
446+
done();
447+
});
448+
})
449+
.catch(error => {
450+
fail(JSON.stringify(error));
451+
done();
452+
});
453+
});
454+
455+
it_exclude_dbs(['postgres'])('succeeds sending a password reset email if appName, publicServerURL, and email adapter are prodvided', done => {
456+
reconfigureServer({
457+
appName: 'coolapp',
458+
publicServerURL: 'http://localhost:1337/1',
459+
emailAdapter: MockEmailAdapterWithOptions({
460+
fromAddress: '[email protected]',
461+
apiKey: 'k',
462+
domain: 'd',
463+
}),
464+
})
465+
.then(() => {
466+
let user = new Parse.User();
467+
user.setPassword("asdf");
468+
user.setUsername("zxcv");
469+
user.set("email", "[email protected]");
470+
user.signUp(null)
471+
.then(user => Parse.User.requestPasswordReset("[email protected]"))
472+
.then(result => {
473+
done();
474+
}, error => {
475+
done(error);
476+
});
477+
})
478+
.catch(error => {
479+
fail(JSON.stringify(error));
480+
done();
481+
});
482+
});
483+
396484
it('does not send verification email if email verification is disabled', done => {
397485
var emailAdapter = {
398486
sendVerificationEmail: () => Promise.resolve(),

src/Config.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ export class Config {
8383
}
8484

8585
static validateEmailConfiguration({emailAdapter, appName, publicServerURL}) {
86-
if (emailAdapter) {
87-
if (typeof appName !== 'string') {
88-
throw 'An app name is required for e-mail verification and password resets.';
89-
}
90-
if (typeof publicServerURL !== 'string') {
91-
throw 'A public server url is required for e-mail verification and password resets.';
92-
}
86+
if (!emailAdapter) {
87+
throw 'An emailAdapter is required for e-mail verification and password resets.';
88+
}
89+
if (typeof appName !== 'string') {
90+
throw 'An app name is required for e-mail verification and password resets.';
91+
}
92+
if (typeof publicServerURL !== 'string') {
93+
throw 'A public server url is required for e-mail verification and password resets.';
9394
}
9495
}
9596

src/Routers/UsersRouter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export class UsersRouter extends ClassesRouter {
163163
handleResetRequest(req) {
164164
try {
165165
Config.validateEmailConfiguration({
166-
emailAdapter: req.config.userController,
166+
emailAdapter: req.config.userController.adapter,
167167
appName: req.config.appName,
168168
publicServerURL: req.config.publicServerURL,
169169
});

0 commit comments

Comments
 (0)