Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
156 changes: 156 additions & 0 deletions spec/Users.authdata.spec.js
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);
});
});
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