(function (root, factory) {
define([
"jasmine",
"mock",
"test-utils"
], factory);
} (this, function (jasmine, mock, test_utils) {
"use strict";
const Strophe = converse.env.Strophe,
$iq = converse.env.$iq,
_ = converse.env._,
u = converse.env.utils;
describe("Broadcast-/MOTD-Messages (XEP-0133)", function () {
describe("Discovering support", function () {
it("is done automatically", mock.initConverse(async (done, _converse) => {
const IQ_stanzas = _converse.connection.IQ_stanzas;
const IQ_ids = _converse.connection.IQ_ids;
await test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], [Strophe.NS.COMMANDS], []);
await u.waitUntil(() => _.filter(
IQ_stanzas,
iq => iq.querySelector('iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#items"]')).length
);
/*
*
*
*
*
*
*/
let stanza = _.find(IQ_stanzas, function (iq) {
return iq.querySelector('iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#items"]');
});
const items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
stanza = $iq({
'type': 'result',
'from': 'montague.lit',
'to': 'romeo@montague.lit/orchard',
'id': items_IQ_id
}).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
.c('item', {
'jid': 'montague.lit',
'name': 'Set message of the day and send to online users',
'node': 'http://jabber.org/protocol/admin#set-motd'})
.c('item', {
'jid': 'montague.lit',
'name': "Update message of the day (don't send)",
'node': 'http://jabber.org/protocol/admin#edit-motd'});
_converse.connection._dataRecv(test_utils.createRequest(stanza));
expect(_converse.serviceAdminCommands.length).toBe(2);
done();
}))
});
describe("When not supported", function () {
it("the service admin menu is not shown", mock.initConverse(async (done, _converse) => {
const IQ_stanzas = _converse.connection.IQ_stanzas;
const IQ_ids = _converse.connection.IQ_ids;
await test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], [Strophe.NS.COMMANDS], []);
await u.waitUntil(() => _.filter(
IQ_stanzas,
iq => iq.querySelector('iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#items"]')).length
);
/*
*
*
*
*
*
*/
let stanza = _.find(IQ_stanzas, function (iq) {
return iq.querySelector('iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#items"]');
});
const items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
stanza = $iq({
'type': 'error',
'from': 'montague.lit',
'to': 'romeo@montague.lit/orchard',
'id': items_IQ_id
}).c('query', {'node':'announce', 'xmlns': 'http://jabber.org/protocol/disco#items'}).up()
.c('error', {'code': '404', 'type': 'cancel'})
.c('item-not-found', {
'xmlns': 'urn:ietf:params:xml:ns:xmpp-stanzas'});
_converse.connection._dataRecv(test_utils.createRequest(stanza));
const view = _converse.chatboxviews.get('controlbox');
expect(view.el.querySelector('.service-admin-menu')).toBe(null);
done();
}))
});
describe("When supported", function () {
it("the service admin menu is shown", mock.initConverse(async (done, _converse) => {
const IQ_stanzas = _converse.connection.IQ_stanzas;
const IQ_ids = _converse.connection.IQ_ids;
await test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], [Strophe.NS.COMMANDS], []);
await u.waitUntil(() => _.filter(
IQ_stanzas,
iq => iq.querySelector('iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#items"]')).length
);
/*
*
*
*
*
*
*/
let stanza = _.find(IQ_stanzas, function (iq) {
return iq.querySelector('iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#items"]');
});
const items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
stanza = $iq({
'type': 'result',
'from': 'montague.lit',
'to': 'romeo@montague.lit/orchard',
'id': items_IQ_id
}).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
.c('item', {
'jid': 'montague.lit',
'name': 'Set message of the day and send to online users',
'node': 'http://jabber.org/protocol/admin#set-motd'})
.c('item', {
'jid': 'montague.lit',
'name': "Update message of the day (don't send)",
'node': 'http://jabber.org/protocol/admin#edit-motd'});
_converse.connection._dataRecv(test_utils.createRequest(stanza));
const view = _converse.chatboxviews.get('controlbox');
expect(view.el.querySelector('.service-admin-menu')).not.toBe(null);
done();
}))
it("a user may send an announce command", mock.initConverse(async (done, _converse) => {
const IQ_stanzas = _converse.connection.IQ_stanzas;
const IQ_ids = _converse.connection.IQ_ids;
await test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], [Strophe.NS.COMMANDS], []);
await u.waitUntil(() => _.filter(
IQ_stanzas,
iq => iq.querySelector('iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#items"]')).length
);
/*
*
*
*
*
*
*/
let stanza = _.find(IQ_stanzas, function (iq) {
return iq.querySelector('iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#items"]');
});
const items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
stanza = $iq({
'type': 'result',
'from': 'montague.lit',
'to': 'romeo@montague.lit/orchard',
'id': items_IQ_id
}).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
.c('item', {
'jid': 'montague.lit',
'name': 'Set message of the day and send to online users',
'node': 'http://jabber.org/protocol/admin#set-motd'})
.c('item', {
'jid': 'montague.lit',
'name': "Update message of the day (don't send)",
'node': 'http://jabber.org/protocol/admin#edit-motd'});
_converse.connection._dataRecv(test_utils.createRequest(stanza));
let view = _converse.chatboxviews.get('controlbox');
expect(view.el.querySelector('.service-admin-menu')).not.toBe(null);
await test_utils.openControlBox(_converse);
view.el.querySelector("a[title='edit-motd']").click();
await u.waitUntil(() => _.filter(
IQ_stanzas,
iq => iq.querySelector('iq[to="montague.lit"] command[node="http://jabber.org/protocol/admin#edit-motd"]')).length
);
stanza = _.find(IQ_stanzas, function (iq) {
return iq.querySelector('iq[to="montague.lit"] command[node="http://jabber.org/protocol/admin#edit-motd"]');
});
expect(stanza).not.toBe(null);
const command_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
stanza = u.toStanza(`
Update message of the day (don't send)
http://jabber.org/protocol/admin
`);
_converse.connection._dataRecv(test_utils.createRequest(stanza));
view = _converse.chatboxviews.get(_converse.SERVICE_ADMIN_TYPE);
const inputs = view.el.querySelectorAll('.form-control');
inputs[0].value = 'Intelligent subject';
inputs[1].value = 'Extraordinary content';
spyOn(_converse.connection, 'send');
view.el.querySelector('.send-button').click();
expect(_converse.connection.send).toHaveBeenCalled();
stanza = _converse.connection.send.calls.argsFor(0)[0];
expect(Strophe.serialize(stanza)).toBe(
``+
``+
``+
`http://jabber.org/protocol/admin`+
`Intelligent subject`+
`Extraordinary content`+
``
);
done();
}))
});
});
}));