3 var assert = require('assert');
4 var Promise = require('bluebird');
5 var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
6 var roomMembershipFlags = require('../lib/room-membership-flags');
7 var userDefaultFlagsService = require('../lib/user-default-flags-service');
8 var userDefaultFlagsUpdateService = require('../lib/user-default-flags-update-service');
10 describe('user-default-flags', function() {
11 describe('useOneToOneDefaultWhenMute', function() {
15 flags: roomMembershipFlags.MODES.all,
16 expected: roomMembershipFlags.MODES.all
20 flags: roomMembershipFlags.MODES.all,
21 expected: roomMembershipFlags.MODES.all
25 flags: roomMembershipFlags.MODES.mute,
26 expected: roomMembershipFlags.DEFAULT_ONE_TO_ONE_FLAGS_WHEN_MUTE
30 describe('without default flag', function() {
31 FIXTURES.forEach(function(meta) {
32 it(meta.name, function() {
33 var result = userDefaultFlagsService.testOnly.useOneToOneDefaultWhenMute(meta.flags);
34 assert.strictEqual(result, meta.expected);
39 describe('strips off the default flag', function() {
40 FIXTURES.forEach(function(meta) {
41 it(meta.name, function() {
42 var flags = roomMembershipFlags.addDefaultFlag(meta.flags);
43 var result = userDefaultFlagsService.testOnly.useOneToOneDefaultWhenMute(flags);
44 assert.strictEqual(result, meta.expected);
50 describe('#slow', function() {
51 var fixture = fixtureLoader.setup({
57 describe('getDefaultFlagsForUserId', function() {
58 it('should handle users without a default', function() {
59 var userId1 = fixture.user1._id;
61 return userDefaultFlagsService
62 .setDefaultFlagsForUserId(userId1, null)
64 return userDefaultFlagsService.getDefaultFlagsForUserId(userId1);
66 .then(function(flags) {
67 assert.strictEqual(flags, roomMembershipFlags.DEFAULT_USER_FLAGS);
71 it('should handle users with a value', function() {
72 var userId1 = fixture.user1._id;
74 return userDefaultFlagsService
75 .setDefaultFlagsForUserId(userId1, 1)
77 return userDefaultFlagsService.getDefaultFlagsForUserId(userId1);
79 .then(function(flags) {
80 assert.strictEqual(flags, 1);
85 describe('getDefaultFlagsForUserIds', function() {
86 it('should handle users without a default', function() {
87 var userId1 = fixture.user1._id;
88 var userId2 = fixture.user2._id;
91 userDefaultFlagsService.setDefaultFlagsForUserId(userId1, null),
92 userDefaultFlagsService.setDefaultFlagsForUserId(userId2, null)
95 return userDefaultFlagsService.getDefaultFlagsForUserIds([userId1, userId2]);
97 .then(function(flags) {
99 expected[userId1] = roomMembershipFlags.DEFAULT_USER_FLAGS;
100 expected[userId2] = roomMembershipFlags.DEFAULT_USER_FLAGS;
101 assert.deepEqual(flags, expected);
105 it('should handle users with a value', function() {
106 var userId1 = fixture.user1._id;
107 var userId2 = fixture.user2._id;
110 userDefaultFlagsService.setDefaultFlagsForUserId(userId1, 1),
111 userDefaultFlagsService.setDefaultFlagsForUserId(userId2, 2)
114 return userDefaultFlagsService.getDefaultFlagsForUserIds([userId1, userId2]);
116 .then(function(flags) {
118 expected[userId1] = 1;
119 expected[userId2] = 2;
120 assert.deepEqual(flags, expected);
125 describe('setDefaultFlagsForUserId', function() {
126 it('should handle setting and unsetting the value', function() {
127 var userId1 = fixture.user1._id;
129 return userDefaultFlagsService
130 .setDefaultFlagsForUserId(userId1, null)
132 return userDefaultFlagsService.getDefaultFlagsForUserId(userId1);
134 .then(function(flags) {
135 assert.strictEqual(flags, roomMembershipFlags.DEFAULT_USER_FLAGS);
136 return userDefaultFlagsService.setDefaultFlagsForUserId(userId1, 1);
139 return userDefaultFlagsService.getDefaultFlagsForUserId(userId1);
141 .then(function(flags) {
142 assert.strictEqual(flags, 1);
143 return userDefaultFlagsService.setDefaultFlagsForUserId(userId1, 2);
146 return userDefaultFlagsService.getDefaultFlagsForUserId(userId1);
148 .then(function(flags) {
149 assert.strictEqual(flags, 2);
150 return userDefaultFlagsService.setDefaultFlagsForUserId(userId1, null);
153 return userDefaultFlagsService.getDefaultFlagsForUserId(userId1);
155 .then(function(flags) {
156 assert.strictEqual(flags, roomMembershipFlags.DEFAULT_USER_FLAGS);
157 return userDefaultFlagsService.setDefaultFlagsForUserId(userId1, 1);
162 describe('getDefaultFlagDetailsForUserId', function() {
163 it('should handle users who have not changed their default', function() {
164 var user3 = fixture.user3;
166 return userDefaultFlagsService
167 .getDefaultFlagDetailsForUserId(user3._id)
168 .then(function(details) {
169 assert.deepEqual(details, {
183 it('should handle users who have changed their default to all', function() {
184 var user1 = fixture.user1;
185 return userDefaultFlagsUpdateService
186 .updateDefaultModeForUser(user1, 'all', false)
188 return userDefaultFlagsService.getDefaultFlagDetailsForUserId(user1._id);
190 .then(function(details) {
191 assert.deepEqual(details, {
205 it('should handle users who have changed their default to announcement', function() {
206 var user1 = fixture.user1;
207 return userDefaultFlagsUpdateService
208 .updateDefaultModeForUser(user1, 'announcement', false)
210 return userDefaultFlagsService.getDefaultFlagDetailsForUserId(user1._id);
212 .then(function(details) {
213 assert.deepEqual(details, {
221 mode: 'announcement',
227 it('should handle users who have changed their default to mute', function() {
228 var user1 = fixture.user1;
229 return userDefaultFlagsUpdateService
230 .updateDefaultModeForUser(user1, 'mute', false)
232 return userDefaultFlagsService.getDefaultFlagDetailsForUserId(user1._id);
234 .then(function(details) {
235 assert.deepEqual(details, {