|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { renderHook, act } from '@testing-library/react-hooks'; |
| 6 | +import { useOAuthFlowRecovery } from '.'; |
| 7 | +import firefox from '../../channels/firefox'; |
| 8 | +import * as ReactUtils from 'fxa-react/lib/utils'; |
| 9 | +import { |
| 10 | + IntegrationType, |
| 11 | + isProbablyFirefox, |
| 12 | + isOAuthNativeIntegration, |
| 13 | +} from '../../../models'; |
| 14 | + |
| 15 | +jest.mock('../../channels/firefox', () => ({ |
| 16 | + __esModule: true, |
| 17 | + default: { |
| 18 | + fxaOAuthFlowBegin: jest.fn(), |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +jest.mock('../../../models', () => { |
| 23 | + const actual = jest.requireActual('../../../models'); |
| 24 | + return { |
| 25 | + ...actual, |
| 26 | + isProbablyFirefox: jest.fn(() => true), |
| 27 | + isOAuthNativeIntegration: jest.fn(() => true), |
| 28 | + }; |
| 29 | +}); |
| 30 | + |
| 31 | +const mockIntegration = (isOAuthNative: boolean = true) => { |
| 32 | + (isOAuthNativeIntegration as unknown as jest.Mock).mockReturnValue( |
| 33 | + isOAuthNative |
| 34 | + ); |
| 35 | + return { |
| 36 | + type: IntegrationType.OAuthNative, |
| 37 | + getPermissions: jest |
| 38 | + .fn() |
| 39 | + .mockReturnValue(['profile', 'https://identity.mozilla.com/apps/oldsync']), |
| 40 | + }; |
| 41 | +}; |
| 42 | + |
| 43 | +describe('useOAuthFlowRecovery', () => { |
| 44 | + let hardNavigateSpy: jest.SpyInstance; |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + jest.clearAllMocks(); |
| 48 | + (isProbablyFirefox as unknown as jest.Mock).mockReturnValue(true); |
| 49 | + (isOAuthNativeIntegration as unknown as jest.Mock).mockReturnValue(true); |
| 50 | + hardNavigateSpy = jest |
| 51 | + .spyOn(ReactUtils, 'hardNavigate') |
| 52 | + .mockImplementation(() => {}); |
| 53 | + |
| 54 | + Object.defineProperty(window, 'location', { |
| 55 | + value: { search: '?flowId=abc123&utm_source=firefox' }, |
| 56 | + writable: true, |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + hardNavigateSpy.mockRestore(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('skips recovery for non-OAuth Native integrations', async () => { |
| 65 | + const integration = mockIntegration(false); |
| 66 | + const { result } = renderHook(() => |
| 67 | + useOAuthFlowRecovery(integration as any) |
| 68 | + ); |
| 69 | + |
| 70 | + let response: any; |
| 71 | + await act(async () => { |
| 72 | + response = await result.current.attemptOAuthFlowRecovery(); |
| 73 | + }); |
| 74 | + |
| 75 | + expect(response.success).toBe(false); |
| 76 | + expect(firefox.fxaOAuthFlowBegin).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('skips recovery for non-Firefox browsers', async () => { |
| 80 | + (isProbablyFirefox as unknown as jest.Mock).mockReturnValue(false); |
| 81 | + const integration = mockIntegration(); |
| 82 | + const { result } = renderHook(() => |
| 83 | + useOAuthFlowRecovery(integration as any) |
| 84 | + ); |
| 85 | + |
| 86 | + let response: any; |
| 87 | + await act(async () => { |
| 88 | + response = await result.current.attemptOAuthFlowRecovery(); |
| 89 | + }); |
| 90 | + |
| 91 | + expect(response.success).toBe(false); |
| 92 | + expect(firefox.fxaOAuthFlowBegin).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('navigates to /signin with fresh OAuth params on success', async () => { |
| 96 | + (firefox.fxaOAuthFlowBegin as jest.Mock).mockResolvedValue({ |
| 97 | + client_id: 'new-client-id', |
| 98 | + state: 'new-state', |
| 99 | + scope: 'profile https://identity.mozilla.com/apps/oldsync', |
| 100 | + access_type: 'offline', |
| 101 | + action: 'signin', |
| 102 | + code_challenge: 'pkce-challenge', |
| 103 | + code_challenge_method: 'S256', |
| 104 | + }); |
| 105 | + |
| 106 | + const integration = mockIntegration(); |
| 107 | + const { result } = renderHook(() => |
| 108 | + useOAuthFlowRecovery(integration as any) |
| 109 | + ); |
| 110 | + |
| 111 | + let response: any; |
| 112 | + await act(async () => { |
| 113 | + response = await result.current.attemptOAuthFlowRecovery(); |
| 114 | + }); |
| 115 | + |
| 116 | + expect(response.success).toBe(true); |
| 117 | + const url = hardNavigateSpy.mock.calls[0][0]; |
| 118 | + expect(url).toContain('/signin?'); |
| 119 | + expect(url).toContain('client_id=new-client-id'); |
| 120 | + expect(url).toContain('state=new-state'); |
| 121 | + expect(url).toContain('context=oauth_webchannel_v1'); |
| 122 | + expect(url).toContain('flowId=abc123'); // preserved |
| 123 | + expect(url).toContain('utm_source=firefox'); // preserved |
| 124 | + }); |
| 125 | + |
| 126 | + it('sets recoveryFailed when fxaOAuthFlowBegin returns null', async () => { |
| 127 | + (firefox.fxaOAuthFlowBegin as jest.Mock).mockResolvedValue(null); |
| 128 | + |
| 129 | + const integration = mockIntegration(); |
| 130 | + const { result } = renderHook(() => |
| 131 | + useOAuthFlowRecovery(integration as any) |
| 132 | + ); |
| 133 | + |
| 134 | + expect(result.current.recoveryFailed).toBe(false); |
| 135 | + |
| 136 | + await act(async () => { |
| 137 | + await result.current.attemptOAuthFlowRecovery(); |
| 138 | + }); |
| 139 | + |
| 140 | + expect(result.current.recoveryFailed).toBe(true); |
| 141 | + expect(hardNavigateSpy).not.toHaveBeenCalled(); |
| 142 | + }); |
| 143 | + |
| 144 | + it('sets recoveryFailed when fxaOAuthFlowBegin throws', async () => { |
| 145 | + (firefox.fxaOAuthFlowBegin as jest.Mock).mockRejectedValue( |
| 146 | + new Error('WebChannel error') |
| 147 | + ); |
| 148 | + |
| 149 | + const integration = mockIntegration(); |
| 150 | + const { result } = renderHook(() => |
| 151 | + useOAuthFlowRecovery(integration as any) |
| 152 | + ); |
| 153 | + |
| 154 | + let response: any; |
| 155 | + await act(async () => { |
| 156 | + response = await result.current.attemptOAuthFlowRecovery(); |
| 157 | + }); |
| 158 | + |
| 159 | + expect(response.success).toBe(false); |
| 160 | + expect(response.error).toBeDefined(); |
| 161 | + expect(result.current.recoveryFailed).toBe(true); |
| 162 | + }); |
| 163 | + |
| 164 | + it('uses fallback scopes when getPermissions throws', async () => { |
| 165 | + (firefox.fxaOAuthFlowBegin as jest.Mock).mockResolvedValue(null); |
| 166 | + |
| 167 | + const integration = { |
| 168 | + type: IntegrationType.OAuthNative, |
| 169 | + getPermissions: jest.fn().mockImplementation(() => { |
| 170 | + throw new Error('No permissions'); |
| 171 | + }), |
| 172 | + }; |
| 173 | + |
| 174 | + const { result } = renderHook(() => |
| 175 | + useOAuthFlowRecovery(integration as any) |
| 176 | + ); |
| 177 | + |
| 178 | + await act(async () => { |
| 179 | + await result.current.attemptOAuthFlowRecovery(); |
| 180 | + }); |
| 181 | + |
| 182 | + expect(firefox.fxaOAuthFlowBegin).toHaveBeenCalledWith([ |
| 183 | + 'profile', |
| 184 | + 'https://identity.mozilla.com/apps/oldsync', |
| 185 | + ]); |
| 186 | + }); |
| 187 | +}); |
0 commit comments