-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
fix(auth): Treat authData[provider]=null as unlink; skip provider validation for unlink #9856
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
SNtGog
wants to merge
4
commits into
parse-community:alpha
Choose a base branch
from
SNtGog:refactor-restwrite-authdata-handling
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.
+76
−1
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7669bd9
Optimize authData handling logic and integrate delta-based updates
SNtGog 7db1f8e
Revert "Optimize authData handling logic and integrate delta-based up…
SNtGog 2979264
Fix authData handling to exclude unlinked providers and add tests for…
SNtGog 4e3ac64
remove redundant tests
SNtGog 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 |
---|---|---|
@@ -0,0 +1,156 @@ | ||
describe('RestWrite.handleAuthData', () => { | ||
const MOCK_USER_ID = 'mockUserId'; | ||
const MOCK_ACCESS_TOKEN = 'mockAccessToken123'; | ||
|
||
const createMockUser = () => ({ | ||
id: MOCK_USER_ID, | ||
code: 'C1', | ||
}); | ||
|
||
const mockGooglePlayGamesAPI = () => { | ||
mockFetch([ | ||
{ | ||
url: 'https://oauth2.googleapis.com/token', | ||
method: 'POST', | ||
response: { | ||
ok: true, | ||
json: () => Promise.resolve({ access_token: MOCK_ACCESS_TOKEN }), | ||
}, | ||
}, | ||
{ | ||
url: `https://www.googleapis.com/games/v1/players/${MOCK_USER_ID}`, | ||
method: 'GET', | ||
response: { | ||
ok: true, | ||
json: () => Promise.resolve({ playerId: MOCK_USER_ID }), | ||
}, | ||
}, | ||
]); | ||
}; | ||
|
||
const setupAuthConfig = (additionalProviders = {}) => { | ||
return reconfigureServer({ | ||
auth: { | ||
gpgames: { | ||
clientId: 'validClientId', | ||
clientSecret: 'validClientSecret', | ||
}, | ||
someAdapter1: { | ||
validateAuthData: () => Promise.resolve(), | ||
validateAppId: () => Promise.resolve(), | ||
validateOptions: () => {}, | ||
}, | ||
someAdapter2: { | ||
validateAuthData: () => Promise.resolve(), | ||
validateAppId: () => Promise.resolve(), | ||
validateOptions: () => {}, | ||
}, | ||
...additionalProviders, | ||
}, | ||
}); | ||
}; | ||
|
||
beforeEach(async () => { | ||
await setupAuthConfig(); | ||
}); | ||
|
||
it('should handle multiple providers correctly', async () => { | ||
mockGooglePlayGamesAPI(); | ||
|
||
const authData = { | ||
gpgames: { id: MOCK_USER_ID, code: 'C4' }, | ||
someAdapter2: { id: 'F1', access_token: 'fb_token' }, | ||
}; | ||
|
||
const user = new Parse.User(); | ||
user.set('authData', authData); | ||
await user.save(); | ||
|
||
const sessionToken = user.getSessionToken(); | ||
|
||
await user.fetch({ sessionToken }); | ||
const currentAuthData = user.get('authData') || {}; | ||
|
||
user.set('authData', { | ||
someAdapter2: currentAuthData.someAdapter2, | ||
someAdapter1: { id: 'T2', access_token: 'tw_token' }, | ||
gpgames: null, // Unlink Google Play Games | ||
}); | ||
await user.save(null, { sessionToken }); | ||
|
||
const updatedUser = await new Parse.Query(Parse.User).get(user.id, { useMasterKey: true }); | ||
const finalAuthData = updatedUser.get('authData') || {}; | ||
|
||
expect(finalAuthData.gpgames).toBeUndefined(); | ||
expect(finalAuthData.someAdapter2?.id).toBe('F1'); | ||
expect(finalAuthData.someAdapter1?.id).toBe('T2'); | ||
}); | ||
|
||
it('should unlink provider via null', async () => { | ||
mockGooglePlayGamesAPI(); | ||
|
||
const authData = createMockUser(); | ||
const user = await Parse.User.logInWith('gpgames', { authData }); | ||
const sessionToken = user.getSessionToken(); | ||
|
||
await user.fetch({ sessionToken }); | ||
const currentAuthData = user.get('authData') || {}; | ||
|
||
user.set('authData', { | ||
...currentAuthData, | ||
gpgames: null, | ||
}); | ||
await user.save(null, { sessionToken }); | ||
|
||
const updatedUser = await new Parse.Query(Parse.User).get(user.id, { useMasterKey: true }); | ||
const finalAuthData = updatedUser.get('authData') || {}; | ||
|
||
expect(finalAuthData.gpgames).toBeUndefined(); | ||
}); | ||
|
||
it('should handle empty authData gracefully', async () => { | ||
mockGooglePlayGamesAPI(); | ||
|
||
const user = await Parse.User.signUp('test', 'password123'); | ||
|
||
const sessionToken = user.getSessionToken(); | ||
await user.fetch({ sessionToken }); | ||
|
||
user.set('authData', { | ||
someAdapter1: { id: 'T3', access_token: 'token456' }, | ||
}); | ||
await user.save(null, { sessionToken }); | ||
|
||
const updatedUser = await new Parse.Query(Parse.User).get(user.id, { useMasterKey: true }); | ||
const finalAuthData = updatedUser.get('authData'); | ||
|
||
expect(finalAuthData).toBeDefined(); | ||
expect(finalAuthData.someAdapter1?.id).toBe('T3'); | ||
}); | ||
|
||
it('should handle partial provider data updates correctly', async () => { | ||
mockGooglePlayGamesAPI(); | ||
|
||
const authData = createMockUser(); | ||
const user = await Parse.User.logInWith('gpgames', { authData }); | ||
|
||
const sessionToken = user.getSessionToken(); | ||
|
||
await user.fetch({ sessionToken }); | ||
|
||
const currentAuthData = user.get('authData') || {}; | ||
user.set('authData', { | ||
...currentAuthData, | ||
gpgames: { | ||
...currentAuthData.gpgames, | ||
code: 'new', | ||
}, | ||
}); | ||
await user.save(null, { sessionToken }); | ||
|
||
const updatedUser = await new Parse.Query(Parse.User).get(user.id, { useMasterKey: true }); | ||
const finalAuthData = updatedUser.get('authData'); | ||
|
||
expect(finalAuthData.gpgames.id).toBe(MOCK_USER_ID); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: using dedicated tools and functional prog to perform ops on objects, may be you will learn something
const authDataWithoutNullish = Object.fromEntries(Object.entries(authData).filter([_, data] => data ?? false))
When you want to perform filtering on objects, combining Object.fromEntries + Object.entries + .filter() works well (in case of functional programming)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.fromEntries combined with Object.entries is underrated: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries