push.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. const node = _.filter(IQ_stanzas, function (iq) {
  33. return iq.nodeTree.querySelector('iq[type="set"] enable[xmlns="urn:xmpp:push:0"]');
  34. }).pop();
  35. if (node) {
  36. stanza = node.nodeTree;
  37. return true;
  38. }
  39. })
  40. }).then(() => {
  41. expect(stanza.outerHTML).toEqual(
  42. `<iq type="set" xmlns="jabber:client" id="${stanza.getAttribute('id')}">`+
  43. '<enable xmlns="urn:xmpp:push:0" jid="push-5@client.example" node="yxs32uqsflafdk3iuqo"/>'+
  44. '</iq>'
  45. )
  46. _converse.connection._dataRecv(test_utils.createRequest($iq({
  47. 'to': _converse.connection.jid,
  48. 'type': 'result',
  49. 'id': stanza.getAttribute('id')
  50. })));
  51. return test_utils.waitUntil(() => _converse.session.get('push_enabled'))
  52. }).then(done).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  53. }));
  54. it("can be enabled for a MUC domain",
  55. mock.initConverseWithPromises(null,
  56. ['rosterGroupsFetched'], {
  57. 'enable_muc_push': true,
  58. 'push_app_servers': [{
  59. 'jid': 'push-5@client.example',
  60. 'node': 'yxs32uqsflafdk3iuqo'
  61. }]
  62. }, function (done, _converse) {
  63. const IQ_stanzas = _converse.connection.IQ_stanzas,
  64. room_jid = 'coven@chat.shakespeare.lit';
  65. expect(_converse.session.get('push_enabled')).toBeFalsy();
  66. test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'oldhag')
  67. .then(() => test_utils.waitUntilDiscoConfirmed(
  68. _converse, _converse.push_app_servers[0].jid,
  69. [{'category': 'pubsub', 'type':'push'}],
  70. ['urn:xmpp:push:0'], [], 'info'))
  71. .then(() => {
  72. return test_utils.waitUntilDiscoConfirmed(
  73. _converse, 'chat.shakespeare.lit',
  74. [{'category': 'account', 'type':'registered'}],
  75. ['urn:xmpp:push:0'], [], 'info')
  76. }).then(() => {
  77. return test_utils.waitUntil(
  78. () => _.filter(IQ_stanzas, (iq) => iq.nodeTree.querySelector('iq[type="set"] enable[xmlns="urn:xmpp:push:0"]')).pop())
  79. }).then(stanza => {
  80. expect(stanza.nodeTree.outerHTML).toEqual(
  81. `<iq type="set" xmlns="jabber:client" to="chat.shakespeare.lit" id="${stanza.nodeTree.getAttribute('id')}">`+
  82. '<enable xmlns="urn:xmpp:push:0" jid="push-5@client.example" node="yxs32uqsflafdk3iuqo"/>'+
  83. '</iq>'
  84. )
  85. _converse.connection._dataRecv(test_utils.createRequest($iq({
  86. 'to': _converse.connection.jid,
  87. 'type': 'result',
  88. 'id': stanza.nodeTree.getAttribute('id')
  89. })));
  90. return test_utils.waitUntil(() => f.includes('chat.shakespeare.lit', _converse.session.get('push_enabled')));
  91. }).then(done).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  92. }));
  93. it("can be disabled",
  94. mock.initConverseWithPromises(null,
  95. ['rosterGroupsFetched'], {
  96. 'push_app_servers': [{
  97. 'jid': 'push-5@client.example',
  98. 'node': 'yxs32uqsflafdk3iuqo',
  99. 'disable': true
  100. }]
  101. }, function (done, _converse) {
  102. const IQ_stanzas = _converse.connection.IQ_stanzas;
  103. let stanza;
  104. expect(_converse.session.get('push_enabled')).toBeFalsy();
  105. test_utils.waitUntilDiscoConfirmed(
  106. _converse,
  107. _converse.bare_jid,
  108. [{'category': 'account', 'type':'registered'}],
  109. ['urn:xmpp:push:0'], [], 'info')
  110. .then(() => {
  111. return test_utils.waitUntil(() => {
  112. const node = _.filter(IQ_stanzas, function (iq) {
  113. return iq.nodeTree.querySelector('iq[type="set"] disable[xmlns="urn:xmpp:push:0"]');
  114. }).pop();
  115. if (node) {
  116. stanza = node.nodeTree;
  117. return true;
  118. }
  119. })
  120. }).then(() => {
  121. expect(stanza.outerHTML).toEqual(
  122. `<iq type="set" xmlns="jabber:client" id="${stanza.getAttribute('id')}">`+
  123. '<disable xmlns="urn:xmpp:push:0" jid="push-5@client.example" node="yxs32uqsflafdk3iuqo"/>'+
  124. '</iq>'
  125. )
  126. _converse.connection._dataRecv(test_utils.createRequest($iq({
  127. 'to': _converse.connection.jid,
  128. 'type': 'result',
  129. 'id': stanza.getAttribute('id')
  130. })));
  131. return test_utils.waitUntil(() => _converse.session.get('push_enabled'))
  132. }).then(done).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  133. }));
  134. it("can require a secret token to be included",
  135. mock.initConverseWithPromises(null,
  136. ['rosterGroupsFetched'], {
  137. 'push_app_servers': [{
  138. 'jid': 'push-5@client.example',
  139. 'node': 'yxs32uqsflafdk3iuqo',
  140. 'secret': 'eruio234vzxc2kla-91'
  141. }]
  142. }, function (done, _converse) {
  143. const IQ_stanzas = _converse.connection.IQ_stanzas;
  144. let stanza;
  145. expect(_converse.session.get('push_enabled')).toBeFalsy();
  146. test_utils.waitUntilDiscoConfirmed(
  147. _converse, _converse.push_app_servers[0].jid,
  148. [{'category': 'pubsub', 'type':'push'}],
  149. ['urn:xmpp:push:0'], [], 'info')
  150. .then(() => test_utils.waitUntilDiscoConfirmed(
  151. _converse,
  152. _converse.bare_jid,
  153. [{'category': 'account', 'type':'registered'}],
  154. ['urn:xmpp:push:0'], [], 'info'))
  155. .then(() => {
  156. return test_utils.waitUntil(() => {
  157. const node = _.filter(IQ_stanzas, function (iq) {
  158. return iq.nodeTree.querySelector('iq[type="set"] enable[xmlns="urn:xmpp:push:0"]');
  159. }).pop();
  160. if (node) {
  161. stanza = node.nodeTree;
  162. return true;
  163. }
  164. })
  165. }).then(() => {
  166. expect(stanza.outerHTML).toEqual(
  167. `<iq type="set" xmlns="jabber:client" id="${stanza.getAttribute('id')}">`+
  168. '<enable xmlns="urn:xmpp:push:0" jid="push-5@client.example" node="yxs32uqsflafdk3iuqo">'+
  169. '<x xmlns="jabber:x:data" type="submit">'+
  170. '<field var="FORM_TYPE"><value>http://jabber.org/protocol/pubsub#publish-options</value></field>'+
  171. '<field var="secret"><value>eruio234vzxc2kla-91</value></field>'+
  172. '</x>'+
  173. '</enable>'+
  174. '</iq>'
  175. )
  176. _converse.connection._dataRecv(test_utils.createRequest($iq({
  177. 'to': _converse.connection.jid,
  178. 'type': 'result',
  179. 'id': stanza.getAttribute('id')
  180. })));
  181. return test_utils.waitUntil(() => _converse.session.get('push_enabled'))
  182. }).then(done).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  183. }));
  184. });
  185. }));