|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { beforeEach, describe, it } from 'mocha'; |
| 3 | +import proxyquire from 'proxyquire'; |
| 4 | +import sinon from 'sinon'; |
| 5 | + |
| 6 | +const Rooms = { |
| 7 | + findDefaultRoomsForTeam: sinon.stub(), |
| 8 | +}; |
| 9 | + |
| 10 | +const Users = { |
| 11 | + findActiveByIds: sinon.stub(), |
| 12 | +}; |
| 13 | + |
| 14 | +const addUserToRoom = sinon.stub(); |
| 15 | + |
| 16 | +const { TeamService } = proxyquire.noCallThru().load('../../../../../server/services/team/service', { |
| 17 | + '@rocket.chat/core-services': { |
| 18 | + Room: {}, |
| 19 | + Authorization: {}, |
| 20 | + Message: {}, |
| 21 | + ServiceClassInternal: class {}, |
| 22 | + api: {}, |
| 23 | + }, |
| 24 | + '@rocket.chat/models': { |
| 25 | + Team: {}, |
| 26 | + Rooms, |
| 27 | + Subscriptions: {}, |
| 28 | + Users, |
| 29 | + TeamMember: {}, |
| 30 | + }, |
| 31 | + '@rocket.chat/string-helpers': { |
| 32 | + escapeRegExp: (value: string) => value, |
| 33 | + }, |
| 34 | + '../../../app/channel-settings/server': { |
| 35 | + saveRoomName: sinon.stub(), |
| 36 | + }, |
| 37 | + '../../../app/channel-settings/server/functions/saveRoomType': { |
| 38 | + saveRoomType: sinon.stub(), |
| 39 | + }, |
| 40 | + '../../../app/lib/server/functions/addUserToRoom': { |
| 41 | + addUserToRoom, |
| 42 | + }, |
| 43 | + '../../../app/lib/server/functions/checkUsernameAvailability': { |
| 44 | + checkUsernameAvailability: sinon.stub(), |
| 45 | + }, |
| 46 | + '../../../app/lib/server/functions/getRoomsWithSingleOwner': { |
| 47 | + getSubscribedRoomsForUserWithDetails: sinon.stub(), |
| 48 | + }, |
| 49 | + '../../../app/lib/server/functions/removeUserFromRoom': { |
| 50 | + removeUserFromRoom: sinon.stub(), |
| 51 | + }, |
| 52 | + '../../../app/lib/server/lib/notifyListener': { |
| 53 | + notifyOnSubscriptionChangedByRoomIdAndUserId: sinon.stub(), |
| 54 | + notifyOnRoomChangedById: sinon.stub(), |
| 55 | + }, |
| 56 | + '../../../app/settings/server': { |
| 57 | + settings: { get: sinon.stub() }, |
| 58 | + }, |
| 59 | +}); |
| 60 | + |
| 61 | +const service = new TeamService(); |
| 62 | + |
| 63 | +describe('Team service', () => { |
| 64 | + beforeEach(() => { |
| 65 | + addUserToRoom.reset(); |
| 66 | + Rooms.findDefaultRoomsForTeam.reset(); |
| 67 | + Users.findActiveByIds.reset(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should wait for default room membership operations to finish', async function () { |
| 71 | + this.timeout(15000); |
| 72 | + |
| 73 | + addUserToRoom.onFirstCall().resolves(true); |
| 74 | + addUserToRoom.onSecondCall().returns( |
| 75 | + new Promise<void>((resolve) => { |
| 76 | + setTimeout(() => { |
| 77 | + resolve(); |
| 78 | + }, 20); |
| 79 | + }), |
| 80 | + ); |
| 81 | + |
| 82 | + Rooms.findDefaultRoomsForTeam.returns({ |
| 83 | + toArray: () => Promise.resolve([{ _id: 'default-room' }]), |
| 84 | + }); |
| 85 | + Users.findActiveByIds.returns({ |
| 86 | + toArray: () => |
| 87 | + Promise.resolve([ |
| 88 | + { _id: 'user-1', username: 'user-1' }, |
| 89 | + { _id: 'user-2', username: 'user-2' }, |
| 90 | + ]), |
| 91 | + }); |
| 92 | + |
| 93 | + await service.addMembersToDefaultRooms({ _id: 'inviter', username: 'inviter' }, 'team-id', [ |
| 94 | + { userId: 'user-1' }, |
| 95 | + { userId: 'user-2' }, |
| 96 | + ]); |
| 97 | + |
| 98 | + expect(addUserToRoom.callCount).to.equal(2); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should propagate errors from default room membership operations', async function () { |
| 102 | + this.timeout(15000); |
| 103 | + |
| 104 | + addUserToRoom.rejects(new Error('room-add-failed')); |
| 105 | + Rooms.findDefaultRoomsForTeam.returns({ |
| 106 | + toArray: () => Promise.resolve([{ _id: 'default-room' }]), |
| 107 | + }); |
| 108 | + Users.findActiveByIds.returns({ |
| 109 | + toArray: () => Promise.resolve([{ _id: 'user-1', username: 'user-1' }]), |
| 110 | + }); |
| 111 | + |
| 112 | + await expect( |
| 113 | + service.addMembersToDefaultRooms({ _id: 'inviter', username: 'inviter' }, 'team-id', [{ userId: 'user-1' }]), |
| 114 | + ).to.be.rejectedWith('room-add-failed'); |
| 115 | + |
| 116 | + expect(addUserToRoom.callCount).to.equal(1); |
| 117 | + }); |
| 118 | +}); |
0 commit comments