Commit 17401cb9 authored by JC Brand's avatar JC Brand

Improvements to automatic nick registrations

Also expand and fix the associated test.
parent 6457bc76
......@@ -4,6 +4,7 @@
const _ = converse.env._,
$iq = converse.env.$iq,
Strophe = converse.env.Strophe,
sizzle = converse.env.sizzle,
u = converse.env.utils;
describe("Chatrooms", function () {
......@@ -18,58 +19,44 @@
const room_jid = 'coven@chat.shakespeare.lit';
test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'romeo')
.then(() => {
return test_utils.waitUntil(() => _.get(_.filter(
_converse.connection.IQ_stanzas,
iq => iq.nodeTree.querySelector(
`iq[to="coven@chat.shakespeare.lit"] query[xmlns="http://jabber.org/protocol/disco#info"]`
)
).pop(), 'nodeTree'));
}).then(stanza => {
view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
spyOn(view.model, 'parseRoomFeatures').and.callThrough();
const features_stanza = $iq({
'from': room_jid,
'id': stanza.getAttribute('id'),
'to': 'dummy@localhost/desktop',
'type': 'result'
})
.c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
.c('identity', {
'category': 'conference',
'name': 'A Dark Cave',
'type': 'text'
}).up()
.c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
.c('feature', {'var': 'jabber:iq:register'}).up()
.c('feature', {'var': 'muc_passwordprotected'}).up()
.c('feature', {'var': 'muc_hidden'}).up()
.c('feature', {'var': 'muc_temporary'}).up()
.c('feature', {'var': 'muc_open'}).up()
.c('feature', {'var': 'muc_unmoderated'}).up()
.c('feature', {'var': 'muc_nonanonymous'});
_converse.connection._dataRecv(test_utils.createRequest(features_stanza));
return test_utils.waitUntil(() => view.model.parseRoomFeatures.calls.count(), 300)
}).then(() => {
return test_utils.waitUntil(() => _.get(_.filter(
_converse.connection.IQ_stanzas,
iq => iq.nodeTree.querySelector(`iq[to="coven@chat.shakespeare.lit"] query[xmlns="jabber:iq:register"]`)
iq => sizzle(`iq[to="coven@chat.shakespeare.lit"][type="get"] query[xmlns="jabber:iq:register"]`, iq.nodeTree).length
).pop(), 'nodeTree'));
}).then(stanza => {
expect(stanza.outerHTML)
.toBe(`<iq from="dummy@localhost" to="coven@chat.shakespeare.lit" `+
.toBe(`<iq to="coven@chat.shakespeare.lit" from="dummy@localhost/resource" `+
`type="get" xmlns="jabber:client" id="${stanza.getAttribute('id')}">`+
`<query xmlns="jabber:iq:register"/></iq>`);
// Room does not exist
view = _converse.chatboxviews.get(room_jid);
const result = $iq({
'from': view.model.get('jid'),
'id': stanza.getAttribute('id'),
'to': _converse.bare_jid,
'type': 'error',
}).c('error', {'type': "cancel"})
.c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"})
'type': 'result',
}).c('query', {'type': 'jabber:iq:register'})
.c('x', {'xmlns': 'jabber:x:data', 'type': 'form'})
.c('field', {
'label': 'Desired Nickname',
'type': 'text-single',
'var': 'muc#register_roomnick'
}).c('required');
_converse.connection._dataRecv(test_utils.createRequest(result));
return test_utils.waitUntil(() => _.get(_.filter(
_converse.connection.IQ_stanzas,
iq => sizzle(`iq[to="coven@chat.shakespeare.lit"][type="set"] query[xmlns="jabber:iq:register"]`, iq.nodeTree).length
).pop(), 'nodeTree'));
}).then(stanza => {
expect(stanza.outerHTML).toBe(
`<iq to="coven@chat.shakespeare.lit" from="dummy@localhost/resource" type="set" xmlns="jabber:client" id="${stanza.getAttribute('id')}">`+
`<query xmlns="jabber:iq:register">`+
`<x xmlns="jabber:x:data" type="submit">`+
`<field var="FORM_TYPE"><value>http://jabber.org/protocol/muc#register</value></field>`+
`<field var="muc#register_roomnick"><value>romeo</value></field>`+
`</x>`+
`</query>`+
`</iq>`);
done();
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
}));
......
......@@ -810,11 +810,12 @@
},
async registerNickname () {
let iq;
try {
await _converse.api.sendIQ(
iq = await _converse.api.sendIQ(
$iq({
'from': _converse.bare_jid,
'to': this.get('jid'),
'from': _converse.connection.jid,
'type': 'get'
}).c('query', {'xmlns': Strophe.NS.MUC_REGISTER})
);
......@@ -826,10 +827,15 @@
}
return _converse.log(e, Strophe.LogLevel.ERROR);
}
const required_fields = _.map(sizzle('field required', iq), 'parentElement');
if (required_fields.length > 1 || required_fields[0].getAttribute('var') !== 'muc#register_roomnick') {
return _converse.log(`Can't register the user register in the groupchat ${this.get('jid')} due to the required fields`);
}
try {
await _converse.api.sendIQ($iq({
'from': _converse.bare_jid,
'to': this.get('jid'),
'from': _converse.connection.jid,
'type': 'set'
}).c('query', {'xmlns': Strophe.NS.MUC_REGISTER})
.c('x', {'xmlns': Strophe.NS.XFORM, 'type': 'submit'})
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment