Skip to content

Commit 0850c18

Browse files
drew-grossflovilmart
authored andcommitted
Fixes #1649 (#1650)
* Regression test #1649 * Address comments * Comment * Change emails to help debug flaky test failures * More logging info to debug flaky tests
1 parent 98f8db3 commit 0850c18

File tree

2 files changed

+77
-22
lines changed

2 files changed

+77
-22
lines changed

spec/ValidationAndPasswordsReset.spec.js

+46-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"use strict";
22

3-
var request = require('request');
4-
var Config = require("../src/Config");
3+
let MockEmailAdapterWithOptions = require('./MockEmailAdapterWithOptions');
4+
let request = require('request');
5+
let Config = require("../src/Config");
6+
57
describe("Custom Pages Configuration", () => {
68
it("should set the custom pages", (done) => {
79
setServerConfiguration({
@@ -62,7 +64,7 @@ describe("Email Verification", () => {
6264
var user = new Parse.User();
6365
user.setPassword("asdf");
6466
user.setUsername("zxcv");
65-
user.setEmail('cool_guy@parse.com');
67+
user.setEmail('testIfEnabled@parse.com');
6668
user.signUp(null, {
6769
success: function(user) {
6870
expect(emailAdapter.sendVerificationEmail).toHaveBeenCalled();
@@ -150,7 +152,7 @@ describe("Email Verification", () => {
150152
expect(emailAdapter.sendVerificationEmail).not.toHaveBeenCalled();
151153
user.fetch()
152154
.then((user) => {
153-
user.set("email", "cool_guy@parse.com");
155+
user.set("email", "testWhenUpdating@parse.com");
154156
return user.save();
155157
}).then((user) => {
156158
return user.fetch();
@@ -204,7 +206,7 @@ describe("Email Verification", () => {
204206
expect(emailAdapter.sendVerificationEmail).not.toHaveBeenCalled();
205207
user.fetch()
206208
.then((user) => {
207-
user.set("email", "cool_guy@parse.com");
209+
user.set("email", "testValidLinkWhenUpdating@parse.com");
208210
return user.save();
209211
}).then((user) => {
210212
return user.fetch();
@@ -228,7 +230,7 @@ describe("Email Verification", () => {
228230
var calls = 0;
229231
var emailAdapter = {
230232
sendMail: function(options){
231-
expect(options.to).toBe('cool_guy@parse.com');
233+
expect(options.to).toBe('testSendSimpleAdapter@parse.com');
232234
if (calls == 0) {
233235
expect(options.subject).toEqual('Please verify your e-mail for My Cool App');
234236
expect(options.text.match(/verify_email/)).not.toBe(null);
@@ -258,15 +260,15 @@ describe("Email Verification", () => {
258260
var user = new Parse.User();
259261
user.setPassword("asdf");
260262
user.setUsername("zxcv");
261-
user.set("email", "cool_guy@parse.com");
263+
user.set("email", "testSendSimpleAdapter@parse.com");
262264
user.signUp(null, {
263265
success: function(user) {
264266
expect(calls).toBe(1);
265267
user.fetch()
266268
.then((user) => {
267269
return user.save();
268270
}).then((user) => {
269-
return Parse.User.requestPasswordReset("cool_guy@parse.com").catch((err) => {
271+
return Parse.User.requestPasswordReset("testSendSimpleAdapter@parse.com").catch((err) => {
270272
fail('Should not fail requesting a password');
271273
done();
272274
})
@@ -282,6 +284,42 @@ describe("Email Verification", () => {
282284
});
283285
});
284286

287+
it('fails if you include an emailAdapter, set verifyUserEmails to false, dont set a publicServerURL, and try to send a password reset email (regression test for #1649)', done => {
288+
setServerConfiguration({
289+
serverURL: 'http://localhost:8378/1',
290+
appId: 'test',
291+
appName: 'unused',
292+
javascriptKey: 'test',
293+
dotNetKey: 'windows',
294+
clientKey: 'client',
295+
restAPIKey: 'rest',
296+
masterKey: 'test',
297+
collectionPrefix: 'test_',
298+
fileKey: 'test',
299+
verifyUserEmails: false,
300+
emailAdapter: MockEmailAdapterWithOptions({
301+
fromAddress: '[email protected]',
302+
apiKey: 'k',
303+
domain: 'd',
304+
}),
305+
})
306+
307+
let user = new Parse.User();
308+
user.setPassword("asdf");
309+
user.setUsername("zxcv");
310+
user.set("email", "[email protected]");
311+
user.signUp(null)
312+
.then(user => Parse.User.requestPasswordReset("[email protected]"))
313+
.then(result => {
314+
console.log(result);
315+
fail('sending password reset email should not have succeeded');
316+
done();
317+
}, error => {
318+
expect(error.message).toEqual('An appName, publicServerURL, and emailAdapter are required for password reset functionality.')
319+
done();
320+
});
321+
});
322+
285323
it('does not send verification email if email verification is disabled', done => {
286324
var emailAdapter = {
287325
sendVerificationEmail: () => Promise.resolve(),

src/Routers/UsersRouter.js

+31-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// These methods handle the User-related routes.
22

33
import deepcopy from 'deepcopy';
4-
4+
import Config from '../Config';
55
import ClassesRouter from './ClassesRouter';
66
import PromiseRouter from '../PromiseRouter';
77
import rest from '../rest';
@@ -156,19 +156,36 @@ export class UsersRouter extends ClassesRouter {
156156
}
157157

158158
handleResetRequest(req) {
159-
let { email } = req.body;
160-
if (!email) {
161-
throw new Parse.Error(Parse.Error.EMAIL_MISSING, "you must provide an email");
162-
}
163-
let userController = req.config.userController;
164-
165-
return userController.sendPasswordResetEmail(email).then((token) => {
166-
return Promise.resolve({
167-
response: {}
168-
});
169-
}, (err) => {
170-
throw new Parse.Error(Parse.Error.EMAIL_NOT_FOUND, `no user found with email ${email}`);
171-
});
159+
try {
160+
Config.validateEmailConfiguration({
161+
verifyUserEmails: true, //A bit of a hack, as this isn't the intended purpose of this parameter
162+
appName: req.config.appName,
163+
publicServerURL: req.config.publicServerURL,
164+
});
165+
} catch (e) {
166+
if (typeof e === 'string') {
167+
// Maybe we need a Bad Configuration error, but the SDKs won't understand it. For now, Internal Server Error.
168+
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'An appName, publicServerURL, and emailAdapter are required for password reset functionality.');
169+
} else {
170+
throw e;
171+
}
172+
}
173+
let { email } = req.body;
174+
if (!email) {
175+
throw new Parse.Error(Parse.Error.EMAIL_MISSING, "you must provide an email");
176+
}
177+
let userController = req.config.userController;
178+
return userController.sendPasswordResetEmail(email).then(token => {
179+
return Promise.resolve({
180+
response: {}
181+
});
182+
}, err => {
183+
if (err.code === Parse.Error.OBJECT_NOT_FOUND) {
184+
throw new Parse.Error(Parse.Error.EMAIL_NOT_FOUND, `No user found with email ${email}.`);
185+
} else {
186+
throw err;
187+
}
188+
});
172189
}
173190

174191

0 commit comments

Comments
 (0)