http-file-upload.js 35 KB

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