http-file-upload.js 46 KB

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