|
| 1 | +import { |
| 2 | + HocuspocusProvider, |
| 3 | + HocuspocusProviderWebsocket, |
| 4 | +} from '@hocuspocus/provider'; |
| 5 | +import request from 'supertest'; |
| 6 | +import WebSocket from 'ws'; |
| 7 | + |
| 8 | +const port = 5555; |
| 9 | +const portWS = 6666; |
| 10 | +const origin = 'http://localhost:3000'; |
| 11 | + |
| 12 | +jest.mock('../src/env', () => { |
| 13 | + return { |
| 14 | + PORT: port, |
| 15 | + COLLABORATION_SERVER_ORIGIN: origin, |
| 16 | + COLLABORATION_SERVER_SECRET: 'test-secret-api-key', |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +console.error = jest.fn(); |
| 21 | + |
| 22 | +import { promiseDone } from '../src/helpers'; |
| 23 | +import { hocuspocusServer, initServer } from '../src/server'; // Adjust the path to your server file |
| 24 | + |
| 25 | +const { app, server } = initServer(); |
| 26 | + |
| 27 | +describe('Server Tests', () => { |
| 28 | + beforeAll(async () => { |
| 29 | + await hocuspocusServer.configure({ port: portWS }).listen(); |
| 30 | + }); |
| 31 | + |
| 32 | + afterAll(() => { |
| 33 | + server.close(); |
| 34 | + void hocuspocusServer.destroy(); |
| 35 | + }); |
| 36 | + |
| 37 | + test('Ping Pong', async () => { |
| 38 | + const response = await request(app as any).get('/ping'); |
| 39 | + |
| 40 | + expect(response.status).toBe(200); |
| 41 | + expect(response.body.message).toBe('pong'); |
| 42 | + }); |
| 43 | + |
| 44 | + test('POST /collaboration/api/reset-connections?room=[ROOM_ID] invalid origin', async () => { |
| 45 | + const response = await request(app as any) |
| 46 | + .post('/collaboration/api/reset-connections/?room=test-room') |
| 47 | + .set('Origin', 'http://invalid-origin.com') |
| 48 | + .send({ document_id: 'test-document' }); |
| 49 | + |
| 50 | + expect(response.status).toBe(403); |
| 51 | + expect(response.body.error).toBe('CORS policy violation: Invalid Origin'); |
| 52 | + }); |
| 53 | + |
| 54 | + test('POST /collaboration/api/reset-connections?room=[ROOM_ID] with incorrect API key should return 403', async () => { |
| 55 | + const response = await request(app as any) |
| 56 | + .post('/collaboration/api/reset-connections/?room=test-room') |
| 57 | + .set('Origin', origin) |
| 58 | + .set('Authorization', 'wrong-api-key'); |
| 59 | + |
| 60 | + expect(response.status).toBe(403); |
| 61 | + expect(response.body.error).toBe('Forbidden: Invalid API Key'); |
| 62 | + }); |
| 63 | + |
| 64 | + test('POST /collaboration/api/reset-connections?room=[ROOM_ID] failed if room not indicated', async () => { |
| 65 | + const response = await request(app as any) |
| 66 | + .post('/collaboration/api/reset-connections/') |
| 67 | + .set('Origin', origin) |
| 68 | + .set('Authorization', 'test-secret-api-key') |
| 69 | + .send({ document_id: 'test-document' }); |
| 70 | + |
| 71 | + expect(response.status).toBe(400); |
| 72 | + expect(response.body.error).toBe('Room name not provided'); |
| 73 | + }); |
| 74 | + |
| 75 | + test('POST /collaboration/api/reset-connections?room=[ROOM_ID] with correct API key should reset connections', async () => { |
| 76 | + // eslint-disable-next-line jest/unbound-method |
| 77 | + const { closeConnections } = hocuspocusServer; |
| 78 | + const mockHandleConnection = jest.fn(); |
| 79 | + (hocuspocusServer.closeConnections as jest.Mock) = mockHandleConnection; |
| 80 | + |
| 81 | + const response = await request(app as any) |
| 82 | + .post('/collaboration/api/reset-connections?room=test-room') |
| 83 | + .set('Origin', origin) |
| 84 | + .set('Authorization', 'test-secret-api-key'); |
| 85 | + |
| 86 | + expect(response.status).toBe(200); |
| 87 | + expect(response.body.message).toBe('Connections reset'); |
| 88 | + |
| 89 | + expect(mockHandleConnection).toHaveBeenCalled(); |
| 90 | + mockHandleConnection.mockClear(); |
| 91 | + hocuspocusServer.closeConnections = closeConnections; |
| 92 | + }); |
| 93 | + |
| 94 | + ['/collaboration/api/anything/', '/', '/anything'].forEach((path) => { |
| 95 | + test(`"${path}" endpoint should be forbidden`, async () => { |
| 96 | + const response = await request(app as any).post(path); |
| 97 | + |
| 98 | + expect(response.status).toBe(403); |
| 99 | + expect(response.body.error).toBe('Forbidden'); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + test('WebSocket connection with correct API key can connect', () => { |
| 104 | + const { promise, done } = promiseDone(); |
| 105 | + |
| 106 | + // eslint-disable-next-line jest/unbound-method |
| 107 | + const { handleConnection } = hocuspocusServer; |
| 108 | + const mockHandleConnection = jest.fn(); |
| 109 | + (hocuspocusServer.handleConnection as jest.Mock) = mockHandleConnection; |
| 110 | + |
| 111 | + const clientWS = new WebSocket( |
| 112 | + `ws://localhost:${port}/collaboration/ws/?room=test-room`, |
| 113 | + { |
| 114 | + headers: { |
| 115 | + authorization: 'test-secret-api-key', |
| 116 | + Origin: origin, |
| 117 | + }, |
| 118 | + }, |
| 119 | + ); |
| 120 | + |
| 121 | + clientWS.on('open', () => { |
| 122 | + expect(mockHandleConnection).toHaveBeenCalled(); |
| 123 | + clientWS.close(); |
| 124 | + mockHandleConnection.mockClear(); |
| 125 | + hocuspocusServer.handleConnection = handleConnection; |
| 126 | + done(); |
| 127 | + }); |
| 128 | + |
| 129 | + return promise; |
| 130 | + }); |
| 131 | + |
| 132 | + test('WebSocket connection with bad origin should be closed', () => { |
| 133 | + const { promise, done } = promiseDone(); |
| 134 | + |
| 135 | + const ws = new WebSocket( |
| 136 | + `ws://localhost:${port}/collaboration/ws/?room=test-room`, |
| 137 | + { |
| 138 | + headers: { |
| 139 | + Origin: 'http://bad-origin.com', |
| 140 | + }, |
| 141 | + }, |
| 142 | + ); |
| 143 | + |
| 144 | + ws.onclose = () => { |
| 145 | + expect(ws.readyState).toBe(ws.CLOSED); |
| 146 | + done(); |
| 147 | + }; |
| 148 | + |
| 149 | + return promise; |
| 150 | + }); |
| 151 | + |
| 152 | + test('WebSocket connection with incorrect API key should be closed', () => { |
| 153 | + const { promise, done } = promiseDone(); |
| 154 | + const ws = new WebSocket( |
| 155 | + `ws://localhost:${port}/collaboration/ws/?room=test-room`, |
| 156 | + { |
| 157 | + headers: { |
| 158 | + Authorization: 'wrong-api-key', |
| 159 | + Origin: origin, |
| 160 | + }, |
| 161 | + }, |
| 162 | + ); |
| 163 | + |
| 164 | + ws.onclose = () => { |
| 165 | + expect(ws.readyState).toBe(ws.CLOSED); |
| 166 | + done(); |
| 167 | + }; |
| 168 | + |
| 169 | + return promise; |
| 170 | + }); |
| 171 | + |
| 172 | + test('WebSocket connection not allowed if room not matching provider name', () => { |
| 173 | + const { promise, done } = promiseDone(); |
| 174 | + |
| 175 | + const wsHocus = new HocuspocusProviderWebsocket({ |
| 176 | + url: `ws://localhost:${portWS}/?room=my-test`, |
| 177 | + WebSocketPolyfill: WebSocket, |
| 178 | + maxAttempts: 1, |
| 179 | + quiet: true, |
| 180 | + }); |
| 181 | + |
| 182 | + const provider = new HocuspocusProvider({ |
| 183 | + websocketProvider: wsHocus, |
| 184 | + name: 'hocuspocus-test', |
| 185 | + broadcast: false, |
| 186 | + quiet: true, |
| 187 | + preserveConnection: false, |
| 188 | + onClose: (data) => { |
| 189 | + wsHocus.stopConnectionAttempt(); |
| 190 | + expect(data.event.reason).toBe('Forbidden'); |
| 191 | + wsHocus.webSocket?.close(); |
| 192 | + wsHocus.disconnect(); |
| 193 | + provider.destroy(); |
| 194 | + wsHocus.destroy(); |
| 195 | + done(); |
| 196 | + }, |
| 197 | + }); |
| 198 | + |
| 199 | + return promise; |
| 200 | + }); |
| 201 | + |
| 202 | + test('WebSocket connection read-only', () => { |
| 203 | + const { promise, done } = promiseDone(); |
| 204 | + |
| 205 | + const wsHocus = new HocuspocusProviderWebsocket({ |
| 206 | + url: `ws://localhost:${portWS}/?room=hocuspocus-test`, |
| 207 | + WebSocketPolyfill: WebSocket, |
| 208 | + }); |
| 209 | + |
| 210 | + const provider = new HocuspocusProvider({ |
| 211 | + websocketProvider: wsHocus, |
| 212 | + name: 'hocuspocus-test', |
| 213 | + broadcast: false, |
| 214 | + quiet: true, |
| 215 | + onConnect: () => { |
| 216 | + void hocuspocusServer |
| 217 | + .openDirectConnection('hocuspocus-test') |
| 218 | + .then((connection) => { |
| 219 | + connection.document?.getConnections().forEach((connection) => { |
| 220 | + expect(connection.readOnly).toBe(true); |
| 221 | + }); |
| 222 | + |
| 223 | + void connection.disconnect(); |
| 224 | + }); |
| 225 | + |
| 226 | + provider.destroy(); |
| 227 | + wsHocus.destroy(); |
| 228 | + done(); |
| 229 | + }, |
| 230 | + }); |
| 231 | + |
| 232 | + return promise; |
| 233 | + }); |
| 234 | +}); |
0 commit comments