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" id="`+IQ_id+`" to="upload.localhost" type="get" xmlns="jabber:client">`+
  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. `id="${iq.nodeTree.getAttribute("id")}" `+
  274. `to="upload.montague.tld" `+
  275. `type="get" `+
  276. `xmlns="jabber:client">`+
  277. `<request `+
  278. `content-type="image/jpeg" `+
  279. `filename="my-juliet.jpg" `+
  280. `size="23456" `+
  281. `xmlns="urn:xmpp:http:upload:0"/>`+
  282. `</iq>`);
  283. var base_url = document.URL.split(window.location.pathname)[0];
  284. var message = base_url+"/logo/conversejs-filled.svg";
  285. var stanza = Strophe.xmlHtmlNode(
  286. "<iq from='upload.montague.tld'"+
  287. " id='"+iq.nodeTree.getAttribute('id')+"'"+
  288. " to='dummy@localhost/resource'"+
  289. " type='result'>"+
  290. "<slot xmlns='urn:xmpp:http:upload:0'>"+
  291. " <put url='https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg'>"+
  292. " <header name='Authorization'>Basic Base64String==</header>"+
  293. " <header name='Cookie'>foo=bar; user=romeo</header>"+
  294. " </put>"+
  295. " <get url='"+message+"' />"+
  296. "</slot>"+
  297. "</iq>").firstElementChild;
  298. spyOn(XMLHttpRequest.prototype, 'send').and.callFake(function () {
  299. const message = view.model.messages.at(0);
  300. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0');
  301. message.set('progress', 0.5);
  302. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0.5');
  303. message.set('progress', 1);
  304. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('1');
  305. message.save({
  306. 'upload': _converse.SUCCESS,
  307. 'oob_url': message.get('get'),
  308. 'message': message.get('get')
  309. });
  310. });
  311. var sent_stanza;
  312. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  313. sent_stanza = stanza;
  314. });
  315. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  316. return test_utils.waitUntil(function () {
  317. return sent_stanza;
  318. }, 1000).then(function () {
  319. expect(sent_stanza.toLocaleString()).toBe(
  320. `<message from="dummy@localhost/resource" `+
  321. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  322. `to="irini.vlastuin@localhost" `+
  323. `type="chat" `+
  324. `xmlns="jabber:client">`+
  325. `<body>${message}</body>`+
  326. `<active xmlns="http://jabber.org/protocol/chatstates"/>`+
  327. `<x xmlns="jabber:x:oob">`+
  328. `<url>${message}</url>`+
  329. `</x>`+
  330. `</message>`);
  331. return test_utils.waitUntil(() => view.el.querySelector('.chat-image'), 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. `id="${iq.nodeTree.getAttribute("id")}" `+
  373. `to="upload.montague.tld" `+
  374. `type="get" `+
  375. `xmlns="jabber:client">`+
  376. `<request `+
  377. `content-type="image/jpeg" `+
  378. `filename="my-juliet.jpg" `+
  379. `size="23456" `+
  380. `xmlns="urn:xmpp:http:upload:0"/>`+
  381. `</iq>`);
  382. var base_url = document.URL.split(window.location.pathname)[0];
  383. var message = base_url+"/logo/conversejs-filled.svg";
  384. var stanza = Strophe.xmlHtmlNode(
  385. "<iq from='upload.montague.tld'"+
  386. " id='"+iq.nodeTree.getAttribute('id')+"'"+
  387. " to='dummy@localhost/resource'"+
  388. " type='result'>"+
  389. "<slot xmlns='urn:xmpp:http:upload:0'>"+
  390. " <put url='https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg'>"+
  391. " <header name='Authorization'>Basic Base64String==</header>"+
  392. " <header name='Cookie'>foo=bar; user=romeo</header>"+
  393. " </put>"+
  394. " <get url='"+message+"' />"+
  395. "</slot>"+
  396. "</iq>").firstElementChild;
  397. spyOn(XMLHttpRequest.prototype, 'send').and.callFake(function () {
  398. const message = view.model.messages.at(0);
  399. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0');
  400. message.set('progress', 0.5);
  401. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0.5');
  402. message.set('progress', 1);
  403. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('1');
  404. message.save({
  405. 'upload': _converse.SUCCESS,
  406. 'oob_url': message.get('get'),
  407. 'message': message.get('get')
  408. });
  409. });
  410. var sent_stanza;
  411. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  412. sent_stanza = stanza;
  413. });
  414. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  415. return test_utils.waitUntil(() => sent_stanza, 1000).then(function () {
  416. expect(sent_stanza.toLocaleString()).toBe(
  417. `<message `+
  418. `from="dummy@localhost/resource" `+
  419. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  420. `to="lounge@localhost" `+
  421. `type="groupchat" `+
  422. `xmlns="jabber:client">`+
  423. `<body>${message}</body>`+
  424. `<active xmlns="http://jabber.org/protocol/chatstates"/>`+
  425. `<x xmlns="jabber:x:oob">`+
  426. `<url>${message}</url>`+
  427. `</x>`+
  428. `</message>`);
  429. return test_utils.waitUntil(() => view.el.querySelector('.chat-image'), 1000);
  430. }).then(function () {
  431. // Check that the image renders
  432. expect(view.el.querySelector('.chat-msg .chat-msg__media').innerHTML.trim()).toEqual(
  433. `<!-- src/templates/image.html -->\n`+
  434. `<a href="${window.location.origin}/logo/conversejs-filled.svg" target="_blank" rel="noopener">`+
  435. `<img class="chat-image img-thumbnail" src="${window.location.origin}/logo/conversejs-filled.svg">`+
  436. `</a>`);
  437. XMLHttpRequest.prototype.send = send_backup;
  438. done();
  439. });
  440. });
  441. });
  442. });
  443. });
  444. });
  445. }));
  446. it("shows an error message if the file is too large", mock.initConverseWithAsync(function (done, _converse) {
  447. const IQ_stanzas = _converse.connection.IQ_stanzas;
  448. const IQ_ids = _converse.connection.IQ_ids;
  449. const send_backup = XMLHttpRequest.prototype.send;
  450. let view, contact_jid;
  451. test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], [])
  452. .then(() => test_utils.waitUntil(() => _.filter(
  453. IQ_stanzas, (iq) => iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]')).length
  454. )).then(() => {
  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. return _converse.api.disco.entities.get();
  475. }).then(function (entities) {
  476. expect(entities.length).toBe(2);
  477. expect(_.includes(entities.pluck('jid'), 'localhost')).toBe(true);
  478. expect(_.includes(entities.pluck('jid'), 'dummy@localhost')).toBe(true);
  479. expect(entities.get(_converse.domain).features.length).toBe(2);
  480. expect(entities.get(_converse.domain).identities.length).toBe(1);
  481. return test_utils.waitUntil(function () {
  482. // Converse.js sees that the entity has a disco#items feature,
  483. // so it will make a query for it.
  484. return _.filter(IQ_stanzas, function (iq) {
  485. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  486. }).length > 0;
  487. }, 300);
  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" id="${IQ_id}" to="upload.localhost" type="get" xmlns="jabber:client">`+
  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. return _converse.api.disco.entities.get();
  535. }).then(function (entities) {
  536. expect(entities.get('localhost').items.get('upload.localhost').identities.where({'category': 'store'}).length).toBe(1);
  537. return _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain);
  538. }).then(function (result) {
  539. test_utils.createContacts(_converse, 'current');
  540. _converse.emit('rosterContactsFetched');
  541. contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  542. return test_utils.openChatBoxFor(_converse, contact_jid);
  543. }).then(() => {
  544. view = _converse.chatboxviews.get(contact_jid);
  545. var file = {
  546. 'type': 'image/jpeg',
  547. 'size': '5242881',
  548. 'lastModifiedDate': "",
  549. 'name': "my-juliet.jpg"
  550. };
  551. view.model.sendFiles([file]);
  552. return test_utils.waitUntil(() => view.el.querySelectorAll('.message').length)
  553. }).then(function () {
  554. const messages = view.el.querySelectorAll('.message.chat-error');
  555. expect(messages.length).toBe(1);
  556. expect(messages[0].textContent).toBe(
  557. 'The size of your file, my-juliet.jpg, exceeds the maximum allowed by your server, which is 5 MB.');
  558. done();
  559. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
  560. }));
  561. });
  562. });
  563. describe("While a file is being uploaded", function () {
  564. it("shows a progress bar", mock.initConverseWithPromises(
  565. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, function (done, _converse) {
  566. test_utils.waitUntilDiscoConfirmed(
  567. _converse, _converse.domain,
  568. [{'category': 'server', 'type':'IM'}],
  569. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  570. var send_backup = XMLHttpRequest.prototype.send;
  571. var IQ_stanzas = _converse.connection.IQ_stanzas;
  572. let view, contact_jid;
  573. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.tld'], 'items')
  574. .then(() => test_utils.waitUntilDiscoConfirmed(_converse, 'upload.montague.tld', [], [Strophe.NS.HTTPUPLOAD], []))
  575. .then(() => {
  576. test_utils.createContacts(_converse, 'current');
  577. _converse.emit('rosterContactsFetched');
  578. contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  579. return test_utils.openChatBoxFor(_converse, contact_jid);
  580. }).then(() => {
  581. view = _converse.chatboxviews.get(contact_jid);
  582. const file = {
  583. 'type': 'image/jpeg',
  584. 'size': '23456' ,
  585. 'lastModifiedDate': "",
  586. 'name': "my-juliet.jpg"
  587. };
  588. view.model.sendFiles([file]);
  589. return test_utils.waitUntil(() => _.filter(IQ_stanzas, (iq) => iq.nodeTree.querySelector('iq[to="upload.montague.tld"] request')).length)
  590. }).then(function () {
  591. const iq = IQ_stanzas.pop();
  592. expect(iq.toLocaleString()).toBe(
  593. `<iq from="dummy@localhost/resource" `+
  594. `id="${iq.nodeTree.getAttribute("id")}" `+
  595. `to="upload.montague.tld" `+
  596. `type="get" `+
  597. `xmlns="jabber:client">`+
  598. `<request `+
  599. `content-type="image/jpeg" `+
  600. `filename="my-juliet.jpg" `+
  601. `size="23456" `+
  602. `xmlns="urn:xmpp:http:upload:0"/>`+
  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. }));