http-file-upload.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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 = _.find(IQ_stanzas, function (iq) {
  40. return iq.nodeTree.querySelector(
  41. 'iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  42. });
  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 = _.find(IQ_stanzas, function (iq) {
  84. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  85. });
  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 = _.find(IQ_stanzas, function (iq) {
  110. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  111. });
  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 not supported", function () {
  164. describe("A file upload toolbar button", function () {
  165. it("does not appear in private chats", mock.initConverseWithAsync(function (done, _converse) {
  166. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  167. test_utils.createContacts(_converse, 'current');
  168. test_utils.openChatBoxFor(_converse, contact_jid);
  169. test_utils.waitUntilDiscoConfirmed(
  170. _converse, _converse.domain,
  171. [{'category': 'server', 'type':'IM'}],
  172. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  173. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], [], 'items').then(function () {
  174. var view = _converse.chatboxviews.get(contact_jid);
  175. expect(view.el.querySelector('.chat-toolbar .upload-file')).toBe(null);
  176. done();
  177. });
  178. });
  179. }));
  180. it("does not appear in MUC chats", mock.initConverseWithPromises(
  181. null, ['rosterGroupsFetched'], {},
  182. function (done, _converse) {
  183. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  184. test_utils.waitUntilDiscoConfirmed(
  185. _converse, _converse.domain,
  186. [{'category': 'server', 'type':'IM'}],
  187. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  188. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.localhost'], 'items').then(function () {
  189. test_utils.waitUntilDiscoConfirmed(_converse, 'upload.localhost', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
  190. var view = _converse.chatboxviews.get('lounge@localhost');
  191. expect(view.el.querySelector('.chat-toolbar .upload-file')).toBe(null);
  192. done();
  193. });
  194. });
  195. });
  196. });
  197. }));
  198. });
  199. });
  200. describe("When supported", function () {
  201. describe("A file upload toolbar button", function () {
  202. it("appears in private chats", mock.initConverseWithAsync(function (done, _converse) {
  203. test_utils.waitUntilDiscoConfirmed(
  204. _converse, _converse.domain,
  205. [{'category': 'server', 'type':'IM'}],
  206. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  207. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.localhost'], 'items').then(function () {
  208. test_utils.waitUntilDiscoConfirmed(_converse, 'upload.localhost', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
  209. test_utils.createContacts(_converse, 'current');
  210. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  211. test_utils.openChatBoxFor(_converse, contact_jid);
  212. var view = _converse.chatboxviews.get(contact_jid);
  213. test_utils.waitUntil(function () {
  214. return view.el.querySelector('.upload-file');
  215. }, 150).then(function () {
  216. expect(view.el.querySelector('.chat-toolbar .upload-file')).not.toBe(null);
  217. done();
  218. });
  219. });
  220. });
  221. });
  222. }));
  223. it("appears in MUC chats", mock.initConverseWithPromises(
  224. null, ['rosterGroupsFetched'], {},
  225. function (done, _converse) {
  226. test_utils.waitUntilDiscoConfirmed(
  227. _converse, _converse.domain,
  228. [{'category': 'server', 'type':'IM'}],
  229. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  230. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.localhost'], 'items').then(function () {
  231. test_utils.waitUntilDiscoConfirmed(_converse, 'upload.localhost', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
  232. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  233. var view = _converse.chatboxviews.get('lounge@localhost');
  234. test_utils.waitUntil(function () {
  235. return view.el.querySelector('.upload-file');
  236. }).then(function () {
  237. expect(view.el.querySelector('.chat-toolbar .upload-file')).not.toBe(null);
  238. done();
  239. });
  240. });
  241. });
  242. });
  243. });
  244. }));
  245. describe("when clicked and a file chosen", function () {
  246. it("is uploaded and sent out", mock.initConverseWithAsync(function (done, _converse) {
  247. test_utils.waitUntilDiscoConfirmed(
  248. _converse, _converse.domain,
  249. [{'category': 'server', 'type':'IM'}],
  250. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  251. var send_backup = XMLHttpRequest.prototype.send;
  252. var IQ_stanzas = _converse.connection.IQ_stanzas;
  253. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.tld'], 'items').then(function () {
  254. test_utils.waitUntilDiscoConfirmed(_converse, 'upload.montague.tld', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
  255. test_utils.createContacts(_converse, 'current');
  256. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  257. test_utils.openChatBoxFor(_converse, contact_jid);
  258. var view = _converse.chatboxviews.get(contact_jid);
  259. var file = {
  260. 'type': 'image/jpeg',
  261. 'size': '23456' ,
  262. 'lastModifiedDate': "",
  263. 'name': "my-juliet.jpg"
  264. };
  265. view.model.sendFiles([file]);
  266. return test_utils.waitUntil(function () {
  267. return _.filter(IQ_stanzas, function (iq) {
  268. return iq.nodeTree.querySelector('iq[to="upload.montague.tld"] request');
  269. }).length > 0;
  270. }).then(function () {
  271. var iq = IQ_stanzas.pop();
  272. expect(iq.toLocaleString()).toBe(
  273. "<iq from='dummy@localhost/resource' "+
  274. "to='upload.montague.tld' "+
  275. "type='get' "+
  276. "xmlns='jabber:client' "+
  277. "id='"+iq.nodeTree.getAttribute('id')+"'>"+
  278. "<request xmlns='urn:xmpp:http:upload:0' "+
  279. "filename='my-juliet.jpg' "+
  280. "size='23456' "+
  281. "content-type='image/jpeg'/>"+
  282. "</iq>");
  283. var base_url = document.URL.split(window.location.pathname)[0];
  284. var message = base_url+"/logo/conversejs-filled.svg";
  285. var stanza = Strophe.xmlHtmlNode(
  286. "<iq from='upload.montague.tld'"+
  287. " id='"+iq.nodeTree.getAttribute('id')+"'"+
  288. " to='dummy@localhost/resource'"+
  289. " type='result'>"+
  290. "<slot xmlns='urn:xmpp:http:upload:0'>"+
  291. " <put url='https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg'>"+
  292. " <header name='Authorization'>Basic Base64String==</header>"+
  293. " <header name='Cookie'>foo=bar; user=romeo</header>"+
  294. " </put>"+
  295. " <get url='"+message+"' />"+
  296. "</slot>"+
  297. "</iq>").firstElementChild;
  298. spyOn(XMLHttpRequest.prototype, 'send').and.callFake(function () {
  299. const message = view.model.messages.at(0);
  300. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0');
  301. message.set('progress', 0.5);
  302. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0.5');
  303. message.set('progress', 1);
  304. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('1');
  305. message.save({
  306. 'upload': _converse.SUCCESS,
  307. 'message': message.get('get')
  308. });
  309. });
  310. var sent_stanza;
  311. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  312. sent_stanza = stanza;
  313. });
  314. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  315. return test_utils.waitUntil(function () {
  316. return sent_stanza;
  317. }, 1000).then(function () {
  318. expect(sent_stanza.toLocaleString()).toBe(
  319. "<message from='dummy@localhost/resource' "+
  320. "to='irini.vlastuin@localhost' "+
  321. "type='chat' "+
  322. "id='"+sent_stanza.nodeTree.getAttribute('id')+"' xmlns='jabber:client'>"+
  323. "<body>"+message+"</body>"+
  324. "<active xmlns='http://jabber.org/protocol/chatstates'/>"+
  325. "<x xmlns='jabber:x:oob'>"+
  326. "<url>"+message+"</url>"+
  327. "</x>"+
  328. "</message>");
  329. return test_utils.waitUntil(function () {
  330. return view.el.querySelector('.chat-image');
  331. }, 1000);
  332. }).then(function () {
  333. // Check that the image renders
  334. expect(view.el.querySelector('.chat-message .chat-msg-content').innerHTML).toEqual(
  335. '<a target="_blank" rel="noopener" href="http://localhost:8000/logo/conversejs-filled.svg">'+
  336. '<img class="chat-image" src="http://localhost:8000/logo/conversejs-filled.svg"></a>')
  337. XMLHttpRequest.prototype.send = send_backup;
  338. done();
  339. });
  340. });
  341. });
  342. });
  343. });
  344. }));
  345. it("is uploaded and sent out from a groupchat", mock.initConverseWithAsync(function (done, _converse) {
  346. test_utils.waitUntilDiscoConfirmed(
  347. _converse, _converse.domain,
  348. [{'category': 'server', 'type':'IM'}],
  349. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  350. var send_backup = XMLHttpRequest.prototype.send;
  351. var IQ_stanzas = _converse.connection.IQ_stanzas;
  352. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.tld'], 'items').then(function () {
  353. test_utils.waitUntilDiscoConfirmed(_converse, 'upload.montague.tld', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
  354. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  355. var view = _converse.chatboxviews.get('lounge@localhost');
  356. var file = {
  357. 'type': 'image/jpeg',
  358. 'size': '23456' ,
  359. 'lastModifiedDate': "",
  360. 'name': "my-juliet.jpg"
  361. };
  362. view.model.sendFiles([file]);
  363. return test_utils.waitUntil(function () {
  364. return _.filter(IQ_stanzas, function (iq) {
  365. return iq.nodeTree.querySelector('iq[to="upload.montague.tld"] request');
  366. }).length > 0;
  367. }).then(function () {
  368. var iq = IQ_stanzas.pop();
  369. expect(iq.toLocaleString()).toBe(
  370. "<iq from='dummy@localhost/resource' "+
  371. "to='upload.montague.tld' "+
  372. "type='get' "+
  373. "xmlns='jabber:client' "+
  374. "id='"+iq.nodeTree.getAttribute('id')+"'>"+
  375. "<request xmlns='urn:xmpp:http:upload:0' "+
  376. "filename='my-juliet.jpg' "+
  377. "size='23456' "+
  378. "content-type='image/jpeg'/>"+
  379. "</iq>");
  380. var base_url = document.URL.split(window.location.pathname)[0];
  381. var message = base_url+"/logo/conversejs-filled.svg";
  382. var stanza = Strophe.xmlHtmlNode(
  383. "<iq from='upload.montague.tld'"+
  384. " id='"+iq.nodeTree.getAttribute('id')+"'"+
  385. " to='dummy@localhost/resource'"+
  386. " type='result'>"+
  387. "<slot xmlns='urn:xmpp:http:upload:0'>"+
  388. " <put url='https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg'>"+
  389. " <header name='Authorization'>Basic Base64String==</header>"+
  390. " <header name='Cookie'>foo=bar; user=romeo</header>"+
  391. " </put>"+
  392. " <get url='"+message+"' />"+
  393. "</slot>"+
  394. "</iq>").firstElementChild;
  395. spyOn(XMLHttpRequest.prototype, 'send').and.callFake(function () {
  396. const message = view.model.messages.at(0);
  397. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0');
  398. message.set('progress', 0.5);
  399. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0.5');
  400. message.set('progress', 1);
  401. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('1');
  402. message.save({
  403. 'upload': _converse.SUCCESS,
  404. 'message': message.get('get')
  405. });
  406. });
  407. var sent_stanza;
  408. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  409. sent_stanza = stanza;
  410. });
  411. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  412. return test_utils.waitUntil(function () {
  413. return sent_stanza;
  414. }, 1000).then(function () {
  415. expect(sent_stanza.toLocaleString()).toBe(
  416. "<message from='dummy@localhost/resource' "+
  417. "to='lounge@localhost' "+
  418. "type='groupchat' "+
  419. "id='"+sent_stanza.nodeTree.getAttribute('id')+"' xmlns='jabber:client'>"+
  420. "<body>"+message+"</body>"+
  421. "<active xmlns='http://jabber.org/protocol/chatstates'/>"+
  422. "<x xmlns='jabber:x:oob'>"+
  423. "<url>"+message+"</url>"+
  424. "</x>"+
  425. "</message>");
  426. return test_utils.waitUntil(function () {
  427. return view.el.querySelector('.chat-image');
  428. }, 1000);
  429. }).then(function () {
  430. // Check that the image renders
  431. expect(view.el.querySelector('.chat-message .chat-msg-content').innerHTML).toEqual(
  432. '<a target="_blank" rel="noopener" href="http://localhost:8000/logo/conversejs-filled.svg">'+
  433. '<img class="chat-image" src="http://localhost:8000/logo/conversejs-filled.svg"></a>')
  434. XMLHttpRequest.prototype.send = send_backup;
  435. done();
  436. });
  437. });
  438. });
  439. });
  440. });
  441. });
  442. }));
  443. it("shows and error message if the file is too large", mock.initConverseWithAsync(function (done, _converse) {
  444. var IQ_stanzas = _converse.connection.IQ_stanzas;
  445. var IQ_ids = _converse.connection.IQ_ids;
  446. var send_backup = XMLHttpRequest.prototype.send;
  447. test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], []).then(function () {
  448. test_utils.waitUntil(function () {
  449. return _.filter(IQ_stanzas, function (iq) {
  450. return iq.nodeTree.querySelector(
  451. 'iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  452. }).length > 0;
  453. }, 300).then(function () {
  454. var stanza = _.find(IQ_stanzas, function (iq) {
  455. return iq.nodeTree.querySelector(
  456. 'iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  457. });
  458. var info_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  459. stanza = $iq({
  460. 'type': 'result',
  461. 'from': 'localhost',
  462. 'to': 'dummy@localhost/resource',
  463. 'id': info_IQ_id
  464. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  465. .c('identity', {
  466. 'category': 'server',
  467. 'type': 'im'}).up()
  468. .c('feature', {
  469. 'var': 'http://jabber.org/protocol/disco#info'}).up()
  470. .c('feature', {
  471. 'var': 'http://jabber.org/protocol/disco#items'});
  472. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  473. _converse.api.disco.entities.get().then(function(entities) {
  474. expect(entities.length).toBe(2);
  475. expect(_.includes(entities.pluck('jid'), 'localhost')).toBe(true);
  476. expect(_.includes(entities.pluck('jid'), 'dummy@localhost')).toBe(true);
  477. expect(entities.get(_converse.domain).features.length).toBe(2);
  478. expect(entities.get(_converse.domain).identities.length).toBe(1);
  479. return test_utils.waitUntil(function () {
  480. // Converse.js sees that the entity has a disco#items feature,
  481. // so it will make a query for it.
  482. return _.filter(IQ_stanzas, function (iq) {
  483. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  484. }).length > 0;
  485. }, 300);
  486. });
  487. }).then(function () {
  488. var stanza = _.find(IQ_stanzas, function (iq) {
  489. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  490. });
  491. var items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  492. stanza = $iq({
  493. 'type': 'result',
  494. 'from': 'localhost',
  495. 'to': 'dummy@localhost/resource',
  496. 'id': items_IQ_id
  497. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
  498. .c('item', {
  499. 'jid': 'upload.localhost',
  500. 'name': 'HTTP File Upload'});
  501. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  502. _converse.api.disco.entities.get().then(function (entities) {
  503. expect(entities.length).toBe(2);
  504. expect(entities.get('localhost').items.length).toBe(1);
  505. return test_utils.waitUntil(function () {
  506. // Converse.js sees that the entity has a disco#info feature,
  507. // so it will make a query for it.
  508. return _.filter(IQ_stanzas, function (iq) {
  509. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  510. }).length > 0;
  511. }, 300);
  512. });
  513. }).then(function () {
  514. var stanza = _.find(IQ_stanzas, function (iq) {
  515. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  516. });
  517. var IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  518. expect(stanza.toLocaleString()).toBe(
  519. "<iq from='dummy@localhost/resource' to='upload.localhost' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  520. "<query xmlns='http://jabber.org/protocol/disco#info'/>"+
  521. "</iq>");
  522. // Upload service responds and reports a maximum file size of 5MiB
  523. stanza = $iq({'type': 'result', 'to': 'dummy@localhost/resource', 'id': IQ_id, 'from': 'upload.localhost'})
  524. .c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  525. .c('identity', {'category':'store', 'type':'file', 'name':'HTTP File Upload'}).up()
  526. .c('feature', {'var':'urn:xmpp:http:upload:0'}).up()
  527. .c('x', {'type':'result', 'xmlns':'jabber:x:data'})
  528. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  529. .c('value').t('urn:xmpp:http:upload:0').up().up()
  530. .c('field', {'var':'max-file-size'})
  531. .c('value').t('5242880');
  532. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  533. _converse.api.disco.entities.get().then(function (entities) {
  534. expect(entities.get('localhost').items.get('upload.localhost').identities.where({'category': 'store'}).length).toBe(1);
  535. _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain).then(
  536. function (result) {
  537. test_utils.createContacts(_converse, 'current');
  538. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  539. test_utils.openChatBoxFor(_converse, contact_jid);
  540. var view = _converse.chatboxviews.get(contact_jid);
  541. var file = {
  542. 'type': 'image/jpeg',
  543. 'size': '5242881',
  544. 'lastModifiedDate': "",
  545. 'name': "my-juliet.jpg"
  546. };
  547. view.model.sendFiles([file]);
  548. return test_utils.waitUntil(function () {
  549. return view.el.querySelectorAll('.message').length;
  550. }).then(function () {
  551. const messages = view.el.querySelectorAll('.message.chat-error');
  552. expect(messages.length).toBe(1);
  553. expect(messages[0].textContent).toBe(
  554. 'The size of your file, my-juliet.jpg, exceeds the maximum allowed by your server, which is 5 MB.');
  555. done();
  556. });
  557. }
  558. );
  559. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  560. })
  561. });
  562. }));
  563. });
  564. });
  565. describe("While a file is being uploaded", function () {
  566. it("shows a progress bar", mock.initConverseWithAsync(function (done, _converse) {
  567. test_utils.waitUntilDiscoConfirmed(
  568. _converse, _converse.domain,
  569. [{'category': 'server', 'type':'IM'}],
  570. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  571. var send_backup = XMLHttpRequest.prototype.send;
  572. var IQ_stanzas = _converse.connection.IQ_stanzas;
  573. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.tld'], 'items').then(function () {
  574. test_utils.waitUntilDiscoConfirmed(_converse, 'upload.montague.tld', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
  575. test_utils.createContacts(_converse, 'current');
  576. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  577. test_utils.openChatBoxFor(_converse, contact_jid);
  578. var view = _converse.chatboxviews.get(contact_jid);
  579. var file = {
  580. 'type': 'image/jpeg',
  581. 'size': '23456' ,
  582. 'lastModifiedDate': "",
  583. 'name': "my-juliet.jpg"
  584. };
  585. view.model.sendFiles([file]);
  586. return test_utils.waitUntil(function () {
  587. return _.filter(IQ_stanzas, function (iq) {
  588. return iq.nodeTree.querySelector('iq[to="upload.montague.tld"] request');
  589. }).length > 0;
  590. }).then(function () {
  591. var iq = IQ_stanzas.pop();
  592. expect(iq.toLocaleString()).toBe(
  593. "<iq from='dummy@localhost/resource' "+
  594. "to='upload.montague.tld' "+
  595. "type='get' "+
  596. "xmlns='jabber:client' "+
  597. "id='"+iq.nodeTree.getAttribute('id')+"'>"+
  598. "<request xmlns='urn:xmpp:http:upload:0' "+
  599. "filename='my-juliet.jpg' "+
  600. "size='23456' "+
  601. "content-type='image/jpeg'/>"+
  602. "</iq>");
  603. var base_url = document.URL.split(window.location.pathname)[0];
  604. var message = base_url+"/logo/conversejs-filled.svg";
  605. var stanza = Strophe.xmlHtmlNode(
  606. "<iq from='upload.montague.tld'"+
  607. " id='"+iq.nodeTree.getAttribute('id')+"'"+
  608. " to='dummy@localhost/resource'"+
  609. " type='result'>"+
  610. "<slot xmlns='urn:xmpp:http:upload:0'>"+
  611. " <put url='https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg'>"+
  612. " <header name='Authorization'>Basic Base64String==</header>"+
  613. " <header name='Cookie'>foo=bar; user=romeo</header>"+
  614. " </put>"+
  615. " <get url='"+message+"' />"+
  616. "</slot>"+
  617. "</iq>").firstElementChild;
  618. spyOn(XMLHttpRequest.prototype, 'send').and.callFake(function () {
  619. const message = view.model.messages.at(0);
  620. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0');
  621. message.set('progress', 0.5);
  622. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0.5');
  623. message.set('progress', 1);
  624. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('1');
  625. expect(view.el.querySelector('.chat-content .chat-msg-content').textContent).toBe('Uploading file: my-juliet.jpg, 22.91 KB');
  626. done();
  627. });
  628. var sent_stanza;
  629. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  630. sent_stanza = stanza;
  631. });
  632. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  633. });
  634. });
  635. });
  636. });
  637. }));
  638. });
  639. });
  640. });
  641. }));