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-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;
|
|
|
|
const u = converse.env.utils;
|
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
|
|
|
},
|
|
|
|
'SessionBuilder': function (storage, remote_address) {
|
|
|
|
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 = {};
|
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-06-03 07:58:51 +02:00
|
|
|
'Escalus, Prince of Verona', 'The Nurse', 'Paris'
|
2013-11-02 09:56:20 +01:00
|
|
|
];
|
|
|
|
mock.pend_names = [
|
2019-06-03 07:58:51 +02:00
|
|
|
'Lord Capulet', 'Lady Capulet', 'Servant'
|
2013-11-02 09:56:20 +01:00
|
|
|
];
|
|
|
|
mock.cur_names = [
|
2019-06-03 07:58:51 +02:00
|
|
|
'Mercutio', 'Juliet Capulet', 'Lady Montague', 'Lord Montague', 'Friar Laurence',
|
|
|
|
'Tybalt', 'Lady Capulet', 'Benviolo', 'Balthasar',
|
|
|
|
'Peter', 'Abram', 'Sampson', 'Gregory', 'Potpan', 'Friar John'
|
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 = [
|
|
|
|
'Dyon van de Wege', 'Thomas Kalb', 'Dirk Theissen', 'Felix Hofmann', 'Ka Lek', 'Anne Ebersbacher'
|
|
|
|
];
|
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-04-05 11:01:31 +02:00
|
|
|
mock.mock_connection = function () { // eslint-disable-line wrap-iife
|
2016-11-02 21:19:17 +01:00
|
|
|
return function () {
|
|
|
|
Strophe.Bosh.prototype._processRequest = function () {}; // Don't attempt to send out stanzas
|
2018-11-14 14:33:07 +01:00
|
|
|
const c = new Strophe.Connection('jasmine tests');
|
|
|
|
const sendIQ = c.sendIQ;
|
2017-07-21 17:38:08 +02:00
|
|
|
|
|
|
|
c.IQ_stanzas = [];
|
|
|
|
c.IQ_ids = [];
|
|
|
|
c.sendIQ = function (iq, callback, errback) {
|
2019-05-20 12:38:33 +02:00
|
|
|
if (!_.isElement(iq)) {
|
|
|
|
iq = iq.nodeTree;
|
|
|
|
}
|
2017-07-21 17:38:08 +02:00
|
|
|
this.IQ_stanzas.push(iq);
|
2018-11-14 14:33:07 +01:00
|
|
|
const id = sendIQ.bind(this)(iq, callback, errback);
|
2017-07-21 17:38:08 +02:00
|
|
|
this.IQ_ids.push(id);
|
|
|
|
return id;
|
|
|
|
}
|
2018-11-14 14:33:07 +01:00
|
|
|
|
|
|
|
const send = c.send;
|
|
|
|
c.sent_stanzas = [];
|
|
|
|
c.send = function (stanza) {
|
|
|
|
if (_.isElement(stanza)) {
|
|
|
|
this.sent_stanzas.push(stanza);
|
|
|
|
} else {
|
|
|
|
this.sent_stanzas.push(stanza.nodeTree);
|
|
|
|
}
|
|
|
|
return send.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
2018-05-28 11:41:02 +02:00
|
|
|
c.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"/>'+
|
|
|
|
'<c 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>'+
|
2018-05-29 12:00:23 +02:00
|
|
|
`<sm xmlns='urn:xmpp:sm:3'/>`+
|
2018-05-28 11:41:02 +02:00
|
|
|
'<session xmlns="urn:ietf:params:xml:ns:xmpp-session">'+
|
|
|
|
'<optional/>'+
|
|
|
|
'</session>'+
|
|
|
|
'</stream:features>').firstChild;
|
2017-07-21 17:38:08 +02:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
c._proto._connect = function () {
|
|
|
|
c.connected = true;
|
|
|
|
c.mock = true;
|
2019-06-03 07:58:51 +02:00
|
|
|
c.jid = 'romeo@montague.lit/orchard';
|
2018-05-29 12:00:23 +02:00
|
|
|
c._changeConnectStatus(Strophe.Status.BINDREQUIRED);
|
|
|
|
};
|
|
|
|
|
|
|
|
c.bind = function () {
|
|
|
|
c.authenticated = true;
|
|
|
|
this.authenticated = true;
|
2016-11-02 21:19:17 +01:00
|
|
|
c._changeConnectStatus(Strophe.Status.CONNECTED);
|
|
|
|
};
|
2019-04-22 14:04:21 +02:00
|
|
|
|
|
|
|
c._proto._disconnect = function () {
|
|
|
|
c._onDisconnectTimeout();
|
|
|
|
}
|
|
|
|
|
|
|
|
c._proto._onDisconnectTimeout = _.noop;
|
2016-11-02 21:19:17 +01:00
|
|
|
return c;
|
2014-10-24 18:58:42 +02:00
|
|
|
};
|
|
|
|
}();
|
2016-11-02 21:19:17 +01:00
|
|
|
|
2019-02-12 14:21:45 +01:00
|
|
|
async function initConverse (settings, spies, promises) {
|
2017-04-05 11:01:31 +02:00
|
|
|
window.localStorage.clear();
|
|
|
|
window.sessionStorage.clear();
|
2018-10-28 22:58:08 +01:00
|
|
|
const el = document.querySelector('#conversejs');
|
|
|
|
if (el) {
|
|
|
|
el.parentElement.removeChild(el);
|
|
|
|
}
|
2017-05-08 20:44:35 +02:00
|
|
|
|
2018-10-28 22:58:08 +01:00
|
|
|
const connection = mock.mock_connection();
|
2019-02-13 11:08:58 +01:00
|
|
|
if (!_.isNil(spies)) {
|
|
|
|
_.forEach(spies.connection, method => spyOn(connection, method));
|
2017-05-08 20:44:35 +02:00
|
|
|
}
|
|
|
|
|
2018-05-29 12:00:23 +02:00
|
|
|
const _converse = await converse.initialize(Object.assign({
|
2017-05-08 20:44:35 +02:00
|
|
|
'i18n': 'en',
|
|
|
|
'auto_subscribe': false,
|
2017-06-14 20:14:45 +02:00
|
|
|
'play_sounds': false,
|
2019-06-03 07:58:51 +02:00
|
|
|
'bosh_service_url': 'montague.lit/http-bind',
|
2017-05-08 20:44:35 +02:00
|
|
|
'connection': connection,
|
|
|
|
'animate': false,
|
2017-12-20 20:29:57 +01:00
|
|
|
'use_emojione': false,
|
2017-05-08 20:44:35 +02:00
|
|
|
'no_trimming': true,
|
2018-02-07 13:26:39 +01:00
|
|
|
'view_mode': mock.view_mode,
|
2017-05-08 20:44:35 +02:00
|
|
|
'debug': false
|
2017-04-05 11:01:31 +02:00
|
|
|
}, settings || {}));
|
2018-02-07 17:30:44 +01:00
|
|
|
|
2019-02-13 11:08:58 +01:00
|
|
|
if (!_.isNil(spies)) {
|
|
|
|
_.forEach(spies._converse, method => spyOn(_converse, method).and.callThrough());
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let jid;
|
|
|
|
if (_.isString(model)) {
|
|
|
|
jid = model;
|
|
|
|
} else if (!model.get('vcard_updated') || force) {
|
|
|
|
jid = model.get('jid') || model.get('muc_jid');
|
|
|
|
}
|
2019-02-12 14:21:45 +01:00
|
|
|
let fullname;
|
2019-06-03 07:58:51 +02:00
|
|
|
if (!jid || jid == 'romeo@montague.lit') {
|
|
|
|
jid = 'romeo@montague.lit';
|
|
|
|
fullname = 'Romeo Montague' ;
|
2018-05-08 14:46:00 +02:00
|
|
|
} else {
|
2019-02-12 14:21:45 +01:00
|
|
|
const name = jid.split('@')[0].replace(/\./g, ' ').split(' ');
|
|
|
|
const last = name.length-1;
|
2018-05-08 14:46:00 +02:00
|
|
|
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(' ');
|
|
|
|
}
|
2019-02-12 14:21:45 +01:00
|
|
|
const vcard = $iq().c('vCard').c('FN').t(fullname).nodeTree;
|
|
|
|
const result = {
|
2018-05-25 16:25:02 +02:00
|
|
|
'vcard': vcard,
|
2018-05-08 14:46:00 +02:00
|
|
|
'fullname': _.get(vcard.querySelector('FN'), 'textContent'),
|
|
|
|
'image': _.get(vcard.querySelector('PHOTO BINVAL'), 'textContent'),
|
|
|
|
'image_type': _.get(vcard.querySelector('PHOTO TYPE'), 'textContent'),
|
2018-05-25 16:25:02 +02:00
|
|
|
'url': _.get(vcard.querySelector('URL'), 'textContent'),
|
2019-05-06 11:16:56 +02:00
|
|
|
'vcard_updated': dayjs().format(),
|
2018-05-25 16:25:02 +02:00
|
|
|
'vcard_error': undefined
|
2018-05-08 14:46:00 +02:00
|
|
|
};
|
|
|
|
resolve(result);
|
|
|
|
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
|
|
|
};
|
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-02-12 14:21:45 +01:00
|
|
|
mock.initConverse = function (spies, promise_names=null, settings=null, func) {
|
|
|
|
if (_.isFunction(spies)) {
|
|
|
|
func = spies;
|
|
|
|
spies = null;
|
|
|
|
promise_names = null
|
|
|
|
settings = null;
|
2017-07-11 10:41:11 +02:00
|
|
|
}
|
2019-03-29 21:36:49 +01:00
|
|
|
return async done => {
|
|
|
|
const _converse = await initConverse(settings, spies);
|
2019-04-22 14:04:21 +02:00
|
|
|
function _done () {
|
|
|
|
_converse.api.user.logout();
|
|
|
|
done();
|
|
|
|
}
|
2019-03-29 21:36:49 +01:00
|
|
|
const promises = _.map(promise_names, _converse.api.waitUntil);
|
|
|
|
await Promise.all(promises);
|
2019-04-22 14:04:21 +02:00
|
|
|
func(_done, _converse);
|
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
|
|
|
}));
|