Merge branch 'hotfix/21.56.9' into master
[gitter.git] / modules / rooms / test / auto-removal-service-test.js
blob72e5ec8c06a0853e6d6c8bcf21f2fbe7dd22a69f
1 'use strict';
3 var assert = require('assert');
4 var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
6 var autoRemovalService = require('../lib/auto-removal-service');
7 var recentRoomService = require('../lib/recent-room-service');
8 var roomMembershipService = require('../lib/room-membership-service');
9 var unreadItemsEngine = require('gitter-web-unread-items/lib/engine');
10 var unreadItemsService = require('gitter-web-unread-items');
11 var Promise = require('bluebird');
12 var ObjectID = require('mongodb').ObjectID;
13 var lazy = require('lazy.js');
15 describe('auto-removal-service', function() {
16   describe('#findRemovalCandidates', function() {
17     var fixture = fixtureLoader.setup({
18       user1: {},
19       user2: {},
20       troupe1: { users: ['user1', 'user2'], lastAccessTime: false }
21     });
23     it('should return a lurk candidate', function() {
24       var tenDaysAgo = new Date(Date.now() - 86400000 * 10);
26       return Promise.join(
27         recentRoomService.saveLastVisitedTroupeforUserId(fixture.user1.id, fixture.troupe1.id, {
28           lastAccessTime: tenDaysAgo
29         }),
30         recentRoomService.saveLastVisitedTroupeforUserId(fixture.user2.id, fixture.troupe1.id, {
31           lastAccessTime: Date.now()
32         }),
33         function() {
34           return autoRemovalService.findRemovalCandidates(fixture.troupe1.id, { minTimeInDays: 1 });
35         }
36       ).then(function(candidates) {
37         assert.strictEqual(candidates.length, 1);
39         assert.equal(candidates[0].userId, fixture.user1.id);
40         assert.equal(candidates[0].lastAccessTime.valueOf(), tenDaysAgo.valueOf());
41       });
42     });
43   });
45   describe('#bulkRemoveUsersFromRoom', function() {
46     var fixture = fixtureLoader.setup({
47       user1: {},
48       user2: {},
49       group1: {},
50       troupe1: { users: ['user1', 'user2'], group: 'group1', lastAccessTime: false },
51       troupe2: { users: ['user1', 'user2'], group: 'group1', lastAccessTime: false }
52     });
54     it('should return a lurk candidate', function() {
55       var itemId = new ObjectID();
56       var userWithMentions = lazy([
57         {
58           userId: fixture.user1.id
59         },
60         {
61           userId: fixture.user2.id
62         }
63       ]);
65       return Promise.join(
66         recentRoomService.saveLastVisitedTroupeforUserId(fixture.user2.id, fixture.troupe1.id, {
67           lastAccessTime: Date.now()
68         }),
69         unreadItemsEngine.newItemWithMentions(fixture.troupe1.id, itemId, userWithMentions),
70         unreadItemsEngine.newItemWithMentions(fixture.troupe2.id, itemId, userWithMentions)
71       )
72         .then(function() {
73           return autoRemovalService.bulkRemoveUsersFromRoom(
74             fixture.troupe1.id,
75             fixture.troupe1.groupId,
76             [fixture.user1.id]
77           );
78         })
79         .then(function() {
80           return roomMembershipService.findMembershipForUsersInRoom(fixture.troupe1.id, [
81             fixture.user1.id,
82             fixture.user2.id
83           ]);
84         })
85         .then(function(membership) {
86           var mString = membership.map(function(f) {
87             return String(f);
88           });
89           assert.deepEqual(mString, [fixture.user2.id]);
91           return roomMembershipService.findRoomIdsForUser(fixture.user1.id);
92         })
93         .then(function(roomIds) {
94           var rString = roomIds.map(function(f) {
95             return String(f);
96           });
97           assert.deepEqual(rString, [fixture.troupe2.id]);
99           return recentRoomService.findInitialRoomUrlForUser(fixture.user1);
100         })
101         .then(function(url) {
102           assert.strictEqual(url, null);
103           return unreadItemsService.getAllUnreadItemCounts(fixture.user1.id);
104         })
105         .then(function(unreadItems) {
106           assert.deepEqual(unreadItems, [
107             {
108               troupeId: fixture.troupe2.id,
109               unreadItems: 1,
110               mentions: 0
111             }
112           ]);
113         });
114     });
115   });