http-file-upload.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. 'oob_url': message.get('get'),
  308. 'message': message.get('get')
  309. });
  310. });
  311. var sent_stanza;
  312. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  313. sent_stanza = stanza;
  314. });
  315. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  316. return test_utils.waitUntil(function () {
  317. return sent_stanza;
  318. }, 1000).then(function () {
  319. expect(sent_stanza.toLocaleString()).toBe(
  320. "<message from='dummy@localhost/resource' "+
  321. "to='irini.vlastuin@localhost' "+
  322. "type='chat' "+
  323. "id='"+sent_stanza.nodeTree.getAttribute('id')+"' xmlns='jabber:client'>"+
  324. "<body>"+message+"</body>"+
  325. "<active xmlns='http://jabber.org/protocol/chatstates'/>"+
  326. "<x xmlns='jabber:x:oob'>"+
  327. "<url>"+message+"</url>"+
  328. "</x>"+
  329. "</message>");
  330. return test_utils.waitUntil(function () {
  331. return view.el.querySelector('.chat-image');
  332. }, 1000);
  333. }).then(function () {
  334. // Check that the image renders
  335. expect(view.el.querySelector('.chat-msg .chat-msg-media').innerHTML.trim()).toEqual(
  336. '<a href="http://localhost:8000/logo/conversejs-filled.svg" target="_blank" rel="noopener">'+
  337. '<img class="chat-image img-thumbnail" src="http://localhost:8000/logo/conversejs-filled.svg">'+
  338. '</a>');
  339. XMLHttpRequest.prototype.send = send_backup;
  340. done();
  341. });
  342. });
  343. });
  344. });
  345. });
  346. }));
  347. it("is uploaded and sent out from a groupchat", mock.initConverseWithAsync(function (done, _converse) {
  348. test_utils.waitUntilDiscoConfirmed(
  349. _converse, _converse.domain,
  350. [{'category': 'server', 'type':'IM'}],
  351. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  352. var send_backup = XMLHttpRequest.prototype.send;
  353. var IQ_stanzas = _converse.connection.IQ_stanzas;
  354. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.tld'], 'items').then(function () {
  355. test_utils.waitUntilDiscoConfirmed(_converse, 'upload.montague.tld', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
  356. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  357. var view = _converse.chatboxviews.get('lounge@localhost');
  358. var file = {
  359. 'type': 'image/jpeg',
  360. 'size': '23456' ,
  361. 'lastModifiedDate': "",
  362. 'name': "my-juliet.jpg"
  363. };
  364. view.model.sendFiles([file]);
  365. return test_utils.waitUntil(function () {
  366. return _.filter(IQ_stanzas, function (iq) {
  367. return iq.nodeTree.querySelector('iq[to="upload.montague.tld"] request');
  368. }).length > 0;
  369. }).then(function () {
  370. var iq = IQ_stanzas.pop();
  371. expect(iq.toLocaleString()).toBe(
  372. "<iq from='dummy@localhost/resource' "+
  373. "to='upload.montague.tld' "+
  374. "type='get' "+
  375. "xmlns='jabber:client' "+
  376. "id='"+iq.nodeTree.getAttribute('id')+"'>"+
  377. "<request xmlns='urn:xmpp:http:upload:0' "+
  378. "filename='my-juliet.jpg' "+
  379. "size='23456' "+
  380. "content-type='image/jpeg'/>"+
  381. "</iq>");
  382. var base_url = document.URL.split(window.location.pathname)[0];
  383. var message = base_url+"/logo/conversejs-filled.svg";
  384. var stanza = Strophe.xmlHtmlNode(
  385. "<iq from='upload.montague.tld'"+
  386. " id='"+iq.nodeTree.getAttribute('id')+"'"+
  387. " to='dummy@localhost/resource'"+
  388. " type='result'>"+
  389. "<slot xmlns='urn:xmpp:http:upload:0'>"+
  390. " <put url='https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg'>"+
  391. " <header name='Authorization'>Basic Base64String==</header>"+
  392. " <header name='Cookie'>foo=bar; user=romeo</header>"+
  393. " </put>"+
  394. " <get url='"+message+"' />"+
  395. "</slot>"+
  396. "</iq>").firstElementChild;
  397. spyOn(XMLHttpRequest.prototype, 'send').and.callFake(function () {
  398. const message = view.model.messages.at(0);
  399. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0');
  400. message.set('progress', 0.5);
  401. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0.5');
  402. message.set('progress', 1);
  403. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('1');
  404. message.save({
  405. 'upload': _converse.SUCCESS,
  406. 'oob_url': message.get('get'),
  407. 'message': message.get('get')
  408. });
  409. });
  410. var sent_stanza;
  411. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  412. sent_stanza = stanza;
  413. });
  414. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  415. return test_utils.waitUntil(function () {
  416. return sent_stanza;
  417. }, 1000).then(function () {
  418. expect(sent_stanza.toLocaleString()).toBe(
  419. "<message from='dummy@localhost/resource' "+
  420. "to='lounge@localhost' "+
  421. "type='groupchat' "+
  422. "id='"+sent_stanza.nodeTree.getAttribute('id')+"' xmlns='jabber:client'>"+
  423. "<body>"+message+"</body>"+
  424. "<active xmlns='http://jabber.org/protocol/chatstates'/>"+
  425. "<x xmlns='jabber:x:oob'>"+
  426. "<url>"+message+"</url>"+
  427. "</x>"+
  428. "</message>");
  429. return test_utils.waitUntil(function () {
  430. return view.el.querySelector('.chat-image');
  431. }, 1000);
  432. }).then(function () {
  433. // Check that the image renders
  434. expect(view.el.querySelector('.chat-msg .chat-msg-media').innerHTML.trim()).toEqual(
  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. }));