From 96236f6a9ed4861742bf7dd37ea0c34e52190823 Mon Sep 17 00:00:00 2001 From: Carlos Santos <4a.santos@gmail.com> Date: Thu, 10 Apr 2025 16:47:47 +0200 Subject: [PATCH] backend: Refactor test server setup by moving to helpers module --- .../integration/api/security/auth.test.ts | 2 +- .../api/security/participant-security.test.ts | 2 +- .../api/security/recording-security.test.ts | 2 +- .../api/security/room-security.test.ts | 2 +- .../utils/{server-setup.ts => helpers.ts} | 45 ++++++++++++++++--- 5 files changed, 44 insertions(+), 9 deletions(-) rename backend/tests/utils/{server-setup.ts => helpers.ts} (53%) diff --git a/backend/tests/integration/api/security/auth.test.ts b/backend/tests/integration/api/security/auth.test.ts index 7437266..2539dce 100644 --- a/backend/tests/integration/api/security/auth.test.ts +++ b/backend/tests/integration/api/security/auth.test.ts @@ -1,7 +1,7 @@ import request from 'supertest'; import { describe, it, expect, beforeAll, afterAll } from '@jest/globals'; import { Express } from 'express'; -import { startTestServer, stopTestServer } from '../../../utils/server-setup.js'; +import { startTestServer, stopTestServer } from '../../../utils/helpers.js'; const INTERNAL_BASE_URL = '/meet/internal-api/v1'; const AUTH_URL = `${INTERNAL_BASE_URL}/auth`; diff --git a/backend/tests/integration/api/security/participant-security.test.ts b/backend/tests/integration/api/security/participant-security.test.ts index fcfc65b..f201b9e 100644 --- a/backend/tests/integration/api/security/participant-security.test.ts +++ b/backend/tests/integration/api/security/participant-security.test.ts @@ -1,7 +1,7 @@ import request from 'supertest'; import { describe, it, expect, beforeAll, afterAll } from '@jest/globals'; import { Express } from 'express'; -import { startTestServer, stopTestServer } from '../../../utils/server-setup.js'; +import { startTestServer, stopTestServer } from '../../../utils/helpers.js'; import { AuthMode, AuthType } from '../../../../src/typings/ce/index.js'; const BASE_URL = '/meet/api/v1'; diff --git a/backend/tests/integration/api/security/recording-security.test.ts b/backend/tests/integration/api/security/recording-security.test.ts index e654457..8cc4a8e 100644 --- a/backend/tests/integration/api/security/recording-security.test.ts +++ b/backend/tests/integration/api/security/recording-security.test.ts @@ -1,7 +1,7 @@ import request from 'supertest'; import { describe, it, expect, beforeAll, afterAll } from '@jest/globals'; import { Express } from 'express'; -import { startTestServer, stopTestServer } from '../../../utils/server-setup.js'; +import { startTestServer, stopTestServer } from '../../../utils/helpers.js'; import { AuthMode, AuthType } from '../../../../src/typings/ce/index.js'; const BASE_URL = '/meet/api/v1'; diff --git a/backend/tests/integration/api/security/room-security.test.ts b/backend/tests/integration/api/security/room-security.test.ts index 397899b..da84599 100644 --- a/backend/tests/integration/api/security/room-security.test.ts +++ b/backend/tests/integration/api/security/room-security.test.ts @@ -1,7 +1,7 @@ import request from 'supertest'; import { describe, it, expect, beforeAll, beforeEach, afterAll } from '@jest/globals'; import { Express } from 'express'; -import { startTestServer, stopTestServer } from '../../../utils/server-setup.js'; +import { startTestServer, stopTestServer } from '../../../utils/helpers.js'; import { AuthMode, AuthType } from '../../../../src/typings/ce/index.js'; const BASE_URL = '/meet/api/v1'; diff --git a/backend/tests/utils/server-setup.ts b/backend/tests/utils/helpers.ts similarity index 53% rename from backend/tests/utils/server-setup.ts rename to backend/tests/utils/helpers.ts index e558e06..8034fab 100644 --- a/backend/tests/utils/server-setup.ts +++ b/backend/tests/utils/helpers.ts @@ -1,14 +1,18 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { createApp, registerDependencies, initializeGlobalPreferences } from '../../src/server.js'; +import { createApp, registerDependencies } from '../../src/server.js'; import request from 'supertest'; import { Express } from 'express'; import { SERVER_PORT } from '../../src/environment.js'; import { Server } from 'http'; -let server: Server +let server: Server; const baseUrl = '/meet/health'; +const BASE_URL = '/meet/api/v1'; +const INTERNAL_BASE_URL = '/meet/internal-api/v1'; +const AUTH_URL = `${INTERNAL_BASE_URL}/auth`; + export const startTestServer = async (): Promise => { registerDependencies(); const app = createApp(); @@ -16,9 +20,6 @@ export const startTestServer = async (): Promise => { return await new Promise((resolve, reject) => { server = app.listen(SERVER_PORT, async () => { try { - // Initialize global preferences once the server is ready - await initializeGlobalPreferences(); - // Check if the server is responding by hitting the health check route const response = await request(app).get(baseUrl); @@ -58,3 +59,37 @@ export const stopTestServer = async (): Promise => { console.log('Server is not running.'); } }; + +export const login = async (app: Express, username?: string, password?: string) => { + const response = await request(app) + .post(`${AUTH_URL}/login`) + .send({ + username, + password + }) + .expect(200); + + const cookies = response.headers['set-cookie'] as unknown as string[]; + const accessTokenCookie = cookies.find((cookie) => cookie.startsWith('OvMeetAccessToken=')) as string; + return accessTokenCookie; +}; + +export const deleteAllRooms = async (app: Express) => { + let nextPageToken = ''; + + do { + const response: any = await request(app) + .get(`${BASE_URL}/rooms?fields=roomId&maxItems=100&nextPageToken=${nextPageToken}`) + // set header to accept json + .set('X-API-KEY', 'meet-api-key') + .expect(200); + + nextPageToken = response.body.pagination?.nextPageToken ?? undefined; + const roomIds = response.body.rooms.map((room: any) => room.roomId); + + await request(app) + .delete(`${BASE_URL}/rooms?roomIds=${roomIds.join(',')}`) + .set('X-API-KEY', 'meet-api-key'); + await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second + } while (nextPageToken); +};