http-file-upload.js 44 KB

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