mattermost-mobile-upstream/detox/e2e/support/ui/screen/server.ts
Felipe Martin 9ccfa0ebe9
Some checks are pending
ci / lint-typecheck (push) Waiting to run
ci / detox-lint-typecheck (push) Waiting to run
ci / test (push) Waiting to run
[MM-70605][MM-70607] Scope pre-auth secret storage per server (#10149)
* [MM-70605][MM-70607] Scope pre-auth secret storage per server

The pre-auth secret was written with react-native-keychain's generic-password
API using the `server` option, which those calls do not read. Every server
therefore resolved to one shared entry, and a falsy secret argument deleted it,
so ordinary operations such as building a client, refreshing the token header or
removing another server discarded the stored value.

Key the entry per server instead. On Android the alias namespace is shared with
internet credentials, so the service is prefixed to keep it away from the
session token; iOS uses the bare server URL, which is what the native reader in
Gekidou already queries. The account value is renamed to `preauth_secret` on
both sides. Writing the token no longer touches the secret: removal is explicit,
via removePreauthSecret.

A one-time migration adopts the previously shared entry when a single server
makes ownership unambiguous, and discards it otherwise. It only deletes the old
entry once the new one is confirmed stored, and defers when no server is active.

Also forward the secret through the login screen to SSO, and fix loginOptions to
count only enabled providers so the single-provider redirect works. Relands the
`.enabled` filter from #9585 by @pltnm239.

Edit Server now updates the live client's headers rather than invalidating the
session, and validates with the candidate secret so a value the server rejects
cannot be saved.

* [MM-70605] Harden preauth secret flows and fix Edit Server Detox on iOS

Address review feedback around migration markers, credential errors, and
keeping re-login secrets out of nav params. Focus the empty Authentication
secret field via Display Name return so iOS Detox can type into it.

Co-authored-by: Cursor <cursoragent@cursor.com>

* [MM-70605] Fix lines-around-comment in Edit Server Detox helper

Co-authored-by: Cursor <cursoragent@cursor.com>

* [MM-70605] Roll back preauth keychain when Edit Server client sync fails

After the secret is stored, header apply or createClient failures left the
new value in secure storage while UI state still held the old baseline.
Restore the previous secret (and live header) before surfacing the error.

Co-authored-by: Cursor <cursoragent@cursor.com>

* [MM-70605] Preserve exact preauth secret on Edit Server rollback

Avoid trimming during keychain restore so boundary whitespace from the
previously stored value is not altered.

Co-authored-by: Cursor <cursoragent@cursor.com>

* [MM-70605] Harden preauth migration ownership and secret handoff

Adopt the legacy secret only when a single server exists in the app DB,
keep add-server candidates in EphemeralStore instead of nav params, and
resync the live client to stored credentials when keychain rollback fails.

Co-authored-by: Cursor <cursoragent@cursor.com>

* [MM-70605] Discard the legacy preauth secret when no server can own it

* [MM-70605] Shorten the preauth migration docblock

* [MM-70605] Skip the Authorization header when there is no bearer token

* [MM-70605] Surface preauth secret rejections with a specific error message

* [MM-70605] Mock clearPendingPreauthSecret in session action tests

* [MM-70605] Remove the unreachable catch from restorePreviousPreauthSecret

setPreauthSecret and removePreauthSecret never reject, and the returned promise was not awaited, so the catch could not run.

* [MM-70605] Use network-client fix for the iOS addHeaders crash

Temporary pin to mattermost/react-native-network-client#175 to check CI. Replace with the released version before merge.

* [MM-70605] Bump react-native-network-client to 1.11.5

1.11.5 gives each recreated iOS session its own delegate in addHeaders, which fixes the SIGTRAP on pre-auth secret changes.

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 12:02:41 +02:00

271 lines
12 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Alert} from '@support/ui/component';
import {isAndroid, isIos, timeouts, wait, waitForElementToExist} from '@support/utils';
import {expect, waitFor} from 'detox';
class ServerScreen {
testID = {
serverScreen: 'server.screen',
closeButton: 'close.server.button',
headerTitleAddServer: 'server_header.title.add_server',
headerTitleConnectToServer: 'server_header.title.connect_to_server',
headerDescription: 'server_header.description',
serverUrlInput: 'server_form.server_url.input',
serverUrlInputError: 'server_form.server_url.input.error',
serverDisplayNameInput: 'server_form.server_display_name.input',
serverDisplayNameInputError: 'server_form.server_display_name.input.error',
displayHelp: 'server_form.display_help',
connectButton: 'server_form.connect.button',
connectButtonDisabled: 'server_form.connect.button.disabled',
advancedOptionsToggle: 'server_form.advanced_options.toggle',
preauthSecretInput: 'server_form.preauth_secret.input',
preauthSecretHelp: 'server_form.preauth_secret_help',
usernameInput: 'login_form.username.input',
usernameInputError: 'login_form.username.input.error',
};
serverScreen = element(by.id(this.testID.serverScreen));
closeButton = element(by.id(this.testID.closeButton));
headerTitleAddServer = element(by.id(this.testID.headerTitleAddServer));
headerTitleConnectToServer = element(by.id(this.testID.headerTitleConnectToServer));
headerDescription = element(by.id(this.testID.headerDescription));
serverUrlInput = element(by.id(this.testID.serverUrlInput));
serverUrlInputError = element(by.id(this.testID.serverUrlInputError));
serverDisplayNameInput = element(by.id(this.testID.serverDisplayNameInput));
serverDisplayNameInputError = element(by.id(this.testID.serverDisplayNameInputError));
displayHelp = element(by.id(this.testID.displayHelp));
connectButton = element(by.id(this.testID.connectButton));
connectButtonDisabled = element(by.id(this.testID.connectButtonDisabled));
advancedOptionsToggle = element(by.id(this.testID.advancedOptionsToggle));
preauthSecretInput = element(by.id(this.testID.preauthSecretInput));
preauthSecretHelp = element(by.id(this.testID.preauthSecretHelp));
usernameInput = element(by.id(this.testID.usernameInput));
toBeVisible = async () => {
// iOS 26.2 CI: use HALF_MIN for server screen — cold launch can exceed 10s.
const timeout = timeouts.HALF_MIN;
await waitFor(this.serverScreen).toExist().withTimeout(timeout);
await waitFor(this.serverUrlInput).toExist().withTimeout(timeout);
return this.serverScreen;
};
connectToServer = async (serverUrl: string, serverDisplayName: string) => {
await this.toBeVisible();
await this.serverUrlInput.replaceText(serverUrl);
await this.serverDisplayNameInput.replaceText(serverDisplayName);
await this.tapConnectButton();
if (isAndroid()) {
await this.waitForAndroidLoginAvailable(timeouts.ONE_MIN);
} else {
// iOS: retry if the server returns "Cannot connect" (transient infra issue).
// Re-entering the URL re-validates the form and re-enables the Connect button.
const MAX_CONNECT_ATTEMPTS = 3;
let lastError: unknown;
/* eslint-disable no-await-in-loop -- sequential retry: each attempt must complete before deciding to retry */
for (let attempt = 1; attempt <= MAX_CONNECT_ATTEMPTS; attempt++) {
if (attempt > 1) {
await this.serverUrlInput.clearText();
await wait(timeouts.TWO_SEC);
await this.serverUrlInput.replaceText(serverUrl);
await wait(timeouts.ONE_SEC);
await this.tapConnectButton();
}
try {
await waitForElementToExist(this.usernameInput, timeouts.HALF_MIN);
lastError = undefined;
break;
} catch (e) {
lastError = e;
if (attempt < MAX_CONNECT_ATTEMPTS) {
// eslint-disable-next-line no-console
console.warn(`[connectToServer] Attempt ${attempt}/${MAX_CONNECT_ATTEMPTS} — login form did not appear, retrying`);
}
}
}
/* eslint-enable no-await-in-loop */
if (lastError) {
throw lastError;
}
}
if (isIos()) {
await this.dismissIosNotificationsAlert();
}
};
/**
* Android: poll for the login form to be reachable, dismissing the
* "Notifications could not be received from this server" AlertDialog as
* soon as it appears. The dialog is a separate Window root that blocks
* Espresso from finding views in the underlying activity, so we must
* dismiss it before `usernameInput` becomes findable.
*/
waitForAndroidLoginAvailable = async (timeout: number) => {
const deadline = Date.now() + timeout;
const POLL = 1000;
const okayButton = element(by.text('Okay'));
/* eslint-disable no-await-in-loop -- sequential probes by design */
while (Date.now() < deadline) {
// First: dismiss the alert if it's up (alert window steals Espresso
// focus, so this matcher resolves against the alert window directly).
try {
await waitFor(Alert.notificationsCannotBeReceivedTitle).toExist().withTimeout(POLL);
try {
await okayButton.tap();
} catch {
// OKAY may have animated out between detection and tap — re-loop.
}
continue;
} catch {
// No alert — proceed to check the login form.
}
// Alert is not up. Try to find usernameInput now.
try {
await waitFor(this.usernameInput).toExist().withTimeout(POLL);
return;
} catch {
// Login form not visible yet — loop.
}
}
/* eslint-enable no-await-in-loop */
throw new Error(`waitForAndroidLoginAvailable: usernameInput not reachable within ${timeout}ms`);
};
/**
* Dismiss the iOS "Notifications could not/cannot be received from this server"
* alert that appears after connecting to a dev/self-hosted server without a valid
* push notification config. iOS 26.x on iPad exposes the Okay button with label
* "Okay" at multiple indexes (button wrapper + label), and the correct index
* varies by device + iOS build. We tap each candidate in sequence and verify the
* alert title disappears before claiming success, so a no-op tap on the wrong
* element does not silently leave the alert blocking the login transition.
*/
dismissIosNotificationsAlert = async () => {
// Alert may not exist (e.g. push-proxy verified cleanly); early-exit the check
// quickly so clean environments aren't penalised by retries.
const alertTitle = element(by.label('Notifications cannot be received from this server')).atIndex(0);
const alertTitleAlt = element(by.label('Notifications could not be received from this server')).atIndex(0);
const alertIsPresent = async () => {
try {
await waitFor(alertTitle).toExist().withTimeout(timeouts.HALF_SEC);
return true;
} catch {
// try alt title
}
try {
await waitFor(alertTitleAlt).toExist().withTimeout(timeouts.HALF_SEC);
return true;
} catch {
return false;
}
};
// Short initial wait so the alert has a chance to animate in before we probe.
try {
await waitFor(alertTitle).toExist().withTimeout(timeouts.FOUR_SEC);
} catch {
try {
await waitFor(alertTitleAlt).toExist().withTimeout(timeouts.ONE_SEC);
} catch {
// No alert — clean connect
return;
}
}
const strategies = [
element(by.label('Okay')).atIndex(0),
element(by.label('Okay')).atIndex(1),
element(by.text('Okay')),
];
/* eslint-disable no-await-in-loop -- sequential fallbacks with verification */
for (const btn of strategies) {
try {
await btn.tap();
if (!(await alertIsPresent())) {
return;
}
} catch {
// Element not found for this strategy — try the next
}
}
/* eslint-enable no-await-in-loop */
// All strategies failed — let the caller's waitFor(username) time out
// with a clear stack trace (better than a silent no-dismiss here).
};
close = async () => {
await this.closeButton.tap();
await expect(this.serverScreen).not.toBeVisible();
};
tapConnectButton = async () => {
// The connect button swaps its testID rather than staying put and going grey:
// server/form.tsx renders 'server_form.connect.button.disabled' while the form is
// invalid and 'server_form.connect.button' once it is not. buttonDisabled starts
// true (server/index.tsx) and flips in a state update after the URL is entered, so
// tapping straight after replaceText races that update and Detox reports
// "No elements found for MATCHER(id == server_form.connect.button)" -- which reads
// like a missing screen but only means the button is still disabled.
// Wait for the enabled id before tapping. (MM-T4691_5, and _6/_7 which cascade off it.)
await waitFor(this.connectButton).toExist().withTimeout(timeouts.TEN_SEC);
await this.connectButton.tap();
await wait(timeouts.ONE_SEC);
};
toggleAdvancedOptions = async () => {
await this.advancedOptionsToggle.tap();
await wait(timeouts.ONE_SEC);
};
enterPreauthSecret = async (secret: string) => {
// On Android, secureTextEntry={true} causes Espresso to match two native
// views under the same testID (the ReactEditText wrapper + the inner EditText).
// Use atIndex(0) to unambiguously target the first match and avoid the
// "AmbiguousMatcher" failure. On iOS there is always exactly one match.
const input = isAndroid() ? element(by.id(this.testID.preauthSecretInput)).atIndex(0) : this.preauthSecretInput;
await waitFor(input).toExist().withTimeout(timeouts.TEN_SEC);
await input.replaceText(secret);
};
connectToServerWithPreauthSecret = async (serverUrl: string, serverDisplayName: string, preauthSecret: string) => {
await this.toBeVisible();
await this.serverUrlInput.replaceText(serverUrl);
await this.serverDisplayNameInput.replaceText(serverDisplayName);
// Toggle advanced options to show preauth secret field
await this.toggleAdvancedOptions();
// Enter preauth secret
await this.enterPreauthSecret(preauthSecret);
// Connect
await this.tapConnectButton();
if (isAndroid()) {
// Same routine connectToServer uses. A single dismissal attempt is not enough:
// the push-proxy alert can animate in after the probe window, and a tap that
// misses leaves it covering the login form.
await this.waitForAndroidLoginAvailable(timeouts.ONE_MIN);
return;
}
await this.dismissIosNotificationsAlert();
// Wait for the login form to appear after server connection with preauth.
await waitFor(this.usernameInput).toExist().withTimeout(timeouts.HALF_MIN);
};
}
const serverScreen = new ServerScreen();
export default serverScreen;