Skip to content

QA environment: sign out when switching into or out of QA - #108234

Open
eszlamczyk wants to merge 7 commits into
Expensify:mainfrom
software-mansion-labs:feat/server-field-logout
Open

eszlamczyk wants to merge 7 commits into
Expensify:mainfrom
software-mansion-labs:feat/server-field-logout

Conversation

@eszlamczyk

@eszlamczyk eszlamczyk commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Context

This PR is part of the web implementation of the QA environment. QA runs on its own server and database, behind Cloudflare Access, so every request the app sends to QA has to carry a Cloudflare token and renew it when it runs out. Earlier parts added the server selection (#99926, #100209) and the Cloudflare sign-in round trip (#100730). The remaining work lands as a series of small PRs.

Explanation of Change

Switching into or out of QA now signs the user out. QA has its own database, so the same email is a different account there, and a token from one side means nothing on the other. Production and staging share a database, so switching between them keeps the session, as before.

No change on production or staging. QA is only offered on builds with QA configured (QA_EXPENSIFY_URL), and no production or staging build sets it yet, so the new sign-out can't be reached there. Every other sign-out works as before.

How the switch signs out. It has its own signOutForServerSwitch, because signOutAndRedirectToSignIn doesn't fit: for a support agent it restores the stashed session, whose token belongs to the server being left, and for an anonymous session it only opens the sign-in modal. The switch sign-out:

  • sends LogOut only for a signed-in session that isn't anonymous or a support session (the same sessions a normal sign-out logs out), and nothing when no one is signed in (the test tools can be opened from the sign-in page),
  • then clears the app through redirectToSignIn, which also drops any stashed session, so nothing from the old server comes back,
  • stores the new server only once LogOut has its answer, right before the clear. Until then the app is still signed in to the old server, so its requests keep going there; this matters for SAML, whose sign-out window can stay open a while,
  • keeps the old server and the session if the sign-out fails or is cancelled (closing the SAML window), and logs a warning,
  • on HybridApp, changes the shouldUseStaging flag together with the stored server, after OldDot is signed out.

Why LogOut is pinned to a server. A request picks its server when it is sent, not when it is created. The active server is shared between tabs, so another tab can switch it while LogOut waits, and an unpinned LogOut would then reach a database that doesn't know the token.

What changes

  • setActiveServer(server, currentServer, session) uses signOutForServerSwitch when moving into or out of QA, and stores the server right away otherwise. ServerSelector passes it the server it currently shows as active (from useActiveServer) and the session.
  • Request gets an optional server field. prepareRequest and API.makeRequestWithSideEffects accept it, it is passed through Request.makeXHR, HttpUtils.xhr and the load-test duplicates, and getApiRoot uses it in place of the active server when it is set.
  • signOut and callSAMLSignOut take an optional server and pin LogOut to it. Their behaviour is otherwise unchanged.
  • The step that opens OldDot's sign-out page when LogOut reports OldDot cookies moves into openOldDotSignOut, shared by both sign-outs. No behaviour change for signOutAndRedirectToSignIn.
  • New CONST.SIGN_OUT_REASON.SERVER_SWITCH for the sign-out telemetry.

Fixed Issues

$ #91419
PROPOSAL:

Tests

Note

I couldn't verify the SAML flow, but it follows the same request code line by line, so it shouldn't behave differently. Cancelling the SAML sign-out during a switch is covered by unit tests.

  1. Sign in, then sign out from Account > Sign out.
  2. Verify the LogOut request is sent (Network tab) and you land on the sign-in page.
  3. Sign in, open the test tools (Cmd/Ctrl+D), switch the server between production and staging, and verify the session is kept, no LogOut is sent, and requests go to the selected server.
  4. With QA configured in .env (QA_EXPENSIFY_URL, QA_CF_TEAM_DOMAIN, QA_CF_OAUTH_CLIENT_ID, QA_AUTH_CHECK_PATH), sign in on a dev build, open the test tools and switch to QA. Verify:
    • one LogOut request goes to the server you left, not to QA (Network tab)
    • you land on the sign-in page, and the test tools show QA selected
    • the Log requests sent after the switch go to the QA host (QA_EXPENSIFY_URL) and return 200 (Log should be on a Cloudflare Access bypass path, so it needs no token)
  5. Signed out, open the test tools and switch to QA. Verify no LogOut request is sent and the test tools show QA selected.
  6. Still on QA, run the QA auth probe from the test tools and verify it succeeds. This checks the Cloudflare Access setup; signing in on QA isn't possible yet, because regular API requests get the Cloudflare token only in a later PR (they return 401 until then). For the same reason, switching out of QA while signed in can't be tested manually yet; unit tests cover it.
  • Verify that no errors appear in the JS console

Offline tests

  1. Sign in and go offline.
  2. Sign out.
  3. Verify you land on the sign-in page.
  4. With QA configured, sign in, go offline, open the test tools and switch to QA.
  5. Verify you land on the sign-in page and the test tools show QA selected.

QA Steps

Steps 1-3 of the tests, on staging. Steps 4-6 need a build with QA configured, which staging doesn't have yet.

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I verified any copy / text that was added to the app is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

MacOS: Chrome / Safari
Screen.Recording.2026-11-30.at.12.17.02.mov

Probe still works as intended.

image

@eszlamczyk eszlamczyk changed the title Feat/server field logout [No QA] QA environment: pass server information to API calls and sign-out Nov 27, 2026
@eszlamczyk eszlamczyk changed the title [No QA] QA environment: pass server information to API calls and sign-out QA environment: sign out when switching into or out of QA Nov 30, 2026

@JakubKorytko JakubKorytko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly about the QA switch not always signing out (supportal, anonymous, cancelled SAML) and the global signOut guard

Comment thread src/libs/actions/User.ts Outdated
const isCrossingQA = (currentServer === CONST.SERVER.QA) !== (server === CONST.SERVER.QA);
if (isCrossingQA) {
// Pinned to the server being left, because LogOut is sent after the Onyx.set below has stored the new one
signOutAndRedirectToSignIn(session, undefined, undefined, undefined, undefined, undefined, currentServer);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two cases where this doesn't sign out, if I read signOutAndRedirectToSignIn right:

  • with a supportal session isSupportal makes it skip signOut and restore the stashed session, so after the switch the old session is put back and its token goes to QA (plus it opens the OldDot supportal restore URL)
  • with an anonymous session it opens the sign in modal and returns early, so the token from the old server stays

did not test myself though. Maybe the QA switch wants its own small path that clears the session and stashed session without restoring anything? That would also get rid of the five undefineds here, which are easy to get wrong

@eszlamczyk eszlamczyk Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch now has its own signOutForServerSwitch: it never restores the stashed session (the clear drops it) and clears an anonymous session instead of opening the sign-in modal. Like a normal sign-out, it only sends LogOut for a signed-in session that isn't anonymous or support. This also got rid of the five undefineds. Done in 8ba6ce6

Comment thread src/libs/actions/User.ts Outdated
signOutAndRedirectToSignIn(session, undefined, undefined, undefined, undefined, undefined, currentServer);
}

Onyx.set(ONYXKEYS.ACTIVE_SERVER, server);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmh, the new server is stored right away, before we know the sign out went through. For a SAML user who cancels the logout browser, callSAMLSignOut rejects with Logout cancelled, signOutAndRedirectToSignIn only logs it and the user stays signed in, but ACTIVE_SERVER is already QA. Could we keep the old server when the sign out is cancelled?

@eszlamczyk eszlamczyk Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the order instead, the new server is now stored only once LogOut has its answer, right before the clear. So the app keeps talking to the old server while it's still signed in there (the SAML window can stay open a while), and if the sign-out fails or is cancelled the old server simply stays, with a Log.warn. Done in 8ba6ce6

Comment thread src/libs/actions/Session/index.ts Outdated
*/
async function signOut(params: {autoGeneratedLogin?: string; signedInWithSAML?: boolean; authToken?: string} = {}): Promise<void | Response<never>> {
async function signOut(params: {autoGeneratedLogin?: string; signedInWithSAML?: boolean; authToken?: string} = {}, server?: Server): Promise<void | Response<never>> {
if (!params.authToken) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this changes signOut for every caller, not only the server switch. A session without authToken but with autoGeneratedLogin used to send LogOut with the partner credentials, which is what invalidates them, and now that and clearPrefetchOnAppStart are skipped. Not sure that state is reachable tho. Could we skip the call where the switch happens instead, so signOut keeps its old contract?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, moved the check into the switch path, so signOut keeps its old contract for every caller. Done in 8ba6ce6

@@ -299,11 +306,11 @@ async function signOut(params: {autoGeneratedLogin?: string; signedInWithSAML?:
};

if (params.signedInWithSAML && params.authToken) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: with the early return above params.authToken is always set here, and the ?? null in logOutParams can't be hit either. Could drop both

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer applies, the early return is gone from signOut in 8ba6ce6

Comment thread src/libs/actions/Session/index.ts Outdated

/**
* Signs out, or switches back to a stashed session, then redirects to sign in.
* @param server the server the session belongs to, so LogOut reaches it even if the active server changes before it is sent

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this @param server line is copied in signOut, callSAMLSignOut and here, and makeRequest, API/index and HttpUtils say the same thing in their own words. I'd keep the full reason in one place (e.g. on Request.server) and make the rest just say which server LogOut is sent to

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8ba6ce6, the full reason now lives on Request.server

});

test('hands the 407 back without reauthenticating a request pinned to a server other than the active one', () => {
// Given a request pinned to staging while the app is on production, like a LogOut sent after switching servers

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Given says the app is on production, but neither this test nor the next one sets the active server, they lean on the test env default. If that default changes, the second test stops covering the "pinned to active server" case without failing. Could we set ACTIVE_SERVER explicitly like ApiUtilsTest does?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed together with the check in 8ba6ce6

Comment thread tests/actions/SessionTest.ts Outdated
signOutAndRedirectToSignIn(await Onyx.get(ONYXKEYS.SESSION), undefined, undefined, undefined, undefined, undefined, CONST.SERVER.STAGING);
await waitForBatchedUpdates();

// Then the server reaches LogOut

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this Then reads a bit off, I think it means LogOut is sent to the server passed in

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is gone, signOutAndRedirectToSignIn doesn't take a server anymore (8ba6ce6)

Comment thread tests/unit/APITest.ts Outdated

// Then the pin reaches HttpUtils.xhr, which builds the URL from it, so a server switch made meanwhile doesn't redirect it
expect(xhr).toHaveBeenCalledTimes(1);
expect(xhr.mock.calls.at(0)?.at(5)).toBe(CONST.SERVER.QA);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: .at(5) depends on the position of server in the xhr args, and if a param is added before it the toBeUndefined one below still passes. toHaveBeenCalledWith(..., CONST.SERVER.QA) would be harder to break

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8ba6ce6

Comment thread src/libs/actions/User.ts
function setActiveServer(server: ValueOf<typeof CONST.SERVER>, session: OnyxEntry<Session>) {
const currentServer = getActiveServer();
if (CONFIG.IS_HYBRID_APP) {
HybridAppModule.shouldUseStaging(server === CONST.SERVER.STAGING);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if QA can be picked on HybridApp yet, but if it can, this flips the native staging flag before the sign out starts, and signOutAndRedirectToSignIn then calls startSignOut and signOutFromOldDot. The pin only covers the JS LogOut, so on a staging to QA switch the OldDot sign out might go to the new host. Did not check the native side tho. Moving this below the sign out would avoid the question, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved in 8ba6ce6, shouldUseStaging now changes together with the stored server, after OldDot is signed out


const saveAndGoBack = () => {
setActiveServer(selectedServer);
setActiveServer(selectedServer, session);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save signs the user out right away with no warning. The feature/qa-env/web branch asks first (confirmServerChangeDescription with a Sign out button) when the switch crosses QA and the user is signed in. Without it a tester who taps Save loses the session and anything still waiting in the queue offline. Could we bring that prompt over here, or is it planned for a later PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The confirmation is planned as a separate pr. I did not want to overcrowd this pr. The PR adds user-facing copy in all languages, which needs its own copy review

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ Changes either increased or maintained existing code coverage, great job!

Files with missing lines Coverage Δ
src/CONST/index.ts 96.29% <ø> (ø)
src/libs/API/index.ts 91.80% <100.00%> (ø)
src/libs/API/makeRequest.ts 92.50% <ø> (ø)
src/libs/ApiUtils.ts 100.00% <100.00%> (ø)
src/libs/HttpUtils.ts 90.82% <100.00%> (ø)
src/libs/Network/LoadTest.ts 100.00% <100.00%> (ø)
src/libs/Request.ts 100.00% <100.00%> (ø)
src/libs/actions/Session/index.ts 74.16% <100.00%> (+1.20%) ⬆️
src/libs/actions/User.ts 54.81% <100.00%> (+1.25%) ⬆️
src/pages/settings/Troubleshoot/ServerSelector.tsx 100.00% <100.00%> (ø)
... and 7367 files with indirect coverage changes

@eszlamczyk
eszlamczyk marked this pull request as ready for review December 1, 2026 15:36
@eszlamczyk
eszlamczyk requested review from a team as code owners December 1, 2026 15:36
@expensify-chore-bot
expensify-chore-bot Bot requested review from huult and removed request for a team December 1, 2026 15:36
@expensify-chore-bot

Copy link
Copy Markdown

@huult Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@expensify-chore-bot
expensify-chore-bot Bot requested review from garrettmknight and removed request for a team December 1, 2026 15:36
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-01T15:55:59.769589Z 9b86ecf Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@huult

huult commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@eszlamczyk could you sync with main to resolve the conflict?

@huult

huult commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified any copy / text that was added to the app is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
Android: mWeb Chrome
iOS: HybridApp
iOS: mWeb Safari
MacOS: Chrome / Safari

@huult

huult commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor
Screen.Recording.2026-12-02.at.14.43.17.mov

Tested with a SAML account. This is the observed behavior. However, SAML isn't working on staging, so we don't need to worry about this for now.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants