tests: Add createRoom and generateParticipantToken helper functions

This commit is contained in:
juancarmore 2025-04-10 18:50:54 +02:00
parent 7590e2bb15
commit 7182870625

View File

@ -13,9 +13,9 @@ import {
MEET_ADMIN_USER, MEET_ADMIN_USER,
MEET_ADMIN_SECRET MEET_ADMIN_SECRET
} from '../../src/environment.js'; } 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 = { const CREDENTIALS = {
user: { user: {
@ -32,7 +32,7 @@ let app: Express;
let server: Server; let server: Server;
/** /**
* Starts the test server. * Starts the test server
*/ */
export const startTestServer = async (): Promise<Express> => { export const startTestServer = async (): Promise<Express> => {
registerDependencies(); registerDependencies();
@ -61,7 +61,7 @@ export const startTestServer = async (): Promise<Express> => {
}; };
/** /**
* Stops the test server. * Stops the test server
*/ */
export const stopTestServer = async (): Promise<void> => { export const stopTestServer = async (): Promise<void> => {
if (!server) { if (!server) {
@ -81,7 +81,36 @@ export const stopTestServer = async (): Promise<void> => {
}; };
/** /**
* 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<string> => { export const loginUserAsRole = async (role: UserRole): Promise<string> => {
if (!app) { if (!app) {
@ -99,6 +128,22 @@ export const loginUserAsRole = async (role: UserRole): Promise<string> => {
return accessTokenCookie; return accessTokenCookie;
}; };
/**
* Creates a room with the given prefix
*/
export const createRoom = async (roomPrefix?: string): Promise<MeetRoom> => {
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 * Deletes all rooms
*/ */
@ -127,36 +172,37 @@ export const deleteAllRooms = async () => {
.delete(`${MEET_API_BASE_PATH_V1}/rooms`) .delete(`${MEET_API_BASE_PATH_V1}/rooms`)
.query({ roomIds: roomIds.join(','), force: true }) .query({ roomIds: roomIds.join(','), force: true })
.set(API_KEY_HEADER, MEET_API_KEY); .set(API_KEY_HEADER, MEET_API_KEY);
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second
} while (nextPageToken); } 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, adminCookie: string,
{ usersCanCreateRooms = true, authRequired = true, authMode = AuthMode.NONE } roomId: string,
) => { participantName: string,
if (!app) { secret: string
throw new Error('App instance is not defined'); ): Promise<string> => {
} // Disable authentication to generate the token
await changeSecurityPreferences(adminCookie, {
authMode: AuthMode.NONE
});
await request(app) // Generate the participant token
.put(`${MEET_INTERNAL_API_BASE_PATH_V1}/preferences/security`) const response = await request(app)
.set('Cookie', adminCookie) .post(`${MEET_INTERNAL_API_BASE_PATH_V1}/participants/token`)
.send({ .send({
roomCreationPolicy: { roomId,
allowRoomCreation: usersCanCreateRooms, participantName,
requireAuthentication: authRequired secret
},
authentication: {
authMode: authMode,
method: {
type: AuthType.SINGLE_USER
}
}
}) })
.expect(200); .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;
}; };