🏗️(y-provider) organize yjs server

Many routes were in the server.ts file, now they
are in their own files in the handlers folder.
The server.ts file is now AppServer that handles
the routes.
We split as well the tests.
This commit is contained in:
Anthony LC
2024-12-15 00:03:13 +01:00
committed by Anthony LC
parent 33d1f3c151
commit e53465ce11
17 changed files with 560 additions and 457 deletions

View File

@@ -0,0 +1,76 @@
import request from 'supertest';
const port = 5555;
const origin = 'http://localhost:3000';
jest.mock('../src/env', () => {
return {
PORT: port,
COLLABORATION_SERVER_ORIGIN: origin,
COLLABORATION_SERVER_SECRET: 'test-secret-api-key',
};
});
console.error = jest.fn();
import { hocusPocusServer } from '@/servers/hocusPocusServer';
import { initServer } from '../src/servers/appServer';
const { app, server } = initServer();
describe('Server Tests', () => {
afterAll(() => {
server.close();
});
test('POST /collaboration/api/reset-connections?room=[ROOM_ID] invalid origin', async () => {
const response = await request(app as any)
.post('/collaboration/api/reset-connections/?room=test-room')
.set('Origin', 'http://invalid-origin.com')
.send({ document_id: 'test-document' });
expect(response.status).toBe(403);
expect(response.body.error).toBe('CORS policy violation: Invalid Origin');
});
test('POST /collaboration/api/reset-connections?room=[ROOM_ID] with incorrect API key should return 403', async () => {
const response = await request(app as any)
.post('/collaboration/api/reset-connections/?room=test-room')
.set('Origin', origin)
.set('Authorization', 'wrong-api-key');
expect(response.status).toBe(403);
expect(response.body.error).toBe('Forbidden: Invalid API Key');
});
test('POST /collaboration/api/reset-connections?room=[ROOM_ID] failed if room not indicated', async () => {
const response = await request(app as any)
.post('/collaboration/api/reset-connections/')
.set('Origin', origin)
.set('Authorization', 'test-secret-api-key')
.send({ document_id: 'test-document' });
expect(response.status).toBe(400);
expect(response.body.error).toBe('Room name not provided');
});
test('POST /collaboration/api/reset-connections?room=[ROOM_ID] with correct API key should reset connections', async () => {
// eslint-disable-next-line jest/unbound-method
const { closeConnections } = hocusPocusServer;
const mockHandleConnection = jest.fn();
(hocusPocusServer.closeConnections as jest.Mock) = mockHandleConnection;
const response = await request(app as any)
.post('/collaboration/api/reset-connections?room=test-room')
.set('Origin', origin)
.set('Authorization', 'test-secret-api-key');
expect(response.status).toBe(200);
expect(response.body.message).toBe('Connections reset');
expect(mockHandleConnection).toHaveBeenCalled();
mockHandleConnection.mockClear();
hocusPocusServer.closeConnections = closeConnections;
});
});