Skip to content
Merged
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
8 changes: 8 additions & 0 deletions app/lib/methods/__tests__/logout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jest.mock('~/lib/services/connect', () => ({
}));

jest.mock('~/lib/services/restApi', () => ({
forgetRegisteredPushToken: jest.fn(),
removePushToken: jest.fn()
}));

Expand Down Expand Up @@ -60,6 +61,7 @@ import { clearRemovedWorkspaceCertificate } from '~/lib/methods/helpers/sslPinni
import { useConferenceCallStore } from '~/lib/services/conference/useConferenceCallStore';
import sdk from '~/lib/services/sdk';
import { disconnect } from '~/lib/services/connect';
import { forgetRegisteredPushToken } from '~/lib/services/restApi';
import database from '~/lib/database';
import UserPreferences from '~/lib/methods/userPreferences';
import {
Expand Down Expand Up @@ -162,6 +164,12 @@ describe('removeServerData', () => {
expect(UserPreferences.getString(certificateKey(SERVER))).toBe('client-certificate');
});

it('forgets the push token registration of the removed server', async () => {
await removeServerData({ server: SERVER });

expect(forgetRegisteredPushToken).toHaveBeenCalledWith(SERVER);
});

it('skips the user token key when the server has no stored userId', async () => {
seedServer(SERVER);
UserPreferences.setString(tokenKey(USER_ID), `token-${USER_ID}`);
Expand Down
3 changes: 2 additions & 1 deletion app/lib/methods/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
getUserTokenKey
} from '../constants/keys';
import UserPreferences from './userPreferences';
import { removePushToken } from '../services/restApi';
import { forgetRegisteredPushToken, removePushToken } from '../services/restApi';
import { closeConferenceCall } from '../services/conference/conferenceCallNavigation';
import { clearServerCookies } from './helpers/setServerCookies';
import { clearRemovedWorkspaceCertificate } from './helpers/sslPinning';
Expand All @@ -39,6 +39,7 @@ function removeServerKeys({ server, userId }: { server: string; userId?: string
}

export async function removeServerData({ server }: { server: string }): Promise<void> {
forgetRegisteredPushToken(server);
try {
const batch: Model[] = [];
const serversDB = database.servers;
Expand Down
181 changes: 149 additions & 32 deletions app/lib/services/__tests__/restApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { mediaCallsStateSignals } from '../restApi';
const mockSdkGet = jest.fn();
const mockSdkPost = jest.fn();
const mockSdkDel = jest.fn();
let mockUniqueId: () => Promise<string> = () => Promise.resolve('unique-device-id');
let mockSdk!: SdkIntegration.IMockSdk;

jest.mock('../sdk', () => {
Expand All @@ -21,6 +22,7 @@ jest.mock('../sdk', () => {
});

const SDK_HOST = 'https://open.rocket.chat';
const OTHER_SDK_HOST = 'https://other.rocket.chat';

jest.mock('~/lib/notifications/deviceToken', () => ({
getDeviceToken: jest.fn()
Expand All @@ -35,7 +37,7 @@ jest.mock('~/lib/native/NativeVoip', () => ({

jest.mock('react-native-device-info', () => {
const mock = require('react-native-device-info/jest/react-native-device-info-mock');
const getUniqueId = jest.fn(() => Promise.resolve('unique-device-id'));
const getUniqueId = jest.fn(() => mockUniqueId());
const defaultExport = {
...mock,
getUniqueId
Expand Down Expand Up @@ -63,13 +65,12 @@ function loadPushTokenApi(platform: 'ios' | 'android' = 'android', mockServerVer
const notifications = require('~/lib/notifications/deviceToken');
// eslint-disable-next-line @typescript-eslint/no-require-imports
const voipNative = require('~/lib/native/NativeVoip').default;
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { registerPushToken, removePushToken } = require('../restApi');
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/consistent-type-imports
const { registerPushToken, removePushToken, forgetRegisteredPushToken } = require('../restApi') as typeof import('../restApi');
return {
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
registerPushToken: registerPushToken as typeof import('../restApi').registerPushToken,
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
removePushToken: removePushToken as typeof import('../restApi').removePushToken,
registerPushToken,
removePushToken,
forgetRegisteredPushToken,
getDeviceToken: jest.mocked(notifications.getDeviceToken),
getLastVoipToken: jest.mocked(voipNative.getLastVoipToken)
};
Expand Down Expand Up @@ -117,6 +118,7 @@ describe('registerPushToken', () => {

beforeEach(() => {
jest.clearAllMocks();
mockUniqueId = () => Promise.resolve('unique-device-id');
mockSdkPost.mockResolvedValue(undefined);
mockSdk.setClient({ host: SDK_HOST });
});
Expand Down Expand Up @@ -144,6 +146,32 @@ describe('registerPushToken', () => {
expect(mockSdkPost).not.toHaveBeenCalled();
});

it('does not post a VoIP token without a device push token', async () => {
const { registerPushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadPushTokenApi('ios', '8.4.0');
getToken.mockReturnValue('');
getVoip.mockReturnValue('voip-token');

await registerPushToken();

expect(mockSdkPost).not.toHaveBeenCalled();
});

it('posts both tokens again when a VoIP token arrives after the push token was registered', async () => {
const { registerPushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadPushTokenApi('ios', '8.4.0');
getToken.mockReturnValue('apns-token');
getVoip.mockReturnValue('');
await registerPushToken();

getVoip.mockReturnValue('voip-token');
await registerPushToken();

expect(mockSdkPost).toHaveBeenCalledTimes(2);
expect(mockSdkPost).toHaveBeenLastCalledWith(
'push.token',
expect.objectContaining({ value: 'apns-token', voipToken: 'voip-token' })
);
});

it('on iOS registers apn payload without voipToken when VoIP token is missing', async () => {
const { registerPushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadPushTokenApi('ios');
getToken.mockReturnValue('apns-token');
Expand Down Expand Up @@ -253,62 +281,151 @@ describe('registerPushToken', () => {
const payload = mockSdkPost.mock.calls[0][1] as Record<string, unknown>;
expect(Object.prototype.hasOwnProperty.call(payload, 'voipToken')).toBe(false);
});

it('records the workspace the post went to when the user switches while the device id is read', async () => {
const { registerPushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadPushTokenApi('ios');
getToken.mockReturnValue('apns-token');
getVoip.mockReturnValue('voip-token');
const postedHosts: (string | null)[] = [];
mockSdkPost.mockImplementation(() => {
postedHosts.push(mockSdk.host);
return Promise.resolve(undefined);
});
let resolveUniqueId!: (id: string) => void;
mockUniqueId = () => new Promise(resolve => (resolveUniqueId = resolve));

const registrationStartedOnA = registerPushToken();
await new Promise(resolve => setImmediate(resolve));
mockSdk.setClient({ host: OTHER_SDK_HOST });
resolveUniqueId('unique-device-id');
await registrationStartedOnA;
mockUniqueId = () => Promise.resolve('unique-device-id');
mockSdk.setClient({ host: SDK_HOST });
await registerPushToken();

expect(postedHosts).toEqual([OTHER_SDK_HOST, SDK_HOST]);
});

it('posts one registration at a time, so the newest token is the last one the server gets', async () => {
const { registerPushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadPushTokenApi('ios');
getVoip.mockReturnValue('');
let resolvePost!: () => void;
mockSdkPost.mockImplementationOnce(() => new Promise<void>(resolve => (resolvePost = resolve)));

getToken.mockReturnValue('first-token');
const loginRegistration = registerPushToken();
await new Promise(resolve => setImmediate(resolve));
getToken.mockReturnValue('refreshed-token');
const refreshRegistration = registerPushToken();
await new Promise(resolve => setImmediate(resolve));
expect(mockSdkPost).toHaveBeenCalledTimes(1);

resolvePost();
await Promise.all([loginRegistration, refreshRegistration]);

expect(mockSdkPost.mock.calls.map(([, data]) => data.value)).toEqual(['first-token', 'refreshed-token']);
});

it('registers the same token on every workspace the user signs in to', async () => {
const { registerPushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadPushTokenApi('ios');
getToken.mockReturnValue('apns-token');
getVoip.mockReturnValue('voip-token');

await registerPushToken();
mockSdk.setClient({ host: OTHER_SDK_HOST });
await registerPushToken();
mockSdk.setClient({ host: SDK_HOST });
await registerPushToken();

expect(mockSdkPost).toHaveBeenCalledTimes(2);
});
});

describe('removePushToken', () => {
beforeEach(() => {
jest.clearAllMocks();
mockSdkPost.mockResolvedValue(undefined);
mockSdkDel.mockResolvedValue({ success: true });
mockSdk.setClient({ host: SDK_HOST });
});

it('deletes the token on the server and forgets the registered tokens', async () => {
const { registerPushToken, removePushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadPushTokenApi('ios');
it('deletes the device token on the server', async () => {
const { removePushToken, getDeviceToken: getToken } = loadPushTokenApi('ios');
getToken.mockReturnValue('apns-token');
getVoip.mockReturnValue('voip-token');
await registerPushToken();
expect(mockSdkPost).toHaveBeenCalledTimes(1);

await removePushToken();

expect(mockSdkDel).toHaveBeenCalledWith('push.token', { token: 'apns-token' });
});

await registerPushToken();
it('skips the delete when the device has no token', async () => {
const { removePushToken, getDeviceToken: getToken } = loadPushTokenApi('ios');
getToken.mockReturnValue('');

expect(mockSdkPost).toHaveBeenCalledTimes(2);
await removePushToken();

expect(mockSdkDel).not.toHaveBeenCalled();
});

it('keeps the registered tokens when the device token is already gone', async () => {
const { registerPushToken, removePushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadPushTokenApi('ios');
it('skips the delete when there is no client', async () => {
const { removePushToken, getDeviceToken: getToken } = loadPushTokenApi('ios');
getToken.mockReturnValue('apns-token');
getVoip.mockReturnValue('voip-token');
await registerPushToken();
expect(mockSdkPost).toHaveBeenCalledTimes(1);
mockSdk.setClient(null);

getToken.mockReturnValue('');
await removePushToken();

expect(mockSdkDel).not.toHaveBeenCalled();
});
});

getToken.mockReturnValue('apns-token');
describe('forgetRegisteredPushToken', () => {
beforeEach(() => {
jest.clearAllMocks();
mockSdkPost.mockResolvedValue(undefined);
mockSdk.setClient({ host: SDK_HOST });
});

const loadSignedInDevice = () => {
const api = loadPushTokenApi('ios');
api.getDeviceToken.mockReturnValue('apns-token');
api.getLastVoipToken.mockReturnValue('voip-token');
return api;
};

it('registers again on a workspace that was removed and added back', async () => {
const { registerPushToken, forgetRegisteredPushToken } = loadSignedInDevice();
await registerPushToken();

expect(mockSdkPost).toHaveBeenCalledTimes(1);
forgetRegisteredPushToken(SDK_HOST);
await registerPushToken();

expect(mockSdkPost).toHaveBeenCalledTimes(2);
});

it('forgets the registered tokens even when there is no client to delete them from', async () => {
const { registerPushToken, removePushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadPushTokenApi('ios');
getToken.mockReturnValue('apns-token');
getVoip.mockReturnValue('voip-token');
it('keeps the registration of other workspaces', async () => {
const { registerPushToken, forgetRegisteredPushToken } = loadSignedInDevice();
await registerPushToken();
mockSdk.setClient({ host: OTHER_SDK_HOST });
await registerPushToken();
expect(mockSdkPost).toHaveBeenCalledTimes(1);

mockSdk.setClient(null);
await removePushToken();
expect(mockSdkDel).not.toHaveBeenCalled();

forgetRegisteredPushToken(OTHER_SDK_HOST);
mockSdk.setClient({ host: SDK_HOST });
await registerPushToken();

expect(mockSdkPost).toHaveBeenCalledTimes(2);
});

it('registers again after a workspace is forgotten while its registration is in flight', async () => {
const { registerPushToken, forgetRegisteredPushToken } = loadSignedInDevice();
let resolvePost!: () => void;
mockSdkPost.mockImplementationOnce(() => new Promise<void>(resolve => (resolvePost = resolve)));
const inFlightRegistration = registerPushToken();
await new Promise(resolve => setImmediate(resolve));

forgetRegisteredPushToken(SDK_HOST);
resolvePost();
await inFlightRegistration;
await registerPushToken();

expect(mockSdkPost).toHaveBeenCalledTimes(2);
});
});
Loading
Loading