Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/sagas/__tests__/selectServer.sdkHost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,34 @@ import { APP, SERVER } from '~/actions/actionsTypes';
import { RootEnum } from '~/definitions';
import sdk from '~/lib/services/sdk';
import { connect } from '~/lib/services/connect';
import UserPreferences from '~/lib/methods/userPreferences';
import { getServerUserIdKey, getUserTokenKey } from '~/lib/constants/keys';
import type { MockConnection } from '~/lib/testUtils/sdkIntegration';
import type * as SdkIntegration from '~/lib/testUtils/sdkIntegration';
import { cancelSagaTasks, createRecordingStore, flushSagaMicrotasks } from '~/lib/testUtils/sagaStore';

const HOST = 'https://open.rocket.chat';
const USER_ID = 'user-open';
const TOKEN = 'token-open';

describe('selectServer saga — redundant select for the live SDK host', () => {
beforeEach(() => {
mockConnections.length = 0;
jest.clearAllMocks();
UserPreferences.removeItem(getServerUserIdKey(HOST));
UserPreferences.removeItem(getUserTokenKey(HOST, USER_ID));
});

afterEach(() => {
cancelSagaTasks();
sdk.disconnect();
UserPreferences.removeItem(getServerUserIdKey(HOST));
UserPreferences.removeItem(getUserTokenKey(HOST, USER_ID));
});

it('reads the live host off the real SDK client and cancels the select without reconnecting', async () => {
UserPreferences.setString(getServerUserIdKey(HOST), USER_ID);
UserPreferences.setString(getUserTokenKey(HOST, USER_ID), TOKEN);
sdk.initialize(HOST);
expect(sdk.host).toBe(HOST);

Expand All @@ -70,4 +81,21 @@ describe('selectServer saga — redundant select for the live SDK host', () => {
expect(cancelIndex).toBeGreaterThan(insideIndex);
expect(connect).not.toHaveBeenCalled();
});

it('reconnects and stays outside when the same host has no stored credentials (workspace retry)', async () => {
sdk.initialize(HOST);
expect(sdk.host).toBe(HOST);

const { store, dispatchedActions } = createRecordingStore(selectServerRoot);

store.dispatch(selectServerRequest(HOST, '7.0.0', false));
await flushSagaMicrotasks();

expect(connect).toHaveBeenCalledWith({ server: HOST });
const outsideIndex = dispatchedActions.findIndex(
action => action.type === APP.START && action.root === RootEnum.ROOT_OUTSIDE
);
expect(outsideIndex).toBeGreaterThanOrEqual(0);
expect(dispatchedActions.map(action => action.type)).not.toContain(SERVER.SELECT_CANCEL);
});
});
11 changes: 8 additions & 3 deletions app/sagas/selectServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,14 @@ const getServerInfoSaga = function* getServerInfoSaga({ server, raiseError = tru
const handleSelectServer = function* handleSelectServer({ server, version, fetchVersion }: ISelectServerAction) {
try {
if (sdk.host === server) {
yield put(appStart({ root: RootEnum.ROOT_INSIDE }));
yield put(selectServerCancel());
return;
// Skip fast-path for unauthenticated retries so they stay OUTSIDE on WorkspaceView.
const storedUserId = UserPreferences.getString(getServerUserIdKey(server));
const storedToken = storedUserId ? UserPreferences.getString(getUserTokenKey(server, storedUserId)) : null;
if (storedUserId && storedToken) {
yield put(appStart({ root: RootEnum.ROOT_INSIDE }));
yield put(selectServerCancel());
return;
}
}
// SSL Pinning - Read certificate alias and set it to be used by network requests
const certificate = UserPreferences.getString(`${CERTIFICATE_KEY}-${server}`);
Expand Down
Loading