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) {
13 return '' + x === '' + userId;
16 describe('one-to-one-room-service', function() {
17 describe('#slow', function() {
18 var fixture = fixtureLoader.setup({
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) {
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);
39 .then(function(userIds) {
40 assert(_.find(userIds, findUserIdPredicate(fixture.user1.id)));
41 assert(_.find(userIds, findUserIdPredicate(fixture.user2.id)));
45 it('should handle the creation of a oneToOneTroupe atomicly', function() {
47 oneToOneRoomService.findOrCreateOneToOneRoom(fixture.user2, fixture.user3.id),
48 oneToOneRoomService.findOrCreateOneToOneRoom(fixture.user3, fixture.user2.id)
50 .spread(function(r1, r2) {
52 var otherUser1 = r1[1];
54 var otherUser2 = r2[1];
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);
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);
72 .then(function(userIds) {
73 assert(_.find(userIds, findUserIdPredicate(fixture.user2.id)));
74 assert(_.find(userIds, findUserIdPredicate(fixture.user3.id)));
78 it('should handle the creation of a oneToOneTroupe atomicly', function() {
80 oneToOneRoomService.findOrCreateOneToOneRoom(fixture.user2, fixture.user3.id),
81 oneToOneRoomService.findOrCreateOneToOneRoom(fixture.user3, fixture.user2.id)
83 .spread(function(r1, r2) {
85 var otherUser1 = r1[1];
87 var otherUser2 = r2[1];
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);
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);
105 .then(function(userIds) {
106 assert(_.find(userIds, findUserIdPredicate(fixture.user2.id)));
107 assert(_.find(userIds, findUserIdPredicate(fixture.user3.id)));
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)
119 .catch(StatusError, function(err) {
120 assert(err.status, 403);
125 describe('https://github.com/troupe/gitter-webapp/issues/1227 #slow', function() {
126 var fixture = fixtureLoader.setup({
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) {
141 return roomMembershipService.removeRoomMember(room._id, userId1);
144 return roomMembershipService.findMembersForRoom(this.room._id);
146 .then(function(members) {
147 assert.strictEqual(members.length, 1);
148 assert.strictEqual(String(members[0]), String(userId2));
150 return oneToOneRoomService.findOrCreateOneToOneRoom(user1, userId2);
152 .spread(function(room) {
153 assert.strictEqual(room.id, this.room.id);
154 return roomMembershipService.findMembersForRoom(this.room._id);
156 .then(function(members) {
157 assert.strictEqual(members.length, 2);
159 members.some(function(x) {
160 return String(x) === String(userId1);
164 members.some(function(x) {
165 return String(x) === String(userId2);
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) {
182 roomMembershipService.removeRoomMember(room._id, userId1),
183 roomMembershipService.removeRoomMember(room._id, userId2)
187 return roomMembershipService.findMembersForRoom(this.room._id);
189 .then(function(members) {
190 assert.strictEqual(members.length, 0);
192 return oneToOneRoomService.findOrCreateOneToOneRoom(user1, userId2);
194 .spread(function(room) {
195 assert.strictEqual(room.id, this.room.id);
196 return roomMembershipService.findMembersForRoom(this.room._id);
198 .then(function(members) {
199 assert.strictEqual(members.length, 2);
200 var actual = members.map(String);
202 var expected = [userId1, userId2].map(String);
204 assert.deepEqual(actual, expected);
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) {
218 return roomMembershipService.removeRoomMember(room._id, userId1);
221 return roomMembershipService.findMembersForRoom(this.room._id);
223 .then(function(members) {
224 assert.strictEqual(members.length, 1);
226 return oneToOneRoomService.findOrCreateOneToOneRoom(user1, userId2);
228 .spread(function(room) {
229 assert.strictEqual(room.id, this.room.id);
230 return roomMembershipService.findMembersForRoom(this.room._id);
232 .then(function(members) {
233 assert.strictEqual(members.length, 2);
234 var actual = members.map(String);
236 var expected = [userId1, userId2].map(String);
238 assert.deepEqual(actual, expected);
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) {
252 return roomMembershipService.removeRoomMember(room._id, userId2);
255 return roomMembershipService.findMembersForRoom(this.room._id);
257 .then(function(members) {
258 assert.strictEqual(members.length, 1);
260 return oneToOneRoomService.findOrCreateOneToOneRoom(user1, userId2);
262 .spread(function(room) {
263 assert.strictEqual(room.id, this.room.id);
264 return roomMembershipService.findMembersForRoom(this.room._id);
266 .then(function(members) {
267 assert.strictEqual(members.length, 2);
268 var actual = members.map(String);
270 var expected = [userId1, userId2].map(String);
272 assert.deepEqual(actual, expected);