123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- (function (root, factory) {
- define([
- "jasmine",
- "jquery",
- "converse-core",
- "mock",
- "test-utils"], factory);
- } (this, function (jasmine, $, converse, mock, test_utils) {
- "use strict";
- var Strophe = converse.env.Strophe;
- var $iq = converse.env.$iq;
- var _ = converse.env._;
- var f = converse.env.f;
- describe("XEP-0363: HTTP File Upload", function () {
- describe("Discovering support", function () {
- it("is done automatically", mock.initConverseWithAsync(function (done, _converse) {
- var IQ_stanzas = _converse.connection.IQ_stanzas;
- var IQ_ids = _converse.connection.IQ_ids;
- test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], []).then(function () {
- test_utils.waitUntil(function () {
- return _.filter(IQ_stanzas, function (iq) {
- return iq.nodeTree.querySelector(
- 'iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
- }).length > 0;
- }, 300).then(function () {
- /* <iq type='result'
- * from='plays.shakespeare.lit'
- * to='romeo@montague.net/orchard'
- * id='info1'>
- * <query xmlns='http://jabber.org/protocol/disco#info'>
- * <identity
- * category='server'
- * type='im'/>
- * <feature var='http://jabber.org/protocol/disco#info'/>
- * <feature var='http://jabber.org/protocol/disco#items'/>
- * </query>
- * </iq>
- */
- var stanza = _.filter(IQ_stanzas, function (iq) {
- return iq.nodeTree.querySelector(
- 'iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
- })[0];
- var info_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
- stanza = $iq({
- 'type': 'result',
- 'from': 'localhost',
- 'to': 'dummy@localhost/resource',
- 'id': info_IQ_id
- }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
- .c('identity', {
- 'category': 'server',
- 'type': 'im'}).up()
- .c('feature', {
- 'var': 'http://jabber.org/protocol/disco#info'}).up()
- .c('feature', {
- 'var': 'http://jabber.org/protocol/disco#items'});
- _converse.connection._dataRecv(test_utils.createRequest(stanza));
- _converse.api.disco.entities.get().then(function(entities) {
- expect(entities.length).toBe(2);
- expect(_.includes(entities.pluck('jid'), 'localhost')).toBe(true);
- expect(_.includes(entities.pluck('jid'), 'dummy@localhost')).toBe(true);
- expect(entities.get(_converse.domain).features.length).toBe(2);
- expect(entities.get(_converse.domain).identities.length).toBe(1);
- return test_utils.waitUntil(function () {
- // Converse.js sees that the entity has a disco#items feature,
- // so it will make a query for it.
- return _.filter(IQ_stanzas, function (iq) {
- return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
- }).length > 0;
- }, 300);
- });
- }).then(function () {
- /* <iq from='montague.tld'
- * id='step_01'
- * to='romeo@montague.tld/garden'
- * type='result'>
- * <query xmlns='http://jabber.org/protocol/disco#items'>
- * <item jid='upload.montague.tld' name='HTTP File Upload' />
- * <item jid='conference.montague.tld' name='Chatroom Service' />
- * </query>
- * </iq>
- */
- var stanza = _.filter(IQ_stanzas, function (iq) {
- return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
- })[0];
- var items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
- stanza = $iq({
- 'type': 'result',
- 'from': 'localhost',
- 'to': 'dummy@localhost/resource',
- 'id': items_IQ_id
- }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
- .c('item', {
- 'jid': 'upload.localhost',
- 'name': 'HTTP File Upload'});
- _converse.connection._dataRecv(test_utils.createRequest(stanza));
- _converse.api.disco.entities.get().then(function (entities) {
- expect(entities.length).toBe(2);
- expect(entities.get('localhost').items.length).toBe(1);
- return test_utils.waitUntil(function () {
- // Converse.js sees that the entity has a disco#info feature,
- // so it will make a query for it.
- return _.filter(IQ_stanzas, function (iq) {
- return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
- }).length > 0;
- }, 300);
- });
- }).then(function () {
- var stanza = _.filter(IQ_stanzas, function (iq) {
- return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
- })[0];
- var IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
- expect(stanza.toLocaleString()).toBe(
- "<iq from='dummy@localhost/resource' to='upload.localhost' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
- "<query xmlns='http://jabber.org/protocol/disco#info'/>"+
- "</iq>");
- // Upload service responds and reports a maximum file size of 5MiB
- /* <iq from='upload.montague.tld'
- * id='step_02'
- * to='romeo@montague.tld/garden'
- * type='result'>
- * <query xmlns='http://jabber.org/protocol/disco#info'>
- * <identity category='store'
- * type='file'
- * name='HTTP File Upload' />
- * <feature var='urn:xmpp:http:upload:0' />
- * <x type='result' xmlns='jabber:x:data'>
- * <field var='FORM_TYPE' type='hidden'>
- * <value>urn:xmpp:http:upload:0</value>
- * </field>
- * <field var='max-file-size'>
- * <value>5242880</value>
- * </field>
- * </x>
- * </query>
- * </iq>
- */
- stanza = $iq({'type': 'result', 'to': 'dummy@localhost/resource', 'id': IQ_id, 'from': 'upload.localhost'})
- .c('query', {'xmlns': 'http://jabber.org/protocol/disco#info'})
- .c('identity', {'category':'store', 'type':'file', 'name':'HTTP File Upload'}).up()
- .c('feature', {'var':'urn:xmpp:http:upload:0'}).up()
- .c('x', {'type':'result', 'xmlns':'jabber:x:data'})
- .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
- .c('value').t('urn:xmpp:http:upload:0').up().up()
- .c('field', {'var':'max-file-size'})
- .c('value').t('5242880');
- _converse.connection._dataRecv(test_utils.createRequest(stanza));
- _converse.api.disco.entities.get().then(function (entities) {
- expect(entities.get('localhost').items.get('upload.localhost').identities.where({'category': 'store'}).length).toBe(1);
- _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain).then(
- function (result) {
- expect(result.length).toBe(1);
- expect(result[0].get('jid')).toBe('upload.localhost');
- done();
- }
- );
- }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
- })
- })
- }));
- });
- describe("When supported", function () {
- describe("A file upload toolbar button", function () {
- it("appears in private chats", mock.initConverseWithAsync(function (done, _converse) {
- test_utils.waitUntilDiscoConfirmed(
- _converse, _converse.domain,
- [{'category': 'server', 'type':'IM'}],
- ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
- test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.localhost'], 'items').then(function () {
- test_utils.waitUntilDiscoConfirmed(_converse, 'upload.localhost', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
- test_utils.createContacts(_converse, 'current');
- var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
- test_utils.openChatBoxFor(_converse, contact_jid);
- var view = _converse.chatboxviews.get(contact_jid);
- test_utils.waitUntil(function () {
- return view.el.querySelector('.upload-file');
- }, 150).then(function () {
- expect(view.el.querySelector('.chat-toolbar .upload-file')).not.toBe(null);
- done();
- });
- });
- });
- });
- }));
- it("appears in MUC chats", mock.initConverseWithAsync(function (done, _converse) {
- done();
- }));
- describe("when clicked", function () {
- it("a file upload slot is requested", mock.initConverseWithAsync(function (done, _converse) {
- test_utils.waitUntilDiscoConfirmed(
- _converse, _converse.domain,
- [{'category': 'server', 'type':'IM'}],
- ['http://jabber.org/protocol/disco#items'], [], 'info').then(function () {
- var IQ_stanzas = _converse.connection.IQ_stanzas;
- test_utils.waitUntilDiscoConfirmed(_converse, _converse.domain, [], [], ['upload.montague.tld'], 'items').then(function () {
- test_utils.waitUntilDiscoConfirmed(_converse, 'upload.montague.tld', [], [Strophe.NS.HTTPUPLOAD], []).then(function () {
- test_utils.createContacts(_converse, 'current');
- var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
- test_utils.openChatBoxFor(_converse, contact_jid);
- var view = _converse.chatboxviews.get(contact_jid);
- var file = {
- 'type': 'image/jpeg',
- 'size': '23456' ,
- 'lastModifiedDate': "",
- 'name': "my-juliet.jpg"
- };
- view.model.sendFile(file);
- return test_utils.waitUntil(function () {
- return _.filter(IQ_stanzas, function (iq) {
- return iq.nodeTree.querySelector('iq[to="upload.montague.tld"] request');
- });
- }).then(function () {
- var iq = IQ_stanzas.pop();
- expect(iq.toLocaleString()).toBe(
- "<iq from='dummy@localhost/resource' "+
- "to='upload.montague.tld' "+
- "type='get' "+
- "xmlns='jabber:client' "+
- "id='"+iq.nodeTree.getAttribute('id')+"'>"+
- "<request xmlns='urn:xmpp:http:upload:0' "+
- "filename='my-juliet.jpg' "+
- "size='23456' "+
- "content-type='image/jpeg'/>"+
- "</iq>");
- var stanza = Strophe.xmlHtmlNode(
- "<iq from='upload.montague.tld'"+
- " id='"+iq.nodeTree.getAttribute('id')+"'"+
- " to='dummy@localhost/resource'"+
- " type='result'>"+
- "<slot xmlns='urn:xmpp:http:upload:0'>"+
- " <put url='https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg'>"+
- " <header name='Authorization'>Basic Base64String==</header>"+
- " <header name='Cookie'>foo=bar; user=romeo</header>"+
- " </put>"+
- " <get url='https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg' />"+
- "</slot>"+
- "</iq>").firstElementChild;
- spyOn(view.model, 'uploadFile').and.callFake(function () {
- return new window.Promise((resolve, reject) => { resolve(); });
- });
- var sent_stanza;
- spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
- sent_stanza = stanza;
- });
- _converse.connection._dataRecv(test_utils.createRequest(stanza));
- return test_utils.waitUntil(function () {
- return sent_stanza;
- }).then(function () {
- expect(view.model.uploadFile).toHaveBeenCalled();
- expect(sent_stanza.toLocaleString()).toBe(
- "<message from='dummy@localhost/resource' "+
- "to='irini.vlastuin@localhost' "+
- "type='chat' "+
- "id='"+sent_stanza.nodeTree.getAttribute('id')+"' xmlns='jabber:client'>"+
- "<body>https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg</body>"+
- "<active xmlns='http://jabber.org/protocol/chatstates'/>"+
- "<x xmlns='jabber:x:oob'>"+
- "<url>https://download.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/my-juliet.jpg</url>"+
- "</x>"+
- "</message>");
- done();
- });
- });
- });
- });
- });
- }));
- });
- });
- });
- });
- }));
|