http-file-upload.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. (function (root, factory) {
  2. define([
  3. "jasmine",
  4. "jquery",
  5. "converse-core",
  6. "mock",
  7. "test-utils"], factory);
  8. } (this, function (jasmine, $, converse, mock, test_utils) {
  9. "use strict";
  10. var Strophe = converse.env.Strophe;
  11. var $iq = converse.env.$iq;
  12. var _ = converse.env._;
  13. var f = converse.env.f;
  14. describe("XEP-0363: HTTP File Upload", function () {
  15. describe("Discovering support", function () {
  16. it("is done automatically", mock.initConverseWithAsync(function (done, _converse) {
  17. var IQ_stanzas = _converse.connection.IQ_stanzas;
  18. var IQ_ids = _converse.connection.IQ_ids;
  19. test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], []).then(function () {
  20. test_utils.waitUntil(function () {
  21. return _.filter(IQ_stanzas, function (iq) {
  22. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  23. }).length > 0;
  24. }, 300).then(function () {
  25. /* <iq type='result'
  26. * from='plays.shakespeare.lit'
  27. * to='romeo@montague.net/orchard'
  28. * id='info1'>
  29. * <query xmlns='http://jabber.org/protocol/disco#info'>
  30. * <identity
  31. * category='server'
  32. * type='im'/>
  33. * <feature var='http://jabber.org/protocol/disco#info'/>
  34. * <feature var='http://jabber.org/protocol/disco#items'/>
  35. * </query>
  36. * </iq>
  37. */
  38. var stanza = _.filter(IQ_stanzas, function (iq) {
  39. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  40. })[0];
  41. var info_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  42. stanza = $iq({
  43. 'type': 'result',
  44. 'from': 'localhost',
  45. 'to': 'dummy@localhost/resource',
  46. 'id': info_IQ_id
  47. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  48. .c('identity', {
  49. 'category': 'server',
  50. 'type': 'im'}).up()
  51. .c('feature', {
  52. 'var': 'http://jabber.org/protocol/disco#info'}).up()
  53. .c('feature', {
  54. 'var': 'http://jabber.org/protocol/disco#items'});
  55. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  56. _converse.api.disco.entities.get().then(function(entities) {
  57. expect(entities.length).toBe(2);
  58. expect(_.includes(entities.pluck('jid'), 'localhost')).toBe(true);
  59. expect(_.includes(entities.pluck('jid'), 'dummy@localhost')).toBe(true);
  60. expect(entities.get(_converse.domain).features.length).toBe(2);
  61. expect(entities.get(_converse.domain).identities.length).toBe(1);
  62. return test_utils.waitUntil(function () {
  63. // Converse.js sees that the entity has a disco#items feature,
  64. // so it will make a query for it.
  65. return _.filter(IQ_stanzas, function (iq) {
  66. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  67. }).length > 0;
  68. }, 300);
  69. });
  70. }).then(function () {
  71. /* <iq from='montague.tld'
  72. * id='step_01'
  73. * to='romeo@montague.tld/garden'
  74. * type='result'>
  75. * <query xmlns='http://jabber.org/protocol/disco#items'>
  76. * <item jid='upload.montague.tld' name='HTTP File Upload' />
  77. * <item jid='conference.montague.tld' name='Chatroom Service' />
  78. * </query>
  79. * </iq>
  80. */
  81. var stanza = _.filter(IQ_stanzas, function (iq) {
  82. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  83. })[0];
  84. var items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  85. stanza = $iq({
  86. 'type': 'result',
  87. 'from': 'localhost',
  88. 'to': 'dummy@localhost/resource',
  89. 'id': items_IQ_id
  90. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
  91. .c('item', {
  92. 'jid': 'upload.localhost',
  93. 'name': 'HTTP File Upload'});
  94. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  95. _converse.api.disco.entities.get().then(function (entities) {
  96. expect(entities.length).toBe(2);
  97. expect(entities.get('localhost').items.length).toBe(1);
  98. return test_utils.waitUntil(function () {
  99. // Converse.js sees that the entity has a disco#info feature,
  100. // so it will make a query for it.
  101. return _.filter(IQ_stanzas, function (iq) {
  102. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  103. }).length > 0;
  104. }, 300);
  105. });
  106. }).then(function () {
  107. var stanza = _.filter(IQ_stanzas, function (iq) {
  108. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  109. })[0];
  110. var IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  111. expect(stanza.toLocaleString()).toBe(
  112. "<iq from='dummy@localhost/resource' to='upload.localhost' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  113. "<query xmlns='http://jabber.org/protocol/disco#info'/>"+
  114. "</iq>");
  115. // Upload service responds and reports a maximum file size of 5MiB
  116. /* <iq from='upload.montague.tld'
  117. * id='step_02'
  118. * to='romeo@montague.tld/garden'
  119. * type='result'>
  120. * <query xmlns='http://jabber.org/protocol/disco#info'>
  121. * <identity category='store'
  122. * type='file'
  123. * name='HTTP File Upload' />
  124. * <feature var='urn:xmpp:http:upload:0' />
  125. * <x type='result' xmlns='jabber:x:data'>
  126. * <field var='FORM_TYPE' type='hidden'>
  127. * <value>urn:xmpp:http:upload:0</value>
  128. * </field>
  129. * <field var='max-file-size'>
  130. * <value>5242880</value>
  131. * </field>
  132. * </x>
  133. * </query>
  134. * </iq>
  135. */
  136. stanza = $iq({'type': 'result', 'to': 'dummy@localhost/resource', 'id': IQ_id, 'from': 'upload.localhost'})
  137. .c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  138. .c('identity', {'category':'store', 'type':'file', 'name':'HTTP File Upload'}).up()
  139. .c('feature', {'var':'urn:xmpp:http:upload:0'}).up()
  140. .c('x', {'type':'result', 'xmlns':'jabber:x:data'})
  141. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  142. .c('value').t('urn:xmpp:http:upload:0').up().up()
  143. .c('field', {'var':'max-file-size'})
  144. .c('value').t('5242880');
  145. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  146. _converse.api.disco.entities.get().then(function (entities) {
  147. expect(entities.get('localhost').items.get('upload.localhost').identities.where({'category': 'store'}).length).toBe(1);
  148. _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain).then(
  149. function (result) {
  150. expect(result.length).toBe(1);
  151. expect(result[0].get('jid')).toBe('upload.localhost');
  152. done();
  153. }
  154. );
  155. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  156. })
  157. })
  158. }));
  159. });
  160. describe("When supported", function () {
  161. describe("A file upload toolbar button", function () {
  162. it("appears in private chats", mock.initConverseWithAsync(function (done, _converse) {
  163. test_utils.waitUntilDiscoConfirmed(
  164. _converse, _converse.domain,
  165. [{'category': 'server', 'type':'IM'}],
  166. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  167. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.localhost'], 'items').then(function () {
  168. test_utils.waitUntilDiscoConfirmed(_converse, 'upload.localhost', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
  169. test_utils.createContacts(_converse, 'current');
  170. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  171. test_utils.openChatBoxFor(_converse, contact_jid);
  172. var view = _converse.chatboxviews.get(contact_jid);
  173. test_utils.waitUntil(function () {
  174. return view.el.querySelector('.upload-file');
  175. }, 150).then(function () {
  176. expect(view.el.querySelector('.chat-toolbar .upload-file')).not.toBe(null);
  177. done();
  178. });
  179. });
  180. });
  181. });
  182. }));
  183. it("appears in MUC chats", mock.initConverseWithAsync(function (done, _converse) {
  184. done();
  185. }));
  186. });
  187. });
  188. });
  189. }));