push.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*global mock */
  2. const $iq = converse.env.$iq;
  3. const Strophe = converse.env.Strophe;
  4. const _ = converse.env._;
  5. const sizzle = converse.env.sizzle;
  6. const u = converse.env.utils;
  7. describe("XEP-0357 Push Notifications", function () {
  8. it("can be enabled",
  9. mock.initConverse(
  10. ['rosterGroupsFetched'], {
  11. 'push_app_servers': [{
  12. 'jid': 'push-5@client.example',
  13. 'node': 'yxs32uqsflafdk3iuqo'
  14. }]
  15. }, async function (done, _converse) {
  16. const IQ_stanzas = _converse.connection.IQ_stanzas;
  17. expect(_converse.session.get('push_enabled')).toBeFalsy();
  18. await mock.waitUntilDiscoConfirmed(
  19. _converse, _converse.push_app_servers[0].jid,
  20. [{'category': 'pubsub', 'type':'push'}],
  21. ['urn:xmpp:push:0'], [], 'info');
  22. await mock.waitUntilDiscoConfirmed(
  23. _converse,
  24. _converse.bare_jid,
  25. [{'category': 'account', 'type':'registered'}],
  26. ['urn:xmpp:push:0'], [], 'info');
  27. const stanza = await u.waitUntil(() =>
  28. _.filter(IQ_stanzas, iq => iq.querySelector('iq[type="set"] enable[xmlns="urn:xmpp:push:0"]')).pop()
  29. );
  30. expect(Strophe.serialize(stanza)).toEqual(
  31. `<iq id="${stanza.getAttribute('id')}" type="set" xmlns="jabber:client">`+
  32. '<enable jid="push-5@client.example" node="yxs32uqsflafdk3iuqo" xmlns="urn:xmpp:push:0"/>'+
  33. '</iq>'
  34. )
  35. _converse.connection._dataRecv(mock.createRequest($iq({
  36. 'to': _converse.connection.jid,
  37. 'type': 'result',
  38. 'id': stanza.getAttribute('id')
  39. })));
  40. await u.waitUntil(() => _converse.session.get('push_enabled'));
  41. done();
  42. }));
  43. it("can be enabled for a MUC domain",
  44. mock.initConverse(
  45. ['rosterGroupsFetched'], {
  46. 'enable_muc_push': true,
  47. 'push_app_servers': [{
  48. 'jid': 'push-5@client.example',
  49. 'node': 'yxs32uqsflafdk3iuqo'
  50. }]
  51. }, async function (done, _converse) {
  52. const IQ_stanzas = _converse.connection.IQ_stanzas;
  53. await mock.waitUntilDiscoConfirmed(
  54. _converse, _converse.push_app_servers[0].jid,
  55. [{'category': 'pubsub', 'type':'push'}],
  56. ['urn:xmpp:push:0'], [], 'info');
  57. await mock.waitUntilDiscoConfirmed(
  58. _converse, _converse.bare_jid, [],
  59. ['urn:xmpp:push:0']);
  60. let iq = await u.waitUntil(() => _.filter(
  61. IQ_stanzas,
  62. iq => sizzle(`iq[type="set"] enable[xmlns="${Strophe.NS.PUSH}"]`, iq).length
  63. ).pop());
  64. expect(Strophe.serialize(iq)).toBe(
  65. `<iq id="${iq.getAttribute('id')}" type="set" xmlns="jabber:client">`+
  66. `<enable jid="push-5@client.example" node="yxs32uqsflafdk3iuqo" xmlns="urn:xmpp:push:0"/>`+
  67. `</iq>`
  68. );
  69. const result = u.toStanza(`<iq type="result" id="${iq.getAttribute('id')}" to="romeo@montague.lit" />`);
  70. _converse.connection._dataRecv(mock.createRequest(result));
  71. await u.waitUntil(() => _converse.session.get('push_enabled'));
  72. expect(_converse.session.get('push_enabled').length).toBe(1);
  73. expect(_.includes(_converse.session.get('push_enabled'), 'romeo@montague.lit')).toBe(true);
  74. mock.openAndEnterChatRoom(_converse, 'coven@chat.shakespeare.lit', 'oldhag');
  75. await mock.waitUntilDiscoConfirmed(
  76. _converse, 'chat.shakespeare.lit',
  77. [{'category': 'account', 'type':'registered'}],
  78. ['urn:xmpp:push:0'], [], 'info');
  79. iq = await u.waitUntil(() => _.filter(
  80. IQ_stanzas,
  81. iq => sizzle(`iq[type="set"][to="chat.shakespeare.lit"] enable[xmlns="${Strophe.NS.PUSH}"]`, iq).length
  82. ).pop());
  83. expect(Strophe.serialize(iq)).toEqual(
  84. `<iq id="${iq.getAttribute('id')}" to="chat.shakespeare.lit" type="set" xmlns="jabber:client">`+
  85. '<enable jid="push-5@client.example" node="yxs32uqsflafdk3iuqo" xmlns="urn:xmpp:push:0"/>'+
  86. '</iq>'
  87. );
  88. _converse.connection._dataRecv(mock.createRequest($iq({
  89. 'to': _converse.connection.jid,
  90. 'type': 'result',
  91. 'id': iq.getAttribute('id')
  92. })));
  93. await u.waitUntil(() => _.includes(_converse.session.get('push_enabled'), 'chat.shakespeare.lit'));
  94. done();
  95. }));
  96. it("can be disabled",
  97. mock.initConverse(
  98. ['rosterGroupsFetched'], {
  99. 'push_app_servers': [{
  100. 'jid': 'push-5@client.example',
  101. 'node': 'yxs32uqsflafdk3iuqo',
  102. 'disable': true
  103. }]
  104. }, async function (done, _converse) {
  105. const IQ_stanzas = _converse.connection.IQ_stanzas;
  106. expect(_converse.session.get('push_enabled')).toBeFalsy();
  107. await mock.waitUntilDiscoConfirmed(
  108. _converse,
  109. _converse.bare_jid,
  110. [{'category': 'account', 'type':'registered'}],
  111. ['urn:xmpp:push:0'], [], 'info');
  112. const stanza = await u.waitUntil(
  113. () => _.filter(IQ_stanzas, iq => iq.querySelector('iq[type="set"] disable[xmlns="urn:xmpp:push:0"]')).pop()
  114. );
  115. expect(Strophe.serialize(stanza)).toEqual(
  116. `<iq id="${stanza.getAttribute('id')}" type="set" xmlns="jabber:client">`+
  117. '<disable jid="push-5@client.example" node="yxs32uqsflafdk3iuqo" xmlns="urn:xmpp:push:0"/>'+
  118. '</iq>'
  119. );
  120. _converse.connection._dataRecv(mock.createRequest($iq({
  121. 'to': _converse.connection.jid,
  122. 'type': 'result',
  123. 'id': stanza.getAttribute('id')
  124. })));
  125. await u.waitUntil(() => _converse.session.get('push_enabled'))
  126. done();
  127. }));
  128. it("can require a secret token to be included",
  129. mock.initConverse(
  130. ['rosterGroupsFetched'], {
  131. 'push_app_servers': [{
  132. 'jid': 'push-5@client.example',
  133. 'node': 'yxs32uqsflafdk3iuqo',
  134. 'secret': 'eruio234vzxc2kla-91'
  135. }]
  136. }, async function (done, _converse) {
  137. const IQ_stanzas = _converse.connection.IQ_stanzas;
  138. expect(_converse.session.get('push_enabled')).toBeFalsy();
  139. await mock.waitUntilDiscoConfirmed(
  140. _converse, _converse.push_app_servers[0].jid,
  141. [{'category': 'pubsub', 'type':'push'}],
  142. ['urn:xmpp:push:0'], [], 'info');
  143. await mock.waitUntilDiscoConfirmed(
  144. _converse,
  145. _converse.bare_jid,
  146. [{'category': 'account', 'type':'registered'}],
  147. ['urn:xmpp:push:0'], [], 'info');
  148. const stanza = await u.waitUntil(
  149. () => _.filter(IQ_stanzas, iq => iq.querySelector('iq[type="set"] enable[xmlns="urn:xmpp:push:0"]')).pop()
  150. );
  151. expect(Strophe.serialize(stanza)).toEqual(
  152. `<iq id="${stanza.getAttribute('id')}" type="set" xmlns="jabber:client">`+
  153. '<enable jid="push-5@client.example" node="yxs32uqsflafdk3iuqo" xmlns="urn:xmpp:push:0">'+
  154. '<x type="submit" xmlns="jabber:x:data">'+
  155. '<field var="FORM_TYPE"><value>http://jabber.org/protocol/pubsub#publish-options</value></field>'+
  156. '<field var="secret"><value>eruio234vzxc2kla-91</value></field>'+
  157. '</x>'+
  158. '</enable>'+
  159. '</iq>'
  160. )
  161. _converse.connection._dataRecv(mock.createRequest($iq({
  162. 'to': _converse.connection.jid,
  163. 'type': 'result',
  164. 'id': stanza.getAttribute('id')
  165. })));
  166. await u.waitUntil(() => _converse.session.get('push_enabled'))
  167. done();
  168. }));
  169. });