3 var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
4 var assert = require('assert');
5 var Promise = require('bluebird');
6 var sinon = require('sinon');
7 var roomMembershipFlags = require('../lib/room-membership-flags');
8 var roomMembershipService = require('../lib/room-membership-service');
9 var persistence = require('gitter-web-persistence');
11 function mongoIdEqualPredicate(value) {
12 var strValue = String(value);
14 return String(x) === strValue;
18 describe('room-membership-service', function() {
19 describe('integration tests #slow', function() {
20 var fixture = fixtureLoader.setup({
29 describe('addRoomMember', function() {
30 beforeEach(function() {
31 return roomMembershipService.removeRoomMembers(fixture.troupe3._id, [
38 it('should add a single user to a room', function() {
39 var troupeId3 = fixture.troupe3._id;
40 var userId1 = fixture.user1._id;
41 var flags = roomMembershipFlags.MODES.all;
43 return roomMembershipService
44 .addRoomMember(troupeId3, userId1, flags)
46 return roomMembershipService.countMembersInRoom(troupeId3);
48 .then(function(count) {
49 assert.strictEqual(count, 1);
50 return roomMembershipService.findMembersForRoom(troupeId3);
52 .then(function(members) {
53 assert.deepEqual(members, [userId1]);
55 return roomMembershipService.findRoomIdsForUser(userId1);
57 .then(function(roomIds) {
58 assert(roomIds.length >= 1);
59 assert(roomIds.some(mongoIdEqualPredicate(troupeId3)));
61 return persistence.TroupeUser.findOne({ troupeId: troupeId3, userId: userId1 }).exec();
63 .then(function(troupeUser) {
64 assert.strictEqual(troupeUser.flags, flags);
68 it('should handle different flags', function() {
69 var troupeId3 = fixture.troupe3._id;
70 var userId1 = fixture.user1._id;
71 var userId2 = fixture.user2._id;
72 var userId3 = fixture.user3._id;
75 roomMembershipService.addRoomMember(troupeId3, userId1, roomMembershipFlags.MODES.all),
76 roomMembershipService.addRoomMember(
79 roomMembershipFlags.MODES.announcement
81 roomMembershipService.addRoomMember(troupeId3, userId3, roomMembershipFlags.MODES.mute)
84 return roomMembershipService.findMembersForRoomWithLurk(troupeId3);
86 .then(function(result) {
88 expected[userId1] = false;
89 expected[userId2] = false;
90 expected[userId3] = true;
91 assert.deepEqual(result, expected);
93 return roomMembershipService.getMembershipDetails(userId1, troupeId3);
95 .then(function(result) {
96 assert.strictEqual(result.flags, roomMembershipFlags.MODES.all);
97 return roomMembershipService.getMembershipDetails(userId2, troupeId3);
99 .then(function(result) {
100 assert.strictEqual(result.flags, roomMembershipFlags.MODES.announcement);
101 return roomMembershipService.getMembershipDetails(userId3, troupeId3);
103 .then(function(result) {
104 assert.strictEqual(result.flags, roomMembershipFlags.MODES.mute);
109 describe('removeRoomMembers', function() {
110 it('should allow users to be removed from a room', function() {
111 var flags = roomMembershipFlags.MODES.all;
114 roomMembershipService.addRoomMember(fixture.troupe2.id, fixture.user1.id, flags),
115 roomMembershipService.addRoomMember(fixture.troupe2.id, fixture.user2.id, flags)
118 return persistence.Troupe.findById(fixture.troupe2.id).exec();
120 .then(function(troupe) {
121 assert.strictEqual(troupe.userCount, 2);
122 return roomMembershipService.removeRoomMembers(fixture.troupe2.id, [fixture.user1.id]);
125 return roomMembershipService.countMembersInRoom(fixture.troupe2.id);
127 .then(function(count) {
128 assert.strictEqual(count, 1);
129 return persistence.Troupe.findById(fixture.troupe2.id).exec();
131 .then(function(troupe) {
132 assert.strictEqual(troupe.userCount, 1);
134 return roomMembershipService.checkRoomMembership(fixture.troupe2.id, fixture.user2.id);
136 .then(function(member) {
138 return roomMembershipService.checkRoomMembership(fixture.troupe2.id, fixture.user1.id);
140 .then(function(member) {
146 describe('membership modes', function() {
147 beforeEach(function() {
148 this.onMembersLurkChange = sinon.spy();
150 roomMembershipService.events.on('members.lurk.change', this.onMembersLurkChange);
153 afterEach(function() {
154 roomMembershipService.events.removeListener(
155 'members.lurk.change',
156 this.onMembersLurkChange
160 it('should handle lurk status alongside membership mode mute', function() {
161 var troupeId2 = fixture.troupe2.id;
162 var userId1 = fixture.user1.id;
164 return roomMembershipService
165 .removeRoomMember(troupeId2, userId1)
168 return roomMembershipService.addRoomMember(
171 roomMembershipFlags.MODES.all
175 return roomMembershipService.setMembershipMode(userId1, troupeId2, 'mute');
178 // Check that the event emitter fired
179 assert.strictEqual(1, this.onMembersLurkChange.callCount);
180 var spyCall = this.onMembersLurkChange.getCall(0);
182 assert.strictEqual(troupeId2, spyCall.args[0]);
183 assert.deepEqual([userId1], spyCall.args[1]);
184 assert.strictEqual(true, spyCall.args[2]);
186 return roomMembershipService.getMembershipDetails(userId1, troupeId2);
188 .then(function(modeExtended) {
189 assert.deepEqual(modeExtended, {
192 flags: parseInt('110', 2),
201 return roomMembershipService.getMemberLurkStatus(troupeId2, userId1);
203 .then(function(lurking) {
204 assert.strictEqual(lurking, true);
208 it('should handle lurk status alongside membership mode announcement', function() {
209 var troupeId2 = fixture.troupe2.id;
210 var userId1 = fixture.user1.id;
212 return roomMembershipService
213 .removeRoomMember(troupeId2, userId1)
216 return roomMembershipService.addRoomMember(
219 roomMembershipFlags.MODES.all
223 return roomMembershipService.setMembershipMode(userId1, troupeId2, 'announcement');
226 // Check that the event emitter fired
227 assert.strictEqual(0, this.onMembersLurkChange.callCount);
228 return roomMembershipService.getMembershipMode(userId1, troupeId2);
230 .then(function(mode) {
231 assert.strictEqual(mode, 'announcement');
232 return roomMembershipService.getMembershipDetails(userId1, troupeId2);
234 .then(function(modeExtended) {
235 assert.deepEqual(modeExtended, {
236 mode: 'announcement',
238 flags: parseInt('1101', 2),
248 return roomMembershipService.getMemberLurkStatus(troupeId2, userId1);
250 .then(function(lurking) {
251 assert.strictEqual(lurking, false);
252 return roomMembershipService.setMembershipMode(userId1, troupeId2, 'mute');
255 // Check that the event emitter fired
256 assert.strictEqual(1, this.onMembersLurkChange.callCount);
257 var spyCall = this.onMembersLurkChange.getCall(0);
259 assert.strictEqual(troupeId2, spyCall.args[0]);
260 assert.deepEqual([userId1], spyCall.args[1]);
261 assert.strictEqual(true, spyCall.args[2]);
263 return roomMembershipService.getMembershipMode(userId1, troupeId2);
265 .then(function(mode) {
266 assert.strictEqual(mode, 'mute');
267 return roomMembershipService.getMembershipDetails(userId1, troupeId2);
269 .then(function(modeExtended) {
270 assert.deepEqual(modeExtended, {
273 flags: parseInt('0110', 2),
283 return roomMembershipService.getMemberLurkStatus(troupeId2, userId1);
285 .then(function(lurking) {
286 assert.strictEqual(lurking, true);
290 it('should handle lurk status alongside membership mode all', function() {
291 var troupeId2 = fixture.troupe2.id;
292 var userId1 = fixture.user1.id;
294 return roomMembershipService
295 .removeRoomMember(troupeId2, userId1)
298 return roomMembershipService.addRoomMember(
301 roomMembershipFlags.MODES.all
305 return roomMembershipService.setMembershipMode(userId1, troupeId2, 'all');
308 assert.strictEqual(0, this.onMembersLurkChange.callCount);
310 return roomMembershipService.getMembershipMode(userId1, troupeId2);
312 .then(function(mode) {
313 assert.strictEqual(mode, 'all');
314 return roomMembershipService.getMembershipDetails(userId1, troupeId2);
316 .then(function(modeExtended) {
317 assert.deepEqual(modeExtended, {
320 flags: parseInt('1101101', 2),
329 return roomMembershipService.getMemberLurkStatus(troupeId2, userId1);
331 .then(function(lurking) {
332 assert.strictEqual(lurking, false);
336 it('should handle transitions to and from lurk mode', function() {
337 var troupeId2 = fixture.troupe2.id;
338 var userId1 = fixture.user1.id;
340 return roomMembershipService
341 .removeRoomMember(troupeId2, userId1)
344 return roomMembershipService.addRoomMember(
347 roomMembershipFlags.MODES.all
351 return roomMembershipService.setMembershipMode(userId1, troupeId2, 'all');
354 assert.strictEqual(0, this.onMembersLurkChange.callCount);
356 return roomMembershipService.getMembershipMode(userId1, troupeId2);
358 .then(function(mode) {
359 assert.strictEqual(mode, 'all');
360 return roomMembershipService.getMemberLurkStatus(troupeId2, userId1);
362 .then(function(lurking) {
363 assert.strictEqual(lurking, false);
364 return roomMembershipService.setMembershipMode(userId1, troupeId2, 'mute');
367 // Check that the event emitter fired
368 assert.strictEqual(1, this.onMembersLurkChange.callCount);
369 var spyCall = this.onMembersLurkChange.getCall(0);
371 assert.strictEqual(troupeId2, spyCall.args[0]);
372 assert.deepEqual([userId1], spyCall.args[1]);
373 assert.strictEqual(true, spyCall.args[2]);
375 return roomMembershipService.getMembershipMode(userId1, troupeId2);
377 .then(function(mode) {
378 assert.strictEqual(mode, 'mute');
379 return roomMembershipService.getMemberLurkStatus(troupeId2, userId1);
381 .then(function(lurking) {
382 assert.strictEqual(lurking, true);
383 return roomMembershipService.setMembershipMode(userId1, troupeId2, 'all');
386 // Check that the event emitter fired
387 assert.strictEqual(2, this.onMembersLurkChange.callCount);
388 var spyCall = this.onMembersLurkChange.getCall(1);
390 assert.strictEqual(troupeId2, spyCall.args[0]);
391 assert.deepEqual([userId1], spyCall.args[1]);
392 assert.strictEqual(false, spyCall.args[2]);
394 return roomMembershipService.getMembershipMode(userId1, troupeId2);
396 .then(function(mode) {
397 assert.strictEqual(mode, 'all');
398 return roomMembershipService.getMembershipDetails(userId1, troupeId2);
400 .then(function(modeExtended) {
401 assert.deepEqual(modeExtended, {
404 flags: parseInt('1101101', 2),
414 return roomMembershipService.getMemberLurkStatus(troupeId2, userId1);
416 .then(function(lurking) {
417 assert.strictEqual(lurking, false);
418 return roomMembershipService.setMembershipMode(userId1, troupeId2, 'all');
423 describe('findMembersForRoom', function() {
424 it('findMembersForRoom should handle skip and limit', function() {
425 var troupeId2 = fixture.troupe2.id;
426 var userId1 = fixture.user1.id;
427 var userId2 = fixture.user2.id;
429 var flags = roomMembershipFlags.MODES.all;
432 roomMembershipService.addRoomMember(troupeId2, userId1, flags),
433 roomMembershipService.addRoomMember(troupeId2, userId2, flags)
436 roomMembershipService.findMembersForRoom(troupeId2, { limit: 1 }),
437 roomMembershipService.findMembersForRoom(troupeId2, { skip: 1, limit: 1 }),
438 function(find1, find2) {
439 assert.strictEqual(find1.length, 1);
440 assert.strictEqual(find2.length, 1);
443 find1.some(mongoIdEqualPredicate(userId1)) ||
444 find2.some(mongoIdEqualPredicate(userId1))
447 find1.some(mongoIdEqualPredicate(userId2)) ||
448 find2.some(mongoIdEqualPredicate(userId2))
456 describe('findRoomIdsForUserWithLurk', function() {
457 it('should return the correct values', function() {
458 var troupeId2 = fixture.troupe2.id;
459 var userId1 = fixture.user1.id;
460 var userId2 = fixture.user2.id;
461 var userId3 = fixture.user3.id;
464 roomMembershipService.addRoomMember(troupeId2, userId1, roomMembershipFlags.MODES.all),
465 roomMembershipService.addRoomMember(
468 roomMembershipFlags.MODES.announcement
470 roomMembershipService.addRoomMember(troupeId2, userId3, roomMembershipFlags.MODES.mute)
473 return roomMembershipService.findRoomIdsForUserWithLurk(userId1);
475 .then(function(result) {
476 assert(typeof result === 'object');
478 assert(result.hasOwnProperty(troupeId2));
479 assert.strictEqual(result[troupeId2], false);
481 return roomMembershipService.findRoomIdsForUserWithLurk(userId2);
483 .then(function(result) {
484 assert(typeof result === 'object');
485 assert(result.hasOwnProperty(troupeId2));
486 assert.strictEqual(result[troupeId2], false);
488 return roomMembershipService.findRoomIdsForUserWithLurk(userId3);
490 .then(function(result) {
491 assert(typeof result === 'object');
492 assert(result.hasOwnProperty(troupeId2));
493 assert.strictEqual(result[troupeId2], true);
498 describe('findLurkingRoomIdsForUserId', function() {
499 it('should return rooms in which a user is lurking', function() {
500 var troupeId1 = fixture.troupe1.id;
501 var troupeId2 = fixture.troupe2.id;
502 var troupeId3 = fixture.troupe3.id;
503 var userId1 = fixture.user1.id;
506 roomMembershipService.removeRoomMember(troupeId1, userId1),
507 roomMembershipService.removeRoomMember(troupeId2, userId1),
508 roomMembershipService.removeRoomMember(troupeId3, userId1)
512 roomMembershipService.addRoomMember(
515 roomMembershipFlags.MODES.all
517 roomMembershipService.addRoomMember(
520 roomMembershipFlags.MODES.announcement
522 roomMembershipService.addRoomMember(
525 roomMembershipFlags.MODES.mute
530 return roomMembershipService.findLurkingRoomIdsForUserId(userId1);
532 .then(function(troupeIds) {
533 assert.strictEqual(troupeIds.length, 1);
534 assert.strictEqual(String(troupeIds[0]), troupeId3);
539 describe('addRoomMember', function() {
540 it('should add a new member to a room', function() {
541 var troupeId = fixture.troupe2.id;
542 var userId = fixture.user1.id;
545 function listener(pTroupeId, members) {
546 assert.strictEqual(pTroupeId, troupeId);
547 assert.deepEqual(members, [userId]);
551 return roomMembershipService
552 .removeRoomMember(troupeId, userId)
554 roomMembershipService.events.on('members.added', listener);
555 return roomMembershipService.addRoomMember(
558 roomMembershipFlags.MODES.all
561 .then(function(result) {
562 assert.strictEqual(result, true);
563 assert.strictEqual(called, 1);
565 .finally(function() {
566 roomMembershipService.events.removeListener('members.added', listener);
570 it('should be idempotent', function() {
571 var troupeId = fixture.troupe2.id;
572 var userId = fixture.user1.id;
575 function listener() {
579 return roomMembershipService
580 .addRoomMember(troupeId, userId, roomMembershipFlags.MODES.all)
582 roomMembershipService.events.on('members.added', listener);
583 return roomMembershipService.addRoomMember(
586 roomMembershipFlags.MODES.all
589 .then(function(result) {
590 assert.strictEqual(result, false);
591 assert.strictEqual(called, 0);
593 .finally(function() {
594 roomMembershipService.events.removeListener('members.added', listener);
599 describe('findUserMembershipInRooms', function() {
600 it('should return some users', function() {
601 var troupeId1 = fixture.troupe1.id;
602 var troupeId2 = fixture.troupe2.id;
603 var userId = fixture.user1.id;
606 roomMembershipService.removeRoomMember(troupeId1, userId),
607 roomMembershipService.addRoomMember(troupeId2, userId, roomMembershipFlags.MODES.all),
609 return roomMembershipService.findUserMembershipInRooms(userId, [troupeId1, troupeId2]);
611 ).then(function(result) {
612 assert.strictEqual(result.length, 1);
613 assert.equal(result[0], troupeId2);
618 describe('findMembershipForUsersInRoom', function() {
619 it('should return some users', function() {
620 var troupeId = fixture.troupe1.id;
621 var userId1 = fixture.user1.id;
622 var userId2 = fixture.user2.id;
625 roomMembershipService.removeRoomMember(troupeId, userId1),
626 roomMembershipService.addRoomMember(troupeId, userId2, roomMembershipFlags.MODES.all),
628 return roomMembershipService.findMembershipForUsersInRoom(troupeId, [userId1, userId2]);
630 ).then(function(result) {
631 assert(Array.isArray(result));
632 assert.strictEqual(result.length, 1);
633 assert.equal(result[0], userId2);
637 it('should return some users when a single user is added', function() {
638 var troupeId = fixture.troupe1.id;
639 var userId1 = fixture.user1.id;
640 var userId2 = fixture.user2.id;
643 roomMembershipService.removeRoomMember(troupeId, userId1),
644 roomMembershipService.addRoomMember(troupeId, userId2, roomMembershipFlags.MODES.all),
646 return roomMembershipService.findMembershipForUsersInRoom(troupeId, [userId2]);
648 ).then(function(result) {
649 assert(Array.isArray(result));
650 assert.strictEqual(result.length, 1);
651 assert.equal(result[0], userId2);
656 describe('findMembersForRoomWithLurk', function() {
657 it('should return some users with lurk', function() {
658 var troupeId = fixture.troupe1.id;
659 var userId1 = fixture.user1.id;
660 var userId2 = fixture.user2.id;
661 var userId3 = fixture.user3.id;
664 roomMembershipService.removeRoomMember(troupeId, userId1),
665 roomMembershipService.addRoomMember(troupeId, userId2, roomMembershipFlags.MODES.all),
666 roomMembershipService.addRoomMember(troupeId, userId3, roomMembershipFlags.MODES.all),
669 roomMembershipService.setMembershipMode(userId2, troupeId, 'all'),
670 roomMembershipService.setMembershipMode(userId3, troupeId, 'mute')
675 return roomMembershipService.findMembersForRoomWithLurk(troupeId, [
681 .then(function(result) {
683 expected[userId2] = false;
684 expected[userId3] = true;
685 assert.deepEqual(result, expected);
690 describe('removeRoomMember', function() {
691 it('should remove a member from a room', function() {
692 var troupeId = fixture.troupe2.id;
693 var userId = fixture.user1.id;
696 function listener(pTroupeId, members) {
697 assert.strictEqual(pTroupeId, troupeId);
698 assert.deepEqual(members, [userId]);
702 return roomMembershipService
703 .addRoomMember(troupeId, userId, roomMembershipFlags.MODES.all)
705 roomMembershipService.events.on('members.removed', listener);
707 return roomMembershipService.removeRoomMember(troupeId, userId);
709 .then(function(result) {
710 assert.strictEqual(result, true);
711 assert.strictEqual(called, 1);
713 .finally(function() {
714 roomMembershipService.events.removeListener('members.removed', listener);
718 it('should be idempotent', function() {
719 var troupeId = fixture.troupe2.id;
720 var userId = fixture.user1.id;
723 function listener() {
727 return roomMembershipService
728 .removeRoomMember(troupeId, userId)
730 roomMembershipService.events.on('members.removed', listener);
731 return roomMembershipService.removeRoomMember(troupeId, userId);
733 .then(function(result) {
734 assert.strictEqual(result, false);
735 assert.strictEqual(called, 0);
737 .finally(function() {
738 roomMembershipService.events.removeListener('members.removed', listener);
743 describe('findAllMembersForRooms', function() {
744 it('should return some users', function() {
745 var troupeId1 = fixture.troupe1.id;
746 var troupeId2 = fixture.troupe2.id;
747 var userId1 = fixture.user1.id;
748 var userId2 = fixture.user2.id;
749 var userId3 = fixture.user3.id;
752 roomMembershipService.removeRoomMember(troupeId1, userId3),
753 roomMembershipService.removeRoomMember(troupeId2, userId3),
754 roomMembershipService.addRoomMember(troupeId1, userId1, roomMembershipFlags.MODES.all),
755 roomMembershipService.addRoomMember(
758 roomMembershipFlags.MODES.announcement
760 roomMembershipService.addRoomMember(troupeId2, userId1, roomMembershipFlags.MODES.mute),
762 return roomMembershipService.findAllMembersForRooms([troupeId1, troupeId2]);
764 ).then(function(result) {
765 assert.strictEqual(result.length, 2);
766 assert(result.some(mongoIdEqualPredicate(userId1)));
767 assert(result.some(mongoIdEqualPredicate(userId2)));
768 assert(!result.some(mongoIdEqualPredicate(userId3)));
773 describe('findMembersForRoomMulti', function() {
774 it('should return some users', function() {
775 var troupeId1 = fixture.troupe1.id;
776 var troupeId2 = fixture.troupe2.id;
777 var userId1 = fixture.user1.id;
778 var userId2 = fixture.user2.id;
779 var userId3 = fixture.user3.id;
782 roomMembershipService.removeRoomMember(troupeId1, userId3),
783 roomMembershipService.removeRoomMember(troupeId2, userId3),
784 roomMembershipService.addRoomMember(troupeId1, userId1, roomMembershipFlags.MODES.all),
785 roomMembershipService.addRoomMember(
788 roomMembershipFlags.MODES.announcement
790 roomMembershipService.addRoomMember(troupeId2, userId1, roomMembershipFlags.MODES.mute),
791 roomMembershipService.removeRoomMember(troupeId2, userId2),
793 return roomMembershipService.findMembersForRoomMulti([troupeId1, troupeId2]);
795 ).then(function(result) {
796 assert.strictEqual(Object.keys(result).length, 2);
797 var t1 = result[troupeId1];
798 var t2 = result[troupeId2];
799 assert(Array.isArray(t1));
800 assert(Array.isArray(t2));
802 assert(t1.some(mongoIdEqualPredicate(userId1)));
803 assert(t1.some(mongoIdEqualPredicate(userId2)));
804 assert(!t1.some(mongoIdEqualPredicate(userId3)));
806 assert(t2.some(mongoIdEqualPredicate(userId1)));
807 assert(!t2.some(mongoIdEqualPredicate(userId2)));
808 assert(!t2.some(mongoIdEqualPredicate(userId3)));
813 describe('setMembershipModeForUsersInRoom', function() {
814 it('should return some users', function() {
815 var troupeId1 = fixture.troupe1.id;
816 var userId1 = fixture.user1.id;
817 var userId2 = fixture.user2.id;
820 roomMembershipService.addRoomMember(troupeId1, userId1, roomMembershipFlags.MODES.mute),
821 roomMembershipService.addRoomMember(troupeId1, userId2, roomMembershipFlags.MODES.mute),
823 return roomMembershipService.setMembershipModeForUsersInRoom(
831 return roomMembershipService.findMembershipModeForUsersInRoom(troupeId1, [
836 .then(function(result) {
838 expected[userId1] = 'all';
839 expected[userId2] = 'all';
840 assert.deepEqual(result, expected);
842 return roomMembershipService.setMembershipModeForUsersInRoom(
849 return roomMembershipService.findMembershipModeForUsersInRoom(troupeId1, [
854 .then(function(result) {
856 expected[userId1] = 'announcement';
857 expected[userId2] = 'all';
858 assert.deepEqual(result, expected);
860 return roomMembershipService.setMembershipModeForUsersInRoom(
867 return roomMembershipService.findMembershipModeForUsersInRoom(troupeId1, [
872 .then(function(result) {
874 expected[userId1] = 'announcement';
875 expected[userId2] = 'mute';
876 assert.deepEqual(result, expected);
878 return roomMembershipService.setMembershipModeForUsersInRoom(
885 return roomMembershipService.findMembershipModeForUsersInRoom(troupeId1, [
890 .then(function(result) {
892 expected[userId1] = 'all';
893 expected[userId2] = 'all';
894 assert.deepEqual(result, expected);
899 describe('findMembersForRoomForNotify', function() {
900 var troupeId1, userId1, userId2, userId3;
902 function roomForNotifySort(a, b) {
908 return u1 > u2 ? 1 : -1;
912 function equivalentValues(array, expected) {
913 var keys = Object.keys(expected);
917 'Expected ' + keys.length + ' items, got ' + array.length
919 array.forEach(function(item) {
920 var expectedItem = expected[item.userId];
921 assert(expectedItem !== undefined, 'Item for user ' + item.userId + ' does not exist');
922 assert.strictEqual(item.flags, expectedItem);
927 troupeId1 = fixture.troupe1.id;
928 userId1 = fixture.user1._id;
929 userId2 = fixture.user2._id;
930 userId3 = fixture.user3._id;
933 roomMembershipService.addRoomMember(troupeId1, userId1, roomMembershipFlags.MODES.all),
934 roomMembershipService.addRoomMember(
937 roomMembershipFlags.MODES.announcement
939 roomMembershipService.addRoomMember(troupeId1, userId3, roomMembershipFlags.MODES.mute),
942 roomMembershipService.setMembershipModeForUsersInRoom(troupeId1, [userId1], 'all'),
943 roomMembershipService.setMembershipModeForUsersInRoom(
948 roomMembershipService.setMembershipModeForUsersInRoom(troupeId1, [userId3], 'mute')
954 it('should return notify users', function() {
955 // No announcement, no
956 return roomMembershipService
957 .findMembersForRoomForNotify(troupeId1, null)
958 .then(function(result) {
959 result.sort(roomForNotifySort);
961 expected[userId1] = roomMembershipFlags.MODES.all;
962 expected[userId2] = roomMembershipFlags.MODES.announcement;
963 expected[userId3] = roomMembershipFlags.MODES.mute;
965 equivalentValues(result, expected);
969 it('should not return the sender', function() {
970 // No announcement, no
971 return roomMembershipService
972 .findMembersForRoomForNotify(troupeId1, userId1)
973 .then(function(result) {
974 result.sort(roomForNotifySort);
976 expected[userId2] = roomMembershipFlags.MODES.announcement;
977 expected[userId3] = roomMembershipFlags.MODES.mute;
979 equivalentValues(result, expected);
983 it('should return notify users and announce users', function() {
984 return roomMembershipService
985 .findMembersForRoomForNotify(troupeId1, null, true)
986 .then(function(result) {
988 expected[userId1] = roomMembershipFlags.MODES.all;
989 expected[userId2] = roomMembershipFlags.MODES.announcement;
990 expected[userId3] = roomMembershipFlags.MODES.mute;
992 equivalentValues(result, expected);
996 it('should return notify users and mention users who are already notify users', function() {
997 return roomMembershipService
998 .findMembersForRoomForNotify(troupeId1, null, false, [userId1])
999 .then(function(result) {
1001 expected[userId1] = roomMembershipFlags.MODES.all;
1002 expected[userId2] = roomMembershipFlags.MODES.announcement;
1003 expected[userId3] = roomMembershipFlags.MODES.mute;
1005 equivalentValues(result, expected);
1009 it('should return notify users and mention users who are announcement users', function() {
1010 return roomMembershipService
1011 .findMembersForRoomForNotify(troupeId1, null, false, [userId2])
1012 .then(function(result) {
1014 expected[userId1] = roomMembershipFlags.MODES.all;
1015 expected[userId2] = roomMembershipFlags.MODES.announcement;
1016 expected[userId3] = roomMembershipFlags.MODES.mute;
1018 equivalentValues(result, expected);
1022 it('should return notify users and mention users who are announcement users', function() {
1023 return roomMembershipService
1024 .findMembersForRoomForNotify(troupeId1, null, false, [userId3])
1025 .then(function(result) {
1027 expected[userId1] = roomMembershipFlags.MODES.all;
1028 expected[userId2] = roomMembershipFlags.MODES.announcement;
1029 expected[userId3] = roomMembershipFlags.MODES.mute;
1031 equivalentValues(result, expected);
1035 it('should return notify users and mention users who are mute users', function() {
1036 return roomMembershipService
1037 .findMembersForRoomForNotify(troupeId1, null, true, [userId3])
1038 .then(function(result) {
1040 expected[userId1] = roomMembershipFlags.MODES.all;
1041 expected[userId2] = roomMembershipFlags.MODES.announcement;
1042 expected[userId3] = roomMembershipFlags.MODES.mute;
1044 equivalentValues(result, expected);
1049 describe('groupMembership change notification', function() {
1050 var fixture = fixtureLoader.setup({
1058 troupe1: { security: 'PUBLIC', group: 'group1', users: ['user3', 'user5', 'user6'] },
1059 troupe2: { security: 'PUBLIC', group: 'group1', users: ['user2', 'user4', 'user5'] }
1066 beforeEach(function() {
1067 _onAdded = function() {
1069 return onAdded.apply(null, arguments);
1072 assert.ok(false, 'Unexpected add');
1075 _onRemoved = function() {
1077 return onRemoved.apply(null, arguments);
1080 assert.ok(false, 'Unexpected remove');
1083 roomMembershipService.events.on('group.members.added', _onAdded);
1084 roomMembershipService.events.on('group.members.removed', _onRemoved);
1087 afterEach(function() {
1088 roomMembershipService.events.removeListener('group.members.added', _onAdded);
1089 roomMembershipService.events.removeListener('group.members.removed', _onRemoved);
1094 it('should notify when a user joins a new group', function() {
1095 var troupeId = fixture.troupe1._id;
1096 var userId = fixture.user1._id;
1097 var groupId = fixture.group1._id;
1099 var p = new Promise(function(resolve) {
1100 onAdded = function(pGroupId, pUserIds) {
1101 assert.strictEqual(pGroupId, groupId);
1102 assert.deepEqual(pUserIds, [userId]);
1107 return roomMembershipService
1108 .addRoomMember(troupeId, userId, roomMembershipFlags.MODES.all, groupId)
1114 it('should not notify when a user joins a new room for a group they are already in', function() {
1115 var troupeId = fixture.troupe1._id;
1116 var userId = fixture.user2._id;
1117 var groupId = fixture.group1._id;
1119 return roomMembershipService
1120 .addRoomMember(troupeId, userId, roomMembershipFlags.MODES.all, groupId)
1121 .delay(10); // Give the caller time to fire the event
1124 it('should notify when a user leaves a room and its the last room in that group theyre a member of', function() {
1125 var troupeId = fixture.troupe1._id;
1126 var userId = fixture.user3._id;
1127 var groupId = fixture.group1._id;
1129 var p = new Promise(function(resolve) {
1130 onRemoved = function(pGroupId, pUserIds) {
1131 assert.strictEqual(pGroupId, groupId);
1132 assert.deepEqual(pUserIds, [userId]);
1137 return roomMembershipService.removeRoomMember(troupeId, userId, groupId).then(function() {
1142 it('should not notify when a user leaves a room and its not the last room in that group theyre a member of', function() {
1143 var troupeId = fixture.troupe1._id;
1144 var userId = fixture.user4._id;
1145 var groupId = fixture.group1._id;
1147 return roomMembershipService.removeRoomMember(troupeId, userId, groupId).delay(10); // Give the caller time to fire the event
1150 it('should emit events when multiple room members are removed at once', function() {
1151 var troupeId = fixture.troupe1._id;
1152 var userId5 = fixture.user5._id;
1153 var userId6 = fixture.user6._id;
1154 var groupId = fixture.group1._id;
1156 var p = new Promise(function(resolve) {
1157 onRemoved = function(pGroupId, pUserIds) {
1158 assert.strictEqual(pGroupId, groupId);
1159 assert.deepEqual(pUserIds, [userId6]);
1164 return roomMembershipService
1165 .removeRoomMembers(troupeId, [userId5, userId6], groupId)
1172 describe('findPrivateRoomIdsForUser', function() {
1173 var fixture = fixtureLoader.setup({
1177 securityDescriptor: {
1183 securityDescriptor: {
1186 users: ['user2', 'user1']
1189 securityDescriptor: {
1196 function contains(ids, expectedIds) {
1197 return expectedIds.every(function(expectedId) {
1198 return ids.some(function(x) {
1199 return String(expectedId) === String(x);
1204 it('should find private rooms', function() {
1205 var userId1 = fixture.user1.id;
1207 return roomMembershipService.findPrivateRoomIdsForUser(userId1).then(function(roomIds) {
1208 assert.strictEqual(roomIds.length, 2);
1209 assert(contains(roomIds, [fixture.troupe1.id, fixture.troupe3.id]));
1213 it('should handle users without private rooms', function() {
1214 var userId1 = fixture.user2.id;
1216 return roomMembershipService.findPrivateRoomIdsForUser(userId1).then(function(roomIds) {
1217 assert.deepEqual(roomIds, []);