Merge branch 'hotfix/21.56.9' into master
[gitter.git] / modules / rooms / test / user-default-flags-update-service-test.js
blobf77fa0eb0309a26cacb2a2dac76e2a384d7010db
1 'use strict';
3 var assert = require('assert');
4 var Promise = require('bluebird');
5 var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
7 var userDefaultFlagsUpdateService = require('../lib/user-default-flags-update-service');
8 var userDefaultFlagsService = require('../lib/user-default-flags-service');
9 var roomMembershipService = require('../lib/room-membership-service');
10 var roomMembershipFlags = require('../lib/room-membership-flags');
11 var roomService = require('../lib/room-service');
13 describe('user-default-flags-update-service', function() {
14   describe('#slow', function() {
15     var fixture = fixtureLoader.setup({
16       user1: {},
17       user2: {},
18       user3: {},
19       troupe1: {
20         security: 'PUBLIC',
21         githubType: 'USER_CHANNEL'
22       },
23       troupe2: {
24         security: 'PUBLIC',
25         githubType: 'USER_CHANNEL'
26       },
27       troupe3: {
28         security: 'PUBLIC',
29         githubType: 'USER_CHANNEL'
30       }
31     });
33     describe('updateDefaultModeForUser', function() {
34       it('should affect future room join operations', function() {
35         var user1 = fixture.user1;
36         var troupe1 = fixture.troupe1;
37         var userId1 = user1._id;
38         var troupeId1 = troupe1._id;
40         return roomMembershipService
41           .removeRoomMembers(troupeId1, [userId1])
42           .then(function() {
43             return userDefaultFlagsUpdateService.updateDefaultModeForUser(user1, 'mute');
44           })
45           .then(function() {
46             return userDefaultFlagsService.getDefaultFlagsForUserId(userId1);
47           })
48           .then(function(flags) {
49             var mode = roomMembershipFlags.getModeFromFlags(flags);
50             assert(mode, 'mute');
52             return roomService.joinRoom(troupe1, user1, {});
53           })
54           .then(function() {
55             return roomMembershipService.getMembershipDetails(userId1, troupeId1);
56           })
57           .then(function(membershipDefaults) {
58             assert.strictEqual(membershipDefaults.mode, 'mute');
59           });
60       });
62       it('should update the rooms the user is currently in when they are default', function() {
63         var user1 = fixture.user1;
64         var userId1 = user1._id;
65         var troupeId1 = fixture.troupe1._id;
66         var troupeId2 = fixture.troupe2._id;
67         var troupeId3 = fixture.troupe3._id;
69         return Promise.join(
70           roomMembershipService.removeRoomMembers(troupeId1, [userId1]),
71           roomMembershipService.removeRoomMembers(troupeId2, [userId1]),
72           roomMembershipService.removeRoomMembers(troupeId3, [userId1])
73         )
74           .then(function() {
75             return Promise.join(
76               roomMembershipService.addRoomMember(
77                 troupeId1,
78                 userId1,
79                 roomMembershipFlags.getFlagsForMode('all', true)
80               ),
81               roomMembershipService.addRoomMember(
82                 troupeId2,
83                 userId1,
84                 roomMembershipFlags.getFlagsForMode('announcement', true)
85               ),
86               roomMembershipService.addRoomMember(
87                 troupeId3,
88                 userId1,
89                 roomMembershipFlags.getFlagsForMode('mute', true)
90               )
91             );
92           })
93           .then(function() {
94             return userDefaultFlagsUpdateService.updateDefaultModeForUser(user1, 'mute', false); // Only override defaults
95           })
96           .then(function() {
97             return Promise.join(
98               roomMembershipService.getMembershipDetails(userId1, troupeId1),
99               roomMembershipService.getMembershipDetails(userId1, troupeId2),
100               roomMembershipService.getMembershipDetails(userId1, troupeId3),
101               function(d1, d2, d3) {
102                 assert.strictEqual(d1.mode, 'mute');
103                 assert.strictEqual(d2.mode, 'mute');
104                 assert.strictEqual(d3.mode, 'mute');
105               }
106             );
107           });
108       });
110       it('should not update the rooms the user is currently in when they are default', function() {
111         var user1 = fixture.user1;
112         var userId1 = user1._id;
113         var troupeId1 = fixture.troupe1._id;
114         var troupeId2 = fixture.troupe2._id;
115         var troupeId3 = fixture.troupe3._id;
117         return Promise.join(
118           roomMembershipService.removeRoomMembers(troupeId1, [userId1]),
119           roomMembershipService.removeRoomMembers(troupeId2, [userId1]),
120           roomMembershipService.removeRoomMembers(troupeId3, [userId1])
121         )
122           .then(function() {
123             return Promise.join(
124               roomMembershipService.addRoomMember(
125                 troupeId1,
126                 userId1,
127                 roomMembershipFlags.getFlagsForMode('all', false)
128               ),
129               roomMembershipService.addRoomMember(
130                 troupeId2,
131                 userId1,
132                 roomMembershipFlags.getFlagsForMode('announcement', false)
133               ),
134               roomMembershipService.addRoomMember(
135                 troupeId3,
136                 userId1,
137                 roomMembershipFlags.getFlagsForMode('mute', false)
138               )
139             );
140           })
141           .then(function() {
142             return userDefaultFlagsUpdateService.updateDefaultModeForUser(user1, 'mute', false); // Only override defaults
143           })
144           .then(function() {
145             return Promise.join(
146               roomMembershipService.getMembershipDetails(userId1, troupeId1),
147               roomMembershipService.getMembershipDetails(userId1, troupeId2),
148               roomMembershipService.getMembershipDetails(userId1, troupeId3),
149               function(d1, d2, d3) {
150                 assert.strictEqual(d1.mode, 'all');
151                 assert.strictEqual(d2.mode, 'announcement');
152                 assert.strictEqual(d3.mode, 'mute');
153               }
154             );
155           });
156       });
157     });
158   });