Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Jan 13, 2025
1 parent 1c5c672 commit f8a3575
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 56 deletions.
10 changes: 5 additions & 5 deletions apps/meteor/app/lib/server/functions/closeLivechatRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export const closeLivechatRoom = async (
throw new Error('error-invalid-room');
}

const subscription = await Subscriptions.findOneByRoomIdAndUserId(roomId, user._id, { projection: { _id: 1 } });
if (!subscription && !(await hasPermissionAsync(user._id, 'close-others-livechat-room'))) {
throw new Error('error-not-authorized');
}

const options: CloseRoomParams['options'] = {
clientAction: true,
tags,
Expand Down Expand Up @@ -73,11 +78,6 @@ export const closeLivechatRoom = async (
throw new Error('error-room-already-closed');
}

const subscription = await Subscriptions.findOneByRoomIdAndUserId(roomId, user._id, { projection: { _id: 1 } });
if (!subscription && !(await hasPermissionAsync(user._id, 'close-others-livechat-room'))) {
throw new Error('error-not-authorized');
}

return Livechat.closeRoom({
room,
user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,57 +59,6 @@ describe('closeLivechatRoom', () => {
expect(subscriptionsStub.removeByRoomId.notCalled).to.be.true;
});

it('should not perform any operation when a non-livechat room is provided', async () => {
livechatRoomsStub.findOneById.resolves({ ...room, t: 'c' });
subscriptionsStub.findOneByRoomIdAndUserId.resolves(subscription);
hasPermissionStub.resolves(true);

await expect(closeLivechatRoom(user, room._id, {})).to.be.rejectedWith('error-invalid-room');
expect(livechatStub.closeRoom.notCalled).to.be.true;
expect(livechatRoomsStub.findOneById.calledOnceWith(room._id)).to.be.true;
expect(subscriptionsStub.findOneByRoomIdAndUserId.notCalled).to.be.true;
expect(subscriptionsStub.removeByRoomId.notCalled).to.be.true;
});

it('should not perform any operation when a closed room with no subscriptions is provided and the caller is not subscribed to it', async () => {
livechatRoomsStub.findOneById.resolves({ ...room, open: false });
subscriptionsStub.removeByRoomId.resolves({ deletedCount: 0 });
subscriptionsStub.findOneByRoomIdAndUserId.resolves(null);
hasPermissionStub.resolves(true);

await expect(closeLivechatRoom(user, room._id, {})).to.be.rejectedWith('error-room-already-closed');
expect(livechatStub.closeRoom.notCalled).to.be.true;
expect(livechatRoomsStub.findOneById.calledOnceWith(room._id)).to.be.true;
expect(subscriptionsStub.findOneByRoomIdAndUserId.notCalled).to.be.true;
expect(subscriptionsStub.removeByRoomId.calledOnceWith(room._id)).to.be.true;
});

it('should remove dangling subscription when a closed room with subscriptions is provided and the caller is not subscribed to it', async () => {
livechatRoomsStub.findOneById.resolves({ ...room, open: false });
subscriptionsStub.removeByRoomId.resolves({ deletedCount: 1 });
subscriptionsStub.findOneByRoomIdAndUserId.resolves(null);
hasPermissionStub.resolves(true);

await closeLivechatRoom(user, room._id, {});
expect(livechatStub.closeRoom.notCalled).to.be.true;
expect(livechatRoomsStub.findOneById.calledOnceWith(room._id)).to.be.true;
expect(subscriptionsStub.findOneByRoomIdAndUserId.notCalled).to.be.true;
expect(subscriptionsStub.removeByRoomId.calledOnceWith(room._id)).to.be.true;
});

it('should remove dangling subscription when a closed room is provided but the user is still subscribed to it', async () => {
livechatRoomsStub.findOneById.resolves({ ...room, open: false });
subscriptionsStub.findOneByRoomIdAndUserId.resolves(subscription);
subscriptionsStub.removeByRoomId.resolves({ deletedCount: 1 });
hasPermissionStub.resolves(true);

await closeLivechatRoom(user, room._id, {});
expect(livechatStub.closeRoom.notCalled).to.be.true;
expect(livechatRoomsStub.findOneById.calledOnceWith(room._id)).to.be.true;
expect(subscriptionsStub.findOneByRoomIdAndUserId.notCalled).to.be.true;
expect(subscriptionsStub.removeByRoomId.calledOnceWith(room._id)).to.be.true;
});

it('should not perform any operation when the caller is not subscribed to an open room and does not have the permission to close others rooms', async () => {
livechatRoomsStub.findOneById.resolves(room);
subscriptionsStub.findOneByRoomIdAndUserId.resolves(null);
Expand Down Expand Up @@ -145,4 +94,32 @@ describe('closeLivechatRoom', () => {
expect(subscriptionsStub.findOneByRoomIdAndUserId.calledOnceWith(room._id, user._id)).to.be.true;
expect(subscriptionsStub.removeByRoomId.notCalled).to.be.true;
});

it('should call Livechat.closeRoom directly when forceClose is true even if room is in invalid state', async () => {
// Here we're using `open: false` as a way to simulate an invalid state. This should not happen in production. A room like this is effectively a "bad egg"
livechatRoomsStub.findOneById.resolves({ ...room, open: false });
subscriptionsStub.findOneByRoomIdAndUserId.resolves(subscription);
hasPermissionStub.resolves(true);

await closeLivechatRoom(user, room._id, { forceClose: true });

expect(
livechatStub.closeRoom.calledOnceWith(
sinon.match({ room: { ...room, open: false }, user, options: sinon.match({ clientAction: true }), forceClose: true }),
),
).to.be.true;
expect(livechatRoomsStub.findOneById.calledOnceWith(room._id)).to.be.true;
expect(subscriptionsStub.findOneByRoomIdAndUserId.calledOnceWith(room._id, user._id)).to.be.true;
});

it('should throw an error if forceClose is false and room is already closed', async () => {
livechatRoomsStub.findOneById.resolves({ ...room, open: false });
subscriptionsStub.findOneByRoomIdAndUserId.resolves(subscription);
hasPermissionStub.resolves(true);

await expect(closeLivechatRoom(user, room._id, { forceClose: false })).to.be.rejectedWith('error-room-already-closed');
expect(livechatStub.closeRoom.notCalled).to.be.true;
expect(livechatRoomsStub.findOneById.calledOnceWith(room._id)).to.be.true;
expect(subscriptionsStub.findOneByRoomIdAndUserId.calledOnceWith(room._id, user._id)).to.be.true;
});
});

0 comments on commit f8a3575

Please sign in to comment.