Merge branch 'fix-ci-yaml-container-scanning' into 'develop'
[gitter.git] / modules / rooms / test / one-to-one-room-service-test.js
blobbf70259959557ece8df875bbdce338490de8bae0
1 'use strict';
3 var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
4 var Promise = require('bluebird');
5 var _ = require('lodash');
6 var assert = require('assert');
7 var StatusError = require('statuserror');
8 var oneToOneRoomService = require('../lib/one-to-one-room-service');
9 var roomMembershipService = require('../lib/room-membership-service');
11 function findUserIdPredicate(userId) {
12   return function(x) {
13     return '' + x === '' + userId;
14   };
16 describe('one-to-one-room-service', function() {
17   describe('#slow', function() {
18     var fixture = fixtureLoader.setup({
19       user1: {},
20       user2: {},
21       user3: {},
22       userRemoved: {
23         state: 'REMOVED'
24       }
25     });
27     it('should handle the creation of a oneToOneTroupe single', function() {
28       return oneToOneRoomService
29         .findOrCreateOneToOneRoom(fixture.user1, fixture.user2.id)
30         .spread(function(troupe, otherUser) {
31           assert(troupe);
32           assert(troupe.oneToOne);
33           assert.strictEqual(troupe.githubType, 'ONETOONE');
34           assert.strictEqual(otherUser.id, fixture.user2.id);
35           assert.strictEqual(troupe.oneToOneUsers.length, 2);
37           return roomMembershipService.findMembersForRoom(troupe.id);
38         })
39         .then(function(userIds) {
40           assert(_.find(userIds, findUserIdPredicate(fixture.user1.id)));
41           assert(_.find(userIds, findUserIdPredicate(fixture.user2.id)));
42         });
43     });
45     it('should handle the creation of a oneToOneTroupe atomicly', function() {
46       return Promise.all([
47         oneToOneRoomService.findOrCreateOneToOneRoom(fixture.user2, fixture.user3.id),
48         oneToOneRoomService.findOrCreateOneToOneRoom(fixture.user3, fixture.user2.id)
49       ])
50         .spread(function(r1, r2) {
51           var troupe1 = r1[0];
52           var otherUser1 = r1[1];
53           var troupe2 = r2[0];
54           var otherUser2 = r2[1];
56           assert(troupe1);
57           assert(troupe1.oneToOne);
58           assert.strictEqual(troupe1.githubType, 'ONETOONE');
59           assert.strictEqual(troupe1.oneToOneUsers.length, 2);
60           assert.strictEqual(otherUser1.id, fixture.user3.id);
62           assert(troupe2);
63           assert(troupe2.oneToOne);
64           assert.strictEqual(troupe2.githubType, 'ONETOONE');
65           assert.strictEqual(troupe2.oneToOneUsers.length, 2);
66           assert.strictEqual(otherUser2.id, fixture.user2.id);
68           assert.strictEqual(troupe1.id, troupe2.id);
70           return roomMembershipService.findMembersForRoom(troupe1.id);
71         })
72         .then(function(userIds) {
73           assert(_.find(userIds, findUserIdPredicate(fixture.user2.id)));
74           assert(_.find(userIds, findUserIdPredicate(fixture.user3.id)));
75         });
76     });
78     it('should handle the creation of a oneToOneTroupe atomicly', function() {
79       return Promise.all([
80         oneToOneRoomService.findOrCreateOneToOneRoom(fixture.user2, fixture.user3.id),
81         oneToOneRoomService.findOrCreateOneToOneRoom(fixture.user3, fixture.user2.id)
82       ])
83         .spread(function(r1, r2) {
84           var troupe1 = r1[0];
85           var otherUser1 = r1[1];
86           var troupe2 = r2[0];
87           var otherUser2 = r2[1];
89           assert(troupe1);
90           assert(troupe1.oneToOne);
91           assert.strictEqual(troupe1.githubType, 'ONETOONE');
92           assert.strictEqual(troupe1.oneToOneUsers.length, 2);
93           assert.strictEqual(otherUser1.id, fixture.user3.id);
95           assert(troupe2);
96           assert(troupe2.oneToOne);
97           assert.strictEqual(troupe2.githubType, 'ONETOONE');
98           assert.strictEqual(troupe2.oneToOneUsers.length, 2);
99           assert.strictEqual(otherUser2.id, fixture.user2.id);
101           assert.strictEqual(troupe1.id, troupe2.id);
103           return roomMembershipService.findMembersForRoom(troupe1.id);
104         })
105         .then(function(userIds) {
106           assert(_.find(userIds, findUserIdPredicate(fixture.user2.id)));
107           assert(_.find(userIds, findUserIdPredicate(fixture.user3.id)));
108         });
109     });
111     it('should not allow a user to create a room with a REMOVED user', function() {
112       var userId2 = fixture.userRemoved._id;
114       return oneToOneRoomService
115         .findOrCreateOneToOneRoom(fixture.user1, userId2)
116         .then(function() {
117           assert.ok(false);
118         })
119         .catch(StatusError, function(err) {
120           assert(err.status, 403);
121         });
122     });
123   });
125   describe('https://github.com/troupe/gitter-webapp/issues/1227 #slow', function() {
126     var fixture = fixtureLoader.setup({
127       user1: {},
128       user2: {}
129     });
131     it("should add the requesting user back into a one-to-one room if they've been removed", function() {
132       var user1 = fixture.user1;
133       var userId1 = user1._id;
134       var userId2 = fixture.user2._id;
136       return oneToOneRoomService
137         .findOrCreateOneToOneRoom(user1, userId2)
138         .bind({ room: undefined })
139         .spread(function(room) {
140           this.room = room;
141           return roomMembershipService.removeRoomMember(room._id, userId1);
142         })
143         .then(function() {
144           return roomMembershipService.findMembersForRoom(this.room._id);
145         })
146         .then(function(members) {
147           assert.strictEqual(members.length, 1);
148           assert.strictEqual(String(members[0]), String(userId2));
150           return oneToOneRoomService.findOrCreateOneToOneRoom(user1, userId2);
151         })
152         .spread(function(room) {
153           assert.strictEqual(room.id, this.room.id);
154           return roomMembershipService.findMembersForRoom(this.room._id);
155         })
156         .then(function(members) {
157           assert.strictEqual(members.length, 2);
158           assert(
159             members.some(function(x) {
160               return String(x) === String(userId1);
161             })
162           );
163           assert(
164             members.some(function(x) {
165               return String(x) === String(userId2);
166             })
167           );
168         });
169     });
171     it("should add the users back into a one-to-one room if they've both been removed", function() {
172       var user1 = fixture.user1;
173       var userId1 = user1._id;
174       var userId2 = fixture.user2._id;
176       return oneToOneRoomService
177         .findOrCreateOneToOneRoom(user1, userId2)
178         .bind({ room: undefined })
179         .spread(function(room) {
180           this.room = room;
181           return Promise.join(
182             roomMembershipService.removeRoomMember(room._id, userId1),
183             roomMembershipService.removeRoomMember(room._id, userId2)
184           );
185         })
186         .then(function() {
187           return roomMembershipService.findMembersForRoom(this.room._id);
188         })
189         .then(function(members) {
190           assert.strictEqual(members.length, 0);
192           return oneToOneRoomService.findOrCreateOneToOneRoom(user1, userId2);
193         })
194         .spread(function(room) {
195           assert.strictEqual(room.id, this.room.id);
196           return roomMembershipService.findMembersForRoom(this.room._id);
197         })
198         .then(function(members) {
199           assert.strictEqual(members.length, 2);
200           var actual = members.map(String);
201           actual.sort();
202           var expected = [userId1, userId2].map(String);
203           expected.sort();
204           assert.deepEqual(actual, expected);
205         });
206     });
208     it('should add the users back into a one-to-one room if the fromUser has been removed', function() {
209       var user1 = fixture.user1;
210       var userId1 = user1._id;
211       var userId2 = fixture.user2._id;
213       return oneToOneRoomService
214         .findOrCreateOneToOneRoom(user1, userId2)
215         .bind({ room: undefined })
216         .spread(function(room) {
217           this.room = room;
218           return roomMembershipService.removeRoomMember(room._id, userId1);
219         })
220         .then(function() {
221           return roomMembershipService.findMembersForRoom(this.room._id);
222         })
223         .then(function(members) {
224           assert.strictEqual(members.length, 1);
226           return oneToOneRoomService.findOrCreateOneToOneRoom(user1, userId2);
227         })
228         .spread(function(room) {
229           assert.strictEqual(room.id, this.room.id);
230           return roomMembershipService.findMembersForRoom(this.room._id);
231         })
232         .then(function(members) {
233           assert.strictEqual(members.length, 2);
234           var actual = members.map(String);
235           actual.sort();
236           var expected = [userId1, userId2].map(String);
237           expected.sort();
238           assert.deepEqual(actual, expected);
239         });
240     });
242     it('should add the users back into a one-to-one room if the toUser has been removed', function() {
243       var user1 = fixture.user1;
244       var userId1 = user1._id;
245       var userId2 = fixture.user2._id;
247       return oneToOneRoomService
248         .findOrCreateOneToOneRoom(user1, userId2)
249         .bind({ room: undefined })
250         .spread(function(room) {
251           this.room = room;
252           return roomMembershipService.removeRoomMember(room._id, userId2);
253         })
254         .then(function() {
255           return roomMembershipService.findMembersForRoom(this.room._id);
256         })
257         .then(function(members) {
258           assert.strictEqual(members.length, 1);
260           return oneToOneRoomService.findOrCreateOneToOneRoom(user1, userId2);
261         })
262         .spread(function(room) {
263           assert.strictEqual(room.id, this.room.id);
264           return roomMembershipService.findMembersForRoom(this.room._id);
265         })
266         .then(function(members) {
267           assert.strictEqual(members.length, 2);
268           var actual = members.map(String);
269           actual.sort();
270           var expected = [userId1, userId2].map(String);
271           expected.sort();
272           assert.deepEqual(actual, expected);
273         });
274     });
275   });