-
-
Notifications
You must be signed in to change notification settings - Fork 771
/
nickname.js
504 lines (436 loc) · 23.2 KB
/
nickname.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
/*global mock, converse */
const { Strophe, sizzle, u, stx } = converse.env;
describe("A MUC", function () {
it("allows you to change your nickname via a modal",
mock.initConverse([], {'view_mode': 'fullscreen'}, async function (_converse) {
const muc_jid = 'lounge@montague.lit';
const nick = 'romeo';
const model = await mock.openAndEnterChatRoom(_converse, muc_jid, nick);
expect(model.get('nick')).toBe(nick);
expect(model.occupants.length).toBe(1);
expect(model.occupants.at(0).get('nick')).toBe(nick);
const view = _converse.chatboxviews.get(muc_jid);
const dropdown_item = view.querySelector(".open-nickname-modal");
dropdown_item.click();
const modal = _converse.api.modal.get('converse-muc-nickname-modal');
await u.waitUntil(() => u.isVisible(modal));
const input = modal.querySelector('input[name="nick"]');
expect(input.value).toBe(nick);
const newnick = 'loverboy';
input.value = newnick;
modal.querySelector('input[type="submit"]')?.click();
await u.waitUntil(() => !u.isVisible(modal));
const { sent_stanzas } = _converse.api.connection.get();
const sent_stanza = sent_stanzas.pop()
expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
`<presence from="${_converse.jid}" id="${sent_stanza.getAttribute('id')}" to="${muc_jid}/${newnick}" xmlns="jabber:client"/>`);
// Two presence stanzas are received from the MUC service
_converse.api.connection.get()._dataRecv(mock.createRequest(
stx`
<presence
xmlns="jabber:server"
from='${muc_jid}/${nick}'
id='DC352437-C019-40EC-B590-AF29E879AF98'
to='${_converse.jid}'
type='unavailable'>
<x xmlns='http://jabber.org/protocol/muc#user'>
<item affiliation='member'
jid='${_converse.jid}'
nick='${newnick}'
role='participant'/>
<status code='303'/>
<status code='110'/>
</x>
</presence>`
));
expect(model.get('nick')).toBe(newnick);
_converse.api.connection.get()._dataRecv(mock.createRequest(
stx`
<presence
xmlns="jabber:server"
from='${muc_jid}/${newnick}'
id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
to='${_converse.jid}'>
<x xmlns='http://jabber.org/protocol/muc#user'>
<item affiliation='member'
jid='${_converse.jid}'
role='participant'/>
<status code='110'/>
</x>
</presence>`
));
await u.waitUntil(() => model.occupants.at(0).get('nick') === newnick);
expect(model.occupants.length).toBe(1);
}));
it("informs users if their nicknames have been changed.",
mock.initConverse([], {}, async function (_converse) {
/* The service then sends two presence stanzas to the full JID
* of each occupant (including the occupant who is changing his
* or her room nickname), one of type "unavailable" for the old
* nickname and one indicating availability for the new
* nickname.
*
* See: https://xmpp.org/extensions/xep-0045.html#changenick
*
* <presence
* from='coven@montague.lit/thirdwitch'
* id='DC352437-C019-40EC-B590-AF29E879AF98'
* to='hag66@shakespeare.lit/pda'
* type='unavailable'>
* <x xmlns='http://jabber.org/protocol/muc#user'>
* <item affiliation='member'
* jid='hag66@shakespeare.lit/pda'
* nick='oldhag'
* role='participant'/>
* <status code='303'/>
* <status code='110'/>
* </x>
* </presence>
*
* <presence
* from='coven@montague.lit/oldhag'
* id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
* to='hag66@shakespeare.lit/pda'>
* <x xmlns='http://jabber.org/protocol/muc#user'>
* <item affiliation='member'
* jid='hag66@shakespeare.lit/pda'
* role='participant'/>
* <status code='110'/>
* </x>
* </presence>
*/
const { __ } = _converse;
await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'oldnick');
const view = _converse.chatboxviews.get('lounge@montague.lit');
await u.waitUntil(() => view.querySelectorAll('li .occupant-nick').length, 500);
let occupants = view.querySelector('.occupant-list');
expect(occupants.querySelectorAll('.occupant-nick').length).toBe(1);
expect(occupants.querySelector('.occupant-nick').textContent.trim()).toBe("oldnick");
const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
expect(csntext.trim()).toEqual("oldnick has entered the groupchat");
let presence = stx`
<presence
xmlns="jabber:client"
from='lounge@montague.lit/oldnick'
id='DC352437-C019-40EC-B590-AF29E879AF98'
to='romeo@montague.lit/pda'
type='unavailable'>
<x xmlns='http://jabber.org/protocol/muc#user'>
<item affiliation='owner'
jid='romeo@montague.lit/pda'
nick='newnick'
role='moderator'/>
<status code='303'/>
<status code='110'/>
</x>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(presence));
await u.waitUntil(() => view.querySelectorAll('.chat-info').length);
expect(sizzle('div.chat-info:last').pop().textContent.trim()).toBe(
__(_converse.muc.new_nickname_messages["303"], "newnick")
);
expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
occupants = view.querySelector('.occupant-list');
expect(occupants.querySelectorAll('.occupant-nick').length).toBe(1);
presence = stx`
<presence
from='lounge@montague.lit/newnick'
id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
to='romeo@montague.lit/pda'
xmlns="jabber:client">
<x xmlns='http://jabber.org/protocol/muc#user'>
<item affiliation='owner'
jid='romeo@montague.lit/pda'
role='moderator'/>
<status code='110'/>
</x>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(presence));
expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
expect(view.querySelectorAll('div.chat-info').length).toBe(1);
expect(sizzle('div.chat-info', view)[0].textContent.trim()).toBe(
__(_converse.muc.new_nickname_messages["303"], "newnick")
);
occupants = view.querySelector('.occupant-list');
await u.waitUntil(() => sizzle('.occupant-nick:first', occupants).pop().textContent.trim() === "newnick");
expect(view.model.occupants.length).toBe(1);
expect(view.model.get('nick')).toBe("newnick");
}));
describe("when being entered", function () {
it("will use the user's reserved nickname, if it exists",
mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
const IQ_stanzas = _converse.api.connection.get().IQ_stanzas;
const muc_jid = 'lounge@montague.lit';
await mock.openChatRoom(_converse, 'lounge', 'montague.lit', 'romeo');
let stanza = await u.waitUntil(() => IQ_stanzas.filter(
iq => iq.querySelector(
`iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
)).pop()
);
// We pretend this is a new room, so no disco info is returned.
const features_stanza = stx`
<iq from="lounge@montague.lit"
id="${stanza.getAttribute("id")}"
to="romeo@montague.lit/desktop"
type="error"
xmlns="jabber:client">
<error type="cancel">
<item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
</error>
</iq>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(features_stanza));
/* <iq from='hag66@shakespeare.lit/pda'
* id='getnick1'
* to='coven@chat.shakespeare.lit'
* type='get'>
* <query xmlns='http://jabber.org/protocol/disco#info'
* node='x-roomuser-item'/>
* </iq>
*/
const iq = await u.waitUntil(() => IQ_stanzas.filter(
s => sizzle(`iq[to="${muc_jid}"] query[node="x-roomuser-item"]`, s).length
).pop());
expect(Strophe.serialize(iq)).toBe(
`<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="lounge@montague.lit" `+
`type="get" xmlns="jabber:client">`+
`<query node="x-roomuser-item" xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
const view = _converse.chatboxviews.get('lounge@montague.lit');
stanza = stx`
<iq type="result"
id="${iq.getAttribute("id")}"
from="${view.model.get("jid")}"
to="${_converse.api.connection.get().jid}"
xmlns="jabber:client">
<query xmlns="http://jabber.org/protocol/disco#info" node="x-roomuser-item">
<identity category="conference" name="thirdwitch" type="text"/>
</query>
</iq>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
// The user has just entered the groupchat (because join was called)
// and receives their own presence from the server.
// See example 24:
// https://xmpp.org/extensions/xep-0045.html#enter-pres
const presence = stx`
<presence
to="romeo@montague.lit/orchard"
from="lounge@montague.lit/thirdwitch"
id="DC352437-C019-40EC-B590-AF29E879AF97"
xmlns="jabber:client">
<x xmlns="http://jabber.org/protocol/muc#user">
<item affiliation="member"
jid="romeo@montague.lit/orchard"
role="participant"/>
<status code="110"/>
<status code="210"/>
</x>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(presence));
await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
await mock.returnMemberLists(_converse, muc_jid, [], ['member', 'admin', 'owner']);
await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
const info_text = sizzle('.chat-content .chat-info:first', view).pop().textContent.trim();
expect(info_text).toBe('Your nickname has been automatically set to thirdwitch');
}));
it("will use the nickname set in the global settings if the user doesn't have a VCard nickname",
mock.initConverse(['chatBoxesFetched'], {'nickname': 'Benedict-Cucumberpatch'},
async function (_converse) {
await mock.openChatRoomViaModal(_converse, 'roomy@muc.montague.lit');
const view = _converse.chatboxviews.get('roomy@muc.montague.lit');
expect(view.model.get('nick')).toBe('Benedict-Cucumberpatch');
}));
it("will render a nickname form if a nickname conflict happens and muc_nickname_from_jid=false",
mock.initConverse([], { vcard: { nickname: '' }}, async function (_converse) {
const muc_jid = 'conflicted@muc.montague.lit';
await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
const iq = await u.waitUntil(() => _converse.api.connection.get().IQ_stanzas.filter(
iq => iq.querySelector(
`iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
)).pop());
const features_stanza = stx`
<iq from="${muc_jid}"
id="${iq.getAttribute('id')}"
to="romeo@montague.lit/desktop"
type="result"
xmlns="jabber:client">
<query xmlns="http://jabber.org/protocol/disco#info">
<identity category="conference" name="A Dark Cave" type="text"/>
<feature var="http://jabber.org/protocol/muc"/>
<feature var="muc_hidden"/>
<feature var="muc_temporary"/>
</query>
</iq>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(features_stanza));
const view = _converse.chatboxviews.get(muc_jid);
await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
const presence = stx`
<presence
from="${muc_jid}/romeo"
id="${u.getUniqueId()}"
to="romeo@montague.lit/pda"
type="error"
xmlns="jabber:client">
<x xmlns="http://jabber.org/protocol/muc"/>
<error by="${muc_jid}" type="cancel">
<conflict xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
</error>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(presence));
const el = await u.waitUntil(() => view.querySelector('.muc-nickname-form .validation-message'));
expect(el.textContent.trim()).toBe('The nickname you chose is reserved or currently in use, please choose a different one.');
}));
it("will automatically choose a new nickname if a nickname conflict happens and muc_nickname_from_jid=true",
mock.initConverse(['chatBoxesFetched'], {vcard: { nickname: '' }}, async function (_converse) {
const { api } = _converse;
const muc_jid = 'conflicting@muc.montague.lit'
await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
/* <presence
* from='coven@chat.shakespeare.lit/thirdwitch'
* id='n13mt3l'
* to='hag66@shakespeare.lit/pda'
* type='error'>
* <x xmlns='http://jabber.org/protocol/muc'/>
* <error by='coven@chat.shakespeare.lit' type='cancel'>
* <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
* </error>
* </presence>
*/
api.settings.set('muc_nickname_from_jid', true);
let presence = stx`
<presence
xmlns="jabber:client"
from='${muc_jid}/romeo'
id='${u.getUniqueId()}'
to='romeo@montague.lit/pda'
type='error'>
<x xmlns='http://jabber.org/protocol/muc'/>
<error by='${muc_jid}' type='cancel'>
<conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>
</presence>`;
const view = _converse.chatboxviews.get(muc_jid);
spyOn(view.model, 'join').and.callThrough();
// Simulate repeatedly that there's already someone in the groupchat
// with that nickname
_converse.api.connection.get()._dataRecv(mock.createRequest(presence));
expect(view.model.join).toHaveBeenCalledWith('romeo-2');
presence = stx`
<presence
xmlns="jabber:client"
from="${muc_jid}/romeo-2"
id="${u.getUniqueId()}"
to="romeo@montague.lit/pda"
type="error">
<x xmlns="http://jabber.org/protocol/muc"/>
<error by="${muc_jid}" type="cancel">
<conflict xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
</error>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(presence));
expect(view.model.join).toHaveBeenCalledWith('romeo-3');
presence = stx`
<presence
xmlns="jabber:client"
from="${muc_jid}/romeo-3"
id="${u.getUniqueId()}"
to="romeo@montague.lit/pda"
type="error">
<x xmlns="http://jabber.org/protocol/muc"/>
<error by="${muc_jid}" type="cancel">
<conflict xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
</error>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(presence));
expect(view.model.join).toHaveBeenCalledWith('romeo-4');
}));
it("will show an error message if the user's nickname doesn't conform to groupchat policy",
mock.initConverse([], {}, async function (_converse) {
const muc_jid = 'conformist@muc.montague.lit'
await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
const iq = await u.waitUntil(() => _converse.api.connection.get().IQ_stanzas.filter(
iq => iq.querySelector(
`iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
)).pop());
const features_stanza = stx`
<iq from="${muc_jid}"
id="${iq.getAttribute('id')}"
to="romeo@montague.lit/desktop"
type="result"
xmlns="jabber:client">
<query xmlns="http://jabber.org/protocol/disco#info">
<identity category="conference" name="A Dark Cave" type="text"/>
<feature var="http://jabber.org/protocol/muc"/>
</query>
</iq>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(features_stanza));
const view = _converse.chatboxviews.get(muc_jid);
await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
const presence = stx`
<presence
xmlns="jabber:client"
from='${muc_jid}/romeo'
id='${u.getUniqueId()}'
to='romeo@montague.lit/pda'
type='error'>
<x xmlns='http://jabber.org/protocol/muc'/>
<error by='lounge@montague.lit' type='cancel'>
<not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(presence));
const el = await u.waitUntil(() => view.querySelector('.chatroom-body converse-muc-disconnected .disconnect-msg:last-child'));
expect(el.textContent.trim()).toBe("Your nickname doesn't conform to this groupchat's policies.");
}));
it("doesn't show the nickname field if locked_muc_nickname is true",
mock.initConverse(['chatBoxesFetched'], {
locked_muc_nickname: true,
muc_nickname_from_jid: true,
vcard: { nickname: '' },
}, async function (_converse) {
await mock.openControlBox(_converse);
await mock.waitForRoster(_converse, 'current', 0);
const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
roomspanel.querySelector('.show-add-muc-modal').click();
const modal = _converse.api.modal.get('converse-add-muc-modal');
await u.waitUntil(() => u.isVisible(modal), 1000)
const name_input = modal.querySelector('input[name="chatroom"]');
name_input.value = 'lounge@montague.lit';
expect(modal.querySelector('label[for="nickname"]')).toBe(null);
expect(modal.querySelector('input[name="nickname"]')).toBe(null);
modal.querySelector('form input[type="submit"]').click();
await u.waitUntil(() => _converse.chatboxes.length > 1);
const chatroom = _converse.chatboxes.get('lounge@montague.lit');
expect(chatroom.get('nick')).toBe('romeo');
}));
it("uses the JID node if muc_nickname_from_jid is set to true",
mock.initConverse(
['chatBoxesFetched'],
{
'muc_nickname_from_jid': true,
blacklisted_plugins: ['converse-vcard']
}, async function (_converse) {
await mock.openControlBox(_converse);
await mock.waitForRoster(_converse, 'current', 0);
const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
roomspanel.querySelector('.show-add-muc-modal').click();
const modal = _converse.api.modal.get('converse-add-muc-modal');
await u.waitUntil(() => u.isVisible(modal), 1000)
const label_nick = modal.querySelector('label[for="nickname"]');
expect(label_nick.textContent.trim()).toBe('Nickname:');
const nick_input = modal.querySelector('input[name="nickname"]');
expect(nick_input.value).toBe('romeo');
}));
it("uses the nickname passed in to converse.initialize",
mock.initConverse(['chatBoxesFetched'], {'nickname': 'st.nick'}, async function (_converse) {
await mock.openControlBox(_converse);
await mock.waitForRoster(_converse, 'current', 0);
const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
roomspanel.querySelector('.show-add-muc-modal').click();
const modal = _converse.api.modal.get('converse-add-muc-modal');
await u.waitUntil(() => u.isVisible(modal), 1000)
const label_nick = modal.querySelector('label[for="nickname"]');
expect(label_nick.textContent.trim()).toBe('Nickname:');
const nick_input = modal.querySelector('input[name="nickname"]');
expect(nick_input.value).toBe('st.nick');
}));
});
});