http-file-upload.js 12 KB

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