protocol.js 25 KB

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