diff --git a/src/libs/Request.ts b/src/libs/Request.ts index 6fcc13210180..9a54bf8d9bbb 100644 --- a/src/libs/Request.ts +++ b/src/libs/Request.ts @@ -23,8 +23,9 @@ let middlewares: Middleware[] = []; const latestApplyAttemptByRequest = new WeakMap(); function makeXHR(request: Request): Promise | void> { - const finalParameters = enhanceParameters(request.command, request?.data ?? {}); return hasReadRequiredDataFromStorage().then((): Promise | void> => { + // Add the session only once storage has it, otherwise the first request of a launch goes without one + const finalParameters = enhanceParameters(request.command, request?.data ?? {}); return HttpUtils.xhr(request.command, finalParameters, request.type, request.shouldUseSecure, request.initiatedOffline); }); } diff --git a/tests/unit/RequestTest.ts b/tests/unit/RequestTest.ts index d2e8a288c8f1..c672732c4f0c 100644 --- a/tests/unit/RequestTest.ts +++ b/tests/unit/RequestTest.ts @@ -4,8 +4,11 @@ import Log from '@src/libs/Log'; import * as Request from '@src/libs/Request'; import type {Middleware} from '@src/libs/Request'; import {endSpan, endSpanWithAttributes, startSpan} from '@src/libs/telemetry/activeSpans'; +import type ONYXKEYS from '@src/ONYXKEYS'; import type * as OnyxTypes from '@src/types/onyx'; +import type Onyx from 'react-native-onyx'; + import * as TestHelper from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; @@ -156,3 +159,35 @@ test('Request.processWithMiddleware() keeps startup on the StartupData span name expect(startedSpanIds.some((spanId) => spanId.startsWith('SearchData.'))).toBe(false); }); }); + +test('Request.processWithMiddleware() sends the saved session on a request made as the request code first loads', async () => { + let sentParameters: Record | undefined; + await jest.isolateModulesAsync(async () => { + jest.doMock('@libs/HttpUtils', () => ({ + __esModule: true, + default: { + xhr: (command: string, parameters: Record) => { + sentParameters = parameters; + return Promise.resolve({jsonCode: 200}); + }, + cancelPendingRequests: jest.fn(), + processHTTPRequest: jest.fn(), + }, + })); + + // Given a session saved on the device, read by a fresh Onyx before any network code is loaded + const isolatedOnyx = jest.requireActual<{default: typeof Onyx}>('react-native-onyx').default; + const isolatedOnyxKeys = jest.requireActual<{default: typeof ONYXKEYS}>('@src/ONYXKEYS').default; + isolatedOnyx.init({keys: isolatedOnyxKeys}); + await isolatedOnyx.set(isolatedOnyxKeys.SESSION, {authToken: 'savedToken', email: 'member@saml.com', accountID: 1}); + await isolatedOnyx.set(isolatedOnyxKeys.CREDENTIALS, {autoGeneratedLogin: 'login', autoGeneratedPassword: 'password'}); + await waitForBatchedUpdates(); + + // When a request is made in the same moment the request code loads, the way the phone app loads code on first use + const isolatedRequest = jest.requireActual('@src/libs/Request'); + await isolatedRequest.processWithMiddleware({command: WRITE_COMMANDS.RECONNECT_APP, data: {}}); + }); + + // Then the request carries the saved session instead of going out without one + expect(sentParameters?.authToken).toBe('savedToken'); +});