protocol.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. (function (root, factory) {
  2. define([
  3. "jasmine",
  4. "jquery",
  5. "mock",
  6. "test-utils"], factory);
  7. } (this, function (_jasmine, $, mock, test_utils) {
  8. "use strict";
  9. const jasmine = _jasmine.default;
  10. var Strophe = converse.env.Strophe;
  11. var $iq = converse.env.$iq;
  12. var $pres = converse.env.$pres;
  13. var _ = converse.env._;
  14. var u = converse.env.utils;
  15. // See:
  16. // https://xmpp.org/rfcs/rfc3921.html
  17. describe("The Protocol", function () {
  18. describe("Integration of Roster Items and Presence Subscriptions", function () {
  19. // Stub the trimChat method. It causes havoc when running with
  20. // phantomJS.
  21. /* Some level of integration between roster items and presence
  22. * subscriptions is normally expected by an instant messaging user
  23. * regarding the user's subscriptions to and from other contacts. This
  24. * section describes the level of integration that MUST be supported
  25. * within an XMPP instant messaging applications.
  26. *
  27. * There are four primary subscription states:
  28. *
  29. * None -- the user does not have a subscription to the contact's
  30. * presence information, and the contact does not have a subscription
  31. * to the user's presence information
  32. * To -- the user has a subscription to the contact's presence
  33. * information, but the contact does not have a subscription to the
  34. * user's presence information
  35. * From -- the contact has a subscription to the user's presence
  36. * information, but the user does not have a subscription to the
  37. * contact's presence information
  38. * Both -- both the user and the contact have subscriptions to each
  39. * other's presence information (i.e., the union of 'from' and 'to')
  40. *
  41. * Each of these states is reflected in the roster of both the user and
  42. * the contact, thus resulting in durable subscription states.
  43. *
  44. * The 'from' and 'to' addresses are OPTIONAL in roster pushes; if
  45. * included, their values SHOULD be the full JID of the resource for
  46. * that session. A client MUST acknowledge each roster push with an IQ
  47. * stanza of type "result".
  48. */
  49. it("Subscribe to contact, contact accepts and subscribes back",
  50. mock.initConverseWithPromises(
  51. null, ['rosterGroupsFetched'],
  52. { roster_groups: false },
  53. async function (done, _converse) {
  54. var contact, sent_stanza, IQ_id, stanza, modal;
  55. await test_utils.waitUntilDiscoConfirmed(_converse, 'localhost', [], ['vcard-temp']);
  56. await test_utils.waitUntil(() => _converse.xmppstatus.vcard.get('fullname'), 300);
  57. /* The process by which a user subscribes to a contact, including
  58. * the interaction between roster items and subscription states.
  59. */
  60. test_utils.openControlBox(_converse);
  61. const cbview = _converse.chatboxviews.get('controlbox');
  62. spyOn(_converse.roster, "addAndSubscribe").and.callThrough();
  63. spyOn(_converse.roster, "addContactToRoster").and.callThrough();
  64. spyOn(_converse.roster, "sendContactAddIQ").and.callThrough();
  65. spyOn(_converse.api.vcard, "get").and.callThrough();
  66. const sendIQ = _converse.connection.sendIQ;
  67. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  68. sent_stanza = iq;
  69. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  70. });
  71. cbview.el.querySelector('.add-contact').click()
  72. modal = _converse.rosterview.add_contact_modal;
  73. await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
  74. spyOn(modal, "addContactFromForm").and.callThrough();
  75. modal.delegateEvents();
  76. // Fill in the form and submit
  77. const form = modal.el.querySelector('form.add-xmpp-contact');
  78. form.querySelector('input').value = 'contact@example.org';
  79. form.querySelector('[type="submit"]').click();
  80. /* In preparation for being able to render the contact in the
  81. * user's client interface and for the server to keep track of the
  82. * subscription, the user's client SHOULD perform a "roster set"
  83. * for the new roster item.
  84. */
  85. expect(modal.addContactFromForm).toHaveBeenCalled();
  86. expect(_converse.roster.addAndSubscribe).toHaveBeenCalled();
  87. expect(_converse.roster.addContactToRoster).toHaveBeenCalled();
  88. /* _converse request consists of sending an IQ
  89. * stanza of type='set' containing a <query/> element qualified by
  90. * the 'jabber:iq:roster' namespace, which in turn contains an
  91. * <item/> element that defines the new roster item; the <item/>
  92. * element MUST possess a 'jid' attribute, MAY possess a 'name'
  93. * attribute, MUST NOT possess a 'subscription' attribute, and MAY
  94. * contain one or more <group/> child elements:
  95. *
  96. * <iq type='set' id='set1'>
  97. * <query xmlns='jabber:iq:roster'>
  98. * <item
  99. * jid='contact@example.org'
  100. * name='MyContact'>
  101. * <group>MyBuddies</group>
  102. * </item>
  103. * </query>
  104. * </iq>
  105. */
  106. expect(_converse.roster.sendContactAddIQ).toHaveBeenCalled();
  107. expect(sent_stanza.toLocaleString()).toBe(
  108. `<iq id="${IQ_id}" type="set" xmlns="jabber:client">`+
  109. `<query xmlns="jabber:iq:roster">`+
  110. `<item jid="contact@example.org" name="contact@example.org"/>`+
  111. `</query>`+
  112. `</iq>`
  113. );
  114. /* As a result, the user's server (1) MUST initiate a roster push
  115. * for the new roster item to all available resources associated
  116. * with _converse user that have requested the roster, setting the
  117. * 'subscription' attribute to a value of "none"; and (2) MUST
  118. * reply to the sending resource with an IQ result indicating the
  119. * success of the roster set:
  120. *
  121. * <iq type='set'>
  122. * <query xmlns='jabber:iq:roster'>
  123. * <item
  124. * jid='contact@example.org'
  125. * subscription='none'
  126. * name='MyContact'>
  127. * <group>MyBuddies</group>
  128. * </item>
  129. * </query>
  130. * </iq>
  131. */
  132. const create = _converse.roster.create;
  133. const sent_stanzas = [];
  134. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  135. sent_stanza = stanza;
  136. sent_stanzas.push(stanza.toLocaleString());
  137. });
  138. spyOn(_converse.roster, 'create').and.callFake(function () {
  139. contact = create.apply(_converse.roster, arguments);
  140. spyOn(contact, 'subscribe').and.callThrough();
  141. return contact;
  142. });
  143. stanza = $iq({'type': 'set'}).c('query', {'xmlns': 'jabber:iq:roster'})
  144. .c('item', {
  145. 'jid': 'contact@example.org',
  146. 'subscription': 'none',
  147. 'name': 'contact@example.org'});
  148. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  149. /* <iq type='result' id='set1'/>
  150. */
  151. stanza = $iq({'type': 'result', 'id':IQ_id});
  152. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  153. await test_utils.waitUntil(() => _converse.roster.create.calls.count());
  154. // A contact should now have been created
  155. expect(_converse.roster.get('contact@example.org') instanceof _converse.RosterContact).toBeTruthy();
  156. expect(contact.get('jid')).toBe('contact@example.org');
  157. expect(_converse.api.vcard.get).toHaveBeenCalled();
  158. /* To subscribe to the contact's presence information,
  159. * the user's client MUST send a presence stanza of
  160. * type='subscribe' to the contact:
  161. *
  162. * <presence to='contact@example.org' type='subscribe'/>
  163. */
  164. await test_utils.waitUntil(() => sent_stanzas.filter(s => s.match('presence')));
  165. expect(contact.subscribe).toHaveBeenCalled();
  166. expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
  167. `<presence to="contact@example.org" type="subscribe" xmlns="jabber:client">`+
  168. `<nick xmlns="http://jabber.org/protocol/nick">Max Mustermann</nick>`+
  169. `</presence>`
  170. );
  171. /* As a result, the user's server MUST initiate a second roster
  172. * push to all of the user's available resources that have
  173. * requested the roster, setting the contact to the pending
  174. * sub-state of the 'none' subscription state; _converse pending
  175. * sub-state is denoted by the inclusion of the ask='subscribe'
  176. * attribute in the roster item:
  177. *
  178. * <iq type='set'>
  179. * <query xmlns='jabber:iq:roster'>
  180. * <item
  181. * jid='contact@example.org'
  182. * subscription='none'
  183. * ask='subscribe'
  184. * name='MyContact'>
  185. * <group>MyBuddies</group>
  186. * </item>
  187. * </query>
  188. * </iq>
  189. */
  190. spyOn(_converse.roster, "updateContact").and.callThrough();
  191. stanza = $iq({'type': 'set', 'from': _converse.bare_jid})
  192. .c('query', {'xmlns': 'jabber:iq:roster'})
  193. .c('item', {
  194. 'jid': 'contact@example.org',
  195. 'subscription': 'none',
  196. 'ask': 'subscribe',
  197. 'name': 'contact@example.org'});
  198. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  199. expect(_converse.roster.updateContact).toHaveBeenCalled();
  200. // Check that the user is now properly shown as a pending
  201. // contact in the roster.
  202. await test_utils.waitUntil(function () {
  203. var $header = $('a:contains("Pending contacts")');
  204. var $contacts = $header.parent().find('li:visible');
  205. return $contacts.length;
  206. }, 600);
  207. var $header = $('a:contains("Pending contacts")');
  208. var $contacts = $header.parent().find('li');
  209. expect($contacts.length).toBe(1);
  210. expect($contacts.is(':visible')).toBeTruthy();
  211. spyOn(contact, "ackSubscribe").and.callThrough();
  212. /* Here we assume the "happy path" that the contact
  213. * approves the subscription request
  214. *
  215. * <presence
  216. * to='user@example.com'
  217. * from='contact@example.org'
  218. * type='subscribed'/>
  219. */
  220. stanza = $pres({
  221. 'to': _converse.bare_jid,
  222. 'from': 'contact@example.org',
  223. 'type': 'subscribed'
  224. });
  225. sent_stanza = ""; // Reset
  226. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  227. /* Upon receiving the presence stanza of type "subscribed",
  228. * the user SHOULD acknowledge receipt of that
  229. * subscription state notification by sending a presence
  230. * stanza of type "subscribe".
  231. */
  232. expect(contact.ackSubscribe).toHaveBeenCalled();
  233. expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
  234. `<presence to="contact@example.org" type="subscribe" xmlns="jabber:client"/>`
  235. );
  236. /* The user's server MUST initiate a roster push to all of the user's
  237. * available resources that have requested the roster,
  238. * containing an updated roster item for the contact with
  239. * the 'subscription' attribute set to a value of "to";
  240. *
  241. * <iq type='set'>
  242. * <query xmlns='jabber:iq:roster'>
  243. * <item
  244. * jid='contact@example.org'
  245. * subscription='to'
  246. * name='MyContact'>
  247. * <group>MyBuddies</group>
  248. * </item>
  249. * </query>
  250. * </iq>
  251. */
  252. IQ_id = _converse.connection.getUniqueId('roster');
  253. stanza = $iq({'type': 'set', 'id': IQ_id})
  254. .c('query', {'xmlns': 'jabber:iq:roster'})
  255. .c('item', {
  256. 'jid': 'contact@example.org',
  257. 'subscription': 'to',
  258. 'name': 'contact@example.org'});
  259. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  260. // Check that the IQ set was acknowledged.
  261. expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
  262. `<iq from="dummy@localhost/resource" id="${IQ_id}" type="result" xmlns="jabber:client"/>`
  263. );
  264. expect(_converse.roster.updateContact).toHaveBeenCalled();
  265. // The contact should now be visible as an existing
  266. // contact (but still offline).
  267. await test_utils.waitUntil(function () {
  268. var $header = $('a:contains("My contacts")');
  269. var $contacts = $header.parent().find('li:visible');
  270. return $contacts.length;
  271. }, 600);
  272. $header = $('a:contains("My contacts")');
  273. expect($header.length).toBe(1);
  274. expect($header.is(":visible")).toBeTruthy();
  275. $contacts = $header.parent().find('li');
  276. expect($contacts.length).toBe(1);
  277. // Check that it has the right classes and text
  278. expect($contacts.hasClass('to')).toBeTruthy();
  279. expect($contacts.hasClass('both')).toBeFalsy();
  280. expect($contacts.hasClass('current-xmpp-contact')).toBeTruthy();
  281. expect($contacts.text().trim()).toBe('Contact');
  282. expect(contact.presence.get('show')).toBe('offline');
  283. /* <presence
  284. * from='contact@example.org/resource'
  285. * to='user@example.com/resource'/>
  286. */
  287. stanza = $pres({'to': _converse.bare_jid, 'from': 'contact@example.org/resource'});
  288. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  289. // Now the contact should also be online.
  290. expect(contact.presence.get('show')).toBe('online');
  291. /* Section 8.3. Creating a Mutual Subscription
  292. *
  293. * If the contact wants to create a mutual subscription,
  294. * the contact MUST send a subscription request to the
  295. * user.
  296. *
  297. * <presence from='contact@example.org' to='user@example.com' type='subscribe'/>
  298. */
  299. spyOn(contact, 'authorize').and.callThrough();
  300. spyOn(_converse.roster, 'handleIncomingSubscription').and.callThrough();
  301. stanza = $pres({
  302. 'to': _converse.bare_jid,
  303. 'from': 'contact@example.org/resource',
  304. 'type': 'subscribe'});
  305. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  306. expect(_converse.roster.handleIncomingSubscription).toHaveBeenCalled();
  307. /* The user's client MUST send a presence stanza of type
  308. * "subscribed" to the contact in order to approve the
  309. * subscription request.
  310. *
  311. * <presence to='contact@example.org' type='subscribed'/>
  312. */
  313. expect(contact.authorize).toHaveBeenCalled();
  314. expect(sent_stanza.toLocaleString()).toBe(
  315. `<presence to="contact@example.org" type="subscribed" xmlns="jabber:client"/>`
  316. );
  317. /* As a result, the user's server MUST initiate a
  318. * roster push containing a roster item for the
  319. * contact with the 'subscription' attribute set to
  320. * a value of "both".
  321. *
  322. * <iq type='set'>
  323. * <query xmlns='jabber:iq:roster'>
  324. * <item
  325. * jid='contact@example.org'
  326. * subscription='both'
  327. * name='MyContact'>
  328. * <group>MyBuddies</group>
  329. * </item>
  330. * </query>
  331. * </iq>
  332. */
  333. stanza = $iq({'type': 'set'}).c('query', {'xmlns': 'jabber:iq:roster'})
  334. .c('item', {
  335. 'jid': 'contact@example.org',
  336. 'subscription': 'both',
  337. 'name': 'contact@example.org'});
  338. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  339. expect(_converse.roster.updateContact).toHaveBeenCalled();
  340. // The class on the contact will now have switched.
  341. expect($contacts.hasClass('to')).toBeFalsy();
  342. expect($contacts.hasClass('both')).toBeTruthy();
  343. done();
  344. }));
  345. it("Alternate Flow: Contact Declines Subscription Request",
  346. mock.initConverseWithPromises(
  347. null, ['rosterGroupsFetched'], {},
  348. function (done, _converse) {
  349. /* The process by which a user subscribes to a contact, including
  350. * the interaction between roster items and subscription states.
  351. */
  352. var contact, stanza, sent_stanza, sent_IQ;
  353. test_utils.openControlBox(_converse);
  354. // Add a new roster contact via roster push
  355. stanza = $iq({'type': 'set'}).c('query', {'xmlns': 'jabber:iq:roster'})
  356. .c('item', {
  357. 'jid': 'contact@example.org',
  358. 'subscription': 'none',
  359. 'ask': 'subscribe',
  360. 'name': 'contact@example.org'});
  361. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  362. // A pending contact should now exist.
  363. contact = _converse.roster.get('contact@example.org');
  364. expect(_converse.roster.get('contact@example.org') instanceof _converse.RosterContact).toBeTruthy();
  365. spyOn(contact, "ackUnsubscribe").and.callThrough();
  366. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  367. sent_stanza = stanza;
  368. });
  369. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  370. sent_IQ = iq;
  371. });
  372. /* We now assume the contact declines the subscription
  373. * requests.
  374. *
  375. * Upon receiving the presence stanza of type "unsubscribed"
  376. * addressed to the user, the user's server (1) MUST deliver
  377. * that presence stanza to the user and (2) MUST initiate a
  378. * roster push to all of the user's available resources that
  379. * have requested the roster, containing an updated roster
  380. * item for the contact with the 'subscription' attribute
  381. * set to a value of "none" and with no 'ask' attribute:
  382. *
  383. * <presence
  384. * from='contact@example.org'
  385. * to='user@example.com'
  386. * type='unsubscribed'/>
  387. *
  388. * <iq type='set'>
  389. * <query xmlns='jabber:iq:roster'>
  390. * <item
  391. * jid='contact@example.org'
  392. * subscription='none'
  393. * name='MyContact'>
  394. * <group>MyBuddies</group>
  395. * </item>
  396. * </query>
  397. * </iq>
  398. */
  399. // FIXME: also add the <iq>
  400. stanza = $pres({
  401. 'to': _converse.bare_jid,
  402. 'from': 'contact@example.org',
  403. 'type': 'unsubscribed'
  404. });
  405. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  406. /* Upon receiving the presence stanza of type "unsubscribed",
  407. * the user SHOULD acknowledge receipt of that subscription
  408. * state notification through either "affirming" it by
  409. * sending a presence stanza of type "unsubscribe
  410. */
  411. expect(contact.ackUnsubscribe).toHaveBeenCalled();
  412. expect(sent_stanza.toLocaleString()).toBe(
  413. `<presence to="contact@example.org" type="unsubscribe" xmlns="jabber:client"/>`
  414. );
  415. /* _converse.js will then also automatically remove the
  416. * contact from the user's roster.
  417. */
  418. expect(sent_IQ.toLocaleString()).toBe(
  419. `<iq type="set" xmlns="jabber:client">`+
  420. `<query xmlns="jabber:iq:roster">`+
  421. `<item jid="contact@example.org" subscription="remove"/>`+
  422. `</query>`+
  423. `</iq>`
  424. );
  425. done();
  426. }));
  427. it("Unsubscribe to a contact when subscription is mutual",
  428. mock.initConverseWithPromises(
  429. null, ['rosterGroupsFetched'],
  430. { roster_groups: false },
  431. async function (done, _converse) {
  432. var sent_IQ, IQ_id, jid = 'annegreet.gomez@localhost';
  433. test_utils.openControlBox(_converse);
  434. test_utils.createContacts(_converse, 'current');
  435. spyOn(window, 'confirm').and.returnValue(true);
  436. // We now have a contact we want to remove
  437. expect(_converse.roster.get(jid) instanceof _converse.RosterContact).toBeTruthy();
  438. var sendIQ = _converse.connection.sendIQ;
  439. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  440. sent_IQ = iq;
  441. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  442. });
  443. const $header = $('a:contains("My contacts")');
  444. await test_utils.waitUntil(() => $header.parent().find('li').length);
  445. // remove the first user
  446. $header.parent().find('li .remove-xmpp-contact').get(0).click();
  447. expect(window.confirm).toHaveBeenCalled();
  448. /* Section 8.6 Removing a Roster Item and Cancelling All
  449. * Subscriptions
  450. *
  451. * First the user is removed from the roster
  452. * Because there may be many steps involved in completely
  453. * removing a roster item and cancelling subscriptions in
  454. * both directions, the roster management protocol includes
  455. * a "shortcut" method for doing so. The process may be
  456. * initiated no matter what the current subscription state
  457. * is by sending a roster set containing an item for the
  458. * contact with the 'subscription' attribute set to a value
  459. * of "remove":
  460. *
  461. * <iq type='set' id='remove1'>
  462. * <query xmlns='jabber:iq:roster'>
  463. * <item jid='contact@example.org' subscription='remove'/>
  464. * </query>
  465. * </iq>
  466. */
  467. expect(sent_IQ.toLocaleString()).toBe(
  468. `<iq id="${IQ_id}" type="set" xmlns="jabber:client">`+
  469. `<query xmlns="jabber:iq:roster">`+
  470. `<item jid="annegreet.gomez@localhost" subscription="remove"/>`+
  471. `</query>`+
  472. `</iq>`);
  473. // Receive confirmation from the contact's server
  474. // <iq type='result' id='remove1'/>
  475. const stanza = $iq({'type': 'result', 'id':IQ_id});
  476. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  477. // Our contact has now been removed
  478. await test_utils.waitUntil(() => typeof _converse.roster.get(jid) === "undefined");
  479. done();
  480. }));
  481. it("Receiving a subscription request", mock.initConverseWithPromises(
  482. null, ['rosterGroupsFetched'], {},
  483. function (done, _converse) {
  484. spyOn(_converse, "emit");
  485. test_utils.openControlBox(_converse);
  486. test_utils.createContacts(_converse, 'current'); // Create some contacts so that we can test positioning
  487. /* <presence
  488. * from='user@example.com'
  489. * to='contact@example.org'
  490. * type='subscribe'/>
  491. */
  492. var stanza = $pres({
  493. 'to': _converse.bare_jid,
  494. 'from': 'contact@example.org',
  495. 'type': 'subscribe'
  496. }).c('nick', {
  497. 'xmlns': Strophe.NS.NICK,
  498. }).t('Clint Contact');
  499. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  500. return test_utils.waitUntil(function () {
  501. var $header = $('a:contains("Contact requests")');
  502. var $contacts = $header.parent().find('li:visible');
  503. return $contacts.length;
  504. }, 600).then(function () {
  505. expect(_converse.emit).toHaveBeenCalledWith('contactRequest', jasmine.any(Object));
  506. var $header = $('a:contains("Contact requests")');
  507. expect($header.length).toBe(1);
  508. expect($header.is(":visible")).toBeTruthy();
  509. var $contacts = $header.parent().find('li');
  510. expect($contacts.length).toBe(1);
  511. done();
  512. });
  513. }));
  514. });
  515. });
  516. }));