|
| 1 | +import redirectToSignIn from '@libs/actions/SignInRedirect'; |
| 2 | +import * as NetworkStore from '@libs/Network/NetworkStore'; |
| 3 | +import reauthenticate from '@libs/Reauthentication'; |
| 4 | +import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates'; |
| 5 | + |
| 6 | +jest.mock('@src/libs/Log'); |
| 7 | + |
| 8 | +jest.mock('@src/libs/actions/SignInRedirect', () => ({ |
| 9 | + __esModule: true, |
| 10 | + default: jest.fn().mockResolvedValue(undefined), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('@src/libs/Network/NetworkStore', () => ({ |
| 14 | + getCredentials: jest.fn(), |
| 15 | + hasReadRequiredDataFromStorage: jest.fn(), |
| 16 | + setIsAuthenticating: jest.fn(), |
| 17 | + setAuthToken: jest.fn(), |
| 18 | + getAuthToken: jest.fn(), |
| 19 | + checkRequiredData: jest.fn(), |
| 20 | + resetHasReadRequiredDataFromStorage: jest.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +jest.mock('@src/libs/SessionUtils', () => ({ |
| 24 | + checkIfShouldUseNewPartnerName: jest.fn().mockReturnValue(true), |
| 25 | +})); |
| 26 | + |
| 27 | +jest.mock('@src/libs/telemetry/trackAuthenticationError', () => ({ |
| 28 | + __esModule: true, |
| 29 | + default: jest.fn(), |
| 30 | +})); |
| 31 | + |
| 32 | +const mockRedirectToSignIn = redirectToSignIn as jest.Mock; |
| 33 | +const mockGetCredentials = NetworkStore.getCredentials as jest.Mock; |
| 34 | +const mockHasReadRequiredDataFromStorage = NetworkStore.hasReadRequiredDataFromStorage as jest.Mock; |
| 35 | +const mockSetIsAuthenticating = NetworkStore.setIsAuthenticating as jest.Mock; |
| 36 | + |
| 37 | +beforeEach(() => { |
| 38 | + jest.clearAllMocks(); |
| 39 | + mockHasReadRequiredDataFromStorage.mockResolvedValue(undefined); |
| 40 | +}); |
| 41 | + |
| 42 | +describe('reauthenticate', () => { |
| 43 | + describe('when credentials are missing', () => { |
| 44 | + it('redirects to sign in when credentials are null', async () => { |
| 45 | + mockGetCredentials.mockReturnValue(null); |
| 46 | + |
| 47 | + const result = await reauthenticate('TestCommand'); |
| 48 | + await waitForBatchedUpdates(); |
| 49 | + |
| 50 | + expect(mockRedirectToSignIn).toHaveBeenCalledWith('No credentials available'); |
| 51 | + expect(result).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it('redirects to sign in when credentials are empty object', async () => { |
| 55 | + mockGetCredentials.mockReturnValue({}); |
| 56 | + |
| 57 | + const result = await reauthenticate('TestCommand'); |
| 58 | + await waitForBatchedUpdates(); |
| 59 | + |
| 60 | + expect(mockRedirectToSignIn).toHaveBeenCalledWith('No credentials available'); |
| 61 | + expect(result).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it('redirects to sign in when only autoGeneratedLogin is present', async () => { |
| 65 | + mockGetCredentials.mockReturnValue({autoGeneratedLogin: 'test-login'}); |
| 66 | + |
| 67 | + const result = await reauthenticate('TestCommand'); |
| 68 | + await waitForBatchedUpdates(); |
| 69 | + |
| 70 | + expect(mockRedirectToSignIn).toHaveBeenCalledWith('No credentials available'); |
| 71 | + expect(result).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('redirects to sign in when only autoGeneratedPassword is present', async () => { |
| 75 | + mockGetCredentials.mockReturnValue({autoGeneratedPassword: 'test-password'}); |
| 76 | + |
| 77 | + const result = await reauthenticate('TestCommand'); |
| 78 | + await waitForBatchedUpdates(); |
| 79 | + |
| 80 | + expect(mockRedirectToSignIn).toHaveBeenCalledWith('No credentials available'); |
| 81 | + expect(result).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it('stops the authenticating state before redirecting', async () => { |
| 85 | + mockGetCredentials.mockReturnValue(null); |
| 86 | + |
| 87 | + await reauthenticate('TestCommand'); |
| 88 | + await waitForBatchedUpdates(); |
| 89 | + |
| 90 | + // setIsAuthenticating(false) should be called to release the network pause |
| 91 | + expect(mockSetIsAuthenticating).toHaveBeenCalledWith(false); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments