This repository was archived by the owner on Nov 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtest.session-joining.selenium.js
617 lines (579 loc) · 26.2 KB
/
test.session-joining.selenium.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
var expect = require('expect.js'),
_ = require("underscore"),
common = require('./common'),
models = require("../lib/server-models.js"),
Promise = require("bluebird");
requests = require("superagent");
describe("SESSION JOINING PARTICIPANT LISTS", function() {
if (process.env.SKIP_SELENIUM_TESTS) {
return;
}
this.timeout(60000); // Extra long timeout for selenium :(
var browser = null,
event = null;
// Different leave-stop-timeout to monkey-patch in to test leave-stops.
// Selenium is not compatible with sinon.useFakeTimers, so tests have to
// wait this long in real-time.
var ORIG_LEAVE_STOP_TIMEOUT;
var TEST_LEAVE_STOP_TIMEOUT = 3000;
var ORIG_JOINING_TIMEOUT;
var TEST_JOINING_TIMEOUT = 3000;
before(function(done) {
// Reduce joining timeout to speed up the test.
ORIG_LEAVE_STOP_TIMEOUT = models.ServerSession.prototype.HANGOUT_LEAVE_STOP_TIMEOUT;
ORIG_JOINING_TIMEOUT = models.ServerSession.prototype.JOINING_EXPIRATION_TIMEOUT;
models.ServerSession.prototype.HANGOUT_LEAVE_STOP_TIMEOUT = TEST_LEAVE_STOP_TIMEOUT;
models.ServerSession.prototype.JOINING_EXPIRATION_TIMEOUT = TEST_JOINING_TIMEOUT;
this.timeout(240000);
common.stopSeleniumServer().then(function() {
common.getSeleniumBrowser(function (theBrowser) {
browser = theBrowser;
common.standardSetup(function() {
event = common.server.db.events.findWhere({shortName: "writers-at-work"});
event.save({open: true});
done();
});
});
});
});
after(function(done) {
models.ServerSession.prototype.HANGOUT_LEAVE_STOP_TIMEOUT = ORIG_LEAVE_STOP_TIMEOUT;
models.ServerSession.prototype.JOINING_EXPIRATION_TIMEOUT = ORIG_JOINING_TIMEOUT;
event.save({open: false});
browser.quit().then(function() {
common.standardShutdown(done);
});
});
it("Updates session participant list when not present in the event", function(done) {
var session = event.get("sessions").at(0);
session.set("approved", true);
var sock;
var participantList = "#session-list .session[data-session-id='" + session.id + "'] li";
var ready = false;
browser.mockAuthenticate("regular1");
browser.get(common.URL + "/event/" + event.id);
browser.waitForEventReady(event, "regular1");
browser.byCsss("#presence-user-gutter .user").then(function(els) {
expect(els.length).to.be(1);
});
// We should have an empty session participant list.
browser.byCsss(participantList).then(function(els) {
expect(els.length).to.be(10);
_.each(els, function(el) {
el.getText().then(function(text) {
expect(text).to.eql("");
});
});
}).then(function() {
// But then we connect a socket directly to the session.
var roomId = session.getRoomId();
common.authedSock("regular2", roomId, function(theSock) {
sock = theSock;
});
});
// Now we should have a user show up in the participant list.
browser.waitForSelector(participantList + " i.fa-user").then(function() {
expect(session.getNumConnectedParticipants()).to.be(1);
expect(session.getState()).to.be("no url");
return sock.promiseClose();
});
// The participant list should clear when the socket closes.
browser.waitWithTimeout(function() {
return browser.byCsss(participantList + " i.fa-user").then(function(els) {
return els.length === 0;
});
}).then(function() {
expect(session.getNumConnectedParticipants()).to.be(0);
// Should stop immediately when there's no hangout URL to preserve.
expect(session.getState()).to.be("stopped");
done();
});
});
it("Updates session participant list when present in the event", function(done) {
var sock1, sock2;
var session = event.get("sessions").at(0);
session.set("approved", true);
var participantList = "#session-list .session[data-session-id='" + session.id + "'] li";
browser.mockAuthenticate("regular1");
// Connect the browser to the event page, then connect a socket
// belonging to the same user to both event and session rooms.
browser.get(common.URL + "/event/" + event.id);
browser.waitForEventReady(event, "regular1");
browser.then(function() {
common.authedSock("regular2", event.getRoomId(), function(sock) {
sock1 = sock;
common.authedSock("regular2", session.getRoomId(), function(sock) {
sock2 = sock;
});
});
});
// Two sockets should show up on the event page -- the browser, and the
// bare socket.
browser.waitWithTimeout(function() {
return browser.byCsss("#presence-user-gutter .user").then(function(els) {
return els.length === 2;
});
});
// One socket should show up in the participant list.
browser.waitForSelector(participantList + " i.fa-user").then(function() {
return sock1.promiseClose();
});
// Have the socket leave the event page, but not the participant list.
browser.waitWithTimeout(function() {
return browser.byCsss("#presence-user-gutter .user").then(function(els) {
return els.length == 1;
});
});
// Should still be in the participant list.
browser.byCsss(participantList + " i.fa-user").then(function(els) {
expect(els.length).to.be(1);
expect(session.getNumConnectedParticipants()).to.be(1);
expect(session.getState()).to.be("no url");
}).then(function() {
// Leave!
return sock2.promiseClose();
});
// Now noone should be left
browser.waitWithTimeout(function() {
return browser.byCsss(participantList + " i.fa-user").then(function(els) {
return els.length == 0;
});
}).then(function() {;
expect(session.getNumConnectedParticipants()).to.be(0);
expect(session.getState()).to.be("stopped");
done();
});
});
it("Preserves session list position", function(done) {
// We'll use 3 sockets --- s1, s2, s3 in the session.
var s1, s2, s3;
var session = event.get("sessions").at(0);
session.set("approved", true);
var u1 = common.server.db.users.findWhere({"sock-key": "regular1"});
var u2 = common.server.db.users.findWhere({"sock-key": "regular2"});
var u3 = common.server.db.users.findWhere({"sock-key": "admin1"});
var observer = common.server.db.users.findWhere({"sock-key": "admin2"});
browser.mockAuthenticate(observer.get("sock-key"));
browser.get(common.URL + event.getEventUrl());
browser.waitForEventReady(event, observer.get("sock-key"));
browser.then(function() {
return Promise.all([
common.authedSock(u1.getSockKey(), session.getRoomId())
.then(function(sock) { s1 = sock; }),
common.authedSock(u2.getSockKey(), session.getRoomId())
.then(function(sock) { s2 = sock; }),
common.authedSock(u3.getSockKey(), session.getRoomId())
.then(function(sock) { s3 = sock; })
]);
});
function checkSessionUsers(list) {
var selector ="[data-session-id='" + session.id + "'] ul.hangout-users li";
return browser.waitWithTimeout(function() {
return browser.byCsss(selector).then(function(els) {
return Promise.all(_.map(els, function(el, i) {
return new Promise(function(resolve, reject) {
el.getAttribute("class").then(function(cls) {
if (list[i] === null && cls === "empty") {
return resolve();
} else if (cls === "user focus") {
return resolve();
} else {
return reject("Unexpected class " + cls);
}
}).then(null, function(err) {
return reject("Selenium error");
});
});
})).catch(function() {
return false;
});
});
}, 45000);
};
checkSessionUsers([u1, u2, u3, null, null, null, null, null, null, null]);
browser.then(function() { return s2.promiseClose(); });
checkSessionUsers([u1, null, u3, null, null, null, null, null, null, null]);
browser.then(function() { return s1.promiseClose(); });
checkSessionUsers([null, null, u3, null, null, null, null, null, null, null]);
browser.then(function() {
return common.authedSock(
u2.getSockKey(), session.getRoomId()
).then(function(sock) { s2 = sock });
});
checkSessionUsers([null, u2, u3, null, null, null, null, null, null, null]);
browser.then(function() { return s3.promiseClose(); })
browser.then(function() { return s2.promiseClose(); });
browser.then(function() { done(); });
});
it("Limits participants to the join cap", function(done) {
var session = event.get("sessions").at(0);
session.set("approved", true);
var joinButtonText = "[data-session-id='" + session.id + "'] .attend .text";
session.set("joinCap", 2);
browser.mockAuthenticate("admin1");
browser.get(common.URL + event.getEventUrl());
function buttonTextIs(text) {
browser.waitForEventReady(event, "admin1");
return browser.waitForSelector(joinButtonText).then(function() {;
return browser.waitWithTimeout(function() {
return browser.byCss(joinButtonText).getText().then(function(txt) {
return text === txt;
}).then(null, function(err) {
return false;
});
});
});
}
// Initially, sessions are locked.
buttonTextIs("LOCKED");
// Open the sessions and we should have JOIN.
browser.then(function() { event.set("sessionsOpen", true); });
// Merely changing sessionsOpen on the event doesn't currently trigger
// broadcast, so just re-grab the page.
browser.get(common.URL + event.getEventUrl());
buttonTextIs("JOIN");
// Add connected participants (triggering broadcast), and we should be full.
browser.then(function() {
session.setConnectedParticipants([
{id: "t1", displayName: "test1"},
{id: "t2", displayName: "test2"},
]);
});
buttonTextIs("FULL");
// Remove one, and we're back to JOINable.
browser.then(function() {
session.setConnectedParticipants([{id: "t1", displayName: "test1"}]);
});
buttonTextIs("JOIN");
// Even if more than the join cap join, we only show the 2.
browser.then(function() {
session.addJoiningParticipant({id: "j1", displayName: "joining1"});
session.addJoiningParticipant({id: "j2", displayName: "joining2"});
session.setConnectedParticipants([
{id: "t1", displayName: "test1"},
{id: "t2", displayName: "test2"},
{id: "t3", displayName: "test3"}
]);
});
var heads = ".session[data-session-id='" + session.id + "'] .hangout-users li";
browser.wait(function() {
return browser.byCsss(heads).then(function(els) {
return els.length === 2;
});
});
browser.then(function() {
// Clean up.
event.set("sessionsOpen", false);
session.set("joinCap", 10);
session.onHangoutStopped();
done();
});
});
it("Handles stop conditions when session has been deleted", function(done) {
var session = event.get("sessions").at(0);
session.set("approved", true);
browser.mockAuthenticate("admin1");
// Visit the session to "start" it.
browser.get(common.URL + "/test/hangout/" + session.id + "/");
browser.waitForHangoutReady(session, "admin1");
// Then leave, by going to the event page, where we'll delete it.
browser.get(common.URL + "/event/" + event.id);
browser.waitWithTimeout(function() {
return session.getState() === "stopping";
});
browser.waitForEventReady(event, "admin1");
function deleteSession() {
browser.byCss(
"[data-session-id='" + session.id + "'] .delete"
).click().then(null, function(err) {
// About 1 in 20 times this runs, we get this error. Just repeat
// the call if so.
if (JSON.stringify(err).indexOf("Uncaught StaleElementReferenceError: Element is no longer attached to the DOM") != -1) {
console.log("Catching delete button error; retrying");
deleteSession();
} else {
throw err;
}
});
}
deleteSession();
browser.then(function() {
setTimeout(function() {
// We're left untouched, as a 'save' would throw an error.
expect(session.getState()).to.eql("stopping overdue; uncleared stopping; stale url; unstopped");
done();
}, TEST_LEAVE_STOP_TIMEOUT + 10);
});
});
it("Correctly stops permalink sessions", function(done) {
// Ensure that permalink sessions aren't broken by the logic that
// prevents deleted sessions from being stopped.
var session = new models.ServerSession({
isPermalinkSession: true,
shortCode: "test"
}, {collection: common.server.db.permalinkSessions});
common.server.db.permalinkSessions.add(session);
session.save({}, {
success: function() {
browser.mockAuthenticate("regular1");
browser.get(common.URL + "/test/hangout/" + session.id + "/");
browser.waitForHangoutReady(session, "regular1");
// leave..
browser.get(common.FAST_URL).then(function() {
expect(session.getState()).to.eql("stopping");
setTimeout(function() {
expect(session.getState()).to.eql("stopped");
done();
}, TEST_LEAVE_STOP_TIMEOUT + 1);
});
},
error: function(err) {
done(err);
}
});
});
function disconnectionModalShowing(isShowing) {
return browser.waitWithTimeout(function() {
return browser.executeScript(
"return window.$ && $('#disconnected-modal').is(':visible')"
).then(function(result) {
return result == isShowing;
});
});
}
it("Auto-reconnects event sockets on server restart", function(done) {
browser.mockAuthenticate("regular1");
var sock;
// Connect the browser and a socket to the event page.
browser.get(common.URL + "/event/" + event.id);
browser.waitForEventReady(event, "regular1");
browser.then(function() {
expect(event.get("connectedUsers").length).to.be(1);
});
browser.then(function() {
common.authedSock("regular2", event.getRoomId(), function(thesock) {
sock = thesock;
});
});
browser.waitWithTimeout(function() {
return browser.byCsss("#presence-user-gutter .user").then(function(els) {
return els.length == 2;
});
}).then(function() {
expect(event.get("connectedUsers").length).to.be(2);
// Now restart the server...
common.restartServer(function onStopped(restart) {
// The browser should get a dialog warning us that disconnection happened.
disconnectionModalShowing(true).then(function() {
restart();
});
}, function onRestarted() {
// When the server restarts, the browser should auto
// re-connect. The bare socket, on the other hand, shouldn't,
// as it didn't fire reconnection. So we expect to see only one
// user now -- simulating what should happen if someone goes
// away during server down time.
var eventId = event.id;
disconnectionModalShowing(false);
browser.waitWithTimeout(function() {
event = common.server.db.events.get(eventId);
return event.get("connectedUsers").length === 1;
});
browser.waitWithTimeout(function() {
return browser.byCsss("#presence-user-gutter .user").then(function(els) {
return els.length === 1;
})
});
browser.then(function() {
done();
});
});
});
});
function framedDisconnectionModalShowing(isShowing) {
return browser.waitWithTimeout(function() {
return browser.executeScript(
// Three frames deep. [[INCEPTION]]
"var f1 = document.getElementsByTagName('iframe'); " +
"if (f1.length) { " +
" var f2 = f1[0].contentWindow.document.getElementsByTagName('iframe'); " +
" if (f2.length) { " +
" var $ = f2[0].contentWindow.$; " +
" return $ && $('#disconnected-modal').attr('aria-hidden') === 'false';" +
" } " +
"} " +
"return false;"
).then(function(result) {
//console.log(result, isShowing, result == isShowing);
return result == isShowing;
});
});
}
it("Reconnects session sockets on server restart", function(done) {
// TODO: This test is the most brittle and inconsistent in the suite.
// We ought to see if there's a more reliable way to test this.
browser.mockAuthenticate("regular1");
var sock;
var session = event.get("sessions").at(0);
session.set("approved", true);
browser.get(common.URL + "/test/hangout/" + session.id + "/")
browser.waitForHangoutReady(session, "regular1");
browser.then(function() {
return common.authedSock("regular2", session.getRoomId()).then(function(thesock) {
sock = thesock;
});
});
browser.waitWithTimeout(function() {
return session.get("connectedParticipants").length == 2;
}).then(function() {
common.restartServer(function onStopped(restart) {
framedDisconnectionModalShowing(true).then(function() {
restart();
});
}, function onRestarted() {
framedDisconnectionModalShowing(false);
browser.waitWithTimeout(function() {
// Refresh session from new DB.
event = common.server.db.events.get(event.id);
session = event.get("sessions").get(session.id);
return session.getNumConnectedParticipants() === 1;
}).then(function() {
done();
});
});
});
});
it("Warns you when you're in the wrong hangout", function(done) {
var session = event.get("sessions").at(0);
session.set("approved", true);
var button = "document.getElementsByTagName('iframe')[0].contentWindow" +
".document.getElementsByTagName('iframe')[0].contentWindow" +
".document.getElementById('wrong-hangout-url')"
browser.mockAuthenticate("regular1").then(function() {
session.set("hangout-url", "http://example.com/");
});
browser.get(common.URL + "/test/hangout/" + session.id + "/");
browser.waitWithTimeout(function() {
return browser.executeScript(
"return !!" + button + ";"
).then(null, function(err) {
return false;
});
});
browser.executeScript("return " + button + ".href").then(function(href) {
expect(href).to.be("http://example.com/");
});
// Go to a different URL that won't throw a modal dialog up.
browser.get(common.FAST_URL).then(function() {
done();
});
});
it("Doesn't clear hangout URL immediately, but rather after a delay.", function(done) {
var session = event.get("sessions").at(1);
session.set("approved", true);
session.set("hangout-url", null);
common.authedSock("regular1", session.getRoomId()).then(function(sock) {
sock.on("data", function(message) {
var msg = JSON.parse(message);
if (msg.type == "session/set-hangout-url-ack") {
expect(session.get("hangout-url")).to.not.be(null);
sock.write(JSON.stringify({
type: "leave",
args: {id: session.getRoomId()}
}));
} else if (msg.type == "leave-ack") {
expect(session.getNumConnectedParticipants()).to.be(0);
expect(session.get("hangout-url")).to.not.be(null);
// We don't test that it actually gets invalidated here,
// because the delay is LONG, and sinon doesn't play well
// with asynchronous socket comms.
sock.promiseClose().then(done);
} else {
sock.promiseClose().then(function() {
done(new Error("Unexpected message: " + message));
});
}
});
sock.write(JSON.stringify({
type: "session/set-hangout-url",
args: {
url: "http://example.com",
sessionId: session.id
},
}));
});
});
it("Doesn't set connected participants if URL is invalid.", function(done) {
var session = event.get("sessions").at(1);
session.set("approved", true);
var participants = [{id: "p1", displayName: "P1", picture: ""},
{id: "p2", displayName: "P2", picture: ""},
{id: "0", displayName: "Regular1 Mock", picture: ""}];
session.set("hangout-url", "http://example.com");
session.set("connectedParticipants", participants);
common.authedSock("regular1", session.getRoomId()).then(function(sock) {
sock.on("data", function(message) {
var msg = JSON.parse(message);
if (msg.type === "session/set-connected-participants-err") {
expect(msg.args).to.eql("Not in correct hangout");
expect(session.get("connectedParticipants")).to.eql(participants);
sock.promiseClose().then(done);
} else {
sock.promiseClose().then(function() {
done(new Error("Unexpected message: " + message));
});
}
});
sock.write(JSON.stringify({
type: "session/set-connected-participants",
args: {
sessionId: session.id,
"hangout-url": "http://example2.com",
connectedParticipants: [
{id: "0", displayName: "Regular1 Mock", picture: ""}
]
}
}));
});
});
it("Adds joining participant UI", function(done) {
var session = event.get("sessions").at(0);
session.set("approved", true);
var u1 = common.server.db.users.findWhere({"sock-key": "regular1"});
var u2 = common.server.db.users.findWhere({"sock-key": "regular2"});
browser.mockAuthenticate(u1.getSockKey());
browser.get(common.URL + "/event/" + event.id);
browser.waitForEventReady(event, u1.getSockKey());
browser.then(function() {
return new Promise(function (resolve, reject) {
var url = common.URL + "/session/" + session.get("session-key");
requests.get(url)
.set("x-mock-user", u2.getSockKey())
.redirects(0)
.end(function(res) {
expect(res.status).to.be(302);
expect(session.get("joiningParticipants")).to.eql([{
id: u2.id,
displayName: u2.get("displayName"),
picture: u2.get("picture")
}]);
resolve();
});
});
});
browser.waitForSelector(".hangout-users li.user.joining");
browser.byCsss(".hangout-users li.user.joining").then(function(els) {
expect(els.length).to.be(1);
});
browser.waitTime(models.ServerSession.prototype.JOINING_EXPIRATION_TIMEOUT + 5000);
browser.then(function() {
expect(session.get("joiningParticipants").length).to.be(0);
});
browser.waitWithTimeout(function() {
return browser.byCsss(".hangout-users li.user.joining").then(function(els) {
return els.length === 0;
});
});
browser.then(function() { done(); });
});
});