push.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 f = converse.env.f;
  9. describe("XEP-0357 Push Notifications", function () {
  10. it("can be enabled",
  11. mock.initConverse(null,
  12. ['rosterGroupsFetched'], {
  13. 'push_app_servers': [{
  14. 'jid': 'push-5@client.example',
  15. 'node': 'yxs32uqsflafdk3iuqo'
  16. }]
  17. }, async function (done, _converse) {
  18. const IQ_stanzas = _converse.connection.IQ_stanzas;
  19. expect(_converse.session.get('push_enabled')).toBeFalsy();
  20. await test_utils.waitUntilDiscoConfirmed(
  21. _converse, _converse.push_app_servers[0].jid,
  22. [{'category': 'pubsub', 'type':'push'}],
  23. ['urn:xmpp:push:0'], [], 'info');
  24. await test_utils.waitUntilDiscoConfirmed(
  25. _converse,
  26. _converse.bare_jid,
  27. [{'category': 'account', 'type':'registered'}],
  28. ['urn:xmpp:push:0'], [], 'info');
  29. const node = await test_utils.waitUntil(() =>
  30. _.filter(IQ_stanzas, iq => iq.nodeTree.querySelector('iq[type="set"] enable[xmlns="urn:xmpp:push:0"]')).pop()
  31. );
  32. const stanza = node.nodeTree;
  33. expect(node.toLocaleString()).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. room_jid = 'coven@chat.shakespeare.lit';
  57. expect(_converse.session.get('push_enabled')).toBeFalsy();
  58. test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'oldhag');
  59. await test_utils.waitUntilDiscoConfirmed(
  60. _converse, _converse.push_app_servers[0].jid,
  61. [{'category': 'pubsub', 'type':'push'}],
  62. ['urn:xmpp:push:0'], [], 'info');
  63. await test_utils.waitUntilDiscoConfirmed(
  64. _converse, 'chat.shakespeare.lit',
  65. [{'category': 'account', 'type':'registered'}],
  66. ['urn:xmpp:push:0'], [], 'info');
  67. const stanza = await test_utils.waitUntil(
  68. () => _.filter(IQ_stanzas, (iq) => iq.nodeTree.querySelector('iq[type="set"] enable[xmlns="urn:xmpp:push:0"]')).pop()
  69. );
  70. expect(stanza.toLocaleString()).toEqual(
  71. `<iq id="${stanza.nodeTree.getAttribute('id')}" to="chat.shakespeare.lit" type="set" xmlns="jabber:client">`+
  72. '<enable jid="push-5@client.example" node="yxs32uqsflafdk3iuqo" xmlns="urn:xmpp:push:0"/>'+
  73. '</iq>'
  74. );
  75. _converse.connection._dataRecv(test_utils.createRequest($iq({
  76. 'to': _converse.connection.jid,
  77. 'type': 'result',
  78. 'id': stanza.nodeTree.getAttribute('id')
  79. })));
  80. await test_utils.waitUntil(() => f.includes('chat.shakespeare.lit', _converse.session.get('push_enabled')));
  81. done();
  82. }));
  83. it("can be disabled",
  84. mock.initConverse(null,
  85. ['rosterGroupsFetched'], {
  86. 'push_app_servers': [{
  87. 'jid': 'push-5@client.example',
  88. 'node': 'yxs32uqsflafdk3iuqo',
  89. 'disable': true
  90. }]
  91. }, async function (done, _converse) {
  92. const IQ_stanzas = _converse.connection.IQ_stanzas;
  93. expect(_converse.session.get('push_enabled')).toBeFalsy();
  94. await test_utils.waitUntilDiscoConfirmed(
  95. _converse,
  96. _converse.bare_jid,
  97. [{'category': 'account', 'type':'registered'}],
  98. ['urn:xmpp:push:0'], [], 'info');
  99. const node = await test_utils.waitUntil(
  100. () => _.filter(IQ_stanzas, iq => iq.nodeTree.querySelector('iq[type="set"] disable[xmlns="urn:xmpp:push:0"]')).pop()
  101. );
  102. const stanza = node.nodeTree;
  103. expect(node.toLocaleString()).toEqual(
  104. `<iq id="${stanza.getAttribute('id')}" type="set" xmlns="jabber:client">`+
  105. '<disable jid="push-5@client.example" node="yxs32uqsflafdk3iuqo" xmlns="urn:xmpp:push:0"/>'+
  106. '</iq>'
  107. );
  108. _converse.connection._dataRecv(test_utils.createRequest($iq({
  109. 'to': _converse.connection.jid,
  110. 'type': 'result',
  111. 'id': stanza.getAttribute('id')
  112. })));
  113. await test_utils.waitUntil(() => _converse.session.get('push_enabled'))
  114. done();
  115. }));
  116. it("can require a secret token to be included",
  117. mock.initConverse(null,
  118. ['rosterGroupsFetched'], {
  119. 'push_app_servers': [{
  120. 'jid': 'push-5@client.example',
  121. 'node': 'yxs32uqsflafdk3iuqo',
  122. 'secret': 'eruio234vzxc2kla-91'
  123. }]
  124. }, async function (done, _converse) {
  125. const IQ_stanzas = _converse.connection.IQ_stanzas;
  126. expect(_converse.session.get('push_enabled')).toBeFalsy();
  127. await test_utils.waitUntilDiscoConfirmed(
  128. _converse, _converse.push_app_servers[0].jid,
  129. [{'category': 'pubsub', 'type':'push'}],
  130. ['urn:xmpp:push:0'], [], 'info');
  131. await test_utils.waitUntilDiscoConfirmed(
  132. _converse,
  133. _converse.bare_jid,
  134. [{'category': 'account', 'type':'registered'}],
  135. ['urn:xmpp:push:0'], [], 'info');
  136. const node = await test_utils.waitUntil(
  137. () => _.filter(IQ_stanzas, iq => iq.nodeTree.querySelector('iq[type="set"] enable[xmlns="urn:xmpp:push:0"]')).pop()
  138. );
  139. const stanza = node.nodeTree;
  140. expect(node.toLocaleString()).toEqual(
  141. `<iq id="${stanza.getAttribute('id')}" type="set" xmlns="jabber:client">`+
  142. '<enable jid="push-5@client.example" node="yxs32uqsflafdk3iuqo" xmlns="urn:xmpp:push:0">'+
  143. '<x type="submit" xmlns="jabber:x:data">'+
  144. '<field var="FORM_TYPE"><value>http://jabber.org/protocol/pubsub#publish-options</value></field>'+
  145. '<field var="secret"><value>eruio234vzxc2kla-91</value></field>'+
  146. '</x>'+
  147. '</enable>'+
  148. '</iq>'
  149. )
  150. _converse.connection._dataRecv(test_utils.createRequest($iq({
  151. 'to': _converse.connection.jid,
  152. 'type': 'result',
  153. 'id': stanza.getAttribute('id')
  154. })));
  155. await test_utils.waitUntil(() => _converse.session.get('push_enabled'))
  156. done();
  157. }));
  158. });
  159. }));