http-file-upload.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. '<a href="http://localhost:8000/logo/conversejs-filled.svg" target="_blank" rel="noopener">'+
  335. '<img class="chat-image img-thumbnail" src="http://localhost:8000/logo/conversejs-filled.svg">'+
  336. '</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. 'oob_url': message.get('get'),
  405. 'message': message.get('get')
  406. });
  407. });
  408. var sent_stanza;
  409. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  410. sent_stanza = stanza;
  411. });
  412. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  413. return test_utils.waitUntil(function () {
  414. return sent_stanza;
  415. }, 1000).then(function () {
  416. expect(sent_stanza.toLocaleString()).toBe(
  417. "<message from='dummy@localhost/resource' "+
  418. "to='lounge@localhost' "+
  419. "type='groupchat' "+
  420. "id='"+sent_stanza.nodeTree.getAttribute('id')+"' xmlns='jabber:client'>"+
  421. "<body>"+message+"</body>"+
  422. "<active xmlns='http://jabber.org/protocol/chatstates'/>"+
  423. "<x xmlns='jabber:x:oob'>"+
  424. "<url>"+message+"</url>"+
  425. "</x>"+
  426. "</message>");
  427. return test_utils.waitUntil(function () {
  428. return view.el.querySelector('.chat-image');
  429. }, 1000);
  430. }).then(function () {
  431. // Check that the image renders
  432. expect(view.el.querySelector('.chat-msg .chat-msg-media').innerHTML.trim()).toEqual(
  433. '<a href="http://localhost:8000/logo/conversejs-filled.svg" target="_blank" rel="noopener">'+
  434. '<img class="chat-image img-thumbnail" src="http://localhost:8000/logo/conversejs-filled.svg"></a>')
  435. XMLHttpRequest.prototype.send = send_backup;
  436. done();
  437. });
  438. });
  439. });
  440. });
  441. });
  442. });
  443. }));
  444. it("shows and error message if the file is too large", mock.initConverseWithAsync(function (done, _converse) {
  445. var IQ_stanzas = _converse.connection.IQ_stanzas;
  446. var IQ_ids = _converse.connection.IQ_ids;
  447. var send_backup = XMLHttpRequest.prototype.send;
  448. test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], []).then(function () {
  449. test_utils.waitUntil(function () {
  450. return _.filter(IQ_stanzas, function (iq) {
  451. return iq.nodeTree.querySelector(
  452. 'iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  453. }).length > 0;
  454. }, 300).then(function () {
  455. var stanza = _.find(IQ_stanzas, function (iq) {
  456. return iq.nodeTree.querySelector(
  457. 'iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  458. });
  459. var info_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  460. stanza = $iq({
  461. 'type': 'result',
  462. 'from': 'localhost',
  463. 'to': 'dummy@localhost/resource',
  464. 'id': info_IQ_id
  465. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  466. .c('identity', {
  467. 'category': 'server',
  468. 'type': 'im'}).up()
  469. .c('feature', {
  470. 'var': 'http://jabber.org/protocol/disco#info'}).up()
  471. .c('feature', {
  472. 'var': 'http://jabber.org/protocol/disco#items'});
  473. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  474. _converse.api.disco.entities.get().then(function(entities) {
  475. expect(entities.length).toBe(2);
  476. expect(_.includes(entities.pluck('jid'), 'localhost')).toBe(true);
  477. expect(_.includes(entities.pluck('jid'), 'dummy@localhost')).toBe(true);
  478. expect(entities.get(_converse.domain).features.length).toBe(2);
  479. expect(entities.get(_converse.domain).identities.length).toBe(1);
  480. return test_utils.waitUntil(function () {
  481. // Converse.js sees that the entity has a disco#items feature,
  482. // so it will make a query for it.
  483. return _.filter(IQ_stanzas, function (iq) {
  484. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  485. }).length > 0;
  486. }, 300);
  487. });
  488. }).then(function () {
  489. var stanza = _.find(IQ_stanzas, function (iq) {
  490. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  491. });
  492. var items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  493. stanza = $iq({
  494. 'type': 'result',
  495. 'from': 'localhost',
  496. 'to': 'dummy@localhost/resource',
  497. 'id': items_IQ_id
  498. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
  499. .c('item', {
  500. 'jid': 'upload.localhost',
  501. 'name': 'HTTP File Upload'});
  502. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  503. _converse.api.disco.entities.get().then(function (entities) {
  504. expect(entities.length).toBe(2);
  505. expect(entities.get('localhost').items.length).toBe(1);
  506. return test_utils.waitUntil(function () {
  507. // Converse.js sees that the entity has a disco#info feature,
  508. // so it will make a query for it.
  509. return _.filter(IQ_stanzas, function (iq) {
  510. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  511. }).length > 0;
  512. }, 300);
  513. });
  514. }).then(function () {
  515. var stanza = _.find(IQ_stanzas, function (iq) {
  516. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  517. });
  518. var IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  519. expect(stanza.toLocaleString()).toBe(
  520. "<iq from='dummy@localhost/resource' to='upload.localhost' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  521. "<query xmlns='http://jabber.org/protocol/disco#info'/>"+
  522. "</iq>");
  523. // Upload service responds and reports a maximum file size of 5MiB
  524. stanza = $iq({'type': 'result', 'to': 'dummy@localhost/resource', 'id': IQ_id, 'from': 'upload.localhost'})
  525. .c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  526. .c('identity', {'category':'store', 'type':'file', 'name':'HTTP File Upload'}).up()
  527. .c('feature', {'var':'urn:xmpp:http:upload:0'}).up()
  528. .c('x', {'type':'result', 'xmlns':'jabber:x:data'})
  529. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  530. .c('value').t('urn:xmpp:http:upload:0').up().up()
  531. .c('field', {'var':'max-file-size'})
  532. .c('value').t('5242880');
  533. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  534. _converse.api.disco.entities.get().then(function (entities) {
  535. expect(entities.get('localhost').items.get('upload.localhost').identities.where({'category': 'store'}).length).toBe(1);
  536. _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain).then(
  537. function (result) {
  538. test_utils.createContacts(_converse, 'current');
  539. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  540. test_utils.openChatBoxFor(_converse, contact_jid);
  541. var view = _converse.chatboxviews.get(contact_jid);
  542. var file = {
  543. 'type': 'image/jpeg',
  544. 'size': '5242881',
  545. 'lastModifiedDate': "",
  546. 'name': "my-juliet.jpg"
  547. };
  548. view.model.sendFiles([file]);
  549. return test_utils.waitUntil(function () {
  550. return view.el.querySelectorAll('.message').length;
  551. }).then(function () {
  552. const messages = view.el.querySelectorAll('.message.chat-error');
  553. expect(messages.length).toBe(1);
  554. expect(messages[0].textContent).toBe(
  555. 'The size of your file, my-juliet.jpg, exceeds the maximum allowed by your server, which is 5 MB.');
  556. done();
  557. });
  558. }
  559. );
  560. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  561. })
  562. });
  563. }));
  564. });
  565. });
  566. describe("While a file is being uploaded", function () {
  567. it("shows a progress bar", mock.initConverseWithAsync(function (done, _converse) {
  568. test_utils.waitUntilDiscoConfirmed(
  569. _converse, _converse.domain,
  570. [{'category': 'server', 'type':'IM'}],
  571. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  572. var send_backup = XMLHttpRequest.prototype.send;
  573. var IQ_stanzas = _converse.connection.IQ_stanzas;
  574. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.tld'], 'items').then(function () {
  575. test_utils.waitUntilDiscoConfirmed(_converse, 'upload.montague.tld', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
  576. test_utils.createContacts(_converse, 'current');
  577. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  578. test_utils.openChatBoxFor(_converse, contact_jid);
  579. var view = _converse.chatboxviews.get(contact_jid);
  580. var file = {
  581. 'type': 'image/jpeg',
  582. 'size': '23456' ,
  583. 'lastModifiedDate': "",
  584. 'name': "my-juliet.jpg"
  585. };
  586. view.model.sendFiles([file]);
  587. return test_utils.waitUntil(function () {
  588. return _.filter(IQ_stanzas, function (iq) {
  589. return iq.nodeTree.querySelector('iq[to="upload.montague.tld"] request');
  590. }).length > 0;
  591. }).then(function () {
  592. var iq = IQ_stanzas.pop();
  593. expect(iq.toLocaleString()).toBe(
  594. "<iq from='dummy@localhost/resource' "+
  595. "to='upload.montague.tld' "+
  596. "type='get' "+
  597. "xmlns='jabber:client' "+
  598. "id='"+iq.nodeTree.getAttribute('id')+"'>"+
  599. "<request xmlns='urn:xmpp:http:upload:0' "+
  600. "filename='my-juliet.jpg' "+
  601. "size='23456' "+
  602. "content-type='image/jpeg'/>"+
  603. "</iq>");
  604. var base_url = document.URL.split(window.location.pathname)[0];
  605. var message = base_url+"/logo/conversejs-filled.svg";
  606. var stanza = Strophe.xmlHtmlNode(
  607. "<iq from='upload.montague.tld'"+
  608. " id='"+iq.nodeTree.getAttribute('id')+"'"+
  609. " to='dummy@localhost/resource'"+
  610. " type='result'>"+
  611. "<slot xmlns='urn:xmpp:http:upload:0'>"+
  612. " <put url='https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg'>"+
  613. " <header name='Authorization'>Basic Base64String==</header>"+
  614. " <header name='Cookie'>foo=bar; user=romeo</header>"+
  615. " </put>"+
  616. " <get url='"+message+"' />"+
  617. "</slot>"+
  618. "</iq>").firstElementChild;
  619. spyOn(XMLHttpRequest.prototype, 'send').and.callFake(function () {
  620. const message = view.model.messages.at(0);
  621. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0');
  622. message.set('progress', 0.5);
  623. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0.5');
  624. message.set('progress', 1);
  625. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('1');
  626. expect(view.el.querySelector('.chat-content .chat-msg-text').textContent).toBe('Uploading file: my-juliet.jpg, 22.91 KB');
  627. done();
  628. });
  629. var sent_stanza;
  630. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  631. sent_stanza = stanza;
  632. });
  633. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  634. });
  635. });
  636. });
  637. });
  638. }));
  639. });
  640. });
  641. });
  642. }));