Skip to content

Commit

Permalink
Feat/backend/direct message tests (#179)
Browse files Browse the repository at this point in the history
* [backend] Add direct room tests for `GET /rooms/:roomId` API
* [backend] Modify test descriptions for `POST /rooms/:roomId/invite/:userId`
* [backend] Add direct room tests for `GET /rooms` API
* [backend] Add direct room tests for `PATCH /rooms/:roomId`
* [backend] Fix UpdateRoomGuard for DIRECT room
- Add exhaustive check for existing/updating accessLevel
* [backend] Add direct room tests for `DELETE /room/:id`
* [backend] Set default role to OWNER for DIRECT rooms so that both users in the room can delete the room
* [backend] Add direct room tests for leave and kick
* [backend] Add direct room tests for `PATCH /room/:id/:userId` API
  • Loading branch information
usatie authored Dec 27, 2023
1 parent a4f3ce9 commit 91abf0a
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 63 deletions.
68 changes: 48 additions & 20 deletions backend/src/room/guards/update-room.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,54 @@ export class UpdateRoomGuard implements CanActivate {
}
const room = await this.roomService.findRoom(Number(roomId));
const dto: UpdateRoomDto = req.body;
// Remove password from PROTECTED by changing accessLevel to PUBLIC/PRIVATE is ok
if (
room.accessLevel === 'PROTECTED' &&
dto.accessLevel !== 'PROTECTED' &&
!dto.password
) {
return true;
switch (room.accessLevel) {
case 'PUBLIC':
case 'PRIVATE':
switch (dto.accessLevel) {
case 'PUBLIC':
case 'PRIVATE':
case undefined:
if (dto.password) {
throw new BadRequestException(
'cannot set password for PUBLIC/PRIVATE room',
);
}
return true;
case 'PROTECTED':
if (!dto.password) {
throw new BadRequestException('password is required');
}
return true;
case 'DIRECT':
throw new BadRequestException('cannot update to DIRECT');
default:
throw new BadRequestException('unreachable');
}
case 'PROTECTED':
switch (dto.accessLevel) {
case 'PUBLIC':
case 'PRIVATE':
if (dto.password) {
throw new BadRequestException(
'cannot set password for PUBLIC/PRIVATE room',
);
}
return true;
case 'PROTECTED':
case undefined:
if (dto.password === null || dto.password === '') {
throw new BadRequestException('password cannot be empty');
}
return true;
case 'DIRECT':
throw new BadRequestException('cannot update to DIRECT');
default:
throw new BadRequestException('unreachable');
}
case 'DIRECT':
throw new BadRequestException('cannot update DIRECT room');
default:
throw new BadRequestException('unreachable');
}

const updated = { ...room, ...dto };
// non-PROTECTED room must not have password
if (updated.accessLevel !== 'PROTECTED' && updated.password) {
throw new BadRequestException(
'password is only allowed for PROTECTED rooms',
);
}
// PROTECTED room must have password
if (updated.accessLevel === 'PROTECTED' && !updated.password) {
throw new BadRequestException('password is required');
}
return true;
}
}
11 changes: 10 additions & 1 deletion backend/src/room/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export class RoomService {
if (createRoomDto.accessLevel === 'DIRECT' && userIds.length !== 1) {
throw new BadRequestException('Direct room should have only one user');
}
// If accessLevel is DIRECT, set defaultRole to OWNER
const defaultRole =
createRoomDto.accessLevel === 'DIRECT' ? Role.OWNER : Role.MEMBER;

const room = await this.prisma.room.create({
data: {
Expand All @@ -43,7 +46,7 @@ export class RoomService {
},
...userIds.map((userId) => ({
userId: userId,
role: Role.MEMBER,
role: defaultRole,
})),
],
},
Expand Down Expand Up @@ -205,6 +208,12 @@ export class RoomService {
};

async kickUser(roomId: number, userId: number): Promise<UserOnRoomEntity> {
const room = await this.prisma.room.findUniqueOrThrow({
where: { id: roomId },
});
if (room.accessLevel === 'DIRECT') {
throw new ForbiddenException('Direct room cannot kick/leave user');
}
const deletedUserOnRoom = await this.prisma.userOnRoom.delete({
where: {
userId_roomId_unique: {
Expand Down
Loading

0 comments on commit 91abf0a

Please sign in to comment.