http-file-upload.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. (function (root, factory) {
  2. define([
  3. "jasmine",
  4. "jquery",
  5. "converse-core",
  6. "mock",
  7. "test-utils"], factory);
  8. } (this, function (jasmine, $, converse, mock, test_utils) {
  9. "use strict";
  10. var Strophe = converse.env.Strophe;
  11. var $iq = converse.env.$iq;
  12. var _ = converse.env._;
  13. var f = converse.env.f;
  14. describe("XEP-0363: HTTP File Upload", function () {
  15. describe("Discovering support", function () {
  16. it("is done automatically", mock.initConverseWithAsync(function (done, _converse) {
  17. var IQ_stanzas = _converse.connection.IQ_stanzas;
  18. var IQ_ids = _converse.connection.IQ_ids;
  19. test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], []).then(function () {
  20. test_utils.waitUntil(function () {
  21. return _.filter(IQ_stanzas, function (iq) {
  22. return iq.nodeTree.querySelector(
  23. 'iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  24. }).length > 0;
  25. }, 300).then(function () {
  26. /* <iq type='result'
  27. * from='plays.shakespeare.lit'
  28. * to='romeo@montague.net/orchard'
  29. * id='info1'>
  30. * <query xmlns='http://jabber.org/protocol/disco#info'>
  31. * <identity
  32. * category='server'
  33. * type='im'/>
  34. * <feature var='http://jabber.org/protocol/disco#info'/>
  35. * <feature var='http://jabber.org/protocol/disco#items'/>
  36. * </query>
  37. * </iq>
  38. */
  39. var stanza = _.filter(IQ_stanzas, function (iq) {
  40. return iq.nodeTree.querySelector(
  41. 'iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  42. })[0];
  43. var info_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  44. stanza = $iq({
  45. 'type': 'result',
  46. 'from': 'localhost',
  47. 'to': 'dummy@localhost/resource',
  48. 'id': info_IQ_id
  49. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  50. .c('identity', {
  51. 'category': 'server',
  52. 'type': 'im'}).up()
  53. .c('feature', {
  54. 'var': 'http://jabber.org/protocol/disco#info'}).up()
  55. .c('feature', {
  56. 'var': 'http://jabber.org/protocol/disco#items'});
  57. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  58. _converse.api.disco.entities.get().then(function(entities) {
  59. expect(entities.length).toBe(2);
  60. expect(_.includes(entities.pluck('jid'), 'localhost')).toBe(true);
  61. expect(_.includes(entities.pluck('jid'), 'dummy@localhost')).toBe(true);
  62. expect(entities.get(_converse.domain).features.length).toBe(2);
  63. expect(entities.get(_converse.domain).identities.length).toBe(1);
  64. return test_utils.waitUntil(function () {
  65. // Converse.js sees that the entity has a disco#items feature,
  66. // so it will make a query for it.
  67. return _.filter(IQ_stanzas, function (iq) {
  68. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  69. }).length > 0;
  70. }, 300);
  71. });
  72. }).then(function () {
  73. /* <iq from='montague.tld'
  74. * id='step_01'
  75. * to='romeo@montague.tld/garden'
  76. * type='result'>
  77. * <query xmlns='http://jabber.org/protocol/disco#items'>
  78. * <item jid='upload.montague.tld' name='HTTP File Upload' />
  79. * <item jid='conference.montague.tld' name='Chatroom Service' />
  80. * </query>
  81. * </iq>
  82. */
  83. var stanza = _.filter(IQ_stanzas, function (iq) {
  84. return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
  85. })[0];
  86. var items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  87. stanza = $iq({
  88. 'type': 'result',
  89. 'from': 'localhost',
  90. 'to': 'dummy@localhost/resource',
  91. 'id': items_IQ_id
  92. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
  93. .c('item', {
  94. 'jid': 'upload.localhost',
  95. 'name': 'HTTP File Upload'});
  96. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  97. _converse.api.disco.entities.get().then(function (entities) {
  98. expect(entities.length).toBe(2);
  99. expect(entities.get('localhost').items.length).toBe(1);
  100. return test_utils.waitUntil(function () {
  101. // Converse.js sees that the entity has a disco#info feature,
  102. // so it will make a query for it.
  103. return _.filter(IQ_stanzas, function (iq) {
  104. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  105. }).length > 0;
  106. }, 300);
  107. });
  108. }).then(function () {
  109. var stanza = _.filter(IQ_stanzas, function (iq) {
  110. return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
  111. })[0];
  112. var IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
  113. expect(stanza.toLocaleString()).toBe(
  114. "<iq from='dummy@localhost/resource' to='upload.localhost' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  115. "<query xmlns='http://jabber.org/protocol/disco#info'/>"+
  116. "</iq>");
  117. // Upload service responds and reports a maximum file size of 5MiB
  118. /* <iq from='upload.montague.tld'
  119. * id='step_02'
  120. * to='romeo@montague.tld/garden'
  121. * type='result'>
  122. * <query xmlns='http://jabber.org/protocol/disco#info'>
  123. * <identity category='store'
  124. * type='file'
  125. * name='HTTP File Upload' />
  126. * <feature var='urn:xmpp:http:upload:0' />
  127. * <x type='result' xmlns='jabber:x:data'>
  128. * <field var='FORM_TYPE' type='hidden'>
  129. * <value>urn:xmpp:http:upload:0</value>
  130. * </field>
  131. * <field var='max-file-size'>
  132. * <value>5242880</value>
  133. * </field>
  134. * </x>
  135. * </query>
  136. * </iq>
  137. */
  138. stanza = $iq({'type': 'result', 'to': 'dummy@localhost/resource', 'id': IQ_id, 'from': 'upload.localhost'})
  139. .c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
  140. .c('identity', {'category':'store', 'type':'file', 'name':'HTTP File Upload'}).up()
  141. .c('feature', {'var':'urn:xmpp:http:upload:0'}).up()
  142. .c('x', {'type':'result', 'xmlns':'jabber:x:data'})
  143. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  144. .c('value').t('urn:xmpp:http:upload:0').up().up()
  145. .c('field', {'var':'max-file-size'})
  146. .c('value').t('5242880');
  147. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  148. _converse.api.disco.entities.get().then(function (entities) {
  149. expect(entities.get('localhost').items.get('upload.localhost').identities.where({'category': 'store'}).length).toBe(1);
  150. _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain).then(
  151. function (result) {
  152. expect(result.length).toBe(1);
  153. expect(result[0].get('jid')).toBe('upload.localhost');
  154. done();
  155. }
  156. );
  157. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  158. })
  159. })
  160. }));
  161. });
  162. describe("When supported", function () {
  163. describe("A file upload toolbar button", function () {
  164. it("appears in private chats", mock.initConverseWithAsync(function (done, _converse) {
  165. test_utils.waitUntilDiscoConfirmed(
  166. _converse, _converse.domain,
  167. [{'category': 'server', 'type':'IM'}],
  168. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  169. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.localhost'], 'items').then(function () {
  170. test_utils.waitUntilDiscoConfirmed(_converse, 'upload.localhost', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
  171. test_utils.createContacts(_converse, 'current');
  172. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  173. test_utils.openChatBoxFor(_converse, contact_jid);
  174. var view = _converse.chatboxviews.get(contact_jid);
  175. test_utils.waitUntil(function () {
  176. return view.el.querySelector('.upload-file');
  177. }, 150).then(function () {
  178. expect(view.el.querySelector('.chat-toolbar .upload-file')).not.toBe(null);
  179. done();
  180. });
  181. });
  182. });
  183. });
  184. }));
  185. it("appears in MUC chats", mock.initConverseWithAsync(function (done, _converse) {
  186. done();
  187. }));
  188. describe("when clicked", function () {
  189. it("a file upload slot is requested", mock.initConverseWithAsync(function (done, _converse) {
  190. test_utils.waitUntilDiscoConfirmed(
  191. _converse, _converse.domain,
  192. [{'category': 'server', 'type':'IM'}],
  193. ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
  194. var IQ_stanzas = _converse.connection.IQ_stanzas;
  195. test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.tld'], 'items').then(function () {
  196. test_utils.waitUntilDiscoConfirmed(_converse, 'upload.montague.tld', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
  197. test_utils.createContacts(_converse, 'current');
  198. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  199. test_utils.openChatBoxFor(_converse, contact_jid);
  200. var view = _converse.chatboxviews.get(contact_jid);
  201. var file = {
  202. 'type': 'image/jpeg',
  203. 'size': '23456' ,
  204. 'lastModifiedDate': "",
  205. 'name': "my-juliet.jpg"
  206. };
  207. view.model.sendFile(file);
  208. return test_utils.waitUntil(function () {
  209. return _.filter(IQ_stanzas, function (iq) {
  210. return iq.nodeTree.querySelector('iq[to="upload.montague.tld"] request');
  211. });
  212. }).then(function () {
  213. var iq = IQ_stanzas.pop();
  214. expect(iq.toLocaleString()).toBe(
  215. "<iq from='dummy@localhost/resource' "+
  216. "to='upload.montague.tld' "+
  217. "type='get' "+
  218. "xmlns='jabber:client' "+
  219. "id='"+iq.nodeTree.getAttribute('id')+"'>"+
  220. "<request xmlns='urn:xmpp:http:upload:0' "+
  221. "filename='my-juliet.jpg' "+
  222. "size='23456' "+
  223. "content-type='image/jpeg'/>"+
  224. "</iq>");
  225. var stanza = Strophe.xmlHtmlNode(
  226. "<iq from='upload.montague.tld'"+
  227. " id='"+iq.nodeTree.getAttribute('id')+"'"+
  228. " to='dummy@localhost/resource'"+
  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='https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg' />"+
  236. "</slot>"+
  237. "</iq>").firstElementChild;
  238. spyOn(view.model, 'uploadFile').and.callFake(function () {
  239. return new window.Promise((resolve, reject) => { resolve(); });
  240. });
  241. var sent_stanza;
  242. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  243. sent_stanza = stanza;
  244. });
  245. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  246. return test_utils.waitUntil(function () {
  247. return sent_stanza;
  248. }).then(function () {
  249. expect(view.model.uploadFile).toHaveBeenCalled();
  250. expect(sent_stanza.toLocaleString()).toBe(
  251. "<message from='dummy@localhost/resource' "+
  252. "to='irini.vlastuin@localhost' "+
  253. "type='chat' "+
  254. "id='"+sent_stanza.nodeTree.getAttribute('id')+"' xmlns='jabber:client'>"+
  255. "<body>https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg</body>"+
  256. "<active xmlns='http://jabber.org/protocol/chatstates'/>"+
  257. "<x xmlns='jabber:x:oob'>"+
  258. "<url>https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg</url>"+
  259. "</x>"+
  260. "</message>");
  261. done();
  262. });
  263. });
  264. });
  265. });
  266. });
  267. }));
  268. });
  269. });
  270. });
  271. });
  272. }));