http-file-upload.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*global mock, converse, _ */
  2. const Strophe = converse.env.Strophe;
  3. const $iq = converse.env.$iq;
  4. const sizzle = converse.env.sizzle;
  5. const u = converse.env.utils;
  6. describe("XEP-0363: HTTP File Upload", function () {
  7. describe("Discovering support", function () {
  8. it("is done automatically",
  9. mock.initConverse(
  10. ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  11. async function (done, _converse) {
  12. const IQ_stanzas = _converse.connection.IQ_stanzas;
  13. await mock.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], []);
  14. let selector = 'iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#info"]';
  15. let stanza = await u.waitUntil(() => IQ_stanzas.find(iq => iq.querySelector(selector)), 1000);
  16. /* <iq type='result'
  17. * from='plays.shakespeare.lit'
  18. * to='romeo@montague.net/orchard'
  19. * id='info1'>
  20. * <query xmlns='http://jabber.org/protocol/disco#info'>
  21. * <identity
  22. * category='server'
  23. * type='im'/>
  24. * <feature var='http://jabber.org/protocol/disco#info'/>
  25. * <feature var='http://jabber.org/protocol/disco#items'/>
  26. * </query>
  27. * </iq>
  28. */
  29. stanza = $iq({
  30. 'type': 'result',
  31. 'from': 'montague.lit',
  32. 'to': 'romeo@montague.lit/orchard',
  33. 'id': stanza.getAttribute('id'),
  34. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  35. .c('identity', {
  36. 'category': 'server',
  37. 'type': 'im'}).up()
  38. .c('feature', {
  39. 'var': 'http://jabber.org/protocol/disco#info'}).up()
  40. .c('feature', {
  41. 'var': 'http://jabber.org/protocol/disco#items'});
  42. _converse.connection._dataRecv(mock.createRequest(stanza));
  43. let entities = await _converse.api.disco.entities.get();
  44. expect(entities.length).toBe(2);
  45. expect(entities.pluck('jid').includes('montague.lit')).toBe(true);
  46. expect(entities.pluck('jid').includes('romeo@montague.lit')).toBe(true);
  47. expect(entities.get(_converse.domain).features.length).toBe(2);
  48. expect(entities.get(_converse.domain).identities.length).toBe(1);
  49. // Converse.js sees that the entity has a disco#items feature,
  50. // so it will make a query for it.
  51. selector = 'iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#items"]';
  52. await u.waitUntil(() => IQ_stanzas.filter(iq => iq.querySelector(selector)).length, 1000);
  53. /* <iq from='montague.tld'
  54. * id='step_01'
  55. * to='romeo@montague.tld/garden'
  56. * type='result'>
  57. * <query xmlns='http://jabber.org/protocol/disco#items'>
  58. * <item jid='upload.montague.tld' name='HTTP File Upload' />
  59. * <item jid='conference.montague.tld' name='Chatroom Service' />
  60. * </query>
  61. * </iq>
  62. */
  63. selector = 'iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#items"]';
  64. stanza = IQ_stanzas.find(iq => iq.querySelector(selector), 500);
  65. stanza = $iq({
  66. 'type': 'result',
  67. 'from': 'montague.lit',
  68. 'to': 'romeo@montague.lit/orchard',
  69. 'id': stanza.getAttribute('id'),
  70. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
  71. .c('item', {
  72. 'jid': 'upload.montague.lit',
  73. 'name': 'HTTP File Upload'});
  74. _converse.connection._dataRecv(mock.createRequest(stanza));
  75. _converse.api.disco.entities.get().then(entities => {
  76. expect(entities.length).toBe(2);
  77. expect(entities.get('montague.lit').items.length).toBe(1);
  78. // Converse.js sees that the entity has a disco#info feature, so it will make a query for it.
  79. const selector = 'iq[to="upload.montague.lit"] query[xmlns="http://jabber.org/protocol/disco#info"]';
  80. return u.waitUntil(() => IQ_stanzas.filter(iq => iq.querySelector(selector)).length > 0);
  81. });
  82. selector = 'iq[to="upload.montague.lit"] query[xmlns="http://jabber.org/protocol/disco#info"]';
  83. stanza = await u.waitUntil(() => IQ_stanzas.filter(iq => iq.querySelector(selector)).pop(), 1000);
  84. expect(Strophe.serialize(stanza)).toBe(
  85. `<iq from="romeo@montague.lit/orchard" id="`+stanza.getAttribute('id')+`" to="upload.montague.lit" type="get" xmlns="jabber:client">`+
  86. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  87. `</iq>`);
  88. // Upload service responds and reports a maximum file size of 5MiB
  89. /* <iq from='upload.montague.tld'
  90. * id='step_02'
  91. * to='romeo@montague.tld/garden'
  92. * type='result'>
  93. * <query xmlns='http://jabber.org/protocol/disco#info'>
  94. * <identity category='store'
  95. * type='file'
  96. * name='HTTP File Upload' />
  97. * <feature var='urn:xmpp:http:upload:0' />
  98. * <x type='result' xmlns='jabber:x:data'>
  99. * <field var='FORM_TYPE' type='hidden'>
  100. * <value>urn:xmpp:http:upload:0</value>
  101. * </field>
  102. * <field var='max-file-size'>
  103. * <value>5242880</value>
  104. * </field>
  105. * </x>
  106. * </query>
  107. * </iq>
  108. */
  109. stanza = $iq({'type': 'result', 'to': 'romeo@montague.lit/orchard', 'id': stanza.getAttribute('id'), 'from': 'upload.montague.lit'})
  110. .c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  111. .c('identity', {'category':'store', 'type':'file', 'name':'HTTP File Upload'}).up()
  112. .c('feature', {'var':'urn:xmpp:http:upload:0'}).up()
  113. .c('x', {'type':'result', 'xmlns':'jabber:x:data'})
  114. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  115. .c('value').t('urn:xmpp:http:upload:0').up().up()
  116. .c('field', {'var':'max-file-size'})
  117. .c('value').t('5242880');
  118. _converse.connection._dataRecv(mock.createRequest(stanza));
  119. entities = await _converse.api.disco.entities.get();
  120. expect(entities.get('montague.lit').items.get('upload.montague.lit').identities.where({'category': 'store'}).length).toBe(1);
  121. const supported = await _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain);
  122. expect(supported).toBe(true);
  123. const features = await _converse.api.disco.features.get(Strophe.NS.HTTPUPLOAD, _converse.domain);
  124. expect(features.length).toBe(1);
  125. expect(features[0].get('jid')).toBe('upload.montague.lit');
  126. expect(features[0].dataforms.where({'FORM_TYPE': {value: "urn:xmpp:http:upload:0", type: "hidden"}}).length).toBe(1);
  127. done();
  128. }));
  129. });
  130. describe("When not supported", function () {
  131. describe("A file upload toolbar button", function () {
  132. it("does not appear in private chats",
  133. mock.initConverse([], {}, async function (done, _converse) {
  134. await mock.waitForRoster(_converse, 'current', 3);
  135. mock.openControlBox(_converse);
  136. const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  137. await mock.openChatBoxFor(_converse, contact_jid);
  138. await mock.waitUntilDiscoConfirmed(
  139. _converse, _converse.domain,
  140. [{'category': 'server', 'type':'IM'}],
  141. ['http://jabber.org/protocol/disco#items'], [], 'info');
  142. await mock.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], [], 'items');
  143. const view = _converse.chatboxviews.get(contact_jid);
  144. expect(view.el.querySelector('.chat-toolbar .fileupload')).toBe(null);
  145. done();
  146. }));
  147. it("does not appear in MUC chats", mock.initConverse(
  148. ['rosterGroupsFetched'], {},
  149. async (done, _converse) => {
  150. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  151. mock.waitUntilDiscoConfirmed(
  152. _converse, _converse.domain,
  153. [{'category': 'server', 'type':'IM'}],
  154. ['http://jabber.org/protocol/disco#items'], [], 'info');
  155. await mock.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], [], 'items');
  156. const view = _converse.chatboxviews.get('lounge@montague.lit');
  157. await u.waitUntil(() => view.el.querySelector('.chat-toolbar .fileupload') === null);
  158. expect(1).toBe(1);
  159. done();
  160. }));
  161. });
  162. });
  163. describe("When supported", function () {
  164. describe("A file upload toolbar button", function () {
  165. it("appears in private chats", mock.initConverse(async (done, _converse) => {
  166. await mock.waitUntilDiscoConfirmed(
  167. _converse, _converse.domain,
  168. [{'category': 'server', 'type':'IM'}],
  169. ['http://jabber.org/protocol/disco#items'], [], 'info');
  170. await mock.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.lit'], 'items')
  171. await mock.waitUntilDiscoConfirmed(_converse, 'upload.montague.lit', [], [Strophe.NS.HTTPUPLOAD], []);
  172. await mock.waitForRoster(_converse, 'current', 3);
  173. const contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  174. await mock.openChatBoxFor(_converse, contact_jid);
  175. const view = _converse.chatboxviews.get(contact_jid);
  176. const el = await u.waitUntil(() => view.el.querySelector('.chat-toolbar .fileupload'));
  177. expect(el).not.toEqual(null);
  178. done();
  179. }));
  180. it("appears in MUC chats", mock.initConverse(
  181. ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  182. async (done, _converse) => {
  183. await mock.waitUntilDiscoConfirmed(
  184. _converse, _converse.domain,
  185. [{'category': 'server', 'type':'IM'}],
  186. ['http://jabber.org/protocol/disco#items'], [], 'info');
  187. await mock.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.lit'], 'items');
  188. await mock.waitUntilDiscoConfirmed(_converse, 'upload.montague.lit', [], [Strophe.NS.HTTPUPLOAD], []);
  189. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  190. await u.waitUntil(() => _converse.chatboxviews.get('lounge@montague.lit').el.querySelector('.fileupload'));
  191. const view = _converse.chatboxviews.get('lounge@montague.lit');
  192. expect(view.el.querySelector('.chat-toolbar .fileupload')).not.toBe(null);
  193. done();
  194. }));
  195. describe("when clicked and a file chosen", function () {
  196. it("is uploaded and sent out", mock.initConverse(
  197. ['rosterGroupsFetched', 'chatBoxesFetched'], {} ,async (done, _converse) => {
  198. const base_url = 'https://conversejs.org';
  199. await mock.waitUntilDiscoConfirmed(
  200. _converse, _converse.domain,
  201. [{'category': 'server', 'type':'IM'}],
  202. ['http://jabber.org/protocol/disco#items'], [], 'info');
  203. const send_backup = XMLHttpRequest.prototype.send;
  204. const IQ_stanzas = _converse.connection.IQ_stanzas;
  205. await mock.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.tld'], 'items');
  206. await mock.waitUntilDiscoConfirmed(_converse, 'upload.montague.tld', [], [Strophe.NS.HTTPUPLOAD], []);
  207. await mock.waitForRoster(_converse, 'current');
  208. const contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  209. await mock.openChatBoxFor(_converse, contact_jid);
  210. const view = _converse.chatboxviews.get(contact_jid);
  211. const file = {
  212. 'type': 'image/jpeg',
  213. 'size': '23456' ,
  214. 'lastModifiedDate': "",
  215. 'name': "my-juliet.jpg"
  216. };
  217. view.model.sendFiles([file]);
  218. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  219. await u.waitUntil(() => _.filter(IQ_stanzas, iq => iq.querySelector('iq[to="upload.montague.tld"] request')).length);
  220. const iq = IQ_stanzas.pop();
  221. expect(Strophe.serialize(iq)).toBe(
  222. `<iq from="romeo@montague.lit/orchard" `+
  223. `id="${iq.getAttribute("id")}" `+
  224. `to="upload.montague.tld" `+
  225. `type="get" `+
  226. `xmlns="jabber:client">`+
  227. `<request `+
  228. `content-type="image/jpeg" `+
  229. `filename="my-juliet.jpg" `+
  230. `size="23456" `+
  231. `xmlns="urn:xmpp:http:upload:0"/>`+
  232. `</iq>`);
  233. const message = base_url+"/logo/conversejs-filled.svg";
  234. const stanza = u.toStanza(`
  235. <iq from="upload.montague.tld"
  236. id="${iq.getAttribute("id")}"
  237. to="romeo@montague.lit/orchard"
  238. type="result">
  239. <slot xmlns="urn:xmpp:http:upload:0">
  240. <put url="https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg">
  241. <header name="Authorization">Basic Base64String==</header>
  242. <header name="Cookie">foo=bar; user=romeo</header>
  243. </put>
  244. <get url="${message}" />
  245. </slot>
  246. </iq>`);
  247. spyOn(XMLHttpRequest.prototype, 'send').and.callFake(function () {
  248. const message = view.model.messages.at(0);
  249. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0');
  250. message.set('progress', 0.5);
  251. u.waitUntil(() => view.el.querySelector('.chat-content progress').getAttribute('value') === '0.5')
  252. .then(() => {
  253. message.set('progress', 1);
  254. u.waitUntil(() => view.el.querySelector('.chat-content progress').getAttribute('value') === '1')
  255. }).then(() => {
  256. message.save({
  257. 'upload': _converse.SUCCESS,
  258. 'oob_url': message.get('get'),
  259. 'message': message.get('get')
  260. });
  261. return new Promise(resolve => view.model.messages.once('rendered', resolve));
  262. });
  263. });
  264. let sent_stanza;
  265. spyOn(_converse.connection, 'send').and.callFake(stanza => (sent_stanza = stanza));
  266. _converse.connection._dataRecv(mock.createRequest(stanza));
  267. await u.waitUntil(() => sent_stanza, 1000);
  268. expect(sent_stanza.toLocaleString()).toBe(
  269. `<message from="romeo@montague.lit/orchard" `+
  270. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  271. `to="lady.montague@montague.lit" `+
  272. `type="chat" `+
  273. `xmlns="jabber:client">`+
  274. `<body>${message}</body>`+
  275. `<active xmlns="http://jabber.org/protocol/chatstates"/>`+
  276. `<request xmlns="urn:xmpp:receipts"/>`+
  277. `<x xmlns="jabber:x:oob">`+
  278. `<url>${message}</url>`+
  279. `</x>`+
  280. `<origin-id id="${sent_stanza.nodeTree.querySelector('origin-id').getAttribute("id")}" xmlns="urn:xmpp:sid:0"/>`+
  281. `</message>`);
  282. const img_link_el = await u.waitUntil(() => view.el.querySelector('converse-chat-message-body .chat-image__link'), 1000);
  283. // Check that the image renders
  284. expect(img_link_el.outerHTML.replace(/<!---->/g, '').trim()).toEqual(
  285. `<a class="chat-image__link" target="_blank" rel="noopener" href="${base_url}/logo/conversejs-filled.svg">`+
  286. `<img class="chat-image img-thumbnail" src="${base_url}/logo/conversejs-filled.svg"></a>`);
  287. expect(view.el.querySelector('.chat-msg .chat-msg__media').innerHTML.replace(/<!---->/g, '').trim()).toEqual(
  288. `<a target="_blank" rel="noopener" href="${base_url}/logo/conversejs-filled.svg">`+
  289. `Download image file "conversejs-filled.svg"</a>`);
  290. XMLHttpRequest.prototype.send = send_backup;
  291. done();
  292. }));
  293. it("is uploaded and sent out from a groupchat", mock.initConverse(async (done, _converse) => {
  294. const base_url = 'https://conversejs.org';
  295. await mock.waitUntilDiscoConfirmed(
  296. _converse, _converse.domain,
  297. [{'category': 'server', 'type':'IM'}],
  298. ['http://jabber.org/protocol/disco#items'], [], 'info');
  299. const send_backup = XMLHttpRequest.prototype.send;
  300. const IQ_stanzas = _converse.connection.IQ_stanzas;
  301. await mock.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.tld'], 'items');
  302. await mock.waitUntilDiscoConfirmed(_converse, 'upload.montague.tld', [], [Strophe.NS.HTTPUPLOAD], []);
  303. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  304. // Wait until MAM query has been sent out
  305. const sent_stanzas = _converse.connection.sent_stanzas;
  306. await u.waitUntil(() => sent_stanzas.filter(s => sizzle(`[xmlns="${Strophe.NS.MAM}"]`, s).length).pop());
  307. const view = _converse.chatboxviews.get('lounge@montague.lit');
  308. const file = {
  309. 'type': 'image/jpeg',
  310. 'size': '23456' ,
  311. 'lastModifiedDate': "",
  312. 'name': "my-juliet.jpg"
  313. };
  314. view.model.sendFiles([file]);
  315. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  316. await u.waitUntil(() => _.filter(IQ_stanzas, iq => iq.querySelector('iq[to="upload.montague.tld"] request')).length);
  317. const iq = IQ_stanzas.pop();
  318. expect(Strophe.serialize(iq)).toBe(
  319. `<iq from="romeo@montague.lit/orchard" `+
  320. `id="${iq.getAttribute("id")}" `+
  321. `to="upload.montague.tld" `+
  322. `type="get" `+
  323. `xmlns="jabber:client">`+
  324. `<request `+
  325. `content-type="image/jpeg" `+
  326. `filename="my-juliet.jpg" `+
  327. `size="23456" `+
  328. `xmlns="urn:xmpp:http:upload:0"/>`+
  329. `</iq>`);
  330. const message = base_url+"/logo/conversejs-filled.svg";
  331. const stanza = u.toStanza(`
  332. <iq from='upload.montague.tld'
  333. id="${iq.getAttribute('id')}"
  334. to='romeo@montague.lit/orchard'
  335. type='result'>
  336. <slot xmlns='urn:xmpp:http:upload:0'>
  337. <put url='https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg'>
  338. <header name='Authorization'>Basic Base64String==</header>
  339. <header name='Cookie'>foo=bar; user=romeo</header>
  340. </put>
  341. <get url="${message}" />
  342. </slot>
  343. </iq>`);
  344. spyOn(XMLHttpRequest.prototype, 'send').and.callFake(function () {
  345. const message = view.model.messages.at(0);
  346. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0');
  347. message.set('progress', 0.5);
  348. u.waitUntil(() => view.el.querySelector('.chat-content progress').getAttribute('value') === '0.5')
  349. .then(() => {
  350. message.set('progress', 1);
  351. u.waitUntil(() => view.el.querySelector('.chat-content progress')?.getAttribute('value') === '1')
  352. }).then(() => {
  353. message.save({
  354. 'upload': _converse.SUCCESS,
  355. 'oob_url': message.get('get'),
  356. 'message': message.get('get')
  357. });
  358. return new Promise(resolve => view.model.messages.once('rendered', resolve));
  359. });
  360. });
  361. let sent_stanza;
  362. spyOn(_converse.connection, 'send').and.callFake(stanza => (sent_stanza = stanza));
  363. _converse.connection._dataRecv(mock.createRequest(stanza));
  364. await u.waitUntil(() => sent_stanza, 1000);
  365. expect(sent_stanza.toLocaleString()).toBe(
  366. `<message `+
  367. `from="romeo@montague.lit/orchard" `+
  368. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  369. `to="lounge@montague.lit" `+
  370. `type="groupchat" `+
  371. `xmlns="jabber:client">`+
  372. `<body>${message}</body>`+
  373. `<active xmlns="http://jabber.org/protocol/chatstates"/>`+
  374. `<x xmlns="jabber:x:oob">`+
  375. `<url>${message}</url>`+
  376. `</x>`+
  377. `<origin-id id="${sent_stanza.nodeTree.querySelector('origin-id').getAttribute("id")}" xmlns="urn:xmpp:sid:0"/>`+
  378. `</message>`);
  379. const img_link_el = await u.waitUntil(() => view.el.querySelector('converse-chat-message-body .chat-image__link'), 1000);
  380. // Check that the image renders
  381. expect(img_link_el.outerHTML.replace(/<!---->/g, '').trim()).toEqual(
  382. `<a class="chat-image__link" target="_blank" rel="noopener" href="${base_url}/logo/conversejs-filled.svg">`+
  383. `<img class="chat-image img-thumbnail" src="${base_url}/logo/conversejs-filled.svg"></a>`);
  384. expect(view.el.querySelector('.chat-msg .chat-msg__media').innerHTML.replace(/<!---->/g, '').trim()).toEqual(
  385. `<a target="_blank" rel="noopener" href="${base_url}/logo/conversejs-filled.svg">`+
  386. `Download image file "conversejs-filled.svg"</a>`);
  387. XMLHttpRequest.prototype.send = send_backup;
  388. done();
  389. }));
  390. it("shows an error message if the file is too large",
  391. mock.initConverse([], {}, async function (done, _converse) {
  392. const IQ_stanzas = _converse.connection.IQ_stanzas;
  393. const IQ_ids = _converse.connection.IQ_ids;
  394. await mock.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], []);
  395. await u.waitUntil(() => _.filter(
  396. IQ_stanzas,
  397. iq => iq.querySelector('iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#info"]')).length
  398. );
  399. let stanza = _.find(IQ_stanzas, function (iq) {
  400. return iq.querySelector(
  401. 'iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  402. });
  403. const info_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  404. stanza = $iq({
  405. 'type': 'result',
  406. 'from': 'montague.lit',
  407. 'to': 'romeo@montague.lit/orchard',
  408. 'id': info_IQ_id
  409. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  410. .c('identity', {
  411. 'category': 'server',
  412. 'type': 'im'}).up()
  413. .c('feature', {
  414. 'var': 'http://jabber.org/protocol/disco#info'}).up()
  415. .c('feature', {
  416. 'var': 'http://jabber.org/protocol/disco#items'});
  417. _converse.connection._dataRecv(mock.createRequest(stanza));
  418. let entities = await _converse.api.disco.entities.get();
  419. expect(entities.length).toBe(2);
  420. expect(_.includes(entities.pluck('jid'), 'montague.lit')).toBe(true);
  421. expect(_.includes(entities.pluck('jid'), 'romeo@montague.lit')).toBe(true);
  422. expect(entities.get(_converse.domain).features.length).toBe(2);
  423. expect(entities.get(_converse.domain).identities.length).toBe(1);
  424. await u.waitUntil(function () {
  425. // Converse.js sees that the entity has a disco#items feature,
  426. // so it will make a query for it.
  427. return _.filter(IQ_stanzas, function (iq) {
  428. return iq.querySelector('iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  429. }).length > 0;
  430. }, 300);
  431. stanza = _.find(IQ_stanzas, function (iq) {
  432. return iq.querySelector('iq[to="montague.lit"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  433. });
  434. var items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  435. stanza = $iq({
  436. 'type': 'result',
  437. 'from': 'montague.lit',
  438. 'to': 'romeo@montague.lit/orchard',
  439. 'id': items_IQ_id
  440. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
  441. .c('item', {
  442. 'jid': 'upload.montague.lit',
  443. 'name': 'HTTP File Upload'});
  444. _converse.connection._dataRecv(mock.createRequest(stanza));
  445. entities = await _converse.api.disco.entities.get()
  446. expect(entities.length).toBe(2);
  447. expect(entities.get('montague.lit').items.length).toBe(1);
  448. await u.waitUntil(function () {
  449. // Converse.js sees that the entity has a disco#info feature,
  450. // so it will make a query for it.
  451. return _.filter(IQ_stanzas, function (iq) {
  452. return iq.querySelector('iq[to="upload.montague.lit"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  453. }).length > 0;
  454. }, 300);
  455. stanza = _.find(IQ_stanzas, iq => iq.querySelector('iq[to="upload.montague.lit"] query[xmlns="http://jabber.org/protocol/disco#info"]'));
  456. const IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  457. expect(Strophe.serialize(stanza)).toBe(
  458. `<iq from="romeo@montague.lit/orchard" id="${IQ_id}" to="upload.montague.lit" type="get" xmlns="jabber:client">`+
  459. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  460. `</iq>`);
  461. // Upload service responds and reports a maximum file size of 5MiB
  462. stanza = $iq({'type': 'result', 'to': 'romeo@montague.lit/orchard', 'id': IQ_id, 'from': 'upload.montague.lit'})
  463. .c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  464. .c('identity', {'category':'store', 'type':'file', 'name':'HTTP File Upload'}).up()
  465. .c('feature', {'var':'urn:xmpp:http:upload:0'}).up()
  466. .c('x', {'type':'result', 'xmlns':'jabber:x:data'})
  467. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  468. .c('value').t('urn:xmpp:http:upload:0').up().up()
  469. .c('field', {'var':'max-file-size'})
  470. .c('value').t('5242880');
  471. _converse.connection._dataRecv(mock.createRequest(stanza));
  472. entities = await _converse.api.disco.entities.get();
  473. expect(entities.get('montague.lit').items.get('upload.montague.lit').identities.where({'category': 'store'}).length).toBe(1);
  474. await _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain);
  475. await mock.waitForRoster(_converse, 'current');
  476. const contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  477. await mock.openChatBoxFor(_converse, contact_jid);
  478. const view = _converse.chatboxviews.get(contact_jid);
  479. var file = {
  480. 'type': 'image/jpeg',
  481. 'size': '5242881',
  482. 'lastModifiedDate': "",
  483. 'name': "my-juliet.jpg"
  484. };
  485. view.model.sendFiles([file]);
  486. await u.waitUntil(() => view.el.querySelectorAll('.message').length)
  487. const messages = view.el.querySelectorAll('.message.chat-error');
  488. expect(messages.length).toBe(1);
  489. expect(messages[0].textContent.trim()).toBe(
  490. 'The size of your file, my-juliet.jpg, exceeds the maximum allowed by your server, which is 5 MB.');
  491. done();
  492. }));
  493. });
  494. });
  495. describe("While a file is being uploaded", function () {
  496. it("shows a progress bar", mock.initConverse(
  497. ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  498. async function (done, _converse) {
  499. await mock.waitUntilDiscoConfirmed(
  500. _converse, _converse.domain,
  501. [{'category': 'server', 'type':'IM'}],
  502. ['http://jabber.org/protocol/disco#items'], [], 'info');
  503. const IQ_stanzas = _converse.connection.IQ_stanzas;
  504. await mock.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.tld'], 'items');
  505. await mock.waitUntilDiscoConfirmed(_converse, 'upload.montague.tld', [], [Strophe.NS.HTTPUPLOAD], []);
  506. await mock.waitForRoster(_converse, 'current');
  507. const contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  508. await mock.openChatBoxFor(_converse, contact_jid);
  509. const view = _converse.chatboxviews.get(contact_jid);
  510. const file = {
  511. 'type': 'image/jpeg',
  512. 'size': '23456' ,
  513. 'lastModifiedDate': "",
  514. 'name': "my-juliet.jpg"
  515. };
  516. view.model.sendFiles([file]);
  517. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  518. await u.waitUntil(() => _.filter(IQ_stanzas, iq => iq.querySelector('iq[to="upload.montague.tld"] request')).length)
  519. const iq = IQ_stanzas.pop();
  520. expect(Strophe.serialize(iq)).toBe(
  521. `<iq from="romeo@montague.lit/orchard" `+
  522. `id="${iq.getAttribute("id")}" `+
  523. `to="upload.montague.tld" `+
  524. `type="get" `+
  525. `xmlns="jabber:client">`+
  526. `<request `+
  527. `content-type="image/jpeg" `+
  528. `filename="my-juliet.jpg" `+
  529. `size="23456" `+
  530. `xmlns="urn:xmpp:http:upload:0"/>`+
  531. `</iq>`);
  532. const base_url = 'https://conversejs.org';
  533. const message = base_url+"/logo/conversejs-filled.svg";
  534. const stanza = u.toStanza(`
  535. <iq from="upload.montague.tld"
  536. id="${iq.getAttribute("id")}"
  537. to="romeo@montague.lit/orchard"
  538. type="result">
  539. <slot xmlns="urn:xmpp:http:upload:0">
  540. <put url="https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg">
  541. <header name="Authorization">Basic Base64String==</header>
  542. <header name="Cookie">foo=bar; user=romeo</header>
  543. </put>
  544. <get url="${message}" />
  545. </slot>
  546. </iq>`);
  547. spyOn(XMLHttpRequest.prototype, 'send').and.callFake(async () => {
  548. const message = view.model.messages.at(0);
  549. expect(view.el.querySelector('.chat-content progress').getAttribute('value')).toBe('0');
  550. message.set('progress', 0.5);
  551. await u.waitUntil(() => view.el.querySelector('.chat-content progress').getAttribute('value') === '0.5');
  552. message.set('progress', 1);
  553. await u.waitUntil(() => view.el.querySelector('.chat-content progress').getAttribute('value') === '1');
  554. expect(view.el.querySelector('.chat-content .chat-msg__text').textContent).toBe('Uploading file: my-juliet.jpg, 22.91 KB');
  555. done();
  556. });
  557. _converse.connection._dataRecv(mock.createRequest(stanza));
  558. }));
  559. });
  560. });
  561. });