@furystack/websocket-api
Version:
WebSocket API implementation for FuryStack
38 lines • 1.94 kB
JavaScript
import { createInjector } from '@furystack/inject';
import { HttpUserContext } from '@furystack/rest-service';
import { usingAsync } from '@furystack/utils';
import { describe, expect, it, vi } from 'vitest';
import { WhoAmI } from './whoami.js';
describe('WhoAmI action', () => {
const currentUser = { username: 'testuser' };
const contextMock = { getCurrentUser: async () => currentUser };
const request = { url: 'https://google.com' };
const wsMock = {
send: vi.fn(() => undefined),
};
it('cannot be executed if data does not match', () => {
expect(WhoAmI.canExecute({ request, data: 'asd', socket: wsMock })).toBeFalsy();
});
it('can be executed with whoami', () => {
expect(WhoAmI.canExecute({ request, data: 'whoami', socket: wsMock })).toBeTruthy();
});
it('can be executed with whoami /claims', () => {
expect(WhoAmI.canExecute({ request, data: 'whoami /claims', socket: wsMock })).toBeTruthy();
});
it('resolves HttpUserContext from the action injector and sends the current user', async () => {
await usingAsync(createInjector(), async (injector) => {
injector.bind(HttpUserContext, () => contextMock);
await WhoAmI.execute({ request, data: 'whoami', socket: wsMock, injector });
expect(wsMock.send).toBeCalledWith(JSON.stringify({ currentUser }));
});
});
it('sends null when HttpUserContext rejects', async () => {
await usingAsync(createInjector(), async (injector) => {
const failing = { getCurrentUser: async () => Promise.reject(new Error('no user')) };
injector.bind(HttpUserContext, () => failing);
await WhoAmI.execute({ request, data: 'whoami', socket: wsMock, injector });
expect(wsMock.send).toHaveBeenLastCalledWith(JSON.stringify({ currentUser: null }));
});
});
});
//# sourceMappingURL=whoami.spec.js.map