@webex/plugin-rooms 2.59.2 → 2.59.3-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,451 +1,451 @@
1
- /*!
2
- * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
- */
4
-
5
- import '@webex/plugin-logger';
6
- import '@webex/plugin-people';
7
- import '@webex/plugin-rooms';
8
- import '@webex/plugin-memberships';
9
- import '@webex/plugin-messages';
10
- import WebexCore, {WebexHttpError} from '@webex/webex-core';
11
- import {SDK_EVENT, hydraTypes, constructHydraId} from '@webex/common';
12
- import {assert} from '@webex/test-helper-chai';
13
- import sinon from 'sinon';
14
- import testUsers from '@webex/test-helper-test-users';
15
-
16
- const debug = require('debug')('rooms');
17
-
18
- describe('plugin-rooms', function () {
19
- this.timeout(60000);
20
-
21
- let webex, actor;
22
-
23
- before(() =>
24
- testUsers.create({count: 1}).then(async ([user]) => {
25
- // Pause for 5 seconds for CI
26
- await new Promise((done) => setTimeout(done, 5000));
27
-
28
- webex = new WebexCore({credentials: user.token});
29
-
30
- return webex.people.get('me').then((person) => {
31
- actor = person;
32
- debug('SDK User (Actor) for tests:');
33
- debug(`- name: ${actor.displayName}`);
34
- debug(`- id: ${actor.id}`);
35
- });
36
- })
37
- );
38
-
39
- describe('#rooms', () => {
40
- const rooms = [];
41
-
42
- afterEach(() => {
43
- webex.rooms.stopListening();
44
- webex.rooms.off('created');
45
- webex.rooms.off('updated');
46
-
47
- return Promise.all(
48
- rooms.map((room) =>
49
- webex.rooms.remove(room).catch((reason) => {
50
- console.error('Failed to delete room', reason);
51
- })
52
- )
53
- ).then(() => {
54
- while (rooms.length) {
55
- rooms.pop();
56
- }
57
- });
58
- });
59
-
60
- describe('#create()', () => {
61
- it('creates a room and validates the room:created event', () => {
62
- const createdEventPromise = new Promise((resolve) => {
63
- webex.rooms.on('created', (event) => {
64
- debug('room:created event handler called for create test');
65
- resolve(event);
66
- });
67
- });
68
-
69
- return webex.rooms.listen().then(() =>
70
- webex.rooms.create({title: 'Webex Test Room'}).then(async (room) => {
71
- assert.isRoom(room);
72
- rooms.push(room); // for future cleanup
73
- const event = await createdEventPromise;
74
-
75
- validateRoomEvent(event, room, actor);
76
- })
77
- );
78
- });
79
- });
80
-
81
- describe('#one-on-one()', () => {
82
- let user1, room;
83
-
84
- before(() =>
85
- testUsers.create({count: 1}).then(async (users) => {
86
- // Pause for 5 seconds for CI
87
- await new Promise((done) => setTimeout(done, 5000));
88
-
89
- user1 = users[0];
90
- debug('Test User for One-on-One room:');
91
- debug(`- name: ${user1.displayName}`);
92
- debug(`- id: ${constructHydraId(hydraTypes.PEOPLE, user1.id)}`);
93
- })
94
- );
95
-
96
- // We need a one-on-on space for this test
97
- // We create it by sending a message to the test user
98
- it('creates a 1-1 space and validates the room type', () => {
99
- const createdEventPromise = new Promise((resolve) => {
100
- webex.rooms.on('created', (event) => {
101
- debug('room:created event handler called for one-on-one test');
102
- debug(event);
103
- resolve(event);
104
- });
105
- });
106
-
107
- return webex.rooms.listen().then(() =>
108
- webex.messages
109
- .create({
110
- toPersonId: user1.id,
111
- text: 'Message to start a one-on-on space',
112
- })
113
- .then((message) => {
114
- assert.exists(message.roomId);
115
-
116
- return webex.rooms.get({id: message.roomId});
117
- })
118
- .then(async (r) => {
119
- room = r;
120
- assert.isRoom(room);
121
- const event = await createdEventPromise;
122
-
123
- validateRoomEvent(event, room, actor);
124
- })
125
- );
126
- });
127
- });
128
-
129
- describe('#get()', () => {
130
- let room0;
131
-
132
- beforeEach(() =>
133
- Promise.all([
134
- webex.rooms.create({title: 'Webex Test Room 1'}).then((room) => {
135
- rooms.push(room);
136
- }),
137
- webex.rooms.create({title: 'Webex Test Room 0'}).then((room) => {
138
- rooms.push(room);
139
- room0 = room;
140
- }),
141
- ])
142
- );
143
-
144
- it('retrieves a specific room', () =>
145
- webex.rooms.get(room0).then((room) => {
146
- assert.isRoom(room);
147
-
148
- assert.equal(room.id, room0.id);
149
- assert.equal(room.title, room0.title);
150
- }));
151
- });
152
-
153
- describe('#list()', () => {
154
- // Reminder: we can't run the next two creates in parallel because some of
155
- // the tests rely on ordering.
156
- let room0, room1;
157
-
158
- beforeEach(() =>
159
- webex.rooms.create({title: 'Webex Test Room 1'}).then((room) => {
160
- rooms.push(room);
161
- room1 = room;
162
- })
163
- );
164
-
165
- beforeEach(() =>
166
- webex.rooms.create({title: 'Webex Test Room 0'}).then((room) => {
167
- rooms.push(room);
168
- room0 = room;
169
- })
170
- );
171
-
172
- it('retrieves all the rooms to which I have access', () =>
173
- webex.rooms.list().then((rooms) => {
174
- for (const room of rooms) {
175
- assert.isRoom(room);
176
- }
177
- assert.equal(rooms.items[0].id, room0.id, "Room 0's id matches");
178
- assert.equal(rooms.items[0].title, room0.title, "Room 0's title matches");
179
- assert.equal(rooms.items[1].id, room1.id, "Room 1's id matches");
180
- assert.equal(rooms.items[1].title, room1.title, "Room 1's title matches");
181
- }));
182
-
183
- it('retrieves a bounded, pageable set of rooms to which I have access', () => {
184
- const spy = sinon.spy();
185
-
186
- return webex.rooms
187
- .list({max: 1})
188
- .then((rooms) => {
189
- assert.lengthOf(rooms, 1);
190
-
191
- return (function f(page) {
192
- for (const room of page) {
193
- spy(room.id);
194
- }
195
-
196
- if (page.hasNext()) {
197
- return page.next().then(f);
198
- }
199
-
200
- return Promise.resolve();
201
- })(rooms);
202
- })
203
- .then(() => {
204
- assert.isAbove(spy.callCount, 1);
205
- assert.calledWith(spy, room0.id);
206
- assert.calledWith(spy, room1.id);
207
- });
208
- });
209
- });
210
-
211
- describe('#getWithReadStatus()', () => {
212
- let room1, user2;
213
-
214
- before(() =>
215
- testUsers.create({count: 1}).then(async ([user]) => {
216
- // Pause for 5 seconds for CI
217
- await new Promise((done) => setTimeout(done, 5000));
218
-
219
- user2 = user;
220
- user2.webex = new WebexCore({credentials: user2.token});
221
- })
222
- );
223
-
224
- // Create a space with one user who is "caught up" and another "behind"
225
- beforeEach(() =>
226
- webex.rooms
227
- .create({title: 'Space to get Read Status from'})
228
- .then((room) => {
229
- rooms.push(room);
230
- room1 = room;
231
-
232
- return webex.memberships.create({
233
- roomId: room1.id,
234
- personId: user2.id,
235
- });
236
- })
237
- // User 1 will post a message, that user2 won't have seen
238
- .then(() =>
239
- webex.messages.create({
240
- roomId: room1.id,
241
- text: 'First message in room 1 from User 1',
242
- })
243
- )
244
- );
245
-
246
- it('gets the read status for new room for user1', () =>
247
- webex.rooms.getWithReadStatus(room1.id).then((roomInfo) => {
248
- assert.equal(roomInfo.id, room1.id, "Room 1's title matches");
249
- assert.equal(roomInfo.title, room1.title, "Room 1's title matches");
250
- assert.isTrue(
251
- roomInfo.lastSeenActivityDate >= roomInfo.lastActivityDate,
252
- 'Room 1 is read for User 1'
253
- );
254
- }));
255
-
256
- it('gets the read status for a new room for user2', () =>
257
- user2.webex.rooms.getWithReadStatus(room1.id).then((roomInfo) => {
258
- assert.equal(roomInfo.id, room1.id, "Room 1's title matches");
259
- assert.equal(roomInfo.title, room1.title, "Room 1's title matches");
260
- assert.isTrue(
261
- roomInfo.lastSeenActivityDate < roomInfo.lastActivityDate,
262
- 'Room 1 is unread for User 2'
263
- );
264
- }));
265
- });
266
-
267
- describe('#listWithReadStatus()', () => {
268
- // Reminder: we can't run the next two creates in parallel because some of
269
- // the tests rely on ordering.
270
- let room1, room2, user2;
271
-
272
- before(() =>
273
- testUsers.create({count: 1}).then(async ([user]) => {
274
- // Pause for 5 seconds for CI
275
- await new Promise((done) => setTimeout(done, 5000));
276
-
277
- user2 = user;
278
- user2.webex = new WebexCore({credentials: user2.token});
279
- })
280
- );
281
-
282
- // Create two spaces with a message from each user in one of them
283
- beforeEach(() =>
284
- webex.rooms
285
- .create({title: 'Unread Message for User 2'})
286
- .then((room) => {
287
- rooms.push(room);
288
- room1 = room;
289
-
290
- return webex.memberships.create({
291
- roomId: room1.id,
292
- personId: user2.id,
293
- });
294
- })
295
- // User 1 will post a message, that user2 won't have seen
296
- .then(() =>
297
- webex.messages.create({
298
- roomId: room1.id,
299
- text: 'First message in room 1 from User 1',
300
- })
301
- )
302
- // Now create the second space with the two members
303
- .then(() => webex.rooms.create({title: 'Unread Message for User 1'}))
304
- .then((room) => {
305
- rooms.push(room);
306
- room2 = room;
307
- })
308
- .then(() =>
309
- webex.memberships.create({
310
- roomId: room2.id,
311
- personId: user2.id,
312
- })
313
- )
314
- // User 2 will post a message, that User 1 won't have seen
315
- .then(() =>
316
- user2.webex.messages.create({
317
- roomId: room2.id,
318
- text: 'First message in room 2 from User 2',
319
- })
320
- )
321
- );
322
-
323
- it('gets the read status for all rooms User 1 is in', () =>
324
- webex.rooms.listWithReadStatus().then((roomList) => {
325
- assert.isArray(roomList.items, 'Expect a list or rooms from listWithReadStatus()');
326
- assert.isAbove(
327
- roomList.items.length,
328
- 1,
329
- 'Expected two or more rooms from listWithReadStatus()'
330
- );
331
- for (const room of roomList.items) {
332
- if (room.id === room1.id) {
333
- assert.equal(room.title, room1.title, "Room 1's title matches");
334
- assert.isTrue(
335
- room.lastSeenActivityDate >= room.lastActivityDate,
336
- 'Room 1 is read for User 1'
337
- );
338
- }
339
- if (room.id === room2.id) {
340
- assert.equal(room.title, room2.title, "Room 2's title matches");
341
- assert.isTrue(
342
- room.lastSeenActivityDate < room.lastActivityDate,
343
- 'Room 2 is unread for User 1'
344
- );
345
- }
346
- }
347
- }));
348
-
349
- it('gets the read status for all rooms User 2 is in', () =>
350
- user2.webex.rooms.listWithReadStatus().then((roomList) => {
351
- assert.isArray(roomList.items, 'Expect a list or rooms from listWithReadStatus()');
352
- assert.equal(roomList.items.length, 2, 'Expected two rooms from listWithReadStatus()');
353
- for (const room of roomList.items) {
354
- if (room.id === room1.id) {
355
- assert.equal(room.title, room1.title, "Room 1's title matches");
356
- assert.isTrue(
357
- room.lastSeenActivityDate < room.lastActivityDate,
358
- 'Room 1 is unread for User 2'
359
- );
360
- } else {
361
- assert.equal(room.title, room2.title, "Room 2's title matches");
362
- assert.isTrue(
363
- room.lastSeenActivityDate >= room.lastActivityDate,
364
- 'Room 2 is read for User 2'
365
- );
366
- }
367
- }
368
- }));
369
- });
370
-
371
- describe('#update()', () => {
372
- let room;
373
-
374
- beforeEach(() =>
375
- webex.rooms.create({title: 'Webex Test Room'}).then((r) => {
376
- room = r;
377
- rooms.push(room);
378
- assert.property(room, 'id');
379
- })
380
- );
381
-
382
- it("updates a single room's title and validates a room:updated event", () => {
383
- const r = Object.assign({}, room, {title: 'Webex Test Room with New Title'});
384
- const updatedEventPromise = new Promise((resolve) => {
385
- webex.rooms.on('updated', (event) => {
386
- debug('room:updated event handler called');
387
- resolve(event);
388
- });
389
- });
390
-
391
- return webex.rooms.listen().then(() =>
392
- webex.rooms.update(r).then(async (room) => {
393
- assert.isRoom(room);
394
- assert.deepEqual(room, r);
395
- const event = await updatedEventPromise;
396
-
397
- validateRoomEvent(event, room, actor);
398
- })
399
- );
400
- });
401
- });
402
-
403
- describe('#remove()', () => {
404
- let room;
405
-
406
- beforeEach(() =>
407
- webex.rooms.create({title: 'Webex Test Room'}).then((r) => {
408
- room = r;
409
- assert.property(room, 'id');
410
- })
411
- );
412
-
413
- it('deletes a single room', () =>
414
- webex.rooms
415
- .remove(room)
416
- .then((body) => {
417
- assert.notOk(body);
418
-
419
- return assert.isRejected(webex.rooms.get(room));
420
- })
421
- .then((reason) => {
422
- assert.instanceOf(reason, WebexHttpError.NotFound);
423
- }));
424
- });
425
- });
426
- });
427
-
428
- /**
429
- * Validate a rooms event.
430
- * @param {Object} event - rooms event
431
- * @param {Object} room -- return from the API that generated this event
432
- * @param {Object} actor - person object for user who performed action
433
- * @returns {void}
434
- */
435
- function validateRoomEvent(event, room, actor) {
436
- assert.isTrue(event.resource === SDK_EVENT.EXTERNAL.RESOURCE.ROOMS, 'not a room event');
437
- assert.isDefined(event.event, 'room event type not set');
438
- assert.isDefined(event.created, 'event listener created date not set');
439
- assert.equal(event.createdBy, actor.id, 'event listener createdBy not set to our actor');
440
- assert.equal(event.orgId, actor.orgId, "event listener orgId not === to our actor's");
441
- assert.equal(event.ownedBy, 'creator', 'event listener not owned by creator');
442
- assert.equal(event.status, 'active', 'event listener status not active');
443
- assert.equal(event.actorId, actor.id, "event actorId not equal to our actor's id");
444
-
445
- // Ensure event data matches data returned from function call
446
- // Skip this until we figure out how conversations converts the internal test user UUID
447
- assert.equal(event.data.id, room.id, 'event/room.id not equal');
448
- assert.equal(event.data.isLocked, room.isLocked, 'event/room.isLocked not equal');
449
- assert.equal(event.data.type, room.type, 'event/room.type not equal');
450
- debug(`rooms:${event.event} event validated`);
451
- }
1
+ /*!
2
+ * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
+ */
4
+
5
+ import '@webex/plugin-logger';
6
+ import '@webex/plugin-people';
7
+ import '@webex/plugin-rooms';
8
+ import '@webex/plugin-memberships';
9
+ import '@webex/plugin-messages';
10
+ import WebexCore, {WebexHttpError} from '@webex/webex-core';
11
+ import {SDK_EVENT, hydraTypes, constructHydraId} from '@webex/common';
12
+ import {assert} from '@webex/test-helper-chai';
13
+ import sinon from 'sinon';
14
+ import testUsers from '@webex/test-helper-test-users';
15
+
16
+ const debug = require('debug')('rooms');
17
+
18
+ describe('plugin-rooms', function () {
19
+ this.timeout(60000);
20
+
21
+ let webex, actor;
22
+
23
+ before(() =>
24
+ testUsers.create({count: 1}).then(async ([user]) => {
25
+ // Pause for 5 seconds for CI
26
+ await new Promise((done) => setTimeout(done, 5000));
27
+
28
+ webex = new WebexCore({credentials: user.token});
29
+
30
+ return webex.people.get('me').then((person) => {
31
+ actor = person;
32
+ debug('SDK User (Actor) for tests:');
33
+ debug(`- name: ${actor.displayName}`);
34
+ debug(`- id: ${actor.id}`);
35
+ });
36
+ })
37
+ );
38
+
39
+ describe('#rooms', () => {
40
+ const rooms = [];
41
+
42
+ afterEach(() => {
43
+ webex.rooms.stopListening();
44
+ webex.rooms.off('created');
45
+ webex.rooms.off('updated');
46
+
47
+ return Promise.all(
48
+ rooms.map((room) =>
49
+ webex.rooms.remove(room).catch((reason) => {
50
+ console.error('Failed to delete room', reason);
51
+ })
52
+ )
53
+ ).then(() => {
54
+ while (rooms.length) {
55
+ rooms.pop();
56
+ }
57
+ });
58
+ });
59
+
60
+ describe('#create()', () => {
61
+ it('creates a room and validates the room:created event', () => {
62
+ const createdEventPromise = new Promise((resolve) => {
63
+ webex.rooms.on('created', (event) => {
64
+ debug('room:created event handler called for create test');
65
+ resolve(event);
66
+ });
67
+ });
68
+
69
+ return webex.rooms.listen().then(() =>
70
+ webex.rooms.create({title: 'Webex Test Room'}).then(async (room) => {
71
+ assert.isRoom(room);
72
+ rooms.push(room); // for future cleanup
73
+ const event = await createdEventPromise;
74
+
75
+ validateRoomEvent(event, room, actor);
76
+ })
77
+ );
78
+ });
79
+ });
80
+
81
+ describe('#one-on-one()', () => {
82
+ let user1, room;
83
+
84
+ before(() =>
85
+ testUsers.create({count: 1}).then(async (users) => {
86
+ // Pause for 5 seconds for CI
87
+ await new Promise((done) => setTimeout(done, 5000));
88
+
89
+ user1 = users[0];
90
+ debug('Test User for One-on-One room:');
91
+ debug(`- name: ${user1.displayName}`);
92
+ debug(`- id: ${constructHydraId(hydraTypes.PEOPLE, user1.id)}`);
93
+ })
94
+ );
95
+
96
+ // We need a one-on-on space for this test
97
+ // We create it by sending a message to the test user
98
+ it('creates a 1-1 space and validates the room type', () => {
99
+ const createdEventPromise = new Promise((resolve) => {
100
+ webex.rooms.on('created', (event) => {
101
+ debug('room:created event handler called for one-on-one test');
102
+ debug(event);
103
+ resolve(event);
104
+ });
105
+ });
106
+
107
+ return webex.rooms.listen().then(() =>
108
+ webex.messages
109
+ .create({
110
+ toPersonId: user1.id,
111
+ text: 'Message to start a one-on-on space',
112
+ })
113
+ .then((message) => {
114
+ assert.exists(message.roomId);
115
+
116
+ return webex.rooms.get({id: message.roomId});
117
+ })
118
+ .then(async (r) => {
119
+ room = r;
120
+ assert.isRoom(room);
121
+ const event = await createdEventPromise;
122
+
123
+ validateRoomEvent(event, room, actor);
124
+ })
125
+ );
126
+ });
127
+ });
128
+
129
+ describe('#get()', () => {
130
+ let room0;
131
+
132
+ beforeEach(() =>
133
+ Promise.all([
134
+ webex.rooms.create({title: 'Webex Test Room 1'}).then((room) => {
135
+ rooms.push(room);
136
+ }),
137
+ webex.rooms.create({title: 'Webex Test Room 0'}).then((room) => {
138
+ rooms.push(room);
139
+ room0 = room;
140
+ }),
141
+ ])
142
+ );
143
+
144
+ it('retrieves a specific room', () =>
145
+ webex.rooms.get(room0).then((room) => {
146
+ assert.isRoom(room);
147
+
148
+ assert.equal(room.id, room0.id);
149
+ assert.equal(room.title, room0.title);
150
+ }));
151
+ });
152
+
153
+ describe('#list()', () => {
154
+ // Reminder: we can't run the next two creates in parallel because some of
155
+ // the tests rely on ordering.
156
+ let room0, room1;
157
+
158
+ beforeEach(() =>
159
+ webex.rooms.create({title: 'Webex Test Room 1'}).then((room) => {
160
+ rooms.push(room);
161
+ room1 = room;
162
+ })
163
+ );
164
+
165
+ beforeEach(() =>
166
+ webex.rooms.create({title: 'Webex Test Room 0'}).then((room) => {
167
+ rooms.push(room);
168
+ room0 = room;
169
+ })
170
+ );
171
+
172
+ it('retrieves all the rooms to which I have access', () =>
173
+ webex.rooms.list().then((rooms) => {
174
+ for (const room of rooms) {
175
+ assert.isRoom(room);
176
+ }
177
+ assert.equal(rooms.items[0].id, room0.id, "Room 0's id matches");
178
+ assert.equal(rooms.items[0].title, room0.title, "Room 0's title matches");
179
+ assert.equal(rooms.items[1].id, room1.id, "Room 1's id matches");
180
+ assert.equal(rooms.items[1].title, room1.title, "Room 1's title matches");
181
+ }));
182
+
183
+ it('retrieves a bounded, pageable set of rooms to which I have access', () => {
184
+ const spy = sinon.spy();
185
+
186
+ return webex.rooms
187
+ .list({max: 1})
188
+ .then((rooms) => {
189
+ assert.lengthOf(rooms, 1);
190
+
191
+ return (function f(page) {
192
+ for (const room of page) {
193
+ spy(room.id);
194
+ }
195
+
196
+ if (page.hasNext()) {
197
+ return page.next().then(f);
198
+ }
199
+
200
+ return Promise.resolve();
201
+ })(rooms);
202
+ })
203
+ .then(() => {
204
+ assert.isAbove(spy.callCount, 1);
205
+ assert.calledWith(spy, room0.id);
206
+ assert.calledWith(spy, room1.id);
207
+ });
208
+ });
209
+ });
210
+
211
+ describe('#getWithReadStatus()', () => {
212
+ let room1, user2;
213
+
214
+ before(() =>
215
+ testUsers.create({count: 1}).then(async ([user]) => {
216
+ // Pause for 5 seconds for CI
217
+ await new Promise((done) => setTimeout(done, 5000));
218
+
219
+ user2 = user;
220
+ user2.webex = new WebexCore({credentials: user2.token});
221
+ })
222
+ );
223
+
224
+ // Create a space with one user who is "caught up" and another "behind"
225
+ beforeEach(() =>
226
+ webex.rooms
227
+ .create({title: 'Space to get Read Status from'})
228
+ .then((room) => {
229
+ rooms.push(room);
230
+ room1 = room;
231
+
232
+ return webex.memberships.create({
233
+ roomId: room1.id,
234
+ personId: user2.id,
235
+ });
236
+ })
237
+ // User 1 will post a message, that user2 won't have seen
238
+ .then(() =>
239
+ webex.messages.create({
240
+ roomId: room1.id,
241
+ text: 'First message in room 1 from User 1',
242
+ })
243
+ )
244
+ );
245
+
246
+ it('gets the read status for new room for user1', () =>
247
+ webex.rooms.getWithReadStatus(room1.id).then((roomInfo) => {
248
+ assert.equal(roomInfo.id, room1.id, "Room 1's title matches");
249
+ assert.equal(roomInfo.title, room1.title, "Room 1's title matches");
250
+ assert.isTrue(
251
+ roomInfo.lastSeenActivityDate >= roomInfo.lastActivityDate,
252
+ 'Room 1 is read for User 1'
253
+ );
254
+ }));
255
+
256
+ it('gets the read status for a new room for user2', () =>
257
+ user2.webex.rooms.getWithReadStatus(room1.id).then((roomInfo) => {
258
+ assert.equal(roomInfo.id, room1.id, "Room 1's title matches");
259
+ assert.equal(roomInfo.title, room1.title, "Room 1's title matches");
260
+ assert.isTrue(
261
+ roomInfo.lastSeenActivityDate < roomInfo.lastActivityDate,
262
+ 'Room 1 is unread for User 2'
263
+ );
264
+ }));
265
+ });
266
+
267
+ describe('#listWithReadStatus()', () => {
268
+ // Reminder: we can't run the next two creates in parallel because some of
269
+ // the tests rely on ordering.
270
+ let room1, room2, user2;
271
+
272
+ before(() =>
273
+ testUsers.create({count: 1}).then(async ([user]) => {
274
+ // Pause for 5 seconds for CI
275
+ await new Promise((done) => setTimeout(done, 5000));
276
+
277
+ user2 = user;
278
+ user2.webex = new WebexCore({credentials: user2.token});
279
+ })
280
+ );
281
+
282
+ // Create two spaces with a message from each user in one of them
283
+ beforeEach(() =>
284
+ webex.rooms
285
+ .create({title: 'Unread Message for User 2'})
286
+ .then((room) => {
287
+ rooms.push(room);
288
+ room1 = room;
289
+
290
+ return webex.memberships.create({
291
+ roomId: room1.id,
292
+ personId: user2.id,
293
+ });
294
+ })
295
+ // User 1 will post a message, that user2 won't have seen
296
+ .then(() =>
297
+ webex.messages.create({
298
+ roomId: room1.id,
299
+ text: 'First message in room 1 from User 1',
300
+ })
301
+ )
302
+ // Now create the second space with the two members
303
+ .then(() => webex.rooms.create({title: 'Unread Message for User 1'}))
304
+ .then((room) => {
305
+ rooms.push(room);
306
+ room2 = room;
307
+ })
308
+ .then(() =>
309
+ webex.memberships.create({
310
+ roomId: room2.id,
311
+ personId: user2.id,
312
+ })
313
+ )
314
+ // User 2 will post a message, that User 1 won't have seen
315
+ .then(() =>
316
+ user2.webex.messages.create({
317
+ roomId: room2.id,
318
+ text: 'First message in room 2 from User 2',
319
+ })
320
+ )
321
+ );
322
+
323
+ it('gets the read status for all rooms User 1 is in', () =>
324
+ webex.rooms.listWithReadStatus().then((roomList) => {
325
+ assert.isArray(roomList.items, 'Expect a list or rooms from listWithReadStatus()');
326
+ assert.isAbove(
327
+ roomList.items.length,
328
+ 1,
329
+ 'Expected two or more rooms from listWithReadStatus()'
330
+ );
331
+ for (const room of roomList.items) {
332
+ if (room.id === room1.id) {
333
+ assert.equal(room.title, room1.title, "Room 1's title matches");
334
+ assert.isTrue(
335
+ room.lastSeenActivityDate >= room.lastActivityDate,
336
+ 'Room 1 is read for User 1'
337
+ );
338
+ }
339
+ if (room.id === room2.id) {
340
+ assert.equal(room.title, room2.title, "Room 2's title matches");
341
+ assert.isTrue(
342
+ room.lastSeenActivityDate < room.lastActivityDate,
343
+ 'Room 2 is unread for User 1'
344
+ );
345
+ }
346
+ }
347
+ }));
348
+
349
+ it('gets the read status for all rooms User 2 is in', () =>
350
+ user2.webex.rooms.listWithReadStatus().then((roomList) => {
351
+ assert.isArray(roomList.items, 'Expect a list or rooms from listWithReadStatus()');
352
+ assert.equal(roomList.items.length, 2, 'Expected two rooms from listWithReadStatus()');
353
+ for (const room of roomList.items) {
354
+ if (room.id === room1.id) {
355
+ assert.equal(room.title, room1.title, "Room 1's title matches");
356
+ assert.isTrue(
357
+ room.lastSeenActivityDate < room.lastActivityDate,
358
+ 'Room 1 is unread for User 2'
359
+ );
360
+ } else {
361
+ assert.equal(room.title, room2.title, "Room 2's title matches");
362
+ assert.isTrue(
363
+ room.lastSeenActivityDate >= room.lastActivityDate,
364
+ 'Room 2 is read for User 2'
365
+ );
366
+ }
367
+ }
368
+ }));
369
+ });
370
+
371
+ describe('#update()', () => {
372
+ let room;
373
+
374
+ beforeEach(() =>
375
+ webex.rooms.create({title: 'Webex Test Room'}).then((r) => {
376
+ room = r;
377
+ rooms.push(room);
378
+ assert.property(room, 'id');
379
+ })
380
+ );
381
+
382
+ it("updates a single room's title and validates a room:updated event", () => {
383
+ const r = Object.assign({}, room, {title: 'Webex Test Room with New Title'});
384
+ const updatedEventPromise = new Promise((resolve) => {
385
+ webex.rooms.on('updated', (event) => {
386
+ debug('room:updated event handler called');
387
+ resolve(event);
388
+ });
389
+ });
390
+
391
+ return webex.rooms.listen().then(() =>
392
+ webex.rooms.update(r).then(async (room) => {
393
+ assert.isRoom(room);
394
+ assert.deepEqual(room, r);
395
+ const event = await updatedEventPromise;
396
+
397
+ validateRoomEvent(event, room, actor);
398
+ })
399
+ );
400
+ });
401
+ });
402
+
403
+ describe('#remove()', () => {
404
+ let room;
405
+
406
+ beforeEach(() =>
407
+ webex.rooms.create({title: 'Webex Test Room'}).then((r) => {
408
+ room = r;
409
+ assert.property(room, 'id');
410
+ })
411
+ );
412
+
413
+ it('deletes a single room', () =>
414
+ webex.rooms
415
+ .remove(room)
416
+ .then((body) => {
417
+ assert.notOk(body);
418
+
419
+ return assert.isRejected(webex.rooms.get(room));
420
+ })
421
+ .then((reason) => {
422
+ assert.instanceOf(reason, WebexHttpError.NotFound);
423
+ }));
424
+ });
425
+ });
426
+ });
427
+
428
+ /**
429
+ * Validate a rooms event.
430
+ * @param {Object} event - rooms event
431
+ * @param {Object} room -- return from the API that generated this event
432
+ * @param {Object} actor - person object for user who performed action
433
+ * @returns {void}
434
+ */
435
+ function validateRoomEvent(event, room, actor) {
436
+ assert.isTrue(event.resource === SDK_EVENT.EXTERNAL.RESOURCE.ROOMS, 'not a room event');
437
+ assert.isDefined(event.event, 'room event type not set');
438
+ assert.isDefined(event.created, 'event listener created date not set');
439
+ assert.equal(event.createdBy, actor.id, 'event listener createdBy not set to our actor');
440
+ assert.equal(event.orgId, actor.orgId, "event listener orgId not === to our actor's");
441
+ assert.equal(event.ownedBy, 'creator', 'event listener not owned by creator');
442
+ assert.equal(event.status, 'active', 'event listener status not active');
443
+ assert.equal(event.actorId, actor.id, "event actorId not equal to our actor's id");
444
+
445
+ // Ensure event data matches data returned from function call
446
+ // Skip this until we figure out how conversations converts the internal test user UUID
447
+ assert.equal(event.data.id, room.id, 'event/room.id not equal');
448
+ assert.equal(event.data.isLocked, room.isLocked, 'event/room.isLocked not equal');
449
+ assert.equal(event.data.type, room.type, 'event/room.type not equal');
450
+ debug(`rooms:${event.event} event validated`);
451
+ }