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