From 7182870625341902acde8ad256d6fdc07cc33ca7 Mon Sep 17 00:00:00 2001 From: juancarmore Date: Thu, 10 Apr 2025 18:50:54 +0200 Subject: [PATCH] tests: Add createRoom and generateParticipantToken helper functions --- backend/tests/utils/helpers.ts | 98 +++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 26 deletions(-) diff --git a/backend/tests/utils/helpers.ts b/backend/tests/utils/helpers.ts index 2a650bc..dbc018d 100644 --- a/backend/tests/utils/helpers.ts +++ b/backend/tests/utils/helpers.ts @@ -13,9 +13,9 @@ import { MEET_ADMIN_USER, MEET_ADMIN_SECRET } from '../../src/environment.js'; -import { AuthMode, AuthType, UserRole } from '../../src/typings/ce/index.js'; +import { AuthMode, AuthType, MeetRoom, UserRole } from '../../src/typings/ce/index.js'; -export const API_KEY_HEADER = 'X-Meet-API-Key'; +export const API_KEY_HEADER = 'X-API-Key'; const CREDENTIALS = { user: { @@ -32,7 +32,7 @@ let app: Express; let server: Server; /** - * Starts the test server. + * Starts the test server */ export const startTestServer = async (): Promise => { registerDependencies(); @@ -61,7 +61,7 @@ export const startTestServer = async (): Promise => { }; /** - * Stops the test server. + * Stops the test server */ export const stopTestServer = async (): Promise => { if (!server) { @@ -81,7 +81,36 @@ export const stopTestServer = async (): Promise => { }; /** - * Logs in a user as a specific role (admin or user) and returns the access token cookie. + * Updates global security preferences + */ +export const changeSecurityPreferences = async ( + adminCookie: string, + { usersCanCreateRooms = true, authRequired = true, authMode = AuthMode.NONE } +) => { + if (!app) { + throw new Error('App instance is not defined'); + } + + await request(app) + .put(`${MEET_INTERNAL_API_BASE_PATH_V1}/preferences/security`) + .set('Cookie', adminCookie) + .send({ + roomCreationPolicy: { + allowRoomCreation: usersCanCreateRooms, + requireAuthentication: authRequired + }, + authentication: { + authMode: authMode, + method: { + type: AuthType.SINGLE_USER + } + } + }) + .expect(200); +}; + +/** + * Logs in a user as a specific role (admin or user) and returns the access token cookie */ export const loginUserAsRole = async (role: UserRole): Promise => { if (!app) { @@ -99,6 +128,22 @@ export const loginUserAsRole = async (role: UserRole): Promise => { return accessTokenCookie; }; +/** + * Creates a room with the given prefix + */ +export const createRoom = async (roomPrefix?: string): Promise => { + if (!app) { + throw new Error('App instance is not defined'); + } + + const response = await request(app) + .post(`${MEET_API_BASE_PATH_V1}/rooms`) + .set(API_KEY_HEADER, MEET_API_KEY) + .send({ roomPrefix }) + .expect(200); + return response.body; +}; + /** * Deletes all rooms */ @@ -127,36 +172,37 @@ export const deleteAllRooms = async () => { .delete(`${MEET_API_BASE_PATH_V1}/rooms`) .query({ roomIds: roomIds.join(','), force: true }) .set(API_KEY_HEADER, MEET_API_KEY); - + await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second } while (nextPageToken); }; /** - * Updates global security preferences. + * Generates a participant token for a room and returns the cookie containing the token */ -export const changeSecurityPreferences = async ( +export const generateParticipantToken = async ( adminCookie: string, - { usersCanCreateRooms = true, authRequired = true, authMode = AuthMode.NONE } -) => { - if (!app) { - throw new Error('App instance is not defined'); - } + roomId: string, + participantName: string, + secret: string +): Promise => { + // Disable authentication to generate the token + await changeSecurityPreferences(adminCookie, { + authMode: AuthMode.NONE + }); - await request(app) - .put(`${MEET_INTERNAL_API_BASE_PATH_V1}/preferences/security`) - .set('Cookie', adminCookie) + // Generate the participant token + const response = await request(app) + .post(`${MEET_INTERNAL_API_BASE_PATH_V1}/participants/token`) .send({ - roomCreationPolicy: { - allowRoomCreation: usersCanCreateRooms, - requireAuthentication: authRequired - }, - authentication: { - authMode: authMode, - method: { - type: AuthType.SINGLE_USER - } - } + roomId, + participantName, + secret }) .expect(200); + + // Return the participant token cookie + const cookies = response.headers['set-cookie'] as unknown as string[]; + const participantTokenCookie = cookies.find((cookie) => cookie.startsWith('OvMeetParticipantToken=')) as string; + return participantTokenCookie; };