From e45420ac0f46ca524c54936112161e5d50829500 Mon Sep 17 00:00:00 2001 From: juancarmore Date: Tue, 13 May 2025 12:09:57 +0200 Subject: [PATCH] test: add integration tests for retrieving room roles and permissions --- backend/tests/helpers/assertion-helpers.ts | 87 ++++++++++++++++++- backend/tests/helpers/request-helpers.ts | 22 ++++- .../api/rooms/get-room-roles.test.ts | 60 +++++++++++++ 3 files changed, 164 insertions(+), 5 deletions(-) create mode 100644 backend/tests/integration/api/rooms/get-room-roles.test.ts diff --git a/backend/tests/helpers/assertion-helpers.ts b/backend/tests/helpers/assertion-helpers.ts index 1013222..1c08ae3 100644 --- a/backend/tests/helpers/assertion-helpers.ts +++ b/backend/tests/helpers/assertion-helpers.ts @@ -5,7 +5,8 @@ import { MeetRecordingInfo, MeetRecordingStatus, MeetRoom, - MeetRoomPreferences + MeetRoomPreferences, + ParticipantRole } from '../../src/typings/ce'; const RECORDINGS_PATH = `${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/recordings`; @@ -427,3 +428,87 @@ export const expectSuccessListRecordingResponse = ( expect(response.body.pagination.maxItems).toBeLessThanOrEqual(100); expect(response.body.pagination.maxItems).toBe(maxItems); }; + +export const expectValidRoomRolesAndPermissionsResponse = (response: any, roomId: string) => { + expect(response.status).toBe(200); + expect(response.body).toEqual( + expect.arrayContaining([ + { + role: ParticipantRole.MODERATOR, + permissions: getPermissions(roomId, ParticipantRole.MODERATOR) + }, + { + role: ParticipantRole.PUBLISHER, + permissions: getPermissions(roomId, ParticipantRole.PUBLISHER) + } + ]) + ); +}; + +export const expectValidRoomRoleAndPermissionsResponse = ( + response: any, + roomId: string, + participantRole: ParticipantRole +) => { + expect(response.status).toBe(200); + expect(response.body).toEqual({ + role: participantRole, + permissions: getPermissions(roomId, participantRole) + }); +}; + +const getPermissions = (roomId: string, role: ParticipantRole) => { + switch (role) { + case ParticipantRole.MODERATOR: + return { + livekit: { + roomCreate: true, + roomJoin: true, + roomList: true, + roomRecord: true, + roomAdmin: true, + room: roomId, + ingressAdmin: true, + canPublish: true, + canSubscribe: true, + canPublishData: true, + canUpdateOwnMetadata: true, + hidden: false, + recorder: true, + agent: false + }, + openvidu: { + canPublishScreen: true, + canRecord: true, + canChat: true, + canChangeVirtualBackground: true + } + }; + case ParticipantRole.PUBLISHER: + return { + livekit: { + roomJoin: true, + roomList: true, + roomRecord: false, + roomAdmin: false, + room: roomId, + ingressAdmin: false, + canPublish: true, + canSubscribe: true, + canPublishData: true, + canUpdateOwnMetadata: true, + hidden: false, + recorder: false, + agent: false + }, + openvidu: { + canPublishScreen: true, + canRecord: false, + canChat: true, + canChangeVirtualBackground: true + } + }; + default: + throw new Error(`Unknown role ${role}`); + } +}; diff --git a/backend/tests/helpers/request-helpers.ts b/backend/tests/helpers/request-helpers.ts index 75aaf95..0034ff6 100644 --- a/backend/tests/helpers/request-helpers.ts +++ b/backend/tests/helpers/request-helpers.ts @@ -289,6 +289,24 @@ export const runReleaseActiveRecordingLock = async (roomId: string) => { await recordingService.releaseRecordingLockIfNoEgress(roomId); }; +export const getRoomRoles = async (roomId: string) => { + checkAppIsRunning(); + + const response = await request(app) + .get(`${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/rooms/${roomId}/roles`) + .send(); + return response; +}; + +export const getRoomRoleBySecret = async (roomId: string, secret: string) => { + checkAppIsRunning(); + + const response = await request(app) + .get(`${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/rooms/${roomId}/roles/${secret}`) + .send(); + return response; +}; + /** * Generates a participant token for a room and returns the cookie containing the token */ @@ -368,9 +386,6 @@ export const endMeeting = async (roomId: string, moderatorCookie: string) => { return response; }; -/** - * Generates a token for retrieving/deleting recordings from a room and returns the cookie containing the token - */ export const generateRecordingToken = async (roomId: string, secret: string) => { checkAppIsRunning(); @@ -379,7 +394,6 @@ export const generateRecordingToken = async (roomId: string, secret: string) => authMode: AuthMode.NONE }); - // Generate the recording token const response = await request(app) .post(`${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/rooms/${roomId}/recording-token`) .send({ diff --git a/backend/tests/integration/api/rooms/get-room-roles.test.ts b/backend/tests/integration/api/rooms/get-room-roles.test.ts new file mode 100644 index 0000000..fedf941 --- /dev/null +++ b/backend/tests/integration/api/rooms/get-room-roles.test.ts @@ -0,0 +1,60 @@ +import { afterAll, beforeAll, describe, expect, it } from '@jest/globals'; +import { ParticipantRole } from '../../../../src/typings/ce/index.js'; +import { + expectValidRoomRoleAndPermissionsResponse, + expectValidRoomRolesAndPermissionsResponse +} from '../../../helpers/assertion-helpers.js'; +import { + deleteAllRooms, + getRoomRoleBySecret, + getRoomRoles, + startTestServer +} from '../../../helpers/request-helpers.js'; +import { RoomData, setupSingleRoom } from '../../../helpers/test-scenarios.js'; + +describe('Room API Tests', () => { + let roomData: RoomData; + + beforeAll(async () => { + startTestServer(); + roomData = await setupSingleRoom(); + }); + + afterAll(async () => { + await deleteAllRooms(); + }); + + describe('Get Room Roles Tests', () => { + it('should retrieve all roles and associated permissions for a room', async () => { + const response = await getRoomRoles(roomData.room.roomId); + expectValidRoomRolesAndPermissionsResponse(response, roomData.room.roomId); + }); + + it('should return a 404 error if the room does not exist', async () => { + const response = await getRoomRoles('non-existent-room-id'); + expect(response.status).toBe(404); + }); + }); + + describe('Get Room Role Tests', () => { + it('should retrieve moderator role and associated permissions for a room with a valid moderator secret', async () => { + const response = await getRoomRoleBySecret(roomData.room.roomId, roomData.moderatorSecret); + expectValidRoomRoleAndPermissionsResponse(response, roomData.room.roomId, ParticipantRole.MODERATOR); + }); + + it('should retrieve publisher role and associated permissions for a room with a valid publisher secret', async () => { + const response = await getRoomRoleBySecret(roomData.room.roomId, roomData.publisherSecret); + expectValidRoomRoleAndPermissionsResponse(response, roomData.room.roomId, ParticipantRole.PUBLISHER); + }); + + it('should return a 404 error if the room does not exist', async () => { + const response = await getRoomRoleBySecret('non-existent-room-id', roomData.moderatorSecret); + expect(response.status).toBe(404); + }); + + it('should return a 400 error if the secret is invalid', async () => { + const response = await getRoomRoleBySecret(roomData.room.roomId, 'invalid-secret'); + expect(response.status).toBe(400); + }); + }); +});