Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Regression test parse-community#1649

* Address comments

* Comment

* Change emails to help debug flaky test failures

* More logging info to debug flaky tests
  • Loading branch information
drew-gross authored and flovilmart committed May 25, 2016
1 parent 98f8db3 commit 0850c18
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 22 deletions.
54 changes: 46 additions & 8 deletions spec/ValidationAndPasswordsReset.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"use strict";

var request = require('request');
var Config = require("../src/Config");
let MockEmailAdapterWithOptions = require('./MockEmailAdapterWithOptions');
let request = require('request');
let Config = require("../src/Config");

describe("Custom Pages Configuration", () => {
it("should set the custom pages", (done) => {
setServerConfiguration({
Expand Down Expand Up @@ -62,7 +64,7 @@ describe("Email Verification", () => {
var user = new Parse.User();
user.setPassword("asdf");
user.setUsername("zxcv");
user.setEmail('cool_guy@parse.com');
user.setEmail('testIfEnabled@parse.com');
user.signUp(null, {
success: function(user) {
expect(emailAdapter.sendVerificationEmail).toHaveBeenCalled();
Expand Down Expand Up @@ -150,7 +152,7 @@ describe("Email Verification", () => {
expect(emailAdapter.sendVerificationEmail).not.toHaveBeenCalled();
user.fetch()
.then((user) => {
user.set("email", "cool_guy@parse.com");
user.set("email", "testWhenUpdating@parse.com");
return user.save();
}).then((user) => {
return user.fetch();
Expand Down Expand Up @@ -204,7 +206,7 @@ describe("Email Verification", () => {
expect(emailAdapter.sendVerificationEmail).not.toHaveBeenCalled();
user.fetch()
.then((user) => {
user.set("email", "cool_guy@parse.com");
user.set("email", "testValidLinkWhenUpdating@parse.com");
return user.save();
}).then((user) => {
return user.fetch();
Expand All @@ -228,7 +230,7 @@ describe("Email Verification", () => {
var calls = 0;
var emailAdapter = {
sendMail: function(options){
expect(options.to).toBe('cool_guy@parse.com');
expect(options.to).toBe('testSendSimpleAdapter@parse.com');
if (calls == 0) {
expect(options.subject).toEqual('Please verify your e-mail for My Cool App');
expect(options.text.match(/verify_email/)).not.toBe(null);
Expand Down Expand Up @@ -258,15 +260,15 @@ describe("Email Verification", () => {
var user = new Parse.User();
user.setPassword("asdf");
user.setUsername("zxcv");
user.set("email", "cool_guy@parse.com");
user.set("email", "testSendSimpleAdapter@parse.com");
user.signUp(null, {
success: function(user) {
expect(calls).toBe(1);
user.fetch()
.then((user) => {
return user.save();
}).then((user) => {
return Parse.User.requestPasswordReset("cool_guy@parse.com").catch((err) => {
return Parse.User.requestPasswordReset("testSendSimpleAdapter@parse.com").catch((err) => {
fail('Should not fail requesting a password');
done();
})
Expand All @@ -282,6 +284,42 @@ describe("Email Verification", () => {
});
});

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 => {
setServerConfiguration({
serverURL: 'http://localhost:8378/1',
appId: 'test',
appName: 'unused',
javascriptKey: 'test',
dotNetKey: 'windows',
clientKey: 'client',
restAPIKey: 'rest',
masterKey: 'test',
collectionPrefix: 'test_',
fileKey: 'test',
verifyUserEmails: false,
emailAdapter: MockEmailAdapterWithOptions({
fromAddress: '[email protected]',
apiKey: 'k',
domain: 'd',
}),
})

let user = new Parse.User();
user.setPassword("asdf");
user.setUsername("zxcv");
user.set("email", "[email protected]");
user.signUp(null)
.then(user => Parse.User.requestPasswordReset("[email protected]"))
.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();
});
});

it('does not send verification email if email verification is disabled', done => {
var emailAdapter = {
sendVerificationEmail: () => Promise.resolve(),
Expand Down
45 changes: 31 additions & 14 deletions src/Routers/UsersRouter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// These methods handle the User-related routes.

import deepcopy from 'deepcopy';

import Config from '../Config';
import ClassesRouter from './ClassesRouter';
import PromiseRouter from '../PromiseRouter';
import rest from '../rest';
Expand Down Expand Up @@ -156,19 +156,36 @@ export class UsersRouter extends ClassesRouter {
}

handleResetRequest(req) {
let { email } = req.body;
if (!email) {
throw new Parse.Error(Parse.Error.EMAIL_MISSING, "you must provide an email");
}
let userController = req.config.userController;

return userController.sendPasswordResetEmail(email).then((token) => {
return Promise.resolve({
response: {}
});
}, (err) => {
throw new Parse.Error(Parse.Error.EMAIL_NOT_FOUND, `no user found with email ${email}`);
});
try {
Config.validateEmailConfiguration({
verifyUserEmails: true, //A bit of a hack, as this isn't the intended purpose of this parameter
appName: req.config.appName,
publicServerURL: req.config.publicServerURL,
});
} catch (e) {
if (typeof e === 'string') {
// Maybe we need a Bad Configuration error, but the SDKs won't understand it. For now, Internal Server Error.
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'An appName, publicServerURL, and emailAdapter are required for password reset functionality.');
} else {
throw e;
}
}
let { email } = req.body;
if (!email) {
throw new Parse.Error(Parse.Error.EMAIL_MISSING, "you must provide an email");
}
let userController = req.config.userController;
return userController.sendPasswordResetEmail(email).then(token => {
return Promise.resolve({
response: {}
});
}, err => {
if (err.code === Parse.Error.OBJECT_NOT_FOUND) {
throw new Parse.Error(Parse.Error.EMAIL_NOT_FOUND, `No user found with email ${email}.`);
} else {
throw err;
}
});
}


Expand Down

0 comments on commit 0850c18

Please sign in to comment.