converse.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. (function (root, factory) {
  2. define([
  3. "jasmine",
  4. "mock",
  5. "test-utils"], factory);
  6. } (this, function (jasmine, mock, test_utils) {
  7. const _ = converse.env._,
  8. u = converse.env.utils;
  9. describe("Converse", function() {
  10. describe("Authentication", function () {
  11. it("needs either a bosh_service_url a websocket_url or both", mock.initConverse(async (done, _converse) => {
  12. const url = _converse.bosh_service_url;
  13. const connection = _converse.connection;
  14. delete _converse.bosh_service_url;
  15. delete _converse.connection;
  16. try {
  17. await _converse.initConnection();
  18. } catch (e) {
  19. _converse.bosh_service_url = url;
  20. _converse.connection = connection;
  21. expect(e.message).toBe("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
  22. done();
  23. }
  24. }));
  25. });
  26. describe("A chat state indication", function () {
  27. it("are sent out when the client becomes or stops being idle",
  28. mock.initConverse(['discoInitialized'], {}, (done, _converse) => {
  29. spyOn(_converse, 'sendCSI').and.callThrough();
  30. let sent_stanza;
  31. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  32. sent_stanza = stanza;
  33. });
  34. let i = 0;
  35. _converse.idle_seconds = 0; // Usually initialized by registerIntervalHandler
  36. _converse.disco_entities.get(_converse.domain).features['urn:xmpp:csi:0'] = true; // Mock that the server supports CSI
  37. _converse.csi_waiting_time = 3; // The relevant config option
  38. while (i <= _converse.csi_waiting_time) {
  39. expect(_converse.sendCSI).not.toHaveBeenCalled();
  40. _converse.onEverySecond();
  41. i++;
  42. }
  43. expect(_converse.sendCSI).toHaveBeenCalledWith('inactive');
  44. expect(sent_stanza.toLocaleString()).toBe('<inactive xmlns="urn:xmpp:csi:0"/>');
  45. _converse.onUserActivity();
  46. expect(_converse.sendCSI).toHaveBeenCalledWith('active');
  47. expect(sent_stanza.toLocaleString()).toBe('<active xmlns="urn:xmpp:csi:0"/>');
  48. // Reset values
  49. _converse.csi_waiting_time = 0;
  50. _converse.disco_entities.get(_converse.domain).features['urn:xmpp:csi:0'] = false;
  51. done();
  52. }));
  53. });
  54. describe("Automatic status change", function () {
  55. it("happens when the client is idle for long enough", mock.initConverse((done, _converse) => {
  56. let i = 0;
  57. // Usually initialized by registerIntervalHandler
  58. _converse.idle_seconds = 0;
  59. _converse.auto_changed_status = false;
  60. // The relevant config options
  61. _converse.auto_away = 3;
  62. _converse.auto_xa = 6;
  63. expect(_converse.api.user.status.get()).toBe('online');
  64. while (i <= _converse.auto_away) {
  65. _converse.onEverySecond(); i++;
  66. }
  67. expect(_converse.auto_changed_status).toBe(true);
  68. while (i <= _converse.auto_xa) {
  69. expect(_converse.api.user.status.get()).toBe('away');
  70. _converse.onEverySecond();
  71. i++;
  72. }
  73. expect(_converse.api.user.status.get()).toBe('xa');
  74. expect(_converse.auto_changed_status).toBe(true);
  75. _converse.onUserActivity();
  76. expect(_converse.api.user.status.get()).toBe('online');
  77. expect(_converse.auto_changed_status).toBe(false);
  78. // Check that it also works for the chat feature
  79. _converse.api.user.status.set('chat')
  80. i = 0;
  81. while (i <= _converse.auto_away) {
  82. _converse.onEverySecond();
  83. i++;
  84. }
  85. expect(_converse.auto_changed_status).toBe(true);
  86. while (i <= _converse.auto_xa) {
  87. expect(_converse.api.user.status.get()).toBe('away');
  88. _converse.onEverySecond();
  89. i++;
  90. }
  91. expect(_converse.api.user.status.get()).toBe('xa');
  92. expect(_converse.auto_changed_status).toBe(true);
  93. _converse.onUserActivity();
  94. expect(_converse.api.user.status.get()).toBe('online');
  95. expect(_converse.auto_changed_status).toBe(false);
  96. // Check that it doesn't work for 'dnd'
  97. _converse.api.user.status.set('dnd');
  98. i = 0;
  99. while (i <= _converse.auto_away) {
  100. _converse.onEverySecond();
  101. i++;
  102. }
  103. expect(_converse.api.user.status.get()).toBe('dnd');
  104. expect(_converse.auto_changed_status).toBe(false);
  105. while (i <= _converse.auto_xa) {
  106. expect(_converse.api.user.status.get()).toBe('dnd');
  107. _converse.onEverySecond();
  108. i++;
  109. }
  110. expect(_converse.api.user.status.get()).toBe('dnd');
  111. expect(_converse.auto_changed_status).toBe(false);
  112. _converse.onUserActivity();
  113. expect(_converse.api.user.status.get()).toBe('dnd');
  114. expect(_converse.auto_changed_status).toBe(false);
  115. done();
  116. }));
  117. });
  118. describe("The \"user\" grouping", function () {
  119. describe("The \"status\" API", function () {
  120. it("has a method for getting the user's availability", mock.initConverse((done, _converse) => {
  121. _converse.xmppstatus.set('status', 'online');
  122. expect(_converse.api.user.status.get()).toBe('online');
  123. _converse.xmppstatus.set('status', 'dnd');
  124. expect(_converse.api.user.status.get()).toBe('dnd');
  125. done();
  126. }));
  127. it("has a method for setting the user's availability", mock.initConverse((done, _converse) => {
  128. _converse.api.user.status.set('away');
  129. expect(_converse.xmppstatus.get('status')).toBe('away');
  130. _converse.api.user.status.set('dnd');
  131. expect(_converse.xmppstatus.get('status')).toBe('dnd');
  132. _converse.api.user.status.set('xa');
  133. expect(_converse.xmppstatus.get('status')).toBe('xa');
  134. _converse.api.user.status.set('chat');
  135. expect(_converse.xmppstatus.get('status')).toBe('chat');
  136. expect(_.partial(_converse.api.user.status.set, 'invalid')).toThrow(
  137. new Error('Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1')
  138. );
  139. done();
  140. }));
  141. it("allows setting the status message as well", mock.initConverse((done, _converse) => {
  142. _converse.api.user.status.set('away', "I'm in a meeting");
  143. expect(_converse.xmppstatus.get('status')).toBe('away');
  144. expect(_converse.xmppstatus.get('status_message')).toBe("I'm in a meeting");
  145. done();
  146. }));
  147. it("has a method for getting the user's status message", mock.initConverse((done, _converse) => {
  148. _converse.xmppstatus.set('status_message', undefined);
  149. expect(_converse.api.user.status.message.get()).toBe(undefined);
  150. _converse.xmppstatus.set('status_message', "I'm in a meeting");
  151. expect(_converse.api.user.status.message.get()).toBe("I'm in a meeting");
  152. done();
  153. }));
  154. it("has a method for setting the user's status message", mock.initConverse((done, _converse) => {
  155. _converse.xmppstatus.set('status_message', undefined);
  156. _converse.api.user.status.message.set("I'm in a meeting");
  157. expect(_converse.xmppstatus.get('status_message')).toBe("I'm in a meeting");
  158. done();
  159. }));
  160. });
  161. });
  162. describe("The \"tokens\" API", function () {
  163. it("has a method for retrieving the next RID", mock.initConverse((done, _converse) => {
  164. test_utils.createContacts(_converse, 'current');
  165. const old_connection = _converse.connection;
  166. _converse.connection._proto.rid = '1234';
  167. expect(_converse.api.tokens.get('rid')).toBe('1234');
  168. _converse.connection = undefined;
  169. expect(_converse.api.tokens.get('rid')).toBe(null);
  170. // Restore the connection
  171. _converse.connection = old_connection;
  172. done();
  173. }));
  174. it("has a method for retrieving the SID", mock.initConverse((done, _converse) => {
  175. test_utils.createContacts(_converse, 'current');
  176. const old_connection = _converse.connection;
  177. _converse.connection._proto.sid = '1234';
  178. expect(_converse.api.tokens.get('sid')).toBe('1234');
  179. _converse.connection = undefined;
  180. expect(_converse.api.tokens.get('sid')).toBe(null);
  181. // Restore the connection
  182. _converse.connection = old_connection;
  183. done();
  184. }));
  185. });
  186. describe("The \"contacts\" API", function () {
  187. it("has a method 'get' which returns wrapped contacts",
  188. mock.initConverse([], {}, async function (done, _converse) {
  189. await test_utils.waitForRoster(_converse, 'current');
  190. let contact = await _converse.api.contacts.get('non-existing@jabber.org');
  191. expect(contact).toBeFalsy();
  192. // Check when a single jid is given
  193. const jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  194. contact = await _converse.api.contacts.get(jid);
  195. expect(contact.getDisplayName()).toBe(mock.cur_names[0]);
  196. expect(contact.get('jid')).toBe(jid);
  197. // You can retrieve multiple contacts by passing in an array
  198. const jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  199. let list = await _converse.api.contacts.get([jid, jid2]);
  200. expect(Array.isArray(list)).toBeTruthy();
  201. expect(list[0].getDisplayName()).toBe(mock.cur_names[0]);
  202. expect(list[1].getDisplayName()).toBe(mock.cur_names[1]);
  203. // Check that all JIDs are returned if you call without any parameters
  204. list = await _converse.api.contacts.get();
  205. expect(list.length).toBe(mock.cur_names.length);
  206. done();
  207. }));
  208. it("has a method 'add' with which contacts can be added",
  209. mock.initConverse(['rosterInitialized'], {}, async (done, _converse) => {
  210. await test_utils.waitForRoster(_converse, 'current', 0);
  211. try {
  212. await _converse.api.contacts.add();
  213. throw new Error('Call should have failed');
  214. } catch (e) {
  215. expect(e.message).toBe('contacts.add: invalid jid');
  216. }
  217. try {
  218. await _converse.api.contacts.add("invalid jid");
  219. throw new Error('Call should have failed');
  220. } catch (e) {
  221. expect(e.message).toBe('contacts.add: invalid jid');
  222. }
  223. spyOn(_converse.roster, 'addAndSubscribe');
  224. await _converse.api.contacts.add("newcontact@example.org");
  225. expect(_converse.roster.addAndSubscribe).toHaveBeenCalled();
  226. done();
  227. }));
  228. });
  229. describe("The \"chats\" API", function() {
  230. it("has a method 'get' which returns the promise that resolves to a chat model", mock.initConverse(
  231. ['rosterInitialized', 'chatBoxesInitialized'], {},
  232. async (done, _converse) => {
  233. await test_utils.openControlBox(_converse);
  234. await test_utils.waitForRoster(_converse, 'current', 2);
  235. // Test on chat that doesn't exist.
  236. let chat = await _converse.api.chats.get('non-existing@jabber.org');
  237. expect(chat).toBeFalsy();
  238. const jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  239. const jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  240. // Test on chat that's not open
  241. chat = await _converse.api.chats.get(jid);
  242. expect(chat === null).toBeTruthy();
  243. expect(_converse.chatboxes.length).toBe(1);
  244. // Test for one JID
  245. chat = await _converse.api.chats.open(jid);
  246. expect(chat instanceof Object).toBeTruthy();
  247. expect(chat.get('box_id')).toBe(`box-${btoa(jid)}`);
  248. const view = _converse.chatboxviews.get(jid);
  249. await u.waitUntil(() => u.isVisible(view.el));
  250. // Test for multiple JIDs
  251. test_utils.openChatBoxFor(_converse, jid2);
  252. await u.waitUntil(() => _converse.chatboxes.length == 3);
  253. const list = await _converse.api.chats.get([jid, jid2]);
  254. expect(Array.isArray(list)).toBeTruthy();
  255. expect(list[0].get('box_id')).toBe(`box-${btoa(jid)}`);
  256. expect(list[1].get('box_id')).toBe(`box-${btoa(jid2)}`);
  257. done();
  258. }));
  259. it("has a method 'open' which opens and returns a promise that resolves to a chat model", mock.initConverse(
  260. ['rosterGroupsFetched', 'chatBoxesInitialized'], {},
  261. async (done, _converse) => {
  262. await test_utils.openControlBox(_converse);
  263. await test_utils.waitForRoster(_converse, 'current', 2);
  264. const jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  265. const jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  266. // Test on chat that doesn't exist.
  267. let chat = await _converse.api.chats.get('non-existing@jabber.org');
  268. expect(chat).toBeFalsy();
  269. chat = await _converse.api.chats.open(jid);
  270. expect(chat instanceof Object).toBeTruthy();
  271. expect(chat.get('box_id')).toBe(`box-${btoa(jid)}`);
  272. expect(
  273. Object.keys(chat),
  274. ['close', 'endOTR', 'focus', 'get', 'initiateOTR', 'is_chatroom', 'maximize', 'minimize', 'open', 'set']
  275. );
  276. const view = _converse.chatboxviews.get(jid);
  277. await u.waitUntil(() => u.isVisible(view.el));
  278. // Test for multiple JIDs
  279. const list = await _converse.api.chats.open([jid, jid2]);
  280. expect(Array.isArray(list)).toBeTruthy();
  281. expect(list[0].get('box_id')).toBe(`box-${btoa(jid)}`);
  282. expect(list[1].get('box_id')).toBe(`box-${btoa(jid2)}`);
  283. done();
  284. }));
  285. });
  286. describe("The \"settings\" API", function() {
  287. it("has methods 'get' and 'set' to set configuration settings",
  288. mock.initConverse(null, {'play_sounds': true}, (done, _converse) => {
  289. expect(_.keys(_converse.api.settings)).toEqual(["update", "get", "set"]);
  290. expect(_converse.api.settings.get("play_sounds")).toBe(true);
  291. _converse.api.settings.set("play_sounds", false);
  292. expect(_converse.api.settings.get("play_sounds")).toBe(false);
  293. _converse.api.settings.set({"play_sounds": true});
  294. expect(_converse.api.settings.get("play_sounds")).toBe(true);
  295. // Only whitelisted settings allowed.
  296. expect(typeof _converse.api.settings.get("non_existing")).toBe("undefined");
  297. _converse.api.settings.set("non_existing", true);
  298. expect(typeof _converse.api.settings.get("non_existing")).toBe("undefined");
  299. done();
  300. }));
  301. });
  302. describe("The \"plugins\" API", function() {
  303. it("only has a method 'add' for registering plugins", mock.initConverse((done, _converse) => {
  304. expect(_.keys(converse.plugins)).toEqual(["add"]);
  305. // Cheating a little bit. We clear the plugins to test more easily.
  306. const _old_plugins = _converse.pluggable.plugins;
  307. _converse.pluggable.plugins = [];
  308. converse.plugins.add('plugin1', {});
  309. expect(_.keys(_converse.pluggable.plugins)).toEqual(['plugin1']);
  310. converse.plugins.add('plugin2', {});
  311. expect(_.keys(_converse.pluggable.plugins)).toEqual(['plugin1', 'plugin2']);
  312. _converse.pluggable.plugins = _old_plugins;
  313. done();
  314. }));
  315. describe("The \"plugins.add\" method", function() {
  316. it("throws an error when multiple plugins attempt to register with the same name",
  317. mock.initConverse((done, _converse) => { // eslint-disable-line no-unused-vars
  318. converse.plugins.add('myplugin', {});
  319. const error = new TypeError('Error: plugin with name "myplugin" has already been registered!');
  320. expect(_.partial(converse.plugins.add, 'myplugin', {})).toThrow(error);
  321. done();
  322. }));
  323. });
  324. });
  325. });
  326. }));