push.js 8.7 KB

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