push.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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(null,
  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 test_utils.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 test_utils.waitUntil(() => _converse.session.get('push_enabled'));
  44. done();
  45. }));
  46. it("can be enabled for a MUC domain",
  47. mock.initConverse(null,
  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. const room_jid = 'coven@chat.shakespeare.lit';
  57. await test_utils.waitUntilDiscoConfirmed(
  58. _converse, _converse.push_app_servers[0].jid,
  59. [{'category': 'pubsub', 'type':'push'}],
  60. ['urn:xmpp:push:0'], [], 'info');
  61. await test_utils.waitUntilDiscoConfirmed(
  62. _converse, _converse.bare_jid, [],
  63. ['urn:xmpp:push:0']);
  64. let iq = await test_utils.waitUntil(() => _.filter(
  65. IQ_stanzas,
  66. iq => sizzle(`iq[type="set"] enable[xmlns="${Strophe.NS.PUSH}"]`, iq).length
  67. ).pop());
  68. expect(Strophe.serialize(iq)).toBe(
  69. `<iq id="${iq.getAttribute('id')}" type="set" xmlns="jabber:client">`+
  70. `<enable jid="push-5@client.example" node="yxs32uqsflafdk3iuqo" xmlns="urn:xmpp:push:0"/>`+
  71. `</iq>`
  72. );
  73. const result = u.toStanza(`<iq type="result" id="${iq.getAttribute('id')}" to="romeo@montague.lit" />`);
  74. _converse.connection._dataRecv(test_utils.createRequest(result));
  75. await test_utils.waitUntil(() => _converse.session.get('push_enabled'));
  76. expect(_converse.session.get('push_enabled').length).toBe(1);
  77. expect(_.includes(_converse.session.get('push_enabled'), 'romeo@montague.lit')).toBe(true);
  78. test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'oldhag');
  79. await test_utils.waitUntilDiscoConfirmed(
  80. _converse, 'chat.shakespeare.lit',
  81. [{'category': 'account', 'type':'registered'}],
  82. ['urn:xmpp:push:0'], [], 'info');
  83. iq = await test_utils.waitUntil(() => _.filter(
  84. IQ_stanzas,
  85. iq => sizzle(`iq[type="set"][to="chat.shakespeare.lit"] enable[xmlns="${Strophe.NS.PUSH}"]`, iq).length
  86. ).pop());
  87. expect(Strophe.serialize(iq)).toEqual(
  88. `<iq id="${iq.getAttribute('id')}" to="chat.shakespeare.lit" type="set" xmlns="jabber:client">`+
  89. '<enable jid="push-5@client.example" node="yxs32uqsflafdk3iuqo" xmlns="urn:xmpp:push:0"/>'+
  90. '</iq>'
  91. );
  92. _converse.connection._dataRecv(test_utils.createRequest($iq({
  93. 'to': _converse.connection.jid,
  94. 'type': 'result',
  95. 'id': iq.getAttribute('id')
  96. })));
  97. await test_utils.waitUntil(() => _.includes(_converse.session.get('push_enabled'), 'chat.shakespeare.lit'));
  98. done();
  99. }));
  100. it("can be disabled",
  101. mock.initConverse(null,
  102. ['rosterGroupsFetched'], {
  103. 'push_app_servers': [{
  104. 'jid': 'push-5@client.example',
  105. 'node': 'yxs32uqsflafdk3iuqo',
  106. 'disable': true
  107. }]
  108. }, async function (done, _converse) {
  109. const IQ_stanzas = _converse.connection.IQ_stanzas;
  110. expect(_converse.session.get('push_enabled')).toBeFalsy();
  111. await test_utils.waitUntilDiscoConfirmed(
  112. _converse,
  113. _converse.bare_jid,
  114. [{'category': 'account', 'type':'registered'}],
  115. ['urn:xmpp:push:0'], [], 'info');
  116. const stanza = await test_utils.waitUntil(
  117. () => _.filter(IQ_stanzas, iq => iq.querySelector('iq[type="set"] disable[xmlns="urn:xmpp:push:0"]')).pop()
  118. );
  119. expect(Strophe.serialize(stanza)).toEqual(
  120. `<iq id="${stanza.getAttribute('id')}" type="set" xmlns="jabber:client">`+
  121. '<disable jid="push-5@client.example" node="yxs32uqsflafdk3iuqo" xmlns="urn:xmpp:push:0"/>'+
  122. '</iq>'
  123. );
  124. _converse.connection._dataRecv(test_utils.createRequest($iq({
  125. 'to': _converse.connection.jid,
  126. 'type': 'result',
  127. 'id': stanza.getAttribute('id')
  128. })));
  129. await test_utils.waitUntil(() => _converse.session.get('push_enabled'))
  130. done();
  131. }));
  132. it("can require a secret token to be included",
  133. mock.initConverse(null,
  134. ['rosterGroupsFetched'], {
  135. 'push_app_servers': [{
  136. 'jid': 'push-5@client.example',
  137. 'node': 'yxs32uqsflafdk3iuqo',
  138. 'secret': 'eruio234vzxc2kla-91'
  139. }]
  140. }, async function (done, _converse) {
  141. const IQ_stanzas = _converse.connection.IQ_stanzas;
  142. expect(_converse.session.get('push_enabled')).toBeFalsy();
  143. await test_utils.waitUntilDiscoConfirmed(
  144. _converse, _converse.push_app_servers[0].jid,
  145. [{'category': 'pubsub', 'type':'push'}],
  146. ['urn:xmpp:push:0'], [], 'info');
  147. await test_utils.waitUntilDiscoConfirmed(
  148. _converse,
  149. _converse.bare_jid,
  150. [{'category': 'account', 'type':'registered'}],
  151. ['urn:xmpp:push:0'], [], 'info');
  152. const stanza = await test_utils.waitUntil(
  153. () => _.filter(IQ_stanzas, iq => iq.querySelector('iq[type="set"] enable[xmlns="urn:xmpp:push:0"]')).pop()
  154. );
  155. expect(Strophe.serialize(stanza)).toEqual(
  156. `<iq id="${stanza.getAttribute('id')}" type="set" xmlns="jabber:client">`+
  157. '<enable jid="push-5@client.example" node="yxs32uqsflafdk3iuqo" xmlns="urn:xmpp:push:0">'+
  158. '<x type="submit" xmlns="jabber:x:data">'+
  159. '<field var="FORM_TYPE"><value>http://jabber.org/protocol/pubsub#publish-options</value></field>'+
  160. '<field var="secret"><value>eruio234vzxc2kla-91</value></field>'+
  161. '</x>'+
  162. '</enable>'+
  163. '</iq>'
  164. )
  165. _converse.connection._dataRecv(test_utils.createRequest($iq({
  166. 'to': _converse.connection.jid,
  167. 'type': 'result',
  168. 'id': stanza.getAttribute('id')
  169. })));
  170. await test_utils.waitUntil(() => _converse.session.get('push_enabled'))
  171. done();
  172. }));
  173. });
  174. }));