|
| 1 | +import {render} from '@testing-library/react-native'; |
| 2 | +import {Str} from 'expensify-common'; |
| 3 | +import {Linking} from 'react-native'; |
| 4 | +import type {OnyxEntry} from 'react-native-onyx'; |
| 5 | +import Onyx from 'react-native-onyx'; |
| 6 | +// eslint-disable-next-line no-restricted-imports, no-restricted-syntax |
| 7 | +import * as AppActions from '@libs/actions/App'; |
| 8 | +import {hasAuthToken, signOutAndRedirectToSignIn} from '@libs/actions/Session'; |
| 9 | +// eslint-disable-next-line no-restricted-imports, no-restricted-syntax |
| 10 | +import * as Session from '@libs/actions/Session'; |
| 11 | +import {getCurrentUserEmail, setLastShortAuthToken} from '@libs/Network/NetworkStore'; |
| 12 | +import App from '@src/App'; |
| 13 | +import CONST from '@src/CONST'; |
| 14 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 15 | +import ROUTES from '@src/ROUTES'; |
| 16 | +import {createRandomReport} from '../utils/collections/reports'; |
| 17 | +import * as TestHelper from '../utils/TestHelper'; |
| 18 | +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; |
| 19 | +import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; |
| 20 | +import waitForNetworkPromises from '../utils/waitForNetworkPromises'; |
| 21 | + |
| 22 | +const TEST_USER_ACCOUNT_ID_1 = 123; |
| 23 | +const TEST_USER_LOGIN_1 = 'test@test.com'; |
| 24 | +// cspell:disable-next-line |
| 25 | +const TEST_AUTH_TOKEN_1 = 'asdfghjkl'; |
| 26 | + |
| 27 | +const TEST_USER_ACCOUNT_ID_2 = 456; |
| 28 | +const TEST_USER_LOGIN_2 = 'test2@test.com'; |
| 29 | +// cspell:disable-next-line |
| 30 | +const TEST_AUTH_TOKEN_2 = 'zxcvbnm'; |
| 31 | + |
| 32 | +jest.setTimeout(60000); |
| 33 | +TestHelper.setupApp(); |
| 34 | +TestHelper.setupGlobalFetchMock(); |
| 35 | + |
| 36 | +const report = createRandomReport(7); |
| 37 | + |
| 38 | +function getInitialURL() { |
| 39 | + const params = new URLSearchParams(); |
| 40 | + |
| 41 | + params.set('exitTo', `${ROUTES.REPORT}/${report.reportID}`); |
| 42 | + params.set('email', TEST_USER_LOGIN_1); |
| 43 | + params.set('shortLivedAuthToken', TEST_AUTH_TOKEN_1); |
| 44 | + |
| 45 | + const deeplinkUrl = `${CONST.DEEPLINK_BASE_URL}/transition?${params.toString()}`; |
| 46 | + return deeplinkUrl; |
| 47 | +} |
| 48 | + |
| 49 | +describe('Deep linking', () => { |
| 50 | + let lastVisitedPath: string | undefined; |
| 51 | + let originalSignInWithShortLivedAuthToken: typeof Session.signInWithShortLivedAuthToken; |
| 52 | + let originalOpenApp: typeof AppActions.openApp; |
| 53 | + |
| 54 | + beforeAll(() => { |
| 55 | + originalSignInWithShortLivedAuthToken = Session.signInWithShortLivedAuthToken; |
| 56 | + originalOpenApp = AppActions.openApp; |
| 57 | + }); |
| 58 | + |
| 59 | + beforeEach(() => { |
| 60 | + Onyx.connect({ |
| 61 | + key: ONYXKEYS.LAST_VISITED_PATH, |
| 62 | + callback: (val: OnyxEntry<string>) => (lastVisitedPath = val), |
| 63 | + }); |
| 64 | + |
| 65 | + jest.spyOn(Session, 'signInWithShortLivedAuthToken').mockImplementation(() => { |
| 66 | + Onyx.merge(ONYXKEYS.CREDENTIALS, { |
| 67 | + login: TEST_USER_LOGIN_1, |
| 68 | + autoGeneratedLogin: Str.guid('expensify.cash-'), |
| 69 | + autoGeneratedPassword: Str.guid(), |
| 70 | + }); |
| 71 | + Onyx.merge(ONYXKEYS.ACCOUNT, { |
| 72 | + validated: true, |
| 73 | + isUsingExpensifyCard: false, |
| 74 | + }); |
| 75 | + Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, { |
| 76 | + [TEST_USER_ACCOUNT_ID_1]: TestHelper.buildPersonalDetails(TEST_USER_LOGIN_1, TEST_USER_ACCOUNT_ID_1, 'Test'), |
| 77 | + }); |
| 78 | + Onyx.merge(ONYXKEYS.SESSION, { |
| 79 | + authToken: TEST_AUTH_TOKEN_1, |
| 80 | + accountID: TEST_USER_ACCOUNT_ID_1, |
| 81 | + email: TEST_USER_LOGIN_1, |
| 82 | + encryptedAuthToken: TEST_AUTH_TOKEN_1, |
| 83 | + }); |
| 84 | + Onyx.merge(ONYXKEYS.NVP_PRIVATE_PUSH_NOTIFICATION_ID, 'randomID'); |
| 85 | + |
| 86 | + return originalSignInWithShortLivedAuthToken(TEST_AUTH_TOKEN_1); |
| 87 | + }); |
| 88 | + |
| 89 | + jest.spyOn(AppActions, 'openApp').mockImplementation(async () => { |
| 90 | + const originalResult = await originalOpenApp(); |
| 91 | + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report); |
| 92 | + return originalResult; |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + afterEach(async () => { |
| 97 | + await Onyx.clear(); |
| 98 | + await waitForNetworkPromises(); |
| 99 | + jest.clearAllMocks(); |
| 100 | + lastVisitedPath = undefined; |
| 101 | + Linking.setInitialURL(''); |
| 102 | + setLastShortAuthToken(null); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should not remember the report path of the last deep link login after signing out and in again', async () => { |
| 106 | + expect(hasAuthToken()).toBe(false); |
| 107 | + |
| 108 | + const url = getInitialURL(); |
| 109 | + // User signs in automatically when the app is rendered because of the deep link |
| 110 | + Linking.setInitialURL(url); |
| 111 | + const {unmount} = render(<App />); |
| 112 | + |
| 113 | + await waitForBatchedUpdates(); |
| 114 | + |
| 115 | + expect(lastVisitedPath).toBe(`/${ROUTES.REPORT}/${report.reportID}`); |
| 116 | + |
| 117 | + expect(hasAuthToken()).toBe(true); |
| 118 | + |
| 119 | + signOutAndRedirectToSignIn(); |
| 120 | + |
| 121 | + await waitForBatchedUpdatesWithAct(); |
| 122 | + |
| 123 | + expect(hasAuthToken()).toBe(false); |
| 124 | + |
| 125 | + await TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID_2, TEST_USER_LOGIN_2, undefined, TEST_AUTH_TOKEN_2); |
| 126 | + |
| 127 | + await waitForBatchedUpdatesWithAct(); |
| 128 | + |
| 129 | + expect(lastVisitedPath).toBeDefined(); |
| 130 | + expect(lastVisitedPath).not.toBe(`/${ROUTES.REPORT}/${report.reportID}`); |
| 131 | + |
| 132 | + unmount(); |
| 133 | + await waitForBatchedUpdatesWithAct(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should not reuse the last deep link and log in again when signing out', async () => { |
| 137 | + expect(hasAuthToken()).toBe(false); |
| 138 | + |
| 139 | + const {unmount: unmount1} = render(<App />); |
| 140 | + await TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID_2, TEST_USER_LOGIN_2, undefined, TEST_AUTH_TOKEN_2); |
| 141 | + |
| 142 | + await waitForBatchedUpdatesWithAct(); |
| 143 | + |
| 144 | + expect(hasAuthToken()).toBe(true); |
| 145 | + expect(getCurrentUserEmail()).toBe(TEST_USER_LOGIN_2); |
| 146 | + // Unmount so we can prepare the deep link login |
| 147 | + unmount1(); |
| 148 | + |
| 149 | + await waitForBatchedUpdatesWithAct(); |
| 150 | + |
| 151 | + const url = getInitialURL(); |
| 152 | + // User signs in automatically when the app is remounted because of the deep link. |
| 153 | + // This overrides the previous sign-in. |
| 154 | + Linking.setInitialURL(url); |
| 155 | + const {unmount: unmount2} = render(<App />); |
| 156 | + |
| 157 | + await waitForBatchedUpdatesWithAct(); |
| 158 | + |
| 159 | + expect(getCurrentUserEmail()).toBe(TEST_USER_LOGIN_1); |
| 160 | + |
| 161 | + signOutAndRedirectToSignIn(); |
| 162 | + |
| 163 | + await waitForBatchedUpdatesWithAct(); |
| 164 | + |
| 165 | + // In a failing scenario, remounting triggers the sign-in with the deep link again because it still remembers it. |
| 166 | + // However, we've implemented a fix so that it does not reuse the last deep link. |
| 167 | + unmount2(); |
| 168 | + const {unmount: unmount3} = render(<App />); |
| 169 | + |
| 170 | + await waitForBatchedUpdatesWithAct(); |
| 171 | + |
| 172 | + expect(hasAuthToken()).toBe(false); |
| 173 | + |
| 174 | + unmount3(); |
| 175 | + await waitForBatchedUpdatesWithAct(); |
| 176 | + }); |
| 177 | +}); |
0 commit comments