tests: Replace app instance checks with a centralized checkAppIsRunning function

This commit is contained in:
juancarmore 2025-05-05 10:47:09 +02:00
parent 7d75d7948e
commit e5242d16b9

View File

@ -58,9 +58,7 @@ export const changeSecurityPreferences = async (
adminCookie: string, adminCookie: string,
{ usersCanCreateRooms = true, authRequired = true, authMode = AuthMode.NONE } { usersCanCreateRooms = true, authRequired = true, authMode = AuthMode.NONE }
) => { ) => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
await request(app) await request(app)
.put(`${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/preferences/security`) .put(`${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/preferences/security`)
@ -84,9 +82,7 @@ export const changeSecurityPreferences = async (
* Logs in a user as a specific role (admin or user) and returns the access token cookie * 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) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
const credentials = role === UserRole.ADMIN ? CREDENTIALS.admin : CREDENTIALS.user; const credentials = role === UserRole.ADMIN ? CREDENTIALS.admin : CREDENTIALS.user;
const response = await request(app) const response = await request(app)
@ -95,7 +91,9 @@ export const loginUserAsRole = async (role: UserRole): Promise<string> => {
.expect(200); .expect(200);
const cookies = response.headers['set-cookie'] as unknown as string[]; const cookies = response.headers['set-cookie'] as unknown as string[];
const accessTokenCookie = cookies.find((cookie) => cookie.startsWith('OvMeetAccessToken=')) as string; const accessTokenCookie = cookies.find((cookie) =>
cookie.startsWith(`${INTERNAL_CONFIG.ACCESS_TOKEN_COOKIE_NAME}=`)
) as string;
return accessTokenCookie; return accessTokenCookie;
}; };
@ -103,9 +101,7 @@ export const loginUserAsRole = async (role: UserRole): Promise<string> => {
* Creates a room with the given prefix * Creates a room with the given prefix
*/ */
export const createRoom = async (options: MeetRoomOptions = {}): Promise<MeetRoom> => { export const createRoom = async (options: MeetRoomOptions = {}): Promise<MeetRoom> => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
const response = await request(app) const response = await request(app)
.post(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/rooms`) .post(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/rooms`)
@ -120,9 +116,7 @@ export const createRoom = async (options: MeetRoomOptions = {}): Promise<MeetRoo
* Returns the parsed response. * Returns the parsed response.
*/ */
export const getRooms = async (query: Record<string, any> = {}) => { export const getRooms = async (query: Record<string, any> = {}) => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
return await request(app) return await request(app)
.get(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/rooms`) .get(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/rooms`)
@ -131,9 +125,7 @@ export const getRooms = async (query: Record<string, any> = {}) => {
}; };
export const updateRoomPreferences = async (roomId: string, preferences: any) => { export const updateRoomPreferences = async (roomId: string, preferences: any) => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
const userCookie = await loginUserAsRole(UserRole.ADMIN); const userCookie = await loginUserAsRole(UserRole.ADMIN);
return await request(app) return await request(app)
@ -151,9 +143,7 @@ export const updateRoomPreferences = async (roomId: string, preferences: any) =>
* @throws Error if the app instance is not defined * @throws Error if the app instance is not defined
*/ */
export const getRoom = async (roomId: string, fields?: string) => { export const getRoom = async (roomId: string, fields?: string) => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
return await request(app) return await request(app)
.get(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/rooms/${roomId}`) .get(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/rooms/${roomId}`)
@ -162,9 +152,7 @@ export const getRoom = async (roomId: string, fields?: string) => {
}; };
export const deleteRoom = async (roomId: string, query: Record<string, any> = {}) => { export const deleteRoom = async (roomId: string, query: Record<string, any> = {}) => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
return await request(app) return await request(app)
.delete(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/rooms/${roomId}`) .delete(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/rooms/${roomId}`)
@ -173,9 +161,7 @@ export const deleteRoom = async (roomId: string, query: Record<string, any> = {}
}; };
export const bulkDeleteRooms = async (roomIds: any[], force?: any) => { export const bulkDeleteRooms = async (roomIds: any[], force?: any) => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
return await request(app) return await request(app)
.delete(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/rooms`) .delete(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/rooms`)
@ -191,18 +177,14 @@ export const bulkDeleteRooms = async (roomIds: any[], force?: any) => {
* It then waits for 1 second before completing. * It then waits for 1 second before completing.
*/ */
export const runRoomGarbageCollector = async () => { export const runRoomGarbageCollector = async () => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
const roomService = container.get(RoomService); const roomService = container.get(RoomService);
await (roomService as any)['deleteExpiredRooms'](); await (roomService as any)['deleteExpiredRooms']();
}; };
export const runReleaseActiveRecordingLock = async (roomId: string) => { export const runReleaseActiveRecordingLock = async (roomId: string) => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
const recordingService = container.get(RecordingService); const recordingService = container.get(RecordingService);
await recordingService.releaseRecordingLockIfNoEgress(roomId); await recordingService.releaseRecordingLockIfNoEgress(roomId);
@ -212,9 +194,7 @@ export const runReleaseActiveRecordingLock = async (roomId: string) => {
* Deletes all rooms * Deletes all rooms
*/ */
export const deleteAllRooms = async () => { export const deleteAllRooms = async () => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
let nextPageToken: string | undefined; let nextPageToken: string | undefined;
@ -248,6 +228,8 @@ export const generateParticipantToken = async (
participantName: string, participantName: string,
secret: string secret: string
): Promise<string> => { ): Promise<string> => {
checkAppIsRunning();
// Disable authentication to generate the token // Disable authentication to generate the token
await changeSecurityPreferences(adminCookie, { await changeSecurityPreferences(adminCookie, {
authMode: AuthMode.NONE authMode: AuthMode.NONE
@ -265,7 +247,9 @@ export const generateParticipantToken = async (
// Return the participant token cookie // Return the participant token cookie
const cookies = response.headers['set-cookie'] as unknown as string[]; const cookies = response.headers['set-cookie'] as unknown as string[];
const participantTokenCookie = cookies.find((cookie) => cookie.startsWith('OvMeetParticipantToken=')) as string; const participantTokenCookie = cookies.find((cookie) =>
cookie.startsWith(`${INTERNAL_CONFIG.PARTICIPANT_TOKEN_COOKIE_NAME}=`)
) as string;
return participantTokenCookie; return participantTokenCookie;
}; };
@ -328,9 +312,7 @@ export const disconnectFakeParticipants = async () => {
}; };
export const startRecording = async (roomId: string, moderatorCookie = '') => { export const startRecording = async (roomId: string, moderatorCookie = '') => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
return await request(app) return await request(app)
.post(`${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/recordings`) .post(`${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/recordings`)
@ -341,9 +323,7 @@ export const startRecording = async (roomId: string, moderatorCookie = '') => {
}; };
export const stopRecording = async (recordingId: string, moderatorCookie = '') => { export const stopRecording = async (recordingId: string, moderatorCookie = '') => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
const response = await request(app) const response = await request(app)
.post(`${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/recordings/${recordingId}/stop`) .post(`${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/recordings/${recordingId}/stop`)
@ -355,9 +335,7 @@ export const stopRecording = async (recordingId: string, moderatorCookie = '') =
}; };
export const getRecording = async (recordingId: string) => { export const getRecording = async (recordingId: string) => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
return await request(app) return await request(app)
.get(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/recordings/${recordingId}`) .get(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/recordings/${recordingId}`)
@ -365,9 +343,7 @@ export const getRecording = async (recordingId: string) => {
}; };
export const deleteRecording = async (recordingId: string) => { export const deleteRecording = async (recordingId: string) => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
return await request(app) return await request(app)
.delete(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/recordings/${recordingId}`) .delete(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/recordings/${recordingId}`)
@ -375,9 +351,7 @@ export const deleteRecording = async (recordingId: string) => {
}; };
export const getRecordingMedia = async (recordingId: string, range?: string) => { export const getRecordingMedia = async (recordingId: string, range?: string) => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
const req = request(app) const req = request(app)
.get(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/recordings/${recordingId}/media`) .get(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/recordings/${recordingId}/media`)
@ -391,9 +365,7 @@ export const getRecordingMedia = async (recordingId: string, range?: string) =>
}; };
export const stopAllRecordings = async (moderatorCookie: string) => { export const stopAllRecordings = async (moderatorCookie: string) => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
const response = await getAllRecordings({ fields: 'recordingId' }); const response = await getAllRecordings({ fields: 'recordingId' });
const recordingIds: string[] = response.body.recordings.map( const recordingIds: string[] = response.body.recordings.map(
@ -416,9 +388,7 @@ export const stopAllRecordings = async (moderatorCookie: string) => {
}; };
export const getAllRecordings = async (query: Record<string, any> = {}) => { export const getAllRecordings = async (query: Record<string, any> = {}) => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
return await request(app) return await request(app)
.get(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/recordings`) .get(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/recordings`)
@ -427,9 +397,7 @@ export const getAllRecordings = async (query: Record<string, any> = {}) => {
}; };
export const bulkDeleteRecordings = async (recordingIds: any[]): Promise<Response> => { export const bulkDeleteRecordings = async (recordingIds: any[]): Promise<Response> => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
const response = await request(app) const response = await request(app)
.delete(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/recordings`) .delete(`${INTERNAL_CONFIG.API_BASE_PATH_V1}/recordings`)
@ -439,9 +407,7 @@ export const bulkDeleteRecordings = async (recordingIds: any[]): Promise<Respons
}; };
export const deleteAllRecordings = async () => { export const deleteAllRecordings = async () => {
if (!app) { checkAppIsRunning();
throw new Error('App instance is not defined');
}
let nextPageToken: string | undefined; let nextPageToken: string | undefined;
@ -468,6 +434,12 @@ export const deleteAllRecordings = async () => {
// PRIVATE METHODS // PRIVATE METHODS
const checkAppIsRunning = () => {
if (!app) {
throw new Error('App instance is not defined');
}
};
const runCommandSync = (command: string): string => { const runCommandSync = (command: string): string => {
try { try {
const stdout = execSync(command, { encoding: 'utf-8' }); const stdout = execSync(command, { encoding: 'utf-8' });