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
39 changes: 38 additions & 1 deletion app/lib/methods/__tests__/logout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ jest.mock('~/lib/methods/helpers/sslPinning', () => ({

const mockSdkLogout = jest.fn();

const mockWorkspaceClient = {
login: jest.fn(),
del: jest.fn(),
logout: jest.fn(),
disconnect: jest.fn(() => Promise.resolve())
};

jest.mock('@rocket.chat/sdk', () => ({
Rocketchat: jest.fn(() => mockWorkspaceClient),
settings: {}
}));

jest.mock('~/lib/services/sdk', () => {
const { makeSdkMock } = jest.requireActual<typeof SdkIntegration>('~/lib/testUtils/sdkIntegration');
return { __esModule: true, default: makeSdkMock({ logout: () => mockSdkLogout() }) };
Expand All @@ -57,7 +69,8 @@ import {
E2E_PUBLIC_KEY,
E2E_RANDOM_PASSWORD_KEY,
TOKEN_KEY,
getBasicAuthKey
getBasicAuthKey,
getUserTokenKey
} from '~/lib/constants/keys';

const mockSdk = sdk as unknown as SdkIntegration.IMockSdk;
Expand All @@ -66,6 +79,7 @@ const SERVER = 'https://a.rocket.chat';
const OTHER_SERVER = 'https://b.rocket.chat';
const USER_ID = 'user-a';
const OTHER_USER_ID = 'user-b';
const RESUME_TOKEN = 'resume-token-a';

const tokenKey = (suffix: string): string => `${TOKEN_KEY}-${suffix}`;
const certificateKey = (server: string): string => `${CERTIFICATE_KEY}-${server}`;
Expand All @@ -85,6 +99,7 @@ const keysToClear = [
tokenKey(USER_ID),
tokenKey(OTHER_USER_ID),
certificateKey(SERVER),
getUserTokenKey(SERVER, USER_ID),
CURRENT_SERVER
];

Expand Down Expand Up @@ -173,6 +188,28 @@ describe('removeServer', () => {
jest.mocked(clearRemovedWorkspaceCertificate).mock.invocationCallOrder[0]
);
});

it('closes the socket it opened to log out of the removed workspace', async () => {
seedServer(SERVER, USER_ID);
UserPreferences.setString(getUserTokenKey(SERVER, USER_ID), RESUME_TOKEN);

await removeServer({ server: SERVER });

expect(mockWorkspaceClient.login).toHaveBeenCalledWith({ resume: RESUME_TOKEN });
expect(mockWorkspaceClient.logout).toHaveBeenCalled();
expect(mockWorkspaceClient.disconnect).toHaveBeenCalledTimes(1);
});

it('closes the socket when the login to the removed workspace fails', async () => {
seedServer(SERVER, USER_ID);
UserPreferences.setString(getUserTokenKey(SERVER, USER_ID), RESUME_TOKEN);
mockWorkspaceClient.login.mockRejectedValueOnce(new Error('invalid token'));

await removeServer({ server: SERVER });

expect(mockWorkspaceClient.logout).not.toHaveBeenCalled();
expect(mockWorkspaceClient.disconnect).toHaveBeenCalledTimes(1);
});
});

describe('logout', () => {
Expand Down
10 changes: 6 additions & 4 deletions app/lib/methods/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,20 @@ export async function removeServerDatabase({ server }: { server: string }): Prom
}

async function logoutFromServer(server: string, resume: string): Promise<void> {
const workspaceClient = new RocketchatClient({ host: server, protocol: 'ddp', useSsl: isSsl(server) });
try {
const sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl: isSsl(server) });
await sdk.login({ resume });
await workspaceClient.login({ resume });

const token = getDeviceToken();
if (token) {
await sdk.del('push.token', { token });
await workspaceClient.del('push.token', { token });
}

await sdk.logout();
await workspaceClient.logout();
} catch (e) {
log(e);
} finally {
workspaceClient.disconnect().catch(log);
}
}

Expand Down
Loading