-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Improve reset password API #6830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
germanbisogno
wants to merge
9
commits into
parse-community:alpha
Choose a base branch
from
germanbisogno:feature/reset-password-api
base: alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a34101c
adding reset password feature using api
1addf84
adding unit tests
7037a1e
remove focus describe
0196b41
fix public api router error message
eee7b1b
revert back changes for adding new reset password api
23705de
adding comment to unit test
dd47f58
adding comment to unit test
f0da970
adding comment to unit test
62b3ade
Update PasswordPolicy.spec.js
germanbisogno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
const req = require('../lib/request'); | ||
const Config = require('../lib/Config'); | ||
|
||
const request = function(url, callback) { | ||
const request = function (url, callback) { | ||
return req({ | ||
url, | ||
}).then(response => callback(null, response), err => callback(err, err)); | ||
}).then( | ||
response => callback(null, response), | ||
err => callback(err, err) | ||
); | ||
}; | ||
|
||
describe('public API', () => { | ||
|
@@ -208,4 +212,256 @@ describe('public API supplied with invalid application id', () => { | |
} | ||
); | ||
}); | ||
|
||
fdescribe('resetPassword', () => { | ||
let makeRequest; | ||
const re = new RegExp('^(?=.*[a-z]).{8,}'); | ||
let sendEmailOptions; | ||
const emailAdapter = { | ||
sendVerificationEmail: {}, | ||
sendPasswordResetEmail: options => { | ||
sendEmailOptions = options; | ||
}, | ||
sendMail: () => {}, | ||
}; | ||
|
||
const serverURL = 'http://localhost:8378/1'; | ||
|
||
const headers = { | ||
'Content-Type': 'application/json', | ||
'X-Parse-Application-Id': 'test', | ||
'X-Parse-REST-API-Key': 'rest', | ||
'X-Parse-Installation-Id': 'yolo', | ||
}; | ||
|
||
beforeEach(() => { | ||
makeRequest = reconfigureServer({ | ||
appName: 'coolapp', | ||
publicServerURL: 'http://localhost:1337/1', | ||
emailAdapter: emailAdapter, | ||
passwordPolicy: { | ||
validatorPattern: re, | ||
doNotAllowUsername: true, | ||
maxPasswordHistory: 1, | ||
resetTokenValidityDuration: 0.5, // 0.5 second | ||
}, | ||
}).then(() => { | ||
const config = Config.get('test'); | ||
const user = new Parse.User(); | ||
user.setPassword('asdsweqwasas'); | ||
user.setUsername('test'); | ||
user.set('email', '[email protected]'); | ||
return user | ||
.signUp(null) | ||
.then(() => { | ||
// build history | ||
user.setPassword('aaaaaaaaaaaa'); | ||
return user.save(); | ||
}) | ||
.then(() => Parse.User.requestPasswordReset('[email protected]')) | ||
.then(() => | ||
config.database.adapter.find( | ||
'_User', | ||
{ fields: {} }, | ||
{ username: 'test' }, | ||
{ limit: 1 } | ||
) | ||
); | ||
}); | ||
}); | ||
|
||
it('Password reset failed due to password policy', done => { | ||
makeRequest.then(results => { | ||
req({ | ||
url: `${serverURL}/passwordReset`, | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify({ | ||
_method: 'POST', | ||
username: 'test', | ||
token: results[0]['_perishable_token'], | ||
new_password: 'zxcv', | ||
}), | ||
}).then( | ||
() => { | ||
fail('Expected to be failed'); | ||
done(); | ||
}, | ||
err => { | ||
// TODO: Parse.Error.VALIDATION_ERROR is generic, there should be another error code like Parse.Error.PASSWORD_POLICY_NOT_MEET | ||
expect(err.data.code).not.toBe(undefined); | ||
expect(err.data.code).toBe(Parse.Error.VALIDATION_ERROR); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
it('Password reset failed due to invalid token', done => { | ||
makeRequest.then(results => { | ||
req({ | ||
url: `${serverURL}/passwordReset`, | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify({ | ||
_method: 'POST', | ||
username: 'test', | ||
token: results[0]['_perishable_token'] + 'invalid', | ||
new_password: 'zxcv', | ||
}), | ||
}).then( | ||
() => { | ||
fail('Expected to be failed'); | ||
done(); | ||
}, | ||
err => { | ||
// TODO: Missing Parse.Error code, only string message, there should be an error code like Parse.Error.RESET_PASSWORD_ERROR | ||
expect(err.data.code).not.toBe(undefined); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
it('Password reset failed due to password is repeated', done => { | ||
makeRequest.then(results => { | ||
req({ | ||
url: `${serverURL}/passwordReset`, | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify({ | ||
_method: 'POST', | ||
username: 'test', | ||
token: results[0]['_perishable_token'], | ||
new_password: 'aaaaaaaaaaaa', | ||
}), | ||
}).then( | ||
() => { | ||
fail('Expected to be failed'); | ||
done(); | ||
}, | ||
err => { | ||
// TODO: Parse.Error.VALIDATION_ERROR is generic, there should be another error code like Parse.Error.PASSWORD_POLICY_REPEAT | ||
expect(err.data.code).not.toBe(undefined); | ||
expect(err.data.code).toBe(Parse.Error.VALIDATION_ERROR); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
it('Password reset failed due to it contains username', done => { | ||
makeRequest.then(results => { | ||
req({ | ||
url: `${serverURL}/passwordReset`, | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify({ | ||
_method: 'POST', | ||
username: 'test', | ||
token: results[0]['_perishable_token'], | ||
new_password: 'asdsweqwasastest', | ||
}), | ||
}).then( | ||
() => { | ||
fail('Expected to be failed'); | ||
done(); | ||
}, | ||
err => { | ||
// TODO: Parse.Error.VALIDATION_ERROR is generic, there should be another error code like Parse.Error.PASSWORD_POLICY_USERNAME | ||
expect(err.data.code).not.toBe(undefined); | ||
expect(err.data.code).toBe(Parse.Error.VALIDATION_ERROR); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
it('Password reset username not found', done => { | ||
makeRequest.then(results => { | ||
req({ | ||
url: `${serverURL}/passwordReset`, | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify({ | ||
_method: 'POST', | ||
username: 'test1', | ||
token: results[0]['_perishable_token'], | ||
new_password: 'asdsweqwasastest', | ||
}), | ||
}).then( | ||
() => { | ||
fail('Expected to be failed'); | ||
done(); | ||
}, | ||
err => { | ||
// TODO: Missing Parse.Error code, only string message, there should be an error code like Parse.Error.USERNAME_NOT_FOUND | ||
expect(err.data.code).not.toBe(undefined); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
it('Password reset failed due to link has expired', done => { | ||
makeRequest | ||
.then(results => { | ||
// wait for a bit more than the validity duration set | ||
setTimeout(() => { | ||
expect(sendEmailOptions).not.toBeUndefined(); | ||
|
||
req({ | ||
url: `${serverURL}/passwordReset`, | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify({ | ||
_method: 'POST', | ||
username: 'test', | ||
token: results[0]['_perishable_token'], | ||
new_password: 'asdsweqwasas', | ||
}), | ||
}) | ||
.then(() => { | ||
fail('Expected to be failed'); | ||
done(); | ||
}) | ||
.catch(error => { | ||
// TODO: Missing Parse.Error code, only string message, there should be an error code like Parse.Error.RESET_LINK_EXPIRED | ||
expect(error.data.code).not.toBe(undefined); | ||
expect(error.data.code).toBe(Parse.Error.RESET_LINK_EXPIRED); | ||
}); | ||
done(); | ||
}, 1000); | ||
}) | ||
.catch(err => { | ||
jfail(err); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Password successfully reset', done => { | ||
makeRequest.then(results => { | ||
req({ | ||
url: `${serverURL}/passwordReset`, | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify({ | ||
_method: 'POST', | ||
username: 'test', | ||
token: results[0]['_perishable_token'], | ||
new_password: 'asdsweqwasas', | ||
}), | ||
}).then( | ||
res => { | ||
expect(res.status).toBe(200); | ||
done(); | ||
}, | ||
() => { | ||
fail('Expected to not fail'); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.