Skip to content

Commit afdcb4f

Browse files
author
Vladyslav Chygrinov
committed
More tests and redone error return slightly
1 parent b5436fd commit afdcb4f

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

spec/PasswordPolicy.spec.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,97 @@ describe('Password Policy: ', () => {
909909
});
910910
});
911911

912+
it('Should return error when password violates Password Policy and reset through ajax', done => {
913+
const user = new Parse.User();
914+
const emailAdapter = {
915+
sendVerificationEmail: () => Promise.resolve(),
916+
sendPasswordResetEmail: options => {
917+
request({
918+
url: options.link,
919+
followRedirects: false,
920+
simple: false,
921+
resolveWithFullResponse: true,
922+
})
923+
.then(response => {
924+
expect(response.status).toEqual(302);
925+
const re = /http:\/\/localhost:8378\/1\/apps\/choose_password\?token=([a-zA-Z0-9]+)\&id=test\&username=user1/;
926+
const match = response.text.match(re);
927+
if (!match) {
928+
fail('should have a token');
929+
done();
930+
return;
931+
}
932+
const token = match[1];
933+
934+
request({
935+
method: 'POST',
936+
url: 'http://localhost:8378/1/apps/test/request_password_reset',
937+
body: `new_password=xuser12&token=${token}&username=user1`,
938+
headers: {
939+
'Content-Type': 'application/x-www-form-urlencoded',
940+
'X-Requested-With': 'XMLHttpRequest',
941+
},
942+
followRedirects: false,
943+
})
944+
.catch(error => {
945+
expect(error.status).not.toBe(302);
946+
expect(error.text).toEqual(
947+
'{"code":-1,"error":"Password does not meet the Password Policy requirements."}'
948+
);
949+
950+
Parse.User.logIn('user1', 'r@nd0m')
951+
.then(function() {
952+
done();
953+
})
954+
.catch(err => {
955+
jfail(err);
956+
fail('should login with old password');
957+
done();
958+
});
959+
})
960+
.catch(error => {
961+
jfail(error);
962+
fail('Failed to POST request password reset');
963+
done();
964+
});
965+
})
966+
.catch(error => {
967+
jfail(error);
968+
fail('Failed to get the reset link');
969+
done();
970+
});
971+
},
972+
sendMail: () => {},
973+
};
974+
reconfigureServer({
975+
appName: 'passwordPolicy',
976+
verifyUserEmails: false,
977+
emailAdapter: emailAdapter,
978+
passwordPolicy: {
979+
doNotAllowUsername: true,
980+
},
981+
publicServerURL: 'http://localhost:8378/1',
982+
}).then(() => {
983+
user.setUsername('user1');
984+
user.setPassword('r@nd0m');
985+
user.set('email', '[email protected]');
986+
user
987+
.signUp()
988+
.then(() => {
989+
Parse.User.requestPasswordReset('[email protected]').catch(err => {
990+
jfail(err);
991+
fail('Reset password request should not fail');
992+
done();
993+
});
994+
})
995+
.catch(error => {
996+
jfail(error);
997+
fail('signUp should not fail');
998+
done();
999+
});
1000+
});
1001+
});
1002+
9121003
it('should reset password even if the new password contains user name while the policy allows', done => {
9131004
const user = new Parse.User();
9141005
const emailAdapter = {

spec/ValidationAndPasswordsReset.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,31 @@ describe('Custom Pages, Email Verification, Password Reset', () => {
992992
});
993993
});
994994

995+
it('should return ajax failure error on ajax request with wrong data provided', done => {
996+
reconfigureServer({
997+
publicServerURL: 'http://localhost:8378/1',
998+
})
999+
.then(() => {
1000+
return request({
1001+
method: 'POST',
1002+
url: 'http://localhost:8378/1/apps/test/request_password_reset',
1003+
body: `new_password=user1&token=12345&username=Johnny`,
1004+
headers: {
1005+
'Content-Type': 'application/x-www-form-urlencoded',
1006+
'X-Requested-With': 'XMLHttpRequest',
1007+
},
1008+
followRedirects: false,
1009+
});
1010+
})
1011+
.catch(error => {
1012+
expect(error.status).not.toBe(302);
1013+
expect(error.text).toEqual(
1014+
'{"code":-1,"error":"Failed to reset password (Username/email or token is invalid)"}'
1015+
);
1016+
done();
1017+
});
1018+
});
1019+
9951020
it('deletes password reset token on email address change', done => {
9961021
reconfigureServer({
9971022
appName: 'coolapp',

src/Routers/PublicAPIRouter.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,15 @@ export class PublicAPIRouter extends PromiseRouter {
207207
});
208208
}
209209

210-
throw new Parse.Error(Parse.Error.OTHER_CAUSE, result.err);
210+
if (
211+
result.err ===
212+
'Password does not meet the Password Policy requirements.'
213+
)
214+
throw new Parse.Error(Parse.Error.OTHER_CAUSE, `${result.err}`);
215+
throw new Parse.Error(
216+
Parse.Error.OTHER_CAUSE,
217+
'Invalid token or username'
218+
);
211219
}
212220

213221
return Promise.resolve({

0 commit comments

Comments
 (0)