push.js 8.6 KB

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