Skip to content

Commit d789ca6

Browse files
authored
Fix password reset, email verification for custom endpoint (#7236)
* fixed incorrect endpoint for password reset and email verification * added tests
1 parent add67fd commit d789ca6

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

spec/PagesRouter.spec.js

+78-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('Pages Router', () => {
6363
expect(response.status).toBe(200);
6464
});
6565

66-
it('responds with 404 if publicServerURL is not confgured', async () => {
66+
it('responds with 404 if publicServerURL is not configured', async () => {
6767
await reconfigureServer({
6868
appName: 'unused',
6969
pages: { enableRouter: true },
@@ -971,5 +971,82 @@ describe('Pages Router', () => {
971971
expect(response.text).toBe('Not found.');
972972
});
973973
});
974+
975+
describe('custom endpoint', () => {
976+
it('password reset works with custom endpoint', async () => {
977+
config.pages.pagesEndpoint = 'customEndpoint';
978+
await reconfigureServer(config);
979+
const sendPasswordResetEmail = spyOn(
980+
config.emailAdapter,
981+
'sendPasswordResetEmail'
982+
).and.callThrough();
983+
const user = new Parse.User();
984+
user.setUsername('exampleUsername');
985+
user.setPassword('examplePassword');
986+
user.set('email', 'mail@example.com');
987+
await user.signUp();
988+
await Parse.User.requestPasswordReset(user.getEmail());
989+
990+
const link = sendPasswordResetEmail.calls.all()[0].args[0].link;
991+
const linkResponse = await request({
992+
url: link,
993+
followRedirects: false,
994+
});
995+
expect(linkResponse.status).toBe(200);
996+
997+
const appId = linkResponse.headers['x-parse-page-param-appid'];
998+
const token = linkResponse.headers['x-parse-page-param-token'];
999+
const username = linkResponse.headers['x-parse-page-param-username'];
1000+
const publicServerUrl = linkResponse.headers['x-parse-page-param-publicserverurl'];
1001+
const passwordResetPagePath = pageResponse.calls.all()[0].args[0];
1002+
expect(appId).toBeDefined();
1003+
expect(token).toBeDefined();
1004+
expect(username).toBeDefined();
1005+
expect(publicServerUrl).toBeDefined();
1006+
expect(passwordResetPagePath).toMatch(new RegExp(`\/${pages.passwordReset.defaultFile}`));
1007+
pageResponse.calls.reset();
1008+
1009+
const formUrl = `${publicServerUrl}/${config.pages.pagesEndpoint}/${appId}/request_password_reset`;
1010+
const formResponse = await request({
1011+
url: formUrl,
1012+
method: 'POST',
1013+
body: {
1014+
token,
1015+
username,
1016+
new_password: 'newPassword',
1017+
},
1018+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1019+
followRedirects: false,
1020+
});
1021+
expect(formResponse.status).toEqual(200);
1022+
expect(pageResponse.calls.all()[0].args[0]).toContain(
1023+
`/${pages.passwordResetSuccess.defaultFile}`
1024+
);
1025+
});
1026+
1027+
it('email verification works with custom endpoint', async () => {
1028+
config.pages.pagesEndpoint = 'customEndpoint';
1029+
await reconfigureServer(config);
1030+
const sendVerificationEmail = spyOn(
1031+
config.emailAdapter,
1032+
'sendVerificationEmail'
1033+
).and.callThrough();
1034+
const user = new Parse.User();
1035+
user.setUsername('exampleUsername');
1036+
user.setPassword('examplePassword');
1037+
user.set('email', 'mail@example.com');
1038+
await user.signUp();
1039+
1040+
const link = sendVerificationEmail.calls.all()[0].args[0].link;
1041+
const linkResponse = await request({
1042+
url: link,
1043+
followRedirects: false,
1044+
});
1045+
expect(linkResponse.status).toBe(200);
1046+
1047+
const pagePath = pageResponse.calls.all()[0].args[0];
1048+
expect(pagePath).toMatch(new RegExp(`\/${pages.emailVerificationSuccess.defaultFile}`));
1049+
});
1050+
});
9741051
});
9751052
});

src/Config.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ export class Config {
451451
}
452452

453453
get requestResetPasswordURL() {
454-
return `${this.publicServerURL}/apps/${this.applicationId}/request_password_reset`;
454+
return `${this.publicServerURL}/${this.pagesEndpoint}/${this.applicationId}/request_password_reset`;
455455
}
456456

457457
get passwordResetSuccessURL() {
@@ -466,7 +466,15 @@ export class Config {
466466
}
467467

468468
get verifyEmailURL() {
469-
return `${this.publicServerURL}/apps/${this.applicationId}/verify_email`;
469+
return `${this.publicServerURL}/${this.pagesEndpoint}/${this.applicationId}/verify_email`;
470+
}
471+
472+
// TODO: Remove this function once PagesRouter replaces the PublicAPIRouter;
473+
// the (default) endpoint has to be defined in PagesRouter only.
474+
get pagesEndpoint() {
475+
return this.pages && this.pages.enableRouter && this.pages.pagesEndpoint
476+
? this.pages.pagesEndpoint
477+
: 'apps';
470478
}
471479
}
472480

0 commit comments

Comments
 (0)