Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
134 lines
3.9 KiB
TypeScript
134 lines
3.9 KiB
TypeScript
import WS from 'jest-websocket-mock';
|
|
|
|
import { handleConnection, handleMethod } from './helpers';
|
|
import { DDPSDK } from '../src/DDPSDK';
|
|
|
|
let server: WS;
|
|
|
|
beforeEach(async () => {
|
|
server = new WS('ws://localhost:1234/websocket');
|
|
});
|
|
|
|
afterEach(() => {
|
|
server.close();
|
|
WS.clean();
|
|
});
|
|
|
|
describe('login', () => {
|
|
it('should save credentials to user object - loginWithToken', async () => {
|
|
const sdk = DDPSDK.create('ws://localhost:1234');
|
|
|
|
await handleConnection(server, sdk.connection.connect());
|
|
|
|
const messageResult = {
|
|
id: 123,
|
|
token: 'token',
|
|
tokenExpires: { $date: 99999999 },
|
|
};
|
|
|
|
await handleMethod(server, 'login', [{ resume: 'token' }], JSON.stringify(messageResult), sdk.account.loginWithToken('token'));
|
|
|
|
const { user } = sdk.account;
|
|
expect(user?.token).toBe(messageResult.token);
|
|
expect((user?.tokenExpires as Date)?.toISOString()).toBe(new Date(messageResult.tokenExpires.$date).toISOString());
|
|
expect(user?.id).toBe(messageResult.id);
|
|
});
|
|
|
|
it('should save credentials to user object - loginWithPassword', async () => {
|
|
const sdk = DDPSDK.create('ws://localhost:1234');
|
|
|
|
await handleConnection(server, sdk.connection.connect());
|
|
|
|
const messageResult = {
|
|
id: 123,
|
|
token: 'token',
|
|
tokenExpires: { $date: 99999999 },
|
|
};
|
|
|
|
await handleMethod(
|
|
server,
|
|
'login',
|
|
[{ user: { username: 'username' }, password: { digest: 'password', algorithm: 'sha-256' } }],
|
|
JSON.stringify(messageResult),
|
|
sdk.account.loginWithPassword('username', 'password'),
|
|
);
|
|
|
|
const { user } = sdk.account;
|
|
expect(user?.token).toBe(messageResult.token);
|
|
expect((user?.tokenExpires as Date)?.toISOString()).toBe(new Date(messageResult.tokenExpires.$date).toISOString());
|
|
expect(user?.id).toBe(messageResult.id);
|
|
});
|
|
|
|
it('should logout', async () => {
|
|
const sdk = DDPSDK.create('ws://localhost:1234');
|
|
|
|
await handleConnection(server, sdk.connection.connect());
|
|
|
|
const loginResult = {
|
|
id: '123',
|
|
token: 'token',
|
|
tokenExpires: { $date: 99999999 },
|
|
};
|
|
|
|
await handleMethod(server, 'login', [{ resume: 'token' }], JSON.stringify(loginResult), sdk.account.loginWithToken('token'));
|
|
|
|
const cb = jest.fn();
|
|
sdk.account.on('uid', cb);
|
|
|
|
await handleMethod(server, 'logout', [], '{}', sdk.account.logout());
|
|
|
|
// uid setter only fires on transitions, so logging out from a logged-in
|
|
// state emits exactly once (string → undefined).
|
|
expect(cb).toHaveBeenCalledTimes(1);
|
|
expect(cb).toHaveBeenCalledWith(undefined);
|
|
|
|
const { user } = sdk.account;
|
|
expect(user).toBeUndefined();
|
|
});
|
|
|
|
it('should fire onLogin / onLogout on uid transitions only', async () => {
|
|
const sdk = DDPSDK.create('ws://localhost:1234');
|
|
|
|
await handleConnection(server, sdk.connection.connect());
|
|
|
|
const onLogin = jest.fn();
|
|
const onLogout = jest.fn();
|
|
sdk.account.onLogin(onLogin);
|
|
sdk.account.onLogout(onLogout);
|
|
|
|
const loginResult = {
|
|
id: '123',
|
|
token: 'token',
|
|
tokenExpires: { $date: 99999999 },
|
|
};
|
|
await handleMethod(server, 'login', [{ resume: 'token' }], JSON.stringify(loginResult), sdk.account.loginWithToken('token'));
|
|
|
|
expect(onLogin).toHaveBeenCalledTimes(1);
|
|
expect(onLogout).not.toHaveBeenCalled();
|
|
|
|
await handleMethod(server, 'logout', [], '{}', sdk.account.logout());
|
|
|
|
expect(onLogin).toHaveBeenCalledTimes(1);
|
|
expect(onLogout).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should fan out emailVerificationLink and pageLoadLogin events', () => {
|
|
const sdk = DDPSDK.create('ws://localhost:1234');
|
|
|
|
const verify = jest.fn();
|
|
const pageLoad = jest.fn();
|
|
const stopVerify = sdk.account.onEmailVerificationLink(verify);
|
|
sdk.account.onPageLoadLogin(pageLoad);
|
|
|
|
sdk.account.emit('emailVerificationLink', 'tok-1');
|
|
sdk.account.emit('pageLoadLogin', { error: 'totp-required' });
|
|
|
|
expect(verify).toHaveBeenCalledWith('tok-1');
|
|
expect(pageLoad).toHaveBeenCalledWith({ error: 'totp-required' });
|
|
|
|
stopVerify();
|
|
sdk.account.emit('emailVerificationLink', 'tok-2');
|
|
expect(verify).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|