Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions spec/RestWrite.handleAuthData.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 = () => {
return reconfigureServer({
auth: {
gpgames: {
clientId: 'validClientId',
clientSecret: 'validClientSecret',
}
},
});
};

beforeEach(async () => {
await setupAuthConfig();
});

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') || {};
Copy link
Member

@Moumouls Moumouls Sep 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: You should not fall back to "{}". In this test, you expect to receive authData. Otherwise, if authData becomes protected in the future, the test could produce false positives.

Additionally, you need to set another authData in this test to ensure that:

  • gpgames is correctly removed without deleting all authData.
  • You can then expect that the other authData entries remain unchanged and that the null operation was correctly performed.


expect(finalAuthData.gpgames).toBeUndefined();
});
});
10 changes: 9 additions & 1 deletion src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,15 @@ RestWrite.prototype.ensureUniqueAuthDataId = async function () {
};

RestWrite.prototype.handleAuthData = async function (authData) {
const r = await Auth.findUsersWithAuthData(this.config, authData, true);
const withoutUnlinked = {};
for (const provider of Object.keys(authData)) {
if (authData[provider] === null || authData[provider] === undefined) {
continue;
}
withoutUnlinked[provider] = authData[provider];
}
Comment on lines +542 to +547
Copy link
Member

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)

Copy link
Member

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


const r = await Auth.findUsersWithAuthData(this.config, withoutUnlinked, true);
const results = this.filteredObjectsByACL(r);

const userId = this.getUserId();
Expand Down