2024-03-05 09:57:29 -08:00
|
|
|
import type { WebSocket } from 'ws';
|
2024-07-03 09:46:24 -07:00
|
|
|
import { mock } from 'jest-mock-extended';
|
|
|
|
|
2024-03-05 09:57:29 -08:00
|
|
|
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';
|
|
|
|
|
2024-07-03 09:46:24 -07:00
|
|
|
import { mockInstance } from '../../shared/mocking';
|
|
|
|
|
2024-03-05 09:57:29 -08:00
|
|
|
jest.unmock('@/push');
|
|
|
|
|
|
|
|
describe('Push', () => {
|
|
|
|
const user = mock<User>();
|
|
|
|
|
|
|
|
const sseBackend = mockInstance(SSEPush);
|
|
|
|
const wsBackend = mockInstance(WebSocketPush);
|
|
|
|
|
2024-04-03 04:43:14 -07:00
|
|
|
test('should validate pushRef on requests for websocket backend', () => {
|
2024-03-05 09:57:29 -08:00
|
|
|
config.set('push.backend', 'websocket');
|
2024-07-03 09:46:24 -07:00
|
|
|
const push = new Push(mock());
|
2024-03-05 09:57:29 -08:00
|
|
|
const ws = mock<WebSocket>();
|
|
|
|
const request = mock<WebSocketPushRequest>({ user, ws });
|
2024-04-03 04:43:14 -07:00
|
|
|
request.query = { pushRef: '' };
|
2024-03-05 09:57:29 -08:00
|
|
|
push.handleRequest(request, mock());
|
|
|
|
|
|
|
|
expect(ws.send).toHaveBeenCalled();
|
|
|
|
expect(ws.close).toHaveBeenCalledWith(1008);
|
|
|
|
expect(wsBackend.add).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2024-04-03 04:43:14 -07:00
|
|
|
test('should validate pushRef on requests for SSE backend', () => {
|
2024-03-05 09:57:29 -08:00
|
|
|
config.set('push.backend', 'sse');
|
2024-07-03 09:46:24 -07:00
|
|
|
const push = new Push(mock());
|
2024-03-05 09:57:29 -08:00
|
|
|
const request = mock<SSEPushRequest>({ user, ws: undefined });
|
2024-04-03 04:43:14 -07:00
|
|
|
request.query = { pushRef: '' };
|
2024-03-05 09:57:29 -08:00
|
|
|
expect(() => push.handleRequest(request, mock())).toThrow(BadRequestError);
|
|
|
|
|
|
|
|
expect(sseBackend.add).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|