diff --git a/app/lib/services/connect.ios.test.ts b/app/lib/services/connect.ios.test.ts index f09cf1778c..3a265cad26 100644 --- a/app/lib/services/connect.ios.test.ts +++ b/app/lib/services/connect.ios.test.ts @@ -1,4 +1,5 @@ -import { determineAuthType } from './connect'; +import { determineAuthType, getLoginServices } from './connect'; +import { setLoginServices } from '../../actions/login'; jest.mock('./voip/MediaSessionInstance', () => ({ mediaSessionInstance: { reset: jest.fn() } @@ -9,6 +10,14 @@ jest.mock('../methods/helpers/deviceInfo', () => ({ isIOS: true })); +const mockStoreDispatch = jest.fn(); +jest.mock('../store/auxStore', () => ({ + store: { + dispatch: (action: unknown) => mockStoreDispatch(action), + getState: jest.fn() + } +})); + interface IServices { [index: string]: string | boolean; name: string; @@ -143,4 +152,90 @@ describe('determineAuthType - iOS specific tests', () => { expect(result).toBe('not_supported'); }); }); + + describe('getLoginServices', () => { + const originalFetch = global.fetch; + + afterEach(() => { + global.fetch = originalFetch; + }); + + it('should correctly map Apple OAuth with service name "apple" instead of buttonLabelText', async () => { + global.fetch = jest.fn().mockResolvedValue({ + json: jest.fn().mockResolvedValue({ + success: true, + services: [ + { + _id: 'Accounts_OAuth_Apple', + service: 'apple', + buttonLabelText: 'Sign in with Apple', + buttonColor: '#000', + buttonLabelColor: '#FFF', + custom: false + } + ] + }) + }) as any; + + await getLoginServices('https://open.rocket.chat'); + + expect(mockStoreDispatch).toHaveBeenCalledWith( + setLoginServices({ + apple: { + _id: 'Accounts_OAuth_Apple', + service: 'apple', + buttonLabelText: 'Sign in with Apple', + buttonColor: '#000', + buttonLabelColor: '#FFF', + custom: false, + name: 'apple', + authType: 'apple' + } + }) + ); + }); + + it('should correctly map standard OAuth providers', async () => { + global.fetch = jest.fn().mockResolvedValue({ + json: jest.fn().mockResolvedValue({ + success: true, + services: [ + { + _id: 'Accounts_OAuth_Google', + service: 'google', + buttonLabelText: '', + custom: false + } + ] + }) + }) as any; + + await getLoginServices('https://open.rocket.chat'); + + expect(mockStoreDispatch).toHaveBeenCalledWith( + setLoginServices({ + google: { + _id: 'Accounts_OAuth_Google', + service: 'google', + buttonLabelText: '', + custom: false, + name: 'google', + authType: 'oauth' + } + }) + ); + }); + + it('should dispatch empty object when server returns no services or failure', async () => { + global.fetch = jest.fn().mockResolvedValue({ + json: jest.fn().mockResolvedValue({ + success: false + }) + }) as any; + + await getLoginServices('https://open.rocket.chat'); + + expect(mockStoreDispatch).toHaveBeenCalledWith(setLoginServices({})); + }); + }); }); diff --git a/app/lib/services/connect.test.ts b/app/lib/services/connect.test.ts index ff0f4cbca7..5b8984bd3a 100644 --- a/app/lib/services/connect.test.ts +++ b/app/lib/services/connect.test.ts @@ -1,7 +1,7 @@ import { connect, determineAuthType, disconnect, login, loginTOTP } from './connect'; import { mediaSessionInstance } from './voip/MediaSessionInstance'; import { pendingHangups } from './voip/pendingHangups'; -import { setUser } from '../../actions/login'; +import { setLoginServices, setUser } from '../../actions/login'; import database from '../database'; jest.mock('./voip/MediaSessionInstance', () => ({ @@ -641,6 +641,49 @@ describe('connect — stream-notify-logged updateAvatar', () => { }); }); +describe('getLoginServices (non-iOS)', () => { + const originalFetch = global.fetch; + + afterEach(() => { + global.fetch = originalFetch; + }); + + it('should filter out Apple authentication on non-iOS devices', async () => { + global.fetch = jest.fn().mockResolvedValue({ + json: jest.fn().mockResolvedValue({ + success: true, + services: [ + { + _id: 'Accounts_OAuth_Apple', + service: 'apple', + buttonLabelText: 'Sign in with Apple', + custom: false + }, + { + _id: 'Accounts_OAuth_Google', + service: 'google', + custom: false + } + ] + }) + }) as any; + + await getLoginServices('https://open.rocket.chat'); + + expect(mockStoreDispatch).toHaveBeenCalledWith( + setLoginServices({ + google: { + _id: 'Accounts_OAuth_Google', + service: 'google', + custom: false, + name: 'google', + authType: 'oauth' + } + }) + ); + }); +}); + // Note: Apple authentication when isIOS is true is tested in connect.ios.test.ts describe('login', () => { diff --git a/app/lib/services/connect.ts b/app/lib/services/connect.ts index 3635b32c33..2b6e0fc056 100644 --- a/app/lib/services/connect.ts +++ b/app/lib/services/connect.ts @@ -457,7 +457,7 @@ async function getLoginServices(server: string) { loginServices = services; const loginServicesReducer = loginServices.reduce((ret: IServices[], item: IServices) => { - const name = item.name || item.buttonLabelText || item.service; + const name = item.name || item.service || item.buttonLabelText; const authType = determineAuthType(item); if (authType !== 'not_supported') {