-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
feat: Add beforePasswordResetRequest hook
#9906
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
Open
coratgerl
wants to merge
10
commits into
parse-community:alpha
Choose a base branch
from
coratgerl:before-password-reset-request
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.
+292
−10
Open
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c354da3
feat: add beforePasswordResetRequest hook
coratgerl e30572d
fix: feedbacks
coratgerl 2971acf
Merge branch 'alpha' into before-password-reset-request
mtrezza 8076a65
fix: test placement missing
coratgerl 659ab5e
fix: test format
coratgerl fce3723
Merge branch 'alpha' into before-password-reset-request
mtrezza 2045d87
Merge branch 'alpha' into before-password-reset-request
mtrezza 8bfc13b
Merge branch 'alpha' into before-password-reset-request
mtrezza 39f0e47
Merge branch 'alpha' into before-password-reset-request
mtrezza 73aa8dd
Merge branch 'alpha' into before-password-reset-request
mtrezza 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
|
|
@@ -3307,19 +3307,19 @@ | |
| }).not.toThrow('Only the _User class is allowed for the beforeLogin and afterLogin triggers'); | ||
| expect(() => { | ||
| Parse.Cloud.beforeLogin('SomeClass', () => { }); | ||
| }).toThrow('Only the _User class is allowed for the beforeLogin and afterLogin triggers'); | ||
| }).toThrow('Only the _User class is allowed for the beforeLogin, afterLogin, and beforePasswordResetRequest triggers'); | ||
| expect(() => { | ||
| Parse.Cloud.afterLogin(() => { }); | ||
| }).not.toThrow('Only the _User class is allowed for the beforeLogin and afterLogin triggers'); | ||
| }).not.toThrow('Only the _User class is allowed for the beforeLogin, afterLogin, and beforePasswordResetRequest triggers'); | ||
| expect(() => { | ||
| Parse.Cloud.afterLogin('_User', () => { }); | ||
| }).not.toThrow('Only the _User class is allowed for the beforeLogin and afterLogin triggers'); | ||
| }).not.toThrow('Only the _User class is allowed for the beforeLogin, afterLogin, and beforePasswordResetRequest triggers'); | ||
| expect(() => { | ||
| Parse.Cloud.afterLogin(Parse.User, () => { }); | ||
| }).not.toThrow('Only the _User class is allowed for the beforeLogin and afterLogin triggers'); | ||
| }).not.toThrow('Only the _User class is allowed for the beforeLogin, afterLogin, and beforePasswordResetRequest triggers'); | ||
| expect(() => { | ||
| Parse.Cloud.afterLogin('SomeClass', () => { }); | ||
| }).toThrow('Only the _User class is allowed for the beforeLogin and afterLogin triggers'); | ||
| }).toThrow('Only the _User class is allowed for the beforeLogin, afterLogin, and beforePasswordResetRequest triggers'); | ||
| expect(() => { | ||
| Parse.Cloud.afterLogout(() => { }); | ||
| }).not.toThrow(); | ||
|
|
@@ -3620,6 +3620,59 @@ | |
| expect(calledBefore).toBe(true); | ||
| expect(calledAfter).toBe(true); | ||
| }); | ||
| it('afterFind should not be triggered when saving an object', async () => { | ||
| let beforeSaves = 0; | ||
| Parse.Cloud.beforeSave('SavingTest', () => { | ||
| beforeSaves++; | ||
| }); | ||
|
|
||
| let afterSaves = 0; | ||
| Parse.Cloud.afterSave('SavingTest', () => { | ||
| afterSaves++; | ||
| }); | ||
|
|
||
| let beforeFinds = 0; | ||
| Parse.Cloud.beforeFind('SavingTest', () => { | ||
| beforeFinds++; | ||
| }); | ||
|
|
||
| let afterFinds = 0; | ||
| Parse.Cloud.afterFind('SavingTest', () => { | ||
| afterFinds++; | ||
| }); | ||
|
|
||
| const obj = new Parse.Object('SavingTest'); | ||
| obj.set('someField', 'some value 1'); | ||
| await obj.save(); | ||
|
|
||
| expect(beforeSaves).toEqual(1); | ||
| expect(afterSaves).toEqual(1); | ||
| expect(beforeFinds).toEqual(0); | ||
| expect(afterFinds).toEqual(0); | ||
|
|
||
| obj.set('someField', 'some value 2'); | ||
| await obj.save(); | ||
|
|
||
| expect(beforeSaves).toEqual(2); | ||
| expect(afterSaves).toEqual(2); | ||
| expect(beforeFinds).toEqual(0); | ||
| expect(afterFinds).toEqual(0); | ||
|
|
||
| await obj.fetch(); | ||
|
|
||
| expect(beforeSaves).toEqual(2); | ||
| expect(afterSaves).toEqual(2); | ||
| expect(beforeFinds).toEqual(1); | ||
| expect(afterFinds).toEqual(1); | ||
|
|
||
| obj.set('someField', 'some value 3'); | ||
| await obj.save(); | ||
|
|
||
| expect(beforeSaves).toEqual(3); | ||
| expect(afterSaves).toEqual(3); | ||
| expect(beforeFinds).toEqual(1); | ||
| expect(afterFinds).toEqual(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('beforeLogin hook', () => { | ||
|
|
@@ -3777,62 +3830,208 @@ | |
| await Parse.User.logIn('tupac', 'shakur'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('afterFind should not be triggered when saving an object', async () => { | ||
| let beforeSaves = 0; | ||
| Parse.Cloud.beforeSave('SavingTest', () => { | ||
| beforeSaves++; | ||
| describe('beforePasswordResetRequest hook', () => { | ||
| it('should run beforePasswordResetRequest with valid user', async done => { | ||
| let hit = 0; | ||
| let sendPasswordResetEmailCalled = false; | ||
| const emailAdapter = { | ||
| sendVerificationEmail: () => Promise.resolve(), | ||
| sendPasswordResetEmail: () => { | ||
| sendPasswordResetEmailCalled = true; | ||
| }, | ||
| sendMail: () => {}, | ||
| }; | ||
|
|
||
| await reconfigureServer({ | ||
| appName: 'test', | ||
| emailAdapter: emailAdapter, | ||
| publicServerURL: 'http://localhost:8378/1', | ||
| }); | ||
|
|
||
| let afterSaves = 0; | ||
| Parse.Cloud.afterSave('SavingTest', () => { | ||
| afterSaves++; | ||
| Parse.Cloud.beforePasswordResetRequest(req => { | ||
| hit++; | ||
| expect(req.object).toBeDefined(); | ||
| expect(req.object.get('email')).toEqual('[email protected]'); | ||
| expect(req.object.get('username')).toEqual('testuser'); | ||
| }); | ||
|
|
||
| let beforeFinds = 0; | ||
| Parse.Cloud.beforeFind('SavingTest', () => { | ||
| beforeFinds++; | ||
| const user = new Parse.User(); | ||
| user.setUsername('testuser'); | ||
| user.setPassword('password'); | ||
| user.set('email', '[email protected]'); | ||
| await user.signUp(); | ||
|
|
||
| await Parse.User.requestPasswordReset('[email protected]'); | ||
| expect(hit).toBe(1); | ||
| expect(sendPasswordResetEmailCalled).toBe(true); | ||
| done(); | ||
| }); | ||
|
|
||
| it('should be able to block password reset request if an error is thrown', async done => { | ||
| let hit = 0; | ||
| let sendPasswordResetEmailCalled = false; | ||
| const emailAdapter = { | ||
| sendVerificationEmail: () => Promise.resolve(), | ||
| sendPasswordResetEmail: () => { | ||
| sendPasswordResetEmailCalled = true; | ||
| }, | ||
| sendMail: () => {}, | ||
| }; | ||
|
|
||
| await reconfigureServer({ | ||
| appName: 'test', | ||
| emailAdapter: emailAdapter, | ||
| publicServerURL: 'http://localhost:8378/1', | ||
| }); | ||
|
|
||
| let afterFinds = 0; | ||
| Parse.Cloud.afterFind('SavingTest', () => { | ||
| afterFinds++; | ||
| Parse.Cloud.beforePasswordResetRequest(req => { | ||
| hit++; | ||
| if (req.object.get('isBanned')) { | ||
| throw new Error('banned account'); | ||
| } | ||
| }); | ||
|
|
||
| const obj = new Parse.Object('SavingTest'); | ||
| obj.set('someField', 'some value 1'); | ||
| await obj.save(); | ||
| const user = new Parse.User(); | ||
| user.setUsername('banneduser'); | ||
| user.setPassword('password'); | ||
| user.set('email', '[email protected]'); | ||
| await user.signUp(); | ||
| await user.save({ isBanned: true }); | ||
|
|
||
| expect(beforeSaves).toEqual(1); | ||
| expect(afterSaves).toEqual(1); | ||
| expect(beforeFinds).toEqual(0); | ||
| expect(afterFinds).toEqual(0); | ||
| try { | ||
| await Parse.User.requestPasswordReset('[email protected]'); | ||
| throw new Error('should not have sent password reset email.'); | ||
| } catch (e) { | ||
| expect(e.message).toBe('banned account'); | ||
| } | ||
| expect(hit).toBe(1); | ||
| expect(sendPasswordResetEmailCalled).toBe(false); | ||
| done(); | ||
| }); | ||
|
|
||
| obj.set('someField', 'some value 2'); | ||
| await obj.save(); | ||
| it('should be able to block password reset request if an error is thrown even if the user has an attached file', async done => { | ||
| let hit = 0; | ||
| let sendPasswordResetEmailCalled = false; | ||
| const emailAdapter = { | ||
| sendVerificationEmail: () => Promise.resolve(), | ||
| sendPasswordResetEmail: () => { | ||
| sendPasswordResetEmailCalled = true; | ||
| }, | ||
| sendMail: () => {}, | ||
| }; | ||
|
|
||
| expect(beforeSaves).toEqual(2); | ||
| expect(afterSaves).toEqual(2); | ||
| expect(beforeFinds).toEqual(0); | ||
| expect(afterFinds).toEqual(0); | ||
| await reconfigureServer({ | ||
| appName: 'test', | ||
| emailAdapter: emailAdapter, | ||
| publicServerURL: 'http://localhost:8378/1', | ||
| }); | ||
|
|
||
| await obj.fetch(); | ||
| Parse.Cloud.beforePasswordResetRequest(req => { | ||
| hit++; | ||
| if (req.object.get('isBanned')) { | ||
| throw new Error('banned account'); | ||
| } | ||
| }); | ||
|
|
||
| expect(beforeSaves).toEqual(2); | ||
| expect(afterSaves).toEqual(2); | ||
| expect(beforeFinds).toEqual(1); | ||
| expect(afterFinds).toEqual(1); | ||
| const user = new Parse.User(); | ||
| user.setUsername('banneduser2'); | ||
| user.setPassword('password'); | ||
| user.set('email', '[email protected]'); | ||
| await user.signUp(); | ||
| const base64 = 'V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE='; | ||
| const file = new Parse.File('myfile.txt', { base64 }); | ||
| await file.save(); | ||
| await user.save({ isBanned: true, file }); | ||
|
|
||
| obj.set('someField', 'some value 3'); | ||
| await obj.save(); | ||
| try { | ||
| await Parse.User.requestPasswordReset('[email protected]'); | ||
| throw new Error('should not have sent password reset email.'); | ||
| } catch (e) { | ||
| expect(e.message).toBe('banned account'); | ||
| } | ||
| expect(hit).toBe(1); | ||
| expect(sendPasswordResetEmailCalled).toBe(false); | ||
| done(); | ||
| }); | ||
|
|
||
| expect(beforeSaves).toEqual(3); | ||
| expect(afterSaves).toEqual(3); | ||
| expect(beforeFinds).toEqual(1); | ||
| expect(afterFinds).toEqual(1); | ||
| it('should not run beforePasswordResetRequest if email does not exist', async done => { | ||
| let hit = 0; | ||
| const emailAdapter = { | ||
| sendVerificationEmail: () => Promise.resolve(), | ||
| sendPasswordResetEmail: () => {}, | ||
| sendMail: () => {}, | ||
| }; | ||
|
|
||
| await reconfigureServer({ | ||
| emailAdapter: emailAdapter, | ||
| publicServerURL: 'http://localhost:8378/1', | ||
| }); | ||
|
|
||
| Parse.Cloud.beforePasswordResetRequest(req => { | ||
| hit++; | ||
| }); | ||
|
|
||
| try { | ||
| await Parse.User.requestPasswordReset('[email protected]'); | ||
| } catch (e) { | ||
| // May or may not throw depending on passwordPolicy.resetPasswordSuccessOnInvalidEmail | ||
| } | ||
| expect(hit).toBe(0); | ||
| done(); | ||
| }); | ||
|
|
||
| it('should have expected data in request in beforePasswordResetRequest', async done => { | ||
| const emailAdapter = { | ||
| sendVerificationEmail: () => Promise.resolve(), | ||
| sendPasswordResetEmail: () => {}, | ||
| sendMail: () => {}, | ||
| }; | ||
|
|
||
| await reconfigureServer({ | ||
| appName: 'test', | ||
| emailAdapter: emailAdapter, | ||
| publicServerURL: 'http://localhost:8378/1', | ||
| }); | ||
|
|
||
| Parse.Cloud.beforePasswordResetRequest(req => { | ||
| expect(req.object).toBeDefined(); | ||
| expect(req.object.get('email')).toBeDefined(); | ||
| expect(req.headers).toBeDefined(); | ||
| expect(req.ip).toBeDefined(); | ||
| expect(req.installationId).toBeDefined(); | ||
| expect(req.context).toBeDefined(); | ||
| expect(req.config).toBeDefined(); | ||
| }); | ||
|
|
||
| const user = new Parse.User(); | ||
| user.setUsername('testuser2'); | ||
| user.setPassword('password'); | ||
| user.set('email', '[email protected]'); | ||
| await user.signUp(); | ||
| await Parse.User.requestPasswordReset('[email protected]'); | ||
| done(); | ||
| }); | ||
|
|
||
| it('should validate that only _User class is allowed for beforePasswordResetRequest', () => { | ||
| expect(() => { | ||
| Parse.Cloud.beforePasswordResetRequest('SomeClass', () => { }); | ||
| }).toThrow('Only the _User class is allowed for the beforeLogin, afterLogin, and beforePasswordResetRequest triggers'); | ||
| expect(() => { | ||
| Parse.Cloud.beforePasswordResetRequest(() => { }); | ||
| }).not.toThrow(); | ||
| expect(() => { | ||
| Parse.Cloud.beforePasswordResetRequest('_User', () => { }); | ||
| }).not.toThrow(); | ||
| expect(() => { | ||
| Parse.Cloud.beforePasswordResetRequest(Parse.User, () => { }); | ||
| }).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| describe('afterLogin hook', () => { | ||
| it('should run afterLogin after successful login', async done => { | ||
| let hit = 0; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.