mattermost-mobile-upstream/app/actions/local/session/index.test.ts
Carlos Garcia a5ff8a0d95
Mm 68767 zero persistence (#9803)
* implements auto cache cleanup when zero persistence mode is enabled (0 value)

* uses memory flag with sqlite instead of loki when running in zero persistence mode

* renames ephemeral mode manager and improves readability

* zero persistence refactors and restore values during transitions

* fixes ephemeral mode tests

* use initializeClient instead of just initialize to wait for doReconnect to finish

that way teams loading is awaited while WS reconnects during ZPM transitions

* adds inital sync as a mount hook to prevent undesired load error states

during app cold start in ZPM mode, we need a way to load team and channels
without showing loading error during the transition. The nre is initial sync
hook allows for it

* reset started sync subject on wipe

* removes extra log lines

* resets tracked server during ephemeral mode transitions

* improvements to apply persistence mode changes when something fails

* address coderabbit PR suggestions

* add batching time logging

* throws ephemeral mode change error as is

* make deleteServerDatabaseFiles private

* inline derive persistence flag function

* return current user id when refetching current user

* support zero persistence mode server for push notifications

* adds support for zero persistence mode when sharing to mattermost

* adds clarifying comment

* catch and log error deleting files

* reorder translations to correct position

* adds some new tests to improve coverage

* ios push notification native fix

* logout when fetchMe fails with 401 status code during reconnection

* properly surface errors

* return proper false value when checking for zero persistence flag

* change comments and properly await on some operations

* prevent not set persistence mode to report as an error

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* include servers with no persistence flag set in share list

* disable button for accessibility

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* improves when to store and remove signing key from global area

* review push notification reception running in zero persistence mode

* add server to ephemeral mode manager right after login

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-07-16 13:55:45 +02:00

373 lines
16 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import NetInfo, {type NetInfoState} from '@react-native-community/netinfo';
import {Platform} from 'react-native';
import {removePushDisabledInServerAcknowledged, removePushSigningKey} from '@actions/app/global';
import DatabaseManager from '@database/manager';
import {resetMomentLocale} from '@i18n';
import {getAllServerCredentials, removeServerCredentials} from '@init/credentials';
import PushNotifications from '@init/push_notifications';
import NetworkManager from '@managers/network_manager';
import WebsocketManager from '@managers/websocket_manager';
import {getDeviceToken} from '@queries/app/global';
import {getExpiredSession} from '@queries/servers/system';
import {getCurrentUser} from '@queries/servers/user';
import {deleteFileCache, deleteFileCacheByDir} from '@utils/file';
import {clearCookiesForServer, getCSRFFromCookie, urlSafeBase64Encode} from '@utils/security';
import {cancelAllSessionNotifications, cancelSessionNotification, findSession, terminateSession} from './index';
import type ServerDataOperator from '@database/operator/server_data_operator';
import type {Database} from '@nozbe/watermelondb';
import type {ServerDatabase, ServerDatabases} from '@typings/database/database';
import type UserModel from '@typings/database/models/servers/user';
// Mock all dependencies
jest.mock('@react-native-community/netinfo');
jest.mock('expo-image', () => ({
Image: {
clearDiskCache: jest.fn(),
},
}));
jest.mock('@actions/app/global');
jest.mock('@database/manager', () => ({
getServerDatabaseAndOperator: jest.fn(),
getActiveServerDatabase: jest.fn(),
destroyServerDatabase: jest.fn(),
deleteServerDatabase: jest.fn(),
serverDatabases: {},
}));
jest.mock('@i18n', () => ({
resetMomentLocale: jest.fn(),
}));
jest.mock('@init/credentials');
jest.mock('@init/push_notifications', () => ({
removeServerNotifications: jest.fn(),
cancelScheduleNotification: jest.fn(),
}));
jest.mock('@managers/network_manager', () => ({
invalidateClient: jest.fn(),
}));
jest.mock('@managers/websocket_manager', () => ({
invalidateClient: jest.fn(),
}));
jest.mock('@queries/app/global');
jest.mock('@queries/servers/system');
jest.mock('@queries/servers/user');
jest.mock('@utils/file');
jest.mock('@utils/security');
describe('session actions', () => {
const mockServerUrl = 'https://example.com';
const mockDatabase = {database: 'mockDb'};
const mockOperator = {
handleSystem: jest.fn(),
};
beforeEach(() => {
jest.clearAllMocks();
});
describe('findSession', () => {
const mockSessions: Session[] = [
{id: 'session1', device_id: 'device123', props: {csrf: 'csrf123', os: 'ios'}} as Session,
{id: 'session2', device_id: 'device456', props: {csrf: 'csrf456', os: 'android'}} as Session,
{id: 'session3', device_id: 'device789', props: {csrf: 'csrf789', os: 'ios'}} as Session,
];
beforeEach(() => {
jest.mocked(DatabaseManager.getServerDatabaseAndOperator).mockReturnValue({
database: mockDatabase as unknown as Database,
operator: mockOperator as unknown as ServerDataOperator,
});
});
it('should find session by expired session ID', async () => {
const expiredSession = {id: 'session2', notificationId: '123'};
jest.mocked(getExpiredSession).mockResolvedValue(expiredSession as SessionExpiration);
jest.mocked(getDeviceToken).mockResolvedValue('device999');
jest.mocked(getCSRFFromCookie).mockResolvedValue('csrf999');
const result = await findSession(mockServerUrl, mockSessions);
expect(result).toEqual(mockSessions[1]);
expect(getExpiredSession).toHaveBeenCalledWith(mockDatabase);
});
it('should find session by device token', async () => {
jest.mocked(getExpiredSession).mockResolvedValue(undefined);
jest.mocked(getDeviceToken).mockResolvedValue('device456');
jest.mocked(getCSRFFromCookie).mockResolvedValue('csrf999');
const result = await findSession(mockServerUrl, mockSessions);
expect(result).toEqual(mockSessions[1]);
expect(getDeviceToken).toHaveBeenCalled();
});
it('should find session by CSRF token', async () => {
jest.mocked(getExpiredSession).mockResolvedValue(undefined);
jest.mocked(getDeviceToken).mockResolvedValue('device999');
jest.mocked(getCSRFFromCookie).mockResolvedValue('csrf789');
const result = await findSession(mockServerUrl, mockSessions);
expect(result).toEqual(mockSessions[2]);
expect(getCSRFFromCookie).toHaveBeenCalledWith(mockServerUrl);
});
it('should find session by platform OS', async () => {
Platform.OS = 'android';
jest.mocked(getExpiredSession).mockResolvedValue(undefined);
jest.mocked(getDeviceToken).mockResolvedValue('device999');
jest.mocked(getCSRFFromCookie).mockResolvedValue('csrf999');
const result = await findSession(mockServerUrl, mockSessions);
expect(result).toEqual(mockSessions[1]);
});
it('should return undefined when no session matches', async () => {
jest.mocked(getExpiredSession).mockResolvedValue(undefined);
jest.mocked(getDeviceToken).mockResolvedValue('device999');
jest.mocked(getCSRFFromCookie).mockResolvedValue('csrf999');
Platform.OS = 'web';
const result = await findSession(mockServerUrl, mockSessions);
expect(result).toBeUndefined();
});
it('should handle errors gracefully and return undefined', async () => {
jest.mocked(DatabaseManager.getServerDatabaseAndOperator).mockImplementation(() => {
throw new Error('Database error');
});
const result = await findSession(mockServerUrl, mockSessions);
expect(result).toBeUndefined();
});
});
describe('cancelAllSessionNotifications', () => {
it('should cancel notifications for all servers with credentials', async () => {
const mockCredentials = [
{serverUrl: 'https://server1.com', userId: 'user1', token: 'token1'},
{serverUrl: 'https://server2.com', userId: 'user2', token: 'token2'},
];
jest.mocked(getAllServerCredentials).mockResolvedValue(mockCredentials);
jest.mocked(DatabaseManager.getServerDatabaseAndOperator).mockReturnValue({
database: mockDatabase as unknown as Database,
operator: mockOperator as unknown as ServerDataOperator,
});
jest.mocked(getExpiredSession).mockResolvedValue({
id: 'session1',
notificationId: '123',
} as SessionExpiration);
jest.mocked(NetInfo.fetch).mockResolvedValue({
isInternetReachable: true,
} as NetInfoState);
await cancelAllSessionNotifications();
expect(getAllServerCredentials).toHaveBeenCalled();
expect(DatabaseManager.getServerDatabaseAndOperator).toHaveBeenCalledTimes(2);
});
it('should handle empty credentials list', async () => {
jest.mocked(getAllServerCredentials).mockResolvedValue([]);
await cancelAllSessionNotifications();
expect(getAllServerCredentials).toHaveBeenCalled();
expect(DatabaseManager.getServerDatabaseAndOperator).not.toHaveBeenCalled();
});
});
describe('cancelSessionNotification', () => {
beforeEach(() => {
jest.mocked(DatabaseManager.getServerDatabaseAndOperator).mockReturnValue({
database: mockDatabase as unknown as Database,
operator: mockOperator as unknown as ServerDataOperator,
});
});
it('should cancel notification when expired session has notification ID and internet is reachable', async () => {
const expiredSession = {id: 'session1', notificationId: '123'};
jest.mocked(getExpiredSession).mockResolvedValue(expiredSession as SessionExpiration);
jest.mocked(NetInfo.fetch).mockResolvedValue({
isInternetReachable: true,
} as NetInfoState);
const result = await cancelSessionNotification(mockServerUrl);
expect(PushNotifications.cancelScheduleNotification).toHaveBeenCalledWith(123);
expect(mockOperator.handleSystem).toHaveBeenCalledWith({
systems: [{
id: 'sessionExpiration',
value: '',
}],
prepareRecordsOnly: false,
});
expect(result).toEqual({});
});
it('should not cancel notification when no notification ID', async () => {
const expiredSession = {id: 'session1', notificationId: ''};
jest.mocked(getExpiredSession).mockResolvedValue(expiredSession as SessionExpiration);
jest.mocked(NetInfo.fetch).mockResolvedValue({
isInternetReachable: true,
} as NetInfoState);
const result = await cancelSessionNotification(mockServerUrl);
expect(PushNotifications.cancelScheduleNotification).not.toHaveBeenCalled();
expect(mockOperator.handleSystem).not.toHaveBeenCalled();
expect(result).toEqual({});
});
it('should not cancel notification when internet not reachable', async () => {
const expiredSession = {id: 'session1', notificationId: '123'};
jest.mocked(getExpiredSession).mockResolvedValue(expiredSession as SessionExpiration);
jest.mocked(NetInfo.fetch).mockResolvedValue({
isInternetReachable: false,
} as NetInfoState);
const result = await cancelSessionNotification(mockServerUrl);
expect(PushNotifications.cancelScheduleNotification).not.toHaveBeenCalled();
expect(mockOperator.handleSystem).not.toHaveBeenCalled();
expect(result).toEqual({});
});
it('should handle errors gracefully and return error object', async () => {
const error = new Error('Database error');
jest.mocked(DatabaseManager.getServerDatabaseAndOperator).mockImplementation(() => {
throw error;
});
const result = await cancelSessionNotification(mockServerUrl);
expect(result).toEqual({error});
});
});
describe('terminateSession', () => {
const encodedServerUrl = 'aHR0cHM6Ly9leGFtcGxlLmNvbQ==';
beforeEach(() => {
jest.mocked(DatabaseManager.getServerDatabaseAndOperator).mockReturnValue({
database: mockDatabase as unknown as Database,
operator: mockOperator as unknown as ServerDataOperator,
});
jest.mocked(getExpiredSession).mockResolvedValue(undefined);
jest.mocked(NetInfo.fetch).mockResolvedValue({
isInternetReachable: false,
} as NetInfoState);
jest.mocked(urlSafeBase64Encode).mockReturnValue(encodedServerUrl);
jest.mocked(getCurrentUser).mockResolvedValue(undefined);
(DatabaseManager.serverDatabases as ServerDatabases) = {};
});
it('should call all cleanup functions in correct order for removeServer=true', async () => {
await terminateSession(mockServerUrl, true);
// Verify all cleanup functions called
expect(removeServerCredentials).toHaveBeenCalledWith(mockServerUrl);
expect(PushNotifications.removeServerNotifications).toHaveBeenCalledWith(mockServerUrl);
expect(NetworkManager.invalidateClient).toHaveBeenCalledWith(mockServerUrl);
expect(WebsocketManager.invalidateClient).toHaveBeenCalledWith(mockServerUrl);
expect(removePushDisabledInServerAcknowledged).toHaveBeenCalledWith(encodedServerUrl);
expect(removePushSigningKey).toHaveBeenCalledWith(mockServerUrl);
expect(DatabaseManager.destroyServerDatabase).toHaveBeenCalledWith(mockServerUrl);
expect(resetMomentLocale).toHaveBeenCalled();
expect(clearCookiesForServer).toHaveBeenCalledWith(mockServerUrl);
expect(deleteFileCache).toHaveBeenCalledWith(mockServerUrl);
expect(deleteFileCacheByDir).toHaveBeenCalledWith('mmPasteInput');
expect(deleteFileCacheByDir).toHaveBeenCalledWith('thumbnails');
});
it('should call deleteServerDatabase when removeServer=false', async () => {
await terminateSession(mockServerUrl, false);
expect(DatabaseManager.deleteServerDatabase).toHaveBeenCalledWith(mockServerUrl);
expect(DatabaseManager.destroyServerDatabase).not.toHaveBeenCalled();
expect(removePushDisabledInServerAcknowledged).not.toHaveBeenCalled();
expect(removePushSigningKey).not.toHaveBeenCalled();
});
it('should clear cookies for server', async () => {
await terminateSession(mockServerUrl, true);
expect(clearCookiesForServer).toHaveBeenCalledWith(mockServerUrl);
});
it('should clear image cache with URL-safe encoded server URL', async () => {
await terminateSession(mockServerUrl, true);
expect(urlSafeBase64Encode).toHaveBeenCalledWith(mockServerUrl);
});
it('should delete file caches for server and common directories', async () => {
await terminateSession(mockServerUrl, true);
expect(deleteFileCache).toHaveBeenCalledWith(mockServerUrl);
expect(deleteFileCacheByDir).toHaveBeenCalledWith('mmPasteInput');
expect(deleteFileCacheByDir).toHaveBeenCalledWith('thumbnails');
});
it('should reset locale with user locale when active server database exists', async () => {
const mockUser = {locale: 'es'};
const mockServerDatabase = {database: 'serverDb'} as unknown as ServerDatabase;
(DatabaseManager.serverDatabases as ServerDatabases) = {[mockServerUrl]: mockServerDatabase};
jest.mocked(DatabaseManager.getActiveServerDatabase).mockResolvedValue(mockServerDatabase as unknown as Database);
jest.mocked(getCurrentUser).mockResolvedValue(mockUser as unknown as UserModel);
await terminateSession(mockServerUrl, true);
// Wait for the async resetLocale to complete (not awaited in implementation)
await new Promise((resolve) => setImmediate(resolve));
expect(resetMomentLocale).toHaveBeenCalledWith('es');
});
it('should reset locale to default when no active server database', async () => {
(DatabaseManager.serverDatabases as ServerDatabases) = {};
await terminateSession(mockServerUrl, true);
// Wait for the async resetLocale to complete (not awaited in implementation)
await new Promise((resolve) => setImmediate(resolve));
expect(resetMomentLocale).toHaveBeenCalledWith();
});
it('should remove user credentials but preserve pre-auth secret on logout (removeServer=false)', async () => {
await terminateSession(mockServerUrl, false);
// Verify user credentials are removed
expect(removeServerCredentials).toHaveBeenCalledWith(mockServerUrl);
// Verify database is deleted (not destroyed)
expect(DatabaseManager.deleteServerDatabase).toHaveBeenCalledWith(mockServerUrl);
expect(DatabaseManager.destroyServerDatabase).not.toHaveBeenCalled();
// Note: removeServerCredentials now only removes the token, not the pre-auth secret
// The pre-auth secret is only removed when destroyServerDatabase is called
});
it('should call destroyServerDatabase which removes pre-auth secret on server removal (removeServer=true)', async () => {
await terminateSession(mockServerUrl, true);
// Verify database is destroyed (which will remove pre-auth secret internally)
expect(DatabaseManager.destroyServerDatabase).toHaveBeenCalledWith(mockServerUrl);
expect(DatabaseManager.deleteServerDatabase).not.toHaveBeenCalled();
// Note: destroyServerDatabase internally calls removePreauthSecret
});
});
});