push.js 7.1 KB

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