2013-05-11 14:20:07 +02:00
|
|
|
(function (root, factory) {
|
2018-06-03 16:40:20 +02:00
|
|
|
define("mock", [], factory);
|
|
|
|
}(this, function () {
|
2019-10-06 12:48:05 +02:00
|
|
|
|
|
|
|
converse.load();
|
|
|
|
|
2019-02-12 14:21:45 +01:00
|
|
|
const _ = converse.env._;
|
|
|
|
const Promise = converse.env.Promise;
|
|
|
|
const Strophe = converse.env.Strophe;
|
2019-05-06 11:16:56 +02:00
|
|
|
const dayjs = converse.env.dayjs;
|
2019-02-12 14:21:45 +01:00
|
|
|
const $iq = converse.env.$iq;
|
2018-02-07 13:26:39 +01:00
|
|
|
|
2018-05-13 14:12:53 +02:00
|
|
|
window.libsignal = {
|
2018-07-25 12:59:12 +02:00
|
|
|
'SignalProtocolAddress': function (name, device_id) {
|
|
|
|
this.name = name;
|
|
|
|
this.deviceId = device_id;
|
|
|
|
},
|
|
|
|
'SessionCipher': function (storage, remote_address) {
|
|
|
|
this.remoteAddress = remote_address;
|
|
|
|
this.storage = storage;
|
|
|
|
this.encrypt = () => Promise.resolve({
|
2018-07-28 16:36:23 +02:00
|
|
|
'type': 1,
|
|
|
|
'body': 'c1ph3R73X7',
|
2019-02-12 14:21:45 +01:00
|
|
|
'registrationId': '1337'
|
2018-07-25 12:59:12 +02:00
|
|
|
});
|
2018-08-27 14:25:34 +02:00
|
|
|
this.decryptPreKeyWhisperMessage = (key_and_tag) => {
|
2018-08-31 18:49:47 +02:00
|
|
|
return Promise.resolve(key_and_tag);
|
2018-08-27 14:25:34 +02:00
|
|
|
};
|
|
|
|
|
2018-08-04 19:41:06 +02:00
|
|
|
this.decryptWhisperMessage = (key_and_tag) => {
|
2018-08-31 18:49:47 +02:00
|
|
|
return Promise.resolve(key_and_tag);
|
2018-08-04 19:41:06 +02:00
|
|
|
}
|
2018-07-25 12:59:12 +02:00
|
|
|
},
|
2018-11-18 19:14:22 +01:00
|
|
|
'SessionBuilder': function (storage, remote_address) { // eslint-disable-line no-unused-vars
|
2018-07-25 12:59:12 +02:00
|
|
|
this.processPreKey = function () {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
},
|
2018-05-13 14:12:53 +02:00
|
|
|
'KeyHelper': {
|
|
|
|
'generateIdentityKeyPair': function () {
|
|
|
|
return Promise.resolve({
|
2018-07-21 21:51:50 +02:00
|
|
|
'pubKey': new TextEncoder('utf-8').encode('1234'),
|
|
|
|
'privKey': new TextEncoder('utf-8').encode('4321')
|
2018-05-13 14:12:53 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
'generateRegistrationId': function () {
|
2018-05-13 16:04:52 +02:00
|
|
|
return '123456789';
|
2018-05-13 14:12:53 +02:00
|
|
|
},
|
|
|
|
'generatePreKey': function (keyid) {
|
|
|
|
return Promise.resolve({
|
|
|
|
'keyId': keyid,
|
|
|
|
'keyPair': {
|
2018-07-21 21:51:50 +02:00
|
|
|
'pubKey': new TextEncoder('utf-8').encode('1234'),
|
|
|
|
'privKey': new TextEncoder('utf-8').encode('4321')
|
2018-05-13 14:12:53 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'generateSignedPreKey': function (identity_keypair, keyid) {
|
|
|
|
return Promise.resolve({
|
2018-07-21 21:51:50 +02:00
|
|
|
'signature': new TextEncoder('utf-8').encode('11112222333344445555'),
|
2018-05-13 14:12:53 +02:00
|
|
|
'keyId': keyid,
|
|
|
|
'keyPair': {
|
2018-07-21 21:51:50 +02:00
|
|
|
'pubKey': new TextEncoder('utf-8').encode('1234'),
|
|
|
|
'privKey': new TextEncoder('utf-8').encode('4321')
|
2018-05-13 14:12:53 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-12 14:21:45 +01:00
|
|
|
const mock = {};
|
2019-11-20 18:26:41 +01:00
|
|
|
|
|
|
|
mock.default_muc_features = [
|
|
|
|
'http://jabber.org/protocol/muc',
|
|
|
|
'jabber:iq:register',
|
|
|
|
Strophe.NS.SID,
|
|
|
|
Strophe.NS.MAM,
|
|
|
|
'muc_passwordprotected',
|
|
|
|
'muc_hidden',
|
|
|
|
'muc_temporary',
|
|
|
|
'muc_open',
|
|
|
|
'muc_unmoderated',
|
|
|
|
'muc_anonymous'
|
|
|
|
];
|
|
|
|
|
2018-02-07 13:26:39 +01:00
|
|
|
mock.view_mode = 'overlayed';
|
|
|
|
|
2013-11-02 09:56:20 +01:00
|
|
|
// Names from http://www.fakenamegenerator.com/
|
|
|
|
mock.req_names = [
|
2019-09-04 19:11:26 +02:00
|
|
|
'Escalus, prince of Verona', 'The Nurse', 'Paris'
|
2013-11-02 09:56:20 +01:00
|
|
|
];
|
|
|
|
mock.pend_names = [
|
2019-10-24 14:29:15 +02:00
|
|
|
'Lord Capulet', 'Guard', 'Servant'
|
2013-11-02 09:56:20 +01:00
|
|
|
];
|
2019-10-24 14:29:15 +02:00
|
|
|
mock.current_contacts_map = {
|
|
|
|
'Mercutio': ['Colleagues', 'friends & acquaintences'],
|
|
|
|
'Juliet Capulet': ['friends & acquaintences'],
|
|
|
|
'Lady Montague': ['Colleagues', 'Family'],
|
|
|
|
'Lord Montague': ['Family'],
|
|
|
|
'Friar Laurence': ['friends & acquaintences'],
|
|
|
|
'Tybalt': ['friends & acquaintences'],
|
|
|
|
'Lady Capulet': ['ænemies'],
|
|
|
|
'Benviolo': ['friends & acquaintences'],
|
|
|
|
'Balthasar': ['Colleagues'],
|
|
|
|
'Peter': ['Colleagues'],
|
|
|
|
'Abram': ['Colleagues'],
|
|
|
|
'Sampson': ['Colleagues'],
|
|
|
|
'Gregory': ['friends & acquaintences'],
|
|
|
|
'Potpan': [],
|
|
|
|
'Friar John': []
|
|
|
|
};
|
|
|
|
|
|
|
|
const map = mock.current_contacts_map;
|
|
|
|
const groups_map = {};
|
|
|
|
Object.keys(map).forEach(k => {
|
|
|
|
const groups = map[k].length ? map[k] : ["Ungrouped"];
|
|
|
|
Object.values(groups).forEach(g => {
|
|
|
|
groups_map[g] = groups_map[g] ? [...groups_map[g], k] : [k]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
mock.groups_map = groups_map;
|
|
|
|
|
|
|
|
mock.cur_names = Object.keys(mock.current_contacts_map);
|
2013-11-02 09:56:20 +01:00
|
|
|
mock.num_contacts = mock.req_names.length + mock.pend_names.length + mock.cur_names.length;
|
|
|
|
|
2014-08-18 20:37:38 +02:00
|
|
|
mock.groups = {
|
|
|
|
'colleagues': 3,
|
|
|
|
'friends & acquaintences': 3,
|
|
|
|
'Family': 4,
|
|
|
|
'ænemies': 3,
|
|
|
|
'Ungrouped': 2
|
|
|
|
};
|
|
|
|
|
2013-11-02 09:56:20 +01:00
|
|
|
mock.chatroom_names = [
|
2019-07-03 14:54:50 +02:00
|
|
|
'Dyon van de Wege',
|
|
|
|
'Thomas Kalb',
|
|
|
|
'Dirk Theissen',
|
|
|
|
'Felix Hofmann',
|
|
|
|
'Ka Lek',
|
|
|
|
'Anne Ebersbacher'
|
2013-11-02 09:56:20 +01:00
|
|
|
];
|
2015-03-01 11:58:07 +01:00
|
|
|
// TODO: need to also test other roles and affiliations
|
|
|
|
mock.chatroom_roles = {
|
|
|
|
'Anne Ebersbacher': { affiliation: "owner", role: "moderator" },
|
|
|
|
'Dirk Theissen': { affiliation: "admin", role: "moderator" },
|
2015-10-31 17:30:06 +01:00
|
|
|
'Dyon van de Wege': { affiliation: "member", role: "occupant" },
|
|
|
|
'Felix Hofmann': { affiliation: "member", role: "occupant" },
|
|
|
|
'Ka Lek': { affiliation: "member", role: "occupant" },
|
|
|
|
'Thomas Kalb': { affiliation: "member", role: "occupant" }
|
2015-03-01 11:58:07 +01:00
|
|
|
};
|
2013-11-02 09:56:20 +01:00
|
|
|
|
2013-11-03 10:36:31 +01:00
|
|
|
mock.event = {
|
|
|
|
'preventDefault': function () {}
|
|
|
|
};
|
|
|
|
|
2017-07-21 17:38:08 +02:00
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
const OriginalConnection = Strophe.Connection;
|
|
|
|
|
|
|
|
function MockConnection (service, options) {
|
|
|
|
OriginalConnection.call(this, service, options);
|
|
|
|
|
|
|
|
Strophe.Bosh.prototype._processRequest = function () {}; // Don't attempt to send out stanzas
|
|
|
|
const sendIQ = this.sendIQ;
|
|
|
|
|
|
|
|
this.IQ_stanzas = [];
|
|
|
|
this.IQ_ids = [];
|
|
|
|
this.sendIQ = function (iq, callback, errback) {
|
|
|
|
if (!_.isElement(iq)) {
|
|
|
|
iq = iq.nodeTree;
|
2017-07-21 17:38:08 +02:00
|
|
|
}
|
2018-11-18 19:14:22 +01:00
|
|
|
this.IQ_stanzas.push(iq);
|
|
|
|
const id = sendIQ.bind(this)(iq, callback, errback);
|
|
|
|
this.IQ_ids.push(id);
|
|
|
|
return id;
|
|
|
|
}
|
2018-11-14 14:33:07 +01:00
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
const send = this.send;
|
|
|
|
this.sent_stanzas = [];
|
|
|
|
this.send = function (stanza) {
|
|
|
|
if (_.isElement(stanza)) {
|
|
|
|
this.sent_stanzas.push(stanza);
|
|
|
|
} else {
|
|
|
|
this.sent_stanzas.push(stanza.nodeTree);
|
2018-11-14 14:33:07 +01:00
|
|
|
}
|
2018-11-18 19:14:22 +01:00
|
|
|
return send.apply(this, arguments);
|
|
|
|
}
|
2018-11-14 14:33:07 +01:00
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
this.features = Strophe.xmlHtmlNode(
|
|
|
|
'<stream:features xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client">'+
|
|
|
|
'<ver xmlns="urn:xmpp:features:rosterver"/>'+
|
|
|
|
'<csi xmlns="urn:xmpp:csi:0"/>'+
|
|
|
|
'<this xmlns="http://jabber.org/protocol/caps" ver="UwBpfJpEt3IoLYfWma/o/p3FFRo=" hash="sha-1" node="http://prosody.im"/>'+
|
|
|
|
'<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">'+
|
|
|
|
'<required/>'+
|
|
|
|
'</bind>'+
|
|
|
|
`<sm xmlns='urn:xmpp:sm:3'/>`+
|
|
|
|
'<session xmlns="urn:ietf:params:xml:ns:xmpp-session">'+
|
|
|
|
'<optional/>'+
|
|
|
|
'</session>'+
|
|
|
|
'</stream:features>').firstChild;
|
2017-07-21 17:38:08 +02:00
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
this._proto._connect = () => {
|
|
|
|
this.connected = true;
|
|
|
|
this.mock = true;
|
|
|
|
this.jid = 'romeo@montague.lit/orchard';
|
|
|
|
this._changeConnectStatus(Strophe.Status.BINDREQUIRED);
|
|
|
|
};
|
2018-05-29 12:00:23 +02:00
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
this.bind = () => {
|
|
|
|
this.authenticated = true;
|
|
|
|
this.authenticated = true;
|
|
|
|
this._changeConnectStatus(Strophe.Status.CONNECTED);
|
|
|
|
};
|
2019-04-22 14:04:21 +02:00
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
this._proto._disconnect = () => this._onDisconnectTimeout();
|
|
|
|
this._proto._onDisconnectTimeout = _.noop;
|
|
|
|
}
|
2019-04-22 14:04:21 +02:00
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
MockConnection.prototype = Object.create(OriginalConnection.prototype);
|
|
|
|
Strophe.Connection = MockConnection;
|
2016-11-02 21:19:17 +01:00
|
|
|
|
2018-11-18 19:14:22 +01:00
|
|
|
|
2019-10-11 16:38:01 +02:00
|
|
|
async function initConverse (settings) {
|
2017-04-05 11:01:31 +02:00
|
|
|
window.localStorage.clear();
|
|
|
|
window.sessionStorage.clear();
|
2017-05-08 20:44:35 +02:00
|
|
|
|
2018-05-29 12:00:23 +02:00
|
|
|
const _converse = await converse.initialize(Object.assign({
|
2018-11-18 19:14:22 +01:00
|
|
|
'animate': false,
|
2017-05-08 20:44:35 +02:00
|
|
|
'auto_subscribe': false,
|
2019-06-03 07:58:51 +02:00
|
|
|
'bosh_service_url': 'montague.lit/http-bind',
|
2019-11-22 14:23:19 +01:00
|
|
|
'loglevel': 'warn',
|
2018-11-18 19:14:22 +01:00
|
|
|
'i18n': 'en',
|
2017-05-08 20:44:35 +02:00
|
|
|
'no_trimming': true,
|
2018-11-18 19:14:22 +01:00
|
|
|
'play_sounds': false,
|
|
|
|
'use_emojione': false,
|
2019-10-24 14:29:15 +02:00
|
|
|
'view_mode': mock.view_mode
|
2017-04-05 11:01:31 +02:00
|
|
|
}, settings || {}));
|
2018-02-07 17:30:44 +01:00
|
|
|
|
2018-05-08 19:24:50 +02:00
|
|
|
_converse.ChatBoxViews.prototype.trimChat = function () {};
|
2018-05-25 16:25:02 +02:00
|
|
|
|
2018-05-08 14:46:00 +02:00
|
|
|
_converse.api.vcard.get = function (model, force) {
|
2019-10-24 14:29:15 +02:00
|
|
|
let jid;
|
|
|
|
if (_.isString(model)) {
|
|
|
|
jid = model;
|
|
|
|
} else if (!model.get('vcard_updated') || force) {
|
|
|
|
jid = model.get('jid') || model.get('muc_jid');
|
|
|
|
}
|
|
|
|
let fullname;
|
|
|
|
if (!jid || jid == 'romeo@montague.lit') {
|
|
|
|
jid = 'romeo@montague.lit';
|
|
|
|
fullname = 'Romeo Montague' ;
|
|
|
|
} else {
|
|
|
|
const name = jid.split('@')[0].replace(/\./g, ' ').split(' ');
|
|
|
|
const last = name.length-1;
|
|
|
|
name[0] = name[0].charAt(0).toUpperCase()+name[0].slice(1);
|
|
|
|
name[last] = name[last].charAt(0).toUpperCase()+name[last].slice(1);
|
|
|
|
fullname = name.join(' ');
|
|
|
|
}
|
|
|
|
const vcard = $iq().c('vCard').c('FN').t(fullname).nodeTree;
|
|
|
|
return {
|
|
|
|
'vcard': vcard,
|
|
|
|
'fullname': _.get(vcard.querySelector('FN'), 'textContent'),
|
|
|
|
'image': _.get(vcard.querySelector('PHOTO BINVAL'), 'textContent'),
|
|
|
|
'image_type': _.get(vcard.querySelector('PHOTO TYPE'), 'textContent'),
|
|
|
|
'url': _.get(vcard.querySelector('URL'), 'textContent'),
|
|
|
|
'vcard_updated': dayjs().format(),
|
|
|
|
'vcard_error': undefined
|
|
|
|
};
|
2018-05-08 14:46:00 +02:00
|
|
|
};
|
2018-05-08 19:24:50 +02:00
|
|
|
if (_.get(settings, 'auto_login') !== false) {
|
2019-06-25 22:53:39 +02:00
|
|
|
_converse.api.user.login('romeo@montague.lit/orchard', 'secret');
|
2018-05-29 12:00:23 +02:00
|
|
|
await _converse.api.waitUntil('afterResourceBinding');
|
2018-05-08 19:24:50 +02:00
|
|
|
}
|
2017-08-15 21:23:30 +02:00
|
|
|
window.converse_disable_effects = true;
|
2017-07-15 15:15:37 +02:00
|
|
|
return _converse;
|
2017-04-05 11:01:31 +02:00
|
|
|
}
|
2017-05-08 20:44:35 +02:00
|
|
|
|
2019-10-11 16:38:01 +02:00
|
|
|
mock.initConverse = function (promise_names=[], settings=null, func) {
|
|
|
|
if (_.isFunction(promise_names)) {
|
|
|
|
func = promise_names;
|
2019-09-12 18:50:30 +02:00
|
|
|
promise_names = []
|
2019-02-12 14:21:45 +01:00
|
|
|
settings = null;
|
2017-07-11 10:41:11 +02:00
|
|
|
}
|
2019-03-29 21:36:49 +01:00
|
|
|
return async done => {
|
2019-10-11 16:38:01 +02:00
|
|
|
const _converse = await initConverse(settings);
|
2019-10-11 12:06:13 +02:00
|
|
|
async function _done () {
|
2018-11-18 19:14:22 +01:00
|
|
|
if (_converse.api.connection.connected()) {
|
|
|
|
await _converse.api.user.logout();
|
|
|
|
}
|
2019-10-11 12:06:13 +02:00
|
|
|
const el = document.querySelector('#conversejs');
|
2018-11-18 19:14:22 +01:00
|
|
|
if (el) {
|
|
|
|
el.parentElement.removeChild(el);
|
|
|
|
}
|
2019-10-15 12:55:11 +02:00
|
|
|
document.title = "Converse Tests";
|
2019-04-22 14:04:21 +02:00
|
|
|
done();
|
|
|
|
}
|
2019-09-12 19:42:53 +02:00
|
|
|
await Promise.all((promise_names || []).map(_converse.api.waitUntil));
|
2019-10-11 16:38:01 +02:00
|
|
|
try {
|
|
|
|
await func(_done, _converse);
|
|
|
|
} catch(e) {
|
2019-10-15 14:24:01 +02:00
|
|
|
console.error(e);
|
2019-10-11 16:38:01 +02:00
|
|
|
fail(e);
|
|
|
|
_done();
|
|
|
|
}
|
2017-05-16 11:56:33 +02:00
|
|
|
}
|
2016-11-02 21:19:17 +01:00
|
|
|
};
|
2013-11-02 09:56:20 +01:00
|
|
|
return mock;
|
2013-05-11 14:20:07 +02:00
|
|
|
}));
|