http-file-upload.js 45 KB

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