http-file-upload.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. expect(result.length).toBe(1);
  153. expect(result[0].get('jid')).toBe('upload.localhost');
  154. expect(result[0].dataforms.where({'FORM_TYPE': {value: "urn:xmpp:http:upload:0", type: "hidden"}}).length).toBe(1);
  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. describe("when clicked and a file chosen", function () {
  190. it("is uploaded and sent out", mock.initConverseWithAsync(function (done, _converse) {
  191. test_utils.waitUntilDiscoConfirmed(
  192. _converse, _converse.domain,
  193. [{'category': 'server', 'type':'IM'}],
  194. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  195. var send_backup = XMLHttpRequest.prototype.send;
  196. var IQ_stanzas = _converse.connection.IQ_stanzas;
  197. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.tld'], 'items').then(function () {
  198. test_utils.waitUntilDiscoConfirmed(_converse, 'upload.montague.tld', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
  199. test_utils.createContacts(_converse, 'current');
  200. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  201. test_utils.openChatBoxFor(_converse, contact_jid);
  202. var view = _converse.chatboxviews.get(contact_jid);
  203. var file = {
  204. 'type': 'image/jpeg',
  205. 'size': '23456' ,
  206. 'lastModifiedDate': "",
  207. 'name': "my-juliet.jpg"
  208. };
  209. view.model.sendFiles([file]);
  210. return test_utils.waitUntil(function () {
  211. return _.filter(IQ_stanzas, function (iq) {
  212. return iq.nodeTree.querySelector('iq[to="upload.montague.tld"] request');
  213. }).length > 0;
  214. }).then(function () {
  215. var iq = IQ_stanzas.pop();
  216. expect(iq.toLocaleString()).toBe(
  217. "<iq from='dummy@localhost/resource' "+
  218. "to='upload.montague.tld' "+
  219. "type='get' "+
  220. "xmlns='jabber:client' "+
  221. "id='"+iq.nodeTree.getAttribute('id')+"'>"+
  222. "<request xmlns='urn:xmpp:http:upload:0' "+
  223. "filename='my-juliet.jpg' "+
  224. "size='23456' "+
  225. "content-type='image/jpeg'/>"+
  226. "</iq>");
  227. var base_url = document.URL.split(window.location.pathname)[0];
  228. var message = base_url+"/logo/conversejs-filled.svg";
  229. var stanza = Strophe.xmlHtmlNode(
  230. "<iq from='upload.montague.tld'"+
  231. " id='"+iq.nodeTree.getAttribute('id')+"'"+
  232. " to='dummy@localhost/resource'"+
  233. " type='result'>"+
  234. "<slot xmlns='urn:xmpp:http:upload:0'>"+
  235. " <put url='https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg'>"+
  236. " <header name='Authorization'>Basic Base64String==</header>"+
  237. " <header name='Cookie'>foo=bar; user=romeo</header>"+
  238. " </put>"+
  239. " <get url='"+message+"' />"+
  240. "</slot>"+
  241. "</iq>").firstElementChild;
  242. spyOn(XMLHttpRequest.prototype, 'send').and.callFake(function () {
  243. const message = view.model.messages.at(0);
  244. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0');
  245. message.set('progress', 0.5);
  246. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0.5');
  247. message.set('progress', 1);
  248. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('1');
  249. message.save({
  250. 'upload': _converse.SUCCESS,
  251. 'message': message.get('get')
  252. });
  253. });
  254. var sent_stanza;
  255. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  256. sent_stanza = stanza;
  257. });
  258. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  259. return test_utils.waitUntil(function () {
  260. return sent_stanza;
  261. }, 1000).then(function () {
  262. expect(sent_stanza.toLocaleString()).toBe(
  263. "<message from='dummy@localhost/resource' "+
  264. "to='irini.vlastuin@localhost' "+
  265. "type='chat' "+
  266. "id='"+sent_stanza.nodeTree.getAttribute('id')+"' xmlns='jabber:client'>"+
  267. "<body>"+message+"</body>"+
  268. "<active xmlns='http://jabber.org/protocol/chatstates'/>"+
  269. "<x xmlns='jabber:x:oob'>"+
  270. "<url>"+message+"</url>"+
  271. "</x>"+
  272. "</message>");
  273. return test_utils.waitUntil(function () {
  274. return view.el.querySelector('.chat-image');
  275. }, 1000);
  276. }).then(function () {
  277. // Check that the image renders
  278. expect(view.el.querySelector('.chat-message .chat-msg-content').innerHTML).toEqual(
  279. '<a target="_blank" rel="noopener" href="http://localhost:8000/logo/conversejs-filled.svg">'+
  280. '<img class="chat-image" src="http://localhost:8000/logo/conversejs-filled.svg"></a>')
  281. XMLHttpRequest.prototype.send = send_backup;
  282. done();
  283. });
  284. });
  285. });
  286. });
  287. });
  288. }));
  289. it("shows and error message if the file is too large", mock.initConverseWithAsync(function (done, _converse) {
  290. var IQ_stanzas = _converse.connection.IQ_stanzas;
  291. var IQ_ids = _converse.connection.IQ_ids;
  292. var send_backup = XMLHttpRequest.prototype.send;
  293. test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], []).then(function () {
  294. test_utils.waitUntil(function () {
  295. return _.filter(IQ_stanzas, function (iq) {
  296. return iq.nodeTree.querySelector(
  297. 'iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  298. }).length > 0;
  299. }, 300).then(function () {
  300. var stanza = _.filter(IQ_stanzas, function (iq) {
  301. return iq.nodeTree.querySelector(
  302. 'iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  303. })[0];
  304. var info_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  305. stanza = $iq({
  306. 'type': 'result',
  307. 'from': 'localhost',
  308. 'to': 'dummy@localhost/resource',
  309. 'id': info_IQ_id
  310. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  311. .c('identity', {
  312. 'category': 'server',
  313. 'type': 'im'}).up()
  314. .c('feature', {
  315. 'var': 'http://jabber.org/protocol/disco#info'}).up()
  316. .c('feature', {
  317. 'var': 'http://jabber.org/protocol/disco#items'});
  318. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  319. _converse.api.disco.entities.get().then(function(entities) {
  320. expect(entities.length).toBe(2);
  321. expect(_.includes(entities.pluck('jid'), 'localhost')).toBe(true);
  322. expect(_.includes(entities.pluck('jid'), 'dummy@localhost')).toBe(true);
  323. expect(entities.get(_converse.domain).features.length).toBe(2);
  324. expect(entities.get(_converse.domain).identities.length).toBe(1);
  325. return test_utils.waitUntil(function () {
  326. // Converse.js sees that the entity has a disco#items feature,
  327. // so it will make a query for it.
  328. return _.filter(IQ_stanzas, function (iq) {
  329. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  330. }).length > 0;
  331. }, 300);
  332. });
  333. }).then(function () {
  334. var stanza = _.filter(IQ_stanzas, function (iq) {
  335. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  336. })[0];
  337. var items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  338. stanza = $iq({
  339. 'type': 'result',
  340. 'from': 'localhost',
  341. 'to': 'dummy@localhost/resource',
  342. 'id': items_IQ_id
  343. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
  344. .c('item', {
  345. 'jid': 'upload.localhost',
  346. 'name': 'HTTP File Upload'});
  347. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  348. _converse.api.disco.entities.get().then(function (entities) {
  349. expect(entities.length).toBe(2);
  350. expect(entities.get('localhost').items.length).toBe(1);
  351. return test_utils.waitUntil(function () {
  352. // Converse.js sees that the entity has a disco#info feature,
  353. // so it will make a query for it.
  354. return _.filter(IQ_stanzas, function (iq) {
  355. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  356. }).length > 0;
  357. }, 300);
  358. });
  359. }).then(function () {
  360. var stanza = _.filter(IQ_stanzas, function (iq) {
  361. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  362. })[0];
  363. var IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  364. expect(stanza.toLocaleString()).toBe(
  365. "<iq from='dummy@localhost/resource' to='upload.localhost' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  366. "<query xmlns='http://jabber.org/protocol/disco#info'/>"+
  367. "</iq>");
  368. // Upload service responds and reports a maximum file size of 5MiB
  369. stanza = $iq({'type': 'result', 'to': 'dummy@localhost/resource', 'id': IQ_id, 'from': 'upload.localhost'})
  370. .c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  371. .c('identity', {'category':'store', 'type':'file', 'name':'HTTP File Upload'}).up()
  372. .c('feature', {'var':'urn:xmpp:http:upload:0'}).up()
  373. .c('x', {'type':'result', 'xmlns':'jabber:x:data'})
  374. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  375. .c('value').t('urn:xmpp:http:upload:0').up().up()
  376. .c('field', {'var':'max-file-size'})
  377. .c('value').t('5242880');
  378. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  379. _converse.api.disco.entities.get().then(function (entities) {
  380. expect(entities.get('localhost').items.get('upload.localhost').identities.where({'category': 'store'}).length).toBe(1);
  381. _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain).then(
  382. function (result) {
  383. test_utils.createContacts(_converse, 'current');
  384. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  385. test_utils.openChatBoxFor(_converse, contact_jid);
  386. var view = _converse.chatboxviews.get(contact_jid);
  387. var file = {
  388. 'type': 'image/jpeg',
  389. 'size': '5242881',
  390. 'lastModifiedDate': "",
  391. 'name': "my-juliet.jpg"
  392. };
  393. view.model.sendFiles([file]);
  394. return test_utils.waitUntil(function () {
  395. return view.el.querySelectorAll('.message').length;
  396. }).then(function () {
  397. const messages = view.el.querySelectorAll('.message.chat-error');
  398. expect(messages.length).toBe(1);
  399. expect(messages[0].textContent).toBe(
  400. 'The size of your file, my-juliet.jpg, exceeds the maximum allowed by your server, which is 5 MB.');
  401. done();
  402. });
  403. }
  404. );
  405. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  406. })
  407. });
  408. }));
  409. });
  410. });
  411. });
  412. });
  413. }));