2
0

http-file-upload.js 34 KB

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