2014-02-12 11:19:12 +01:00
|
|
|
(function (root, factory) {
|
|
|
|
define([
|
2014-10-15 19:16:02 +02:00
|
|
|
"jquery",
|
2015-10-25 18:49:35 +01:00
|
|
|
"underscore",
|
2014-02-12 11:19:12 +01:00
|
|
|
"mock",
|
2014-08-08 22:06:01 +02:00
|
|
|
"test_utils"
|
2015-10-25 18:49:35 +01:00
|
|
|
], function ($, _, mock, test_utils) {
|
|
|
|
return factory($, _, mock, test_utils);
|
2014-02-12 11:19:12 +01:00
|
|
|
}
|
|
|
|
);
|
2015-10-25 18:49:35 +01:00
|
|
|
} (this, function ($, _, mock, test_utils) {
|
2015-02-01 16:15:34 +01:00
|
|
|
var b64_sha1 = converse_api.env.b64_sha1;
|
2014-02-12 11:19:12 +01:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
describe("Converse", function() {
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
converse_api.user.logout();
|
2016-11-03 11:01:09 +01:00
|
|
|
converse_api.listen.not();
|
2016-11-02 21:19:17 +01:00
|
|
|
test_utils.clearBrowserStorage();
|
|
|
|
});
|
2015-03-31 16:06:33 +02:00
|
|
|
|
|
|
|
describe("Authentication", function () {
|
2016-11-02 21:19:17 +01:00
|
|
|
it("needs either a bosh_service_url a websocket_url or both", mock.initConverse(function (converse) {
|
2016-04-13 17:11:04 +02:00
|
|
|
var url = converse.bosh_service_url;
|
|
|
|
var connection = converse.connection;
|
|
|
|
delete converse.bosh_service_url;
|
|
|
|
delete converse.connection;
|
2016-06-09 11:01:54 +02:00
|
|
|
expect(converse.initConnection.bind(converse)).toThrow(
|
2015-03-31 16:06:33 +02:00
|
|
|
new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both."));
|
2016-04-13 17:11:04 +02:00
|
|
|
converse.bosh_service_url = url;
|
|
|
|
converse.connection = connection;
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2015-03-31 16:06:33 +02:00
|
|
|
|
|
|
|
describe("with prebind", function () {
|
2016-11-02 21:19:17 +01:00
|
|
|
it("needs a jid when also using keepalive", mock.initConverse(function (converse) {
|
2015-04-02 13:07:59 +02:00
|
|
|
var authentication = converse.authentication;
|
2015-03-31 16:06:33 +02:00
|
|
|
var jid = converse.jid;
|
2016-04-13 17:11:04 +02:00
|
|
|
delete converse.jid;
|
2015-03-31 16:06:33 +02:00
|
|
|
converse.keepalive = true;
|
2015-04-02 13:07:59 +02:00
|
|
|
converse.authentication = "prebind";
|
2016-04-13 17:11:04 +02:00
|
|
|
expect(converse.logIn.bind(converse)).toThrow(
|
|
|
|
new Error("attemptPreboundSession: when using 'keepalive' with 'prebind, you must supply the JID of the current user."));
|
2015-04-02 13:07:59 +02:00
|
|
|
converse.authentication= authentication;
|
2015-03-31 16:06:33 +02:00
|
|
|
converse.jid = jid;
|
2016-04-13 17:11:04 +02:00
|
|
|
converse.keepalive = false;
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2015-03-31 16:06:33 +02:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("needs jid, rid and sid values when not using keepalive", mock.initConverse(function (converse) {
|
2015-04-02 13:07:59 +02:00
|
|
|
var authentication = converse.authentication;
|
2015-03-31 16:06:33 +02:00
|
|
|
var jid = converse.jid;
|
2016-04-13 17:11:04 +02:00
|
|
|
delete converse.jid;
|
2015-04-02 13:07:59 +02:00
|
|
|
converse.authentication = "prebind";
|
2016-04-13 17:11:04 +02:00
|
|
|
expect(converse.logIn.bind(converse)).toThrow(
|
|
|
|
new Error("attemptPreboundSession: If you use prebind and not keepalive, then you MUST supply JID, RID and SID values"));
|
2015-04-02 13:07:59 +02:00
|
|
|
converse.authentication= authentication;
|
2015-03-31 16:06:33 +02:00
|
|
|
converse.bosh_service_url = undefined;
|
|
|
|
converse.jid = jid;
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2015-03-31 16:06:33 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-06-22 00:01:31 +02:00
|
|
|
describe("A chat state indication", function () {
|
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("are sent out when the client becomes or stops being idle", mock.initConverse(function (converse) {
|
2015-06-22 00:01:31 +02:00
|
|
|
spyOn(converse, 'sendCSI').andCallThrough();
|
|
|
|
var sent_stanza;
|
|
|
|
spyOn(converse.connection, 'send').andCallFake(function (stanza) {
|
|
|
|
sent_stanza = stanza;
|
|
|
|
});
|
|
|
|
var i = 0;
|
|
|
|
converse.idle_seconds = 0; // Usually initialized by registerIntervalHandler
|
|
|
|
converse.features['urn:xmpp:csi:0'] = true; // Mock that the server supports CSI
|
|
|
|
|
|
|
|
converse.csi_waiting_time = 3; // The relevant config option
|
|
|
|
while (i <= converse.csi_waiting_time) {
|
|
|
|
expect(converse.sendCSI).not.toHaveBeenCalled();
|
|
|
|
converse.onEverySecond();
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
expect(converse.sendCSI).toHaveBeenCalledWith('inactive');
|
|
|
|
expect(sent_stanza.toLocaleString()).toBe(
|
|
|
|
"<inactive xmlns='urn:xmpp:csi:0'/>"
|
|
|
|
);
|
|
|
|
converse.onUserActivity();
|
|
|
|
expect(converse.sendCSI).toHaveBeenCalledWith('active');
|
|
|
|
expect(sent_stanza.toLocaleString()).toBe(
|
|
|
|
"<active xmlns='urn:xmpp:csi:0'/>"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Reset values
|
|
|
|
converse.csi_waiting_time = 0;
|
|
|
|
converse.features['urn:xmpp:csi:0'] = false;
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2015-06-22 00:01:31 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("Automatic status change", function () {
|
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("happens when the client is idle for long enough", mock.initConverse(function (converse) {
|
2015-06-22 00:01:31 +02:00
|
|
|
var i = 0;
|
|
|
|
// Usually initialized by registerIntervalHandler
|
2016-02-15 15:55:34 +01:00
|
|
|
converse.idle_seconds = 0;
|
2015-06-22 00:01:31 +02:00
|
|
|
converse.auto_changed_status = false;
|
|
|
|
|
|
|
|
// The relevant config options
|
|
|
|
converse.auto_away = 3;
|
|
|
|
converse.auto_xa = 6;
|
|
|
|
|
|
|
|
expect(converse.xmppstatus.getStatus()).toBe('online');
|
|
|
|
|
|
|
|
while (i <= converse.auto_away) {
|
|
|
|
converse.onEverySecond();
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
expect(converse.auto_changed_status).toBe(true);
|
|
|
|
|
|
|
|
while (i <= converse.auto_xa) {
|
|
|
|
expect(converse.xmppstatus.getStatus()).toBe('away');
|
|
|
|
converse.onEverySecond();
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
expect(converse.xmppstatus.getStatus()).toBe('xa');
|
|
|
|
expect(converse.auto_changed_status).toBe(true);
|
|
|
|
|
|
|
|
converse.onUserActivity();
|
|
|
|
expect(converse.xmppstatus.getStatus()).toBe('online');
|
|
|
|
expect(converse.auto_changed_status).toBe(false);
|
|
|
|
|
|
|
|
// Reset values
|
|
|
|
converse.auto_away = 0;
|
|
|
|
converse.auto_xa = 0;
|
|
|
|
converse.auto_changed_status = false;
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2015-06-22 00:01:31 +02:00
|
|
|
});
|
|
|
|
|
2015-06-27 21:21:27 +02:00
|
|
|
describe("The \"user\" grouping", function () {
|
|
|
|
|
|
|
|
describe("The \"status\" API", function () {
|
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("has a method for getting the user's availability", mock.initConverse(function (converse) {
|
2015-06-27 21:21:27 +02:00
|
|
|
converse.xmppstatus.set('status', 'online');
|
|
|
|
expect(converse_api.user.status.get()).toBe('online');
|
|
|
|
converse.xmppstatus.set('status', 'dnd');
|
|
|
|
expect(converse_api.user.status.get()).toBe('dnd');
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2015-06-27 21:21:27 +02:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("has a method for setting the user's availability", mock.initConverse(function (converse) {
|
2015-06-27 21:21:27 +02:00
|
|
|
converse_api.user.status.set('away');
|
|
|
|
expect(converse.xmppstatus.get('status')).toBe('away');
|
|
|
|
converse_api.user.status.set('dnd');
|
|
|
|
expect(converse.xmppstatus.get('status')).toBe('dnd');
|
|
|
|
converse_api.user.status.set('xa');
|
|
|
|
expect(converse.xmppstatus.get('status')).toBe('xa');
|
|
|
|
converse_api.user.status.set('chat');
|
|
|
|
expect(converse.xmppstatus.get('status')).toBe('chat');
|
|
|
|
expect(_.partial(converse_api.user.status.set, 'invalid')).toThrow(
|
|
|
|
new Error('Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1')
|
|
|
|
);
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2015-06-27 21:21:27 +02:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("allows setting the status message as well", mock.initConverse(function (converse) {
|
2015-06-27 21:21:27 +02:00
|
|
|
converse_api.user.status.set('away', "I'm in a meeting");
|
|
|
|
expect(converse.xmppstatus.get('status')).toBe('away');
|
|
|
|
expect(converse.xmppstatus.get('status_message')).toBe("I'm in a meeting");
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2015-06-27 21:21:27 +02:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("has a method for getting the user's status message", mock.initConverse(function (converse) {
|
2015-06-27 21:21:27 +02:00
|
|
|
converse.xmppstatus.set('status_message', undefined);
|
|
|
|
expect(converse_api.user.status.message.get()).toBe(undefined);
|
|
|
|
converse.xmppstatus.set('status_message', "I'm in a meeting");
|
|
|
|
expect(converse_api.user.status.message.get()).toBe("I'm in a meeting");
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2015-06-27 21:21:27 +02:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("has a method for setting the user's status message", mock.initConverse(function (converse) {
|
2015-06-27 21:21:27 +02:00
|
|
|
converse.xmppstatus.set('status_message', undefined);
|
|
|
|
converse_api.user.status.message.set("I'm in a meeting");
|
|
|
|
expect(converse.xmppstatus.get('status_message')).toBe("I'm in a meeting");
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2015-06-27 21:21:27 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
describe("The \"tokens\" API", function () {
|
2014-11-09 13:03:56 +01:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("has a method for retrieving the next RID", mock.initConverse(function (converse) {
|
|
|
|
test_utils.createContacts(converse, 'current');
|
2014-11-09 13:03:56 +01:00
|
|
|
var old_connection = converse.connection;
|
|
|
|
converse.connection._proto.rid = '1234';
|
|
|
|
converse.expose_rid_and_sid = false;
|
|
|
|
expect(converse_api.tokens.get('rid')).toBe(null);
|
|
|
|
converse.expose_rid_and_sid = true;
|
|
|
|
expect(converse_api.tokens.get('rid')).toBe('1234');
|
|
|
|
converse.connection = undefined;
|
|
|
|
expect(converse_api.tokens.get('rid')).toBe(null);
|
|
|
|
// Restore the connection
|
|
|
|
converse.connection = old_connection;
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2014-11-09 13:03:56 +01:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("has a method for retrieving the SID", mock.initConverse(function (converse) {
|
|
|
|
test_utils.createContacts(converse, 'current');
|
2014-11-09 13:03:56 +01:00
|
|
|
var old_connection = converse.connection;
|
|
|
|
converse.connection._proto.sid = '1234';
|
|
|
|
converse.expose_rid_and_sid = false;
|
|
|
|
expect(converse_api.tokens.get('sid')).toBe(null);
|
|
|
|
converse.expose_rid_and_sid = true;
|
|
|
|
expect(converse_api.tokens.get('sid')).toBe('1234');
|
|
|
|
converse.connection = undefined;
|
|
|
|
expect(converse_api.tokens.get('sid')).toBe(null);
|
|
|
|
// Restore the connection
|
|
|
|
converse.connection = old_connection;
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("The \"contacts\" API", function () {
|
|
|
|
|
|
|
|
it("has a method 'get' which returns wrapped contacts", mock.initConverse(function (converse) {
|
2015-02-25 18:31:09 +01:00
|
|
|
// Check that it returns nothing if a non-existing JID is given
|
2016-11-02 21:19:17 +01:00
|
|
|
test_utils.createContacts(converse, 'current');
|
2014-11-09 13:03:56 +01:00
|
|
|
expect(converse_api.contacts.get('non-existing@jabber.org')).toBeFalsy();
|
2015-02-25 18:31:09 +01:00
|
|
|
// Check when a single jid is given
|
|
|
|
var jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
|
2014-11-09 13:03:56 +01:00
|
|
|
var attrs = converse_api.contacts.get(jid);
|
|
|
|
expect(typeof attrs).toBe('object');
|
|
|
|
expect(attrs.fullname).toBe(mock.cur_names[0]);
|
|
|
|
expect(attrs.jid).toBe(jid);
|
2015-02-25 18:31:09 +01:00
|
|
|
// You can retrieve multiple contacts by passing in an array
|
|
|
|
var jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
|
|
|
|
var list = converse_api.contacts.get([jid, jid2]);
|
|
|
|
expect(Array.isArray(list)).toBeTruthy();
|
|
|
|
expect(list[0].fullname).toBe(mock.cur_names[0]);
|
|
|
|
expect(list[1].fullname).toBe(mock.cur_names[1]);
|
|
|
|
// Check that all JIDs are returned if you call without any parameters
|
|
|
|
list = converse_api.contacts.get();
|
|
|
|
expect(list.length).toBe(mock.cur_names.length);
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2015-03-31 15:26:57 +02:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("has a method 'add' with which contacts can be added", mock.initConverse(function (converse) {
|
|
|
|
test_utils.createContacts(converse, 'current');
|
2015-03-31 15:26:57 +02:00
|
|
|
var error = new TypeError('contacts.add: invalid jid');
|
|
|
|
expect(converse_api.contacts.add).toThrow(error);
|
|
|
|
expect(converse_api.contacts.add.bind(converse_api, "invalid jid")).toThrow(error);
|
2015-04-06 11:10:05 +02:00
|
|
|
spyOn(converse.roster, 'addAndSubscribe');
|
2015-03-31 15:26:57 +02:00
|
|
|
converse_api.contacts.add("newcontact@example.org");
|
2015-04-06 11:10:05 +02:00
|
|
|
expect(converse.roster.addAndSubscribe).toHaveBeenCalled();
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
|
|
|
});
|
2014-02-12 11:37:39 +01:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
describe("The \"chats\" API", function() {
|
2014-11-09 13:03:56 +01:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("has a method 'get' which returns a wrapped chat box", mock.initConverse(function (converse) {
|
|
|
|
test_utils.createContacts(converse, 'current');
|
2015-02-25 18:31:09 +01:00
|
|
|
// Test on chat that doesn't exist.
|
|
|
|
expect(converse_api.chats.get('non-existing@jabber.org')).toBeFalsy();
|
2016-02-15 15:55:34 +01:00
|
|
|
var jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
|
2015-02-25 18:31:09 +01:00
|
|
|
|
|
|
|
// Test on chat that's not open
|
2014-11-09 13:03:56 +01:00
|
|
|
var box = converse_api.chats.get(jid);
|
2016-08-12 22:38:39 +02:00
|
|
|
expect(typeof box === 'undefined').toBeTruthy();
|
2015-02-25 18:31:09 +01:00
|
|
|
|
2016-08-12 22:38:39 +02:00
|
|
|
var chatboxview = converse.chatboxviews.get(jid);
|
2015-02-25 18:31:09 +01:00
|
|
|
// Test for single JID
|
2016-11-02 21:19:17 +01:00
|
|
|
test_utils.openChatBoxFor(converse, jid);
|
2015-02-25 18:31:09 +01:00
|
|
|
box = converse_api.chats.get(jid);
|
|
|
|
expect(box instanceof Object).toBeTruthy();
|
|
|
|
expect(box.get('box_id')).toBe(b64_sha1(jid));
|
2016-02-15 15:55:34 +01:00
|
|
|
chatboxview = converse.chatboxviews.get(jid);
|
2015-02-25 18:31:09 +01:00
|
|
|
expect(chatboxview.$el.is(':visible')).toBeTruthy();
|
|
|
|
// Test for multiple JIDs
|
|
|
|
var jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
|
2016-11-02 21:19:17 +01:00
|
|
|
test_utils.openChatBoxFor(converse, jid2);
|
2015-02-25 18:31:09 +01:00
|
|
|
var list = converse_api.chats.get([jid, jid2]);
|
|
|
|
expect(Array.isArray(list)).toBeTruthy();
|
|
|
|
expect(list[0].get('box_id')).toBe(b64_sha1(jid));
|
|
|
|
expect(list[1].get('box_id')).toBe(b64_sha1(jid2));
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
2015-02-25 18:31:09 +01:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
it("has a method 'open' which opens and returns a wrapped chat box", mock.initConverse(function (converse) {
|
|
|
|
test_utils.createContacts(converse, 'current');
|
2015-02-25 18:31:09 +01:00
|
|
|
var jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
|
2016-02-15 15:55:34 +01:00
|
|
|
var chatboxview;
|
|
|
|
waits('300'); // ChatBox.show() is debounced for 250ms
|
|
|
|
runs(function () {
|
|
|
|
// Test on chat that doesn't exist.
|
|
|
|
expect(converse_api.chats.get('non-existing@jabber.org')).toBeFalsy();
|
|
|
|
var box = converse_api.chats.open(jid);
|
|
|
|
expect(box instanceof Object).toBeTruthy();
|
|
|
|
expect(box.get('box_id')).toBe(b64_sha1(jid));
|
|
|
|
expect(
|
|
|
|
Object.keys(box),
|
|
|
|
['close', 'endOTR', 'focus', 'get', 'initiateOTR', 'is_chatroom', 'maximize', 'minimize', 'open', 'set']
|
|
|
|
);
|
|
|
|
chatboxview = converse.chatboxviews.get(jid);
|
|
|
|
expect(chatboxview.$el.is(':visible')).toBeTruthy();
|
|
|
|
// Test for multiple JIDs
|
|
|
|
var jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
|
|
|
|
var list = converse_api.chats.open([jid, jid2]);
|
|
|
|
expect(Array.isArray(list)).toBeTruthy();
|
|
|
|
expect(list[0].get('box_id')).toBe(b64_sha1(jid));
|
|
|
|
expect(list[1].get('box_id')).toBe(b64_sha1(jid2));
|
|
|
|
});
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
|
|
|
});
|
2015-02-11 15:02:04 +01:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
describe("The \"settings\" API", function() {
|
|
|
|
it("has methods 'get' and 'set' to set configuration settings", mock.initConverse(function (converse) {
|
2015-02-11 15:02:04 +01:00
|
|
|
expect(Object.keys(converse_api.settings)).toEqual(["get", "set"]);
|
|
|
|
expect(converse_api.settings.get("play_sounds")).toBe(false);
|
|
|
|
converse_api.settings.set("play_sounds", true);
|
|
|
|
expect(converse_api.settings.get("play_sounds")).toBe(true);
|
|
|
|
converse_api.settings.set({"play_sounds": false});
|
|
|
|
expect(converse_api.settings.get("play_sounds")).toBe(false);
|
|
|
|
// Only whitelisted settings allowed.
|
|
|
|
expect(typeof converse_api.settings.get("non_existing")).toBe("undefined");
|
|
|
|
converse_api.settings.set("non_existing", true);
|
|
|
|
expect(typeof converse_api.settings.get("non_existing")).toBe("undefined");
|
2016-11-02 21:19:17 +01:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
2014-02-12 11:19:12 +01:00
|
|
|
}));
|