n8n/packages/cli/test/unit/push/index.test.ts
कारतोफ्फेलस्क्रिप्ट™ 22990342df
refactor(core): Delete all collaboration related code (no-changelog) (#9929)
2024-07-03 18:46:24 +02:00

45 lines
1.5 KiB
TypeScript

import type { WebSocket } from 'ws';
import { mock } from 'jest-mock-extended';
import config from '@/config';
import type { User } from '@db/entities/User';
import { Push } from '@/push';
import { SSEPush } from '@/push/sse.push';
import { WebSocketPush } from '@/push/websocket.push';
import type { WebSocketPushRequest, SSEPushRequest } from '@/push/types';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { mockInstance } from '../../shared/mocking';
jest.unmock('@/push');
describe('Push', () => {
const user = mock<User>();
const sseBackend = mockInstance(SSEPush);
const wsBackend = mockInstance(WebSocketPush);
test('should validate pushRef on requests for websocket backend', () => {
config.set('push.backend', 'websocket');
const push = new Push(mock());
const ws = mock<WebSocket>();
const request = mock<WebSocketPushRequest>({ user, ws });
request.query = { pushRef: '' };
push.handleRequest(request, mock());
expect(ws.send).toHaveBeenCalled();
expect(ws.close).toHaveBeenCalledWith(1008);
expect(wsBackend.add).not.toHaveBeenCalled();
});
test('should validate pushRef on requests for SSE backend', () => {
config.set('push.backend', 'sse');
const push = new Push(mock());
const request = mock<SSEPushRequest>({ user, ws: undefined });
request.query = { pushRef: '' };
expect(() => push.handleRequest(request, mock())).toThrow(BadRequestError);
expect(sseBackend.add).not.toHaveBeenCalled();
});
});