chatroom.js 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169
  1. (function (root, factory) {
  2. define(["mock", "converse-core", "test-utils", "utils" ], factory);
  3. } (this, function (mock, converse, test_utils, utils) {
  4. var _ = converse.env._;
  5. var $ = converse.env.jQuery;
  6. var $pres = converse.env.$pres;
  7. var $iq = converse.env.$iq;
  8. var $msg = converse.env.$msg;
  9. var Strophe = converse.env.Strophe;
  10. return describe("ChatRooms", function () {
  11. describe("The \"rooms\" API", function () {
  12. it("has a method 'close' which closes rooms by JID or all rooms when called with no arguments", mock.initConverse(function (_converse) {
  13. test_utils.createContacts(_converse, 'current');
  14. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  15. test_utils.openAndEnterChatRoom(_converse, 'leisure', 'localhost', 'dummy');
  16. test_utils.openAndEnterChatRoom(_converse, 'news', 'localhost', 'dummy');
  17. expect(_converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy();
  18. expect(_converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
  19. expect(_converse.chatboxviews.get('news@localhost').$el.is(':visible')).toBeTruthy();
  20. // XXX: bit of a cheat here. We want `cleanup()` to be
  21. // called on the room. Either it's this or faking
  22. // `sendPresence`.
  23. _converse.connection.connected = false;
  24. _converse.api.rooms.close('lounge@localhost');
  25. expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
  26. expect(_converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
  27. expect(_converse.chatboxviews.get('news@localhost').$el.is(':visible')).toBeTruthy();
  28. _converse.api.rooms.close(['leisure@localhost', 'news@localhost']);
  29. expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
  30. expect(_converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
  31. expect(_converse.chatboxviews.get('news@localhost')).toBeUndefined();
  32. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  33. test_utils.openAndEnterChatRoom(_converse, 'leisure', 'localhost', 'dummy');
  34. expect(_converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy();
  35. expect(_converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
  36. _converse.api.rooms.close();
  37. expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
  38. expect(_converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
  39. }));
  40. it("has a method 'get' which returns a wrapped chat room (if it exists)", mock.initConverseWithAsync(function (done, _converse) {
  41. test_utils.createContacts(_converse, 'current');
  42. test_utils.waitUntil(function () {
  43. return _converse.rosterview.$el.find('dt').length;
  44. }, 300)
  45. .then(function () {
  46. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  47. var jid = 'lounge@localhost';
  48. var room = _converse.api.rooms.get(jid);
  49. expect(room instanceof Object).toBeTruthy();
  50. expect(room.is_chatroom).toBeTruthy();
  51. var chatroomview = _converse.chatboxviews.get(jid);
  52. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  53. chatroomview.close();
  54. // Test with mixed case
  55. test_utils.openAndEnterChatRoom(_converse, 'Leisure', 'localhost', 'dummy');
  56. jid = 'Leisure@localhost';
  57. room = _converse.api.rooms.get(jid);
  58. expect(room instanceof Object).toBeTruthy();
  59. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  60. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  61. jid = 'leisure@localhost';
  62. room = _converse.api.rooms.get(jid);
  63. expect(room instanceof Object).toBeTruthy();
  64. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  65. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  66. jid = 'leiSure@localhost';
  67. room = _converse.api.rooms.get(jid);
  68. expect(room instanceof Object).toBeTruthy();
  69. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  70. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  71. chatroomview.close();
  72. // Non-existing room
  73. jid = 'lounge2@localhost';
  74. room = _converse.api.rooms.get(jid);
  75. expect(typeof room === 'undefined').toBeTruthy();
  76. done();
  77. });
  78. }));
  79. it("has a method 'open' which opens (optionally configures) and returns a wrapped chat box", mock.initConverseWithAsync(function (done, _converse) {
  80. // Mock 'getRoomFeatures', otherwise the room won't be
  81. // displayed as it waits first for the features to be returned
  82. // (when it's a new room being created).
  83. spyOn(_converse.ChatRoomView.prototype, 'getRoomFeatures').and.callFake(function () {
  84. var deferred = new $.Deferred();
  85. deferred.resolve();
  86. return deferred.promise();
  87. });
  88. test_utils.createContacts(_converse, 'current');
  89. test_utils.waitUntil(function () {
  90. return _converse.rosterview.$el.find('dt').length;
  91. }, 300)
  92. .then(function () {
  93. var chatroomview;
  94. var jid = 'lounge@localhost';
  95. var room = _converse.api.rooms.open(jid);
  96. // Test on chat room that doesn't exist.
  97. expect(room instanceof Object).toBeTruthy();
  98. expect(room.is_chatroom).toBeTruthy();
  99. chatroomview = _converse.chatboxviews.get(jid);
  100. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  101. // Test again, now that the room exists.
  102. room = _converse.api.rooms.open(jid);
  103. expect(room instanceof Object).toBeTruthy();
  104. expect(room.is_chatroom).toBeTruthy();
  105. chatroomview = _converse.chatboxviews.get(jid);
  106. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  107. // Test with mixed case in JID
  108. jid = 'Leisure@localhost';
  109. room = _converse.api.rooms.open(jid);
  110. expect(room instanceof Object).toBeTruthy();
  111. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  112. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  113. jid = 'leisure@localhost';
  114. room = _converse.api.rooms.open(jid);
  115. expect(room instanceof Object).toBeTruthy();
  116. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  117. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  118. jid = 'leiSure@localhost';
  119. room = _converse.api.rooms.open(jid);
  120. expect(room instanceof Object).toBeTruthy();
  121. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  122. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  123. chatroomview.close();
  124. _converse.muc_instant_rooms = false;
  125. var sent_IQ, IQ_id;
  126. var sendIQ = _converse.connection.sendIQ;
  127. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  128. sent_IQ = iq;
  129. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  130. });
  131. // Test with configuration
  132. _converse.api.rooms.open('room@conference.example.org', {
  133. 'nick': 'some1',
  134. 'auto_configure': true,
  135. 'roomconfig': {
  136. 'changesubject': false,
  137. 'membersonly': true,
  138. 'persistentroom': true,
  139. 'publicroom': true,
  140. 'roomdesc': 'Welcome to this room',
  141. 'whois': 'anyone'
  142. }
  143. });
  144. // We pretend this is a new room, so no disco info is returned.
  145. var features_stanza = $iq({
  146. from: 'room@conference.example.org',
  147. 'id': IQ_id,
  148. 'to': 'dummy@localhost/desktop',
  149. 'type': 'error'
  150. }).c('error', {'type': 'cancel'})
  151. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  152. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  153. /* <presence xmlns="jabber:client" to="dummy@localhost/pda" from="room@conference.example.org/yo">
  154. * <x xmlns="http://jabber.org/protocol/muc#user">
  155. * <item affiliation="owner" jid="dummy@localhost/pda" role="moderator"/>
  156. * <status code="110"/>
  157. * <status code="201"/>
  158. * </x>
  159. * </presence>
  160. */
  161. var presence = $pres({
  162. from:'room@conference.example.org/some1',
  163. to:'dummy@localhost/pda'
  164. })
  165. .c('x', {xmlns:'http://jabber.org/protocol/muc#user'})
  166. .c('item', {
  167. affiliation: 'owner',
  168. jid: 'dummy@localhost/pda',
  169. role: 'moderator'
  170. }).up()
  171. .c('status', {code:'110'}).up()
  172. .c('status', {code:'201'});
  173. _converse.connection._dataRecv(test_utils.createRequest(presence));
  174. expect(_converse.connection.sendIQ).toHaveBeenCalled();
  175. expect(sent_IQ.toLocaleString()).toBe(
  176. "<iq to='room@conference.example.org' type='get' xmlns='jabber:client' id='"+IQ_id+
  177. "'><query xmlns='http://jabber.org/protocol/muc#owner'/></iq>"
  178. );
  179. _converse.connection._dataRecv(test_utils.createRequest($(
  180. '<iq xmlns="jabber:client"'+
  181. ' type="result"'+
  182. ' to="dummy@localhost/pda"'+
  183. ' from="room@conference.example.org" id="'+IQ_id+'">'+
  184. ' <query xmlns="http://jabber.org/protocol/muc#owner">'+
  185. ' <x xmlns="jabber:x:data" type="form">'+
  186. ' <title>Configuration for room@conference.example.org</title>'+
  187. ' <instructions>Complete and submit this form to configure the room.</instructions>'+
  188. ' <field var="FORM_TYPE" type="hidden">'+
  189. ' <value>http://jabber.org/protocol/muc#roomconfig</value>'+
  190. ' </field>'+
  191. ' <field type="text-single" var="muc#roomconfig_roomname" label="Name">'+
  192. ' <value>Room</value>'+
  193. ' </field>'+
  194. ' <field type="text-single" var="muc#roomconfig_roomdesc" label="Description"><value/></field>'+
  195. ' <field type="boolean" var="muc#roomconfig_persistentroom" label="Make Room Persistent?"/>'+
  196. ' <field type="boolean" var="muc#roomconfig_publicroom" label="Make Room Publicly Searchable?"><value>1</value></field>'+
  197. ' <field type="boolean" var="muc#roomconfig_changesubject" label="Allow Occupants to Change Subject?"/>'+
  198. ' <field type="list-single" var="muc#roomconfig_whois" label="Who May Discover Real JIDs?"><option label="Moderators Only">'+
  199. ' <value>moderators</value></option><option label="Anyone"><value>anyone</value></option>'+
  200. ' </field>'+
  201. ' <field type="text-private" var="muc#roomconfig_roomsecret" label="Password"><value/></field>'+
  202. ' <field type="boolean" var="muc#roomconfig_moderatedroom" label="Make Room Moderated?"/>'+
  203. ' <field type="boolean" var="muc#roomconfig_membersonly" label="Make Room Members-Only?"/>'+
  204. ' <field type="text-single" var="muc#roomconfig_historylength" label="Maximum Number of History Messages Returned by Room">'+
  205. ' <value>20</value></field>'+
  206. ' </x>'+
  207. ' </query>'+
  208. ' </iq>')[0]));
  209. var $sent_stanza = $(sent_IQ.toLocaleString());
  210. expect($sent_stanza.find('field[var="muc#roomconfig_roomname"] value').text()).toBe('Room');
  211. expect($sent_stanza.find('field[var="muc#roomconfig_roomdesc"] value').text()).toBe('Welcome to this room');
  212. expect($sent_stanza.find('field[var="muc#roomconfig_persistentroom"] value').text()).toBe('1');
  213. expect($sent_stanza.find('field[var="muc#roomconfig_publicroom"] value ').text()).toBe('1');
  214. expect($sent_stanza.find('field[var="muc#roomconfig_changesubject"] value').text()).toBe('0');
  215. expect($sent_stanza.find('field[var="muc#roomconfig_whois"] value ').text()).toBe('anyone');
  216. expect($sent_stanza.find('field[var="muc#roomconfig_membersonly"] value').text()).toBe('1');
  217. expect($sent_stanza.find('field[var="muc#roomconfig_historylength"] value').text()).toBe('20');
  218. done();
  219. });
  220. }));
  221. });
  222. describe("An instant chat room", function () {
  223. it("will be created when muc_instant_rooms is set to true", mock.initConverse(function (_converse) {
  224. var sent_IQ, IQ_id;
  225. var sendIQ = _converse.connection.sendIQ;
  226. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  227. sent_IQ = iq;
  228. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  229. });
  230. /* <iq from="jordie.langen@chat.example.org/converse.js-11659299" to="myroom@conference.chat.example.org" type="get">
  231. * <query xmlns="http://jabber.org/protocol/disco#info"/>
  232. * </iq>
  233. * <iq xmlns="jabber:client" type="error" to="jordie.langen@chat.example.org/converse.js-11659299" from="myroom@conference.chat.example.org">
  234. * <error type="cancel">
  235. * <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  236. * </error>
  237. * </iq>
  238. */
  239. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  240. // We pretend this is a new room, so no disco info is returned.
  241. var features_stanza = $iq({
  242. from: 'lounge@localhost',
  243. 'id': IQ_id,
  244. 'to': 'dummy@localhost/desktop',
  245. 'type': 'error'
  246. }).c('error', {'type': 'cancel'})
  247. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  248. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  249. var view = _converse.chatboxviews.get('lounge@localhost');
  250. spyOn(view, 'join').and.callThrough();
  251. /* <iq to="myroom@conference.chat.example.org"
  252. * from="jordie.langen@chat.example.org/converse.js-11659299"
  253. * type="get">
  254. * <query xmlns="http://jabber.org/protocol/disco#info"
  255. * node="x-roomuser-item"/>
  256. * </iq>
  257. */
  258. expect(sent_IQ.toLocaleString()).toBe(
  259. "<iq to='lounge@localhost' from='dummy@localhost/resource' "+
  260. "type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  261. "<query xmlns='http://jabber.org/protocol/disco#info' node='x-roomuser-item'/></iq>"
  262. );
  263. /* * <iq xmlns="jabber:client" type="error" to="jordie.langen@chat.example.org/converse.js-11659299" from="myroom@conference.chat.example.org">
  264. * <error type="cancel">
  265. * <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  266. * </error>
  267. * </iq>
  268. */
  269. var stanza = $iq({
  270. 'type': 'error',
  271. 'id': IQ_id,
  272. 'from': view.model.get('jid'),
  273. 'to': _converse.connection.jid
  274. }).c('error', {'type': 'cancel'})
  275. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  276. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  277. // TODO: enter nickname
  278. var $input = view.$el.find('input.new-chatroom-nick');
  279. $input.val('nicky').parents('form').submit();
  280. expect(view.join).toHaveBeenCalled();
  281. // The user has just entered the room (because join was called)
  282. // and receives their own presence from the server.
  283. // See example 24:
  284. // http://xmpp.org/extensions/xep-0045.html#enter-pres
  285. //
  286. /* <presence xmlns="jabber:client" to="jordie.langen@chat.example.org/converse.js-11659299" from="myroom@conference.chat.example.org/jc">
  287. * <x xmlns="http://jabber.org/protocol/muc#user">
  288. * <item jid="jordie.langen@chat.example.org/converse.js-11659299" affiliation="owner" role="moderator"/>
  289. * <status code="110"/>
  290. * <status code="201"/>
  291. * </x>
  292. * </presence>
  293. */
  294. var presence = $pres({
  295. to:'dummy@localhost/resource',
  296. from:'lounge@localhost/thirdwitch',
  297. id:'5025e055-036c-4bc5-a227-706e7e352053'
  298. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  299. .c('item').attrs({
  300. affiliation: 'owner',
  301. jid: 'dummy@localhost/resource',
  302. role: 'moderator'
  303. }).up()
  304. .c('status').attrs({code:'110'}).up()
  305. .c('status').attrs({code:'201'}).nodeTree;
  306. _converse.connection._dataRecv(test_utils.createRequest(presence));
  307. var info_text = view.$el.find('.chat-content .chat-info').text();
  308. expect(info_text).toBe('A new room has been created');
  309. // An instant room is created by saving the default configuratoin.
  310. //
  311. /* <iq to="myroom@conference.chat.example.org" type="set" xmlns="jabber:client" id="5025e055-036c-4bc5-a227-706e7e352053:sendIQ">
  312. * <query xmlns="http://jabber.org/protocol/muc#owner"><x xmlns="jabber:x:data" type="submit"/></query>
  313. * </iq>
  314. */
  315. expect(sent_IQ.toLocaleString()).toBe(
  316. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  317. "<query xmlns='http://jabber.org/protocol/muc#owner'><x xmlns='jabber:x:data' type='submit'/>"+
  318. "</query></iq>");
  319. }));
  320. });
  321. describe("A Chat Room", function () {
  322. it("shows join/leave messages when users enter or exit a room", mock.initConverse(function (_converse) {
  323. test_utils.openChatRoom(_converse, "coven", 'chat.shakespeare.lit', 'some1');
  324. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  325. var $chat_content = view.$el.find('.chat-content');
  326. /* We don't show join/leave messages for existing occupants. We
  327. * know about them because we receive their presences before we
  328. * receive our own.
  329. */
  330. presence = $pres({
  331. to: 'dummy@localhost/_converse.js-29092160',
  332. from: 'coven@chat.shakespeare.lit/oldguy'
  333. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  334. .c('item', {
  335. 'affiliation': 'none',
  336. 'jid': 'oldguy@localhost/_converse.js-290929789',
  337. 'role': 'participant'
  338. });
  339. _converse.connection._dataRecv(test_utils.createRequest(presence));
  340. expect($chat_content.find('div.chat-info').length).toBe(0);
  341. /* <presence to="dummy@localhost/_converse.js-29092160"
  342. * from="coven@chat.shakespeare.lit/some1">
  343. * <x xmlns="http://jabber.org/protocol/muc#user">
  344. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  345. * <status code="110"/>
  346. * </x>
  347. * </presence></body>
  348. */
  349. var presence = $pres({
  350. to: 'dummy@localhost/_converse.js-29092160',
  351. from: 'coven@chat.shakespeare.lit/some1'
  352. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  353. .c('item', {
  354. 'affiliation': 'owner',
  355. 'jid': 'dummy@localhost/_converse.js-29092160',
  356. 'role': 'moderator'
  357. }).up()
  358. .c('status', {code: '110'});
  359. _converse.connection._dataRecv(test_utils.createRequest(presence));
  360. expect($chat_content.find('div.chat-info:first').html()).toBe("some1 has joined the room.");
  361. presence = $pres({
  362. to: 'dummy@localhost/_converse.js-29092160',
  363. from: 'coven@chat.shakespeare.lit/newguy'
  364. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  365. .c('item', {
  366. 'affiliation': 'none',
  367. 'jid': 'newguy@localhost/_converse.js-290929789',
  368. 'role': 'participant'
  369. });
  370. _converse.connection._dataRecv(test_utils.createRequest(presence));
  371. expect($chat_content.find('div.chat-info').length).toBe(2);
  372. expect($chat_content.find('div.chat-info:last').html()).toBe("newguy has joined the room.");
  373. // Don't show duplicate join messages
  374. presence = $pres({
  375. to: 'dummy@localhost/_converse.js-290918392',
  376. from: 'coven@chat.shakespeare.lit/newguy'
  377. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  378. .c('item', {
  379. 'affiliation': 'none',
  380. 'jid': 'newguy@localhost/_converse.js-290929789',
  381. 'role': 'participant'
  382. });
  383. _converse.connection._dataRecv(test_utils.createRequest(presence));
  384. expect($chat_content.find('div.chat-info').length).toBe(2);
  385. presence = $pres({
  386. to: 'dummy@localhost/_converse.js-29092160',
  387. type: 'unavailable',
  388. from: 'coven@chat.shakespeare.lit/newguy'
  389. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  390. .c('item', {
  391. 'affiliation': 'none',
  392. 'jid': 'newguy@localhost/_converse.js-290929789',
  393. 'role': 'participant'
  394. });
  395. _converse.connection._dataRecv(test_utils.createRequest(presence));
  396. expect($chat_content.find('div.chat-info').length).toBe(3);
  397. expect($chat_content.find('div.chat-info:last').html()).toBe("newguy has left the room");
  398. }));
  399. it("shows its description in the chat heading", mock.initConverse(function (_converse) {
  400. var sent_IQ, IQ_id;
  401. var sendIQ = _converse.connection.sendIQ;
  402. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  403. sent_IQ = iq;
  404. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  405. });
  406. var view = _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  407. spyOn(view, 'generateHeadingHTML').and.callThrough();
  408. var features_stanza = $iq({
  409. from: 'coven@chat.shakespeare.lit',
  410. 'id': IQ_id,
  411. 'to': 'dummy@localhost/desktop',
  412. 'type': 'result'
  413. })
  414. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  415. .c('identity', {
  416. 'category': 'conference',
  417. 'name': 'A Dark Cave',
  418. 'type': 'text'
  419. }).up()
  420. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  421. .c('feature', {'var': 'muc_passwordprotected'}).up()
  422. .c('feature', {'var': 'muc_hidden'}).up()
  423. .c('feature', {'var': 'muc_temporary'}).up()
  424. .c('feature', {'var': 'muc_open'}).up()
  425. .c('feature', {'var': 'muc_unmoderated'}).up()
  426. .c('feature', {'var': 'muc_nonanonymous'}).up()
  427. .c('feature', {'var': 'urn:xmpp:mam:0'}).up()
  428. .c('x', { 'xmlns':'jabber:x:data', 'type':'result'})
  429. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  430. .c('value').t('http://jabber.org/protocol/muc#roominfo').up().up()
  431. .c('field', {'type':'text-single', 'var':'muc#roominfo_description', 'label':'Description'})
  432. .c('value').t('This is the description').up().up()
  433. .c('field', {'type':'text-single', 'var':'muc#roominfo_occupants', 'label':'Number of occupants'})
  434. .c('value').t(0);
  435. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  436. expect(view.generateHeadingHTML).toHaveBeenCalled();
  437. expect(view.$('.chatroom-description').text()).toBe('This is the description');
  438. }));
  439. it("will specially mark messages in which you are mentioned", mock.initConverse(function (_converse) {
  440. test_utils.createContacts(_converse, 'current');
  441. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  442. var view = _converse.chatboxviews.get('lounge@localhost');
  443. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  444. var message = 'dummy: Your attention is required';
  445. var nick = mock.chatroom_names[0],
  446. msg = $msg({
  447. from: 'lounge@localhost/'+nick,
  448. id: (new Date()).getTime(),
  449. to: 'dummy@localhost',
  450. type: 'groupchat'
  451. }).c('body').t(message).tree();
  452. view.handleMUCMessage(msg);
  453. expect(view.$el.find('.chat-message').hasClass('mentioned')).toBeTruthy();
  454. }));
  455. it("supports the /me command", mock.initConverse(function (_converse) {
  456. test_utils.createContacts(_converse, 'current');
  457. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  458. var view = _converse.chatboxviews.get('lounge@localhost');
  459. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  460. var message = '/me is tired';
  461. var nick = mock.chatroom_names[0],
  462. msg = $msg({
  463. from: 'lounge@localhost/'+nick,
  464. id: (new Date()).getTime(),
  465. to: 'dummy@localhost',
  466. type: 'groupchat'
  467. }).c('body').t(message).tree();
  468. view.handleMUCMessage(msg);
  469. expect(_.includes(view.$el.find('.chat-msg-author').text(), '**Dyon van de Wege')).toBeTruthy();
  470. expect(view.$el.find('.chat-msg-content').text()).toBe(' is tired');
  471. message = '/me is as well';
  472. msg = $msg({
  473. from: 'lounge@localhost/dummy',
  474. id: (new Date()).getTime(),
  475. to: 'dummy@localhost',
  476. type: 'groupchat'
  477. }).c('body').t(message).tree();
  478. view.handleMUCMessage(msg);
  479. expect(_.includes(view.$el.find('.chat-msg-author:last').text(), '**Max Mustermann')).toBeTruthy();
  480. expect(view.$el.find('.chat-msg-content:last').text()).toBe(' is as well');
  481. }));
  482. it("can have spaces and special characters in its name", mock.initConverse(function (_converse) {
  483. test_utils.openChatRoom(_converse, 'lounge & leisure', 'localhost', 'dummy');
  484. var view = _converse.chatboxviews.get(
  485. Strophe.escapeNode('lounge & leisure')+'@localhost');
  486. expect(view instanceof _converse.ChatRoomView).toBe(true);
  487. }));
  488. it("can be configured if you're its owner", mock.initConverse(function (_converse) {
  489. var view;
  490. var sent_IQ, IQ_id;
  491. var sendIQ = _converse.connection.sendIQ;
  492. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  493. sent_IQ = iq;
  494. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  495. });
  496. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  497. view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  498. spyOn(view, 'saveAffiliationAndRole').and.callThrough();
  499. // We pretend this is a new room, so no disco info is returned.
  500. var features_stanza = $iq({
  501. from: 'coven@chat.shakespeare.lit',
  502. 'id': IQ_id,
  503. 'to': 'dummy@localhost/desktop',
  504. 'type': 'error'
  505. }).c('error', {'type': 'cancel'})
  506. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  507. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  508. /* <presence to="dummy@localhost/_converse.js-29092160"
  509. * from="coven@chat.shakespeare.lit/some1">
  510. * <x xmlns="http://jabber.org/protocol/muc#user">
  511. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  512. * <status code="110"/>
  513. * </x>
  514. * </presence></body>
  515. */
  516. var presence = $pres({
  517. to: 'dummy@localhost/_converse.js-29092160',
  518. from: 'coven@chat.shakespeare.lit/some1'
  519. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  520. .c('item', {
  521. 'affiliation': 'owner',
  522. 'jid': 'dummy@localhost/_converse.js-29092160',
  523. 'role': 'moderator'
  524. }).up()
  525. .c('status', {code: '110'});
  526. _converse.connection._dataRecv(test_utils.createRequest(presence));
  527. expect(view.saveAffiliationAndRole).toHaveBeenCalled();
  528. expect(view.$('.configure-chatroom-button').is(':visible')).toBeTruthy();
  529. expect(view.$('.toggle-chatbox-button').is(':visible')).toBeTruthy();
  530. expect(view.$('.toggle-bookmark').is(':visible')).toBeTruthy();
  531. view.$('.configure-chatroom-button').click();
  532. /* Check that an IQ is sent out, asking for the
  533. * configuration form.
  534. * See: // http://xmpp.org/extensions/xep-0045.html#example-163
  535. *
  536. * <iq from='crone1@shakespeare.lit/desktop'
  537. * id='config1'
  538. * to='coven@chat.shakespeare.lit'
  539. * type='get'>
  540. * <query xmlns='http://jabber.org/protocol/muc#owner'/>
  541. * </iq>
  542. */
  543. expect(sent_IQ.toLocaleString()).toBe(
  544. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  545. "<query xmlns='http://jabber.org/protocol/muc#owner'/>"+
  546. "</iq>");
  547. /* Server responds with the configuration form.
  548. * See: // http://xmpp.org/extensions/xep-0045.html#example-165
  549. */
  550. var config_stanza = $iq({from: 'coven@chat.shakespeare.lit',
  551. 'id': IQ_id,
  552. 'to': 'dummy@localhost/desktop',
  553. 'type': 'result'})
  554. .c('query', { 'xmlns': 'http://jabber.org/protocol/muc#owner'})
  555. .c('x', { 'xmlns': 'jabber:x:data', 'type': 'form'})
  556. .c('title').t('Configuration for "coven" Room').up()
  557. .c('instructions').t('Complete this form to modify the configuration of your room.').up()
  558. .c('field', {'type': 'hidden', 'var': 'FORM_TYPE'})
  559. .c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up()
  560. .c('field', {
  561. 'label': 'Natural-Language Room Name',
  562. 'type': 'text-single',
  563. 'var': 'muc#roomconfig_roomname'})
  564. .c('value').t('A Dark Cave').up().up()
  565. .c('field', {
  566. 'label': 'Short Description of Room',
  567. 'type': 'text-single',
  568. 'var': 'muc#roomconfig_roomdesc'})
  569. .c('value').t('The place for all good witches!').up().up()
  570. .c('field', {
  571. 'label': 'Enable Public Logging?',
  572. 'type': 'boolean',
  573. 'var': 'muc#roomconfig_enablelogging'})
  574. .c('value').t(0).up().up()
  575. .c('field', {
  576. 'label': 'Allow Occupants to Change Subject?',
  577. 'type': 'boolean',
  578. 'var': 'muc#roomconfig_changesubject'})
  579. .c('value').t(0).up().up()
  580. .c('field', {
  581. 'label': 'Allow Occupants to Invite Others?',
  582. 'type': 'boolean',
  583. 'var': 'muc#roomconfig_allowinvites'})
  584. .c('value').t(0).up().up()
  585. .c('field', {
  586. 'label': 'Who Can Send Private Messages?',
  587. 'type': 'list-single',
  588. 'var': 'muc#roomconfig_allowpm'})
  589. .c('value').t('anyone').up()
  590. .c('option', {'label': 'Anyone'})
  591. .c('value').t('anyone').up().up()
  592. .c('option', {'label': 'Anyone with Voice'})
  593. .c('value').t('participants').up().up()
  594. .c('option', {'label': 'Moderators Only'})
  595. .c('value').t('moderators').up().up()
  596. .c('option', {'label': 'Nobody'})
  597. .c('value').t('none').up().up().up()
  598. .c('field', {
  599. 'label': 'Roles for which Presence is Broadcasted',
  600. 'type': 'list-multi',
  601. 'var': 'muc#roomconfig_presencebroadcast'})
  602. .c('value').t('moderator').up()
  603. .c('value').t('participant').up()
  604. .c('value').t('visitor').up()
  605. .c('option', {'label': 'Moderator'})
  606. .c('value').t('moderator').up().up()
  607. .c('option', {'label': 'Participant'})
  608. .c('value').t('participant').up().up()
  609. .c('option', {'label': 'Visitor'})
  610. .c('value').t('visitor').up().up().up()
  611. .c('field', {
  612. 'label': 'Roles and Affiliations that May Retrieve Member List',
  613. 'type': 'list-multi',
  614. 'var': 'muc#roomconfig_getmemberlist'})
  615. .c('value').t('moderator').up()
  616. .c('value').t('participant').up()
  617. .c('value').t('visitor').up()
  618. .c('option', {'label': 'Moderator'})
  619. .c('value').t('moderator').up().up()
  620. .c('option', {'label': 'Participant'})
  621. .c('value').t('participant').up().up()
  622. .c('option', {'label': 'Visitor'})
  623. .c('value').t('visitor').up().up().up()
  624. .c('field', {
  625. 'label': 'Make Room Publicly Searchable?',
  626. 'type': 'boolean',
  627. 'var': 'muc#roomconfig_publicroom'})
  628. .c('value').t(0).up().up()
  629. .c('field', {
  630. 'label': 'Make Room Publicly Searchable?',
  631. 'type': 'boolean',
  632. 'var': 'muc#roomconfig_publicroom'})
  633. .c('value').t(0).up().up()
  634. .c('field', {
  635. 'label': 'Make Room Persistent?',
  636. 'type': 'boolean',
  637. 'var': 'muc#roomconfig_persistentroom'})
  638. .c('value').t(0).up().up()
  639. .c('field', {
  640. 'label': 'Make Room Moderated?',
  641. 'type': 'boolean',
  642. 'var': 'muc#roomconfig_moderatedroom'})
  643. .c('value').t(0).up().up()
  644. .c('field', {
  645. 'label': 'Make Room Members Only?',
  646. 'type': 'boolean',
  647. 'var': 'muc#roomconfig_membersonly'})
  648. .c('value').t(0).up().up()
  649. .c('field', {
  650. 'label': 'Password Required for Entry?',
  651. 'type': 'boolean',
  652. 'var': 'muc#roomconfig_passwordprotectedroom'})
  653. .c('value').t(1).up().up()
  654. .c('field', {'type': 'fixed'})
  655. .c('value').t('If a password is required to enter this room,'+
  656. 'you must specify the password below.').up().up()
  657. .c('field', {
  658. 'label': 'Password',
  659. 'type': 'text-private',
  660. 'var': 'muc#roomconfig_roomsecret'})
  661. .c('value').t('cauldronburn');
  662. _converse.connection._dataRecv(test_utils.createRequest(config_stanza));
  663. expect(view.$('form.chatroom-form').length).toBe(1);
  664. expect(view.$('form.chatroom-form fieldset').length).toBe(2);
  665. var $membersonly = view.$('input[name="muc#roomconfig_membersonly"]');
  666. expect($membersonly.length).toBe(1);
  667. expect($membersonly.attr('type')).toBe('checkbox');
  668. $membersonly.prop('checked', true);
  669. var $moderated = view.$('input[name="muc#roomconfig_moderatedroom"]');
  670. expect($moderated.length).toBe(1);
  671. expect($moderated.attr('type')).toBe('checkbox');
  672. $moderated.prop('checked', true);
  673. var $password = view.$('input[name="muc#roomconfig_roomsecret"]');
  674. expect($password.length).toBe(1);
  675. expect($password.attr('type')).toBe('password');
  676. var $allowpm = view.$('select[name="muc#roomconfig_allowpm"]');
  677. expect($allowpm.length).toBe(1);
  678. $allowpm.val('moderators');
  679. var $presencebroadcast = view.$('select[name="muc#roomconfig_presencebroadcast"]');
  680. expect($presencebroadcast.length).toBe(1);
  681. $presencebroadcast.val(['moderator']);
  682. view.$('input[type="submit"]').click();
  683. var $sent_stanza = $(sent_IQ.toLocaleString());
  684. expect($sent_stanza.find('field[var="muc#roomconfig_membersonly"] value').text()).toBe('1');
  685. expect($sent_stanza.find('field[var="muc#roomconfig_moderatedroom"] value').text()).toBe('1');
  686. expect($sent_stanza.find('field[var="muc#roomconfig_allowpm"] value').text()).toBe('moderators');
  687. expect($sent_stanza.find('field[var="muc#roomconfig_presencebroadcast"] value').text()).toBe('moderator');
  688. }));
  689. it("shows users currently present in the room", mock.initConverse(function (_converse) {
  690. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  691. var name;
  692. var view = _converse.chatboxviews.get('lounge@localhost'),
  693. $occupants = view.$('.occupant-list');
  694. var presence, role;
  695. for (var i=0; i<mock.chatroom_names.length; i++) {
  696. name = mock.chatroom_names[i];
  697. role = mock.chatroom_roles[name].role;
  698. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  699. presence = $pres({
  700. to:'dummy@localhost/pda',
  701. from:'lounge@localhost/'+name
  702. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  703. .c('item').attrs({
  704. affiliation: mock.chatroom_roles[name].affiliation,
  705. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  706. role: role
  707. }).up()
  708. .c('status').attrs({code:'110'}).nodeTree;
  709. _converse.connection._dataRecv(test_utils.createRequest(presence));
  710. expect($occupants.find('li').length).toBe(2+i);
  711. expect($($occupants.find('li')[i+1]).text()).toBe(mock.chatroom_names[i]);
  712. expect($($occupants.find('li')[i+1]).hasClass('moderator')).toBe(role === "moderator");
  713. }
  714. // Test users leaving the room
  715. // http://xmpp.org/extensions/xep-0045.html#exit
  716. for (i=mock.chatroom_names.length-1; i>-1; i--) {
  717. name = mock.chatroom_names[i];
  718. role = mock.chatroom_roles[name].role;
  719. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  720. presence = $pres({
  721. to:'dummy@localhost/pda',
  722. from:'lounge@localhost/'+name,
  723. type: 'unavailable'
  724. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  725. .c('item').attrs({
  726. affiliation: mock.chatroom_roles[name].affiliation,
  727. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  728. role: 'none'
  729. }).nodeTree;
  730. _converse.connection._dataRecv(test_utils.createRequest(presence));
  731. expect($occupants.find('li').length).toBe(i+1);
  732. }
  733. }));
  734. it("escapes occupant nicknames when rendering them, to avoid JS-injection attacks", mock.initConverse(function (_converse) {
  735. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  736. /* <presence xmlns="jabber:client" to="jc@chat.example.org/converse.js-17184538"
  737. * from="oo@conference.chat.example.org/&lt;img src=&quot;x&quot; onerror=&quot;alert(123)&quot;/&gt;">
  738. * <x xmlns="http://jabber.org/protocol/muc#user">
  739. * <item jid="jc@chat.example.org/converse.js-17184538" affiliation="owner" role="moderator"/>
  740. * <status code="110"/>
  741. * </x>
  742. * </presence>"
  743. */
  744. var presence = $pres({
  745. to:'dummy@localhost/pda',
  746. from:"lounge@localhost/&lt;img src=&quot;x&quot; onerror=&quot;alert(123)&quot;/&gt;"
  747. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  748. .c('item').attrs({
  749. jid: 'someone@localhost',
  750. role: 'moderator',
  751. }).up()
  752. .c('status').attrs({code:'110'}).nodeTree;
  753. _converse.connection._dataRecv(test_utils.createRequest(presence));
  754. var view = _converse.chatboxviews.get('lounge@localhost');
  755. var occupant = view.$el.find('.occupant-list').find('li');
  756. expect(occupant.length).toBe(2);
  757. expect($(occupant).last().text()).toBe("&lt;img src=&quot;x&quot; onerror=&quot;alert(123)&quot;/&gt;");
  758. }));
  759. it("indicates moderators by means of a special css class and tooltip", mock.initConverse(function (_converse) {
  760. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  761. var view = _converse.chatboxviews.get('lounge@localhost');
  762. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  763. var presence = $pres({
  764. to:'dummy@localhost/pda',
  765. from:'lounge@localhost/moderatorman'
  766. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  767. .c('item').attrs({
  768. affiliation: 'admin',
  769. jid: contact_jid,
  770. role: 'moderator',
  771. }).up()
  772. .c('status').attrs({code:'110'}).nodeTree;
  773. _converse.connection._dataRecv(test_utils.createRequest(presence));
  774. var occupant = view.$el.find('.occupant-list').find('li');
  775. expect(occupant.length).toBe(2);
  776. expect($(occupant).first().text()).toBe("dummy");
  777. expect($(occupant).last().text()).toBe("moderatorman");
  778. expect($(occupant).last().attr('class').indexOf('moderator')).not.toBe(-1);
  779. expect($(occupant).last().attr('title')).toBe(contact_jid + ' This user is a moderator. Click to mention moderatorman in your message.');
  780. }));
  781. it("will use the user's reserved nickname, if it exists", mock.initConverse(function (_converse) {
  782. var sent_IQ, IQ_id;
  783. var sendIQ = _converse.connection.sendIQ;
  784. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  785. sent_IQ = iq;
  786. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  787. });
  788. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  789. // We pretend this is a new room, so no disco info is returned.
  790. var features_stanza = $iq({
  791. from: 'lounge@localhost',
  792. 'id': IQ_id,
  793. 'to': 'dummy@localhost/desktop',
  794. 'type': 'error'
  795. }).c('error', {'type': 'cancel'})
  796. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  797. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  798. var view = _converse.chatboxviews.get('lounge@localhost');
  799. spyOn(view, 'join').and.callThrough();
  800. /* <iq from='hag66@shakespeare.lit/pda'
  801. * id='getnick1'
  802. * to='coven@chat.shakespeare.lit'
  803. * type='get'>
  804. * <query xmlns='http://jabber.org/protocol/disco#info'
  805. * node='x-roomuser-item'/>
  806. * </iq>
  807. */
  808. expect(sent_IQ.toLocaleString()).toBe(
  809. "<iq to='lounge@localhost' from='dummy@localhost/resource' "+
  810. "type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  811. "<query xmlns='http://jabber.org/protocol/disco#info' node='x-roomuser-item'/></iq>"
  812. );
  813. /* <iq from='coven@chat.shakespeare.lit'
  814. * id='getnick1'
  815. * to='hag66@shakespeare.lit/pda'
  816. * type='result'>
  817. * <query xmlns='http://jabber.org/protocol/disco#info'
  818. * node='x-roomuser-item'>
  819. * <identity
  820. * category='conference'
  821. * name='thirdwitch'
  822. * type='text'/>
  823. * </query>
  824. * </iq>
  825. */
  826. var stanza = $iq({
  827. 'type': 'result',
  828. 'id': IQ_id,
  829. 'from': view.model.get('jid'),
  830. 'to': _converse.connection.jid
  831. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info', 'node': 'x-roomuser-item'})
  832. .c('identity', {'category': 'conference', 'name': 'thirdwitch', 'type': 'text'});
  833. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  834. expect(view.join).toHaveBeenCalled();
  835. // The user has just entered the room (because join was called)
  836. // and receives their own presence from the server.
  837. // See example 24:
  838. // http://xmpp.org/extensions/xep-0045.html#enter-pres
  839. var presence = $pres({
  840. to:'dummy@localhost/resource',
  841. from:'lounge@localhost/thirdwitch',
  842. id:'DC352437-C019-40EC-B590-AF29E879AF97'
  843. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  844. .c('item').attrs({
  845. affiliation: 'member',
  846. jid: 'dummy@localhost/resource',
  847. role: 'occupant'
  848. }).up()
  849. .c('status').attrs({code:'110'}).up()
  850. .c('status').attrs({code:'210'}).nodeTree;
  851. _converse.connection._dataRecv(test_utils.createRequest(presence));
  852. var info_text = view.$el.find('.chat-content .chat-info').text();
  853. expect(info_text).toBe('Your nickname has been automatically set to: thirdwitch');
  854. }));
  855. it("allows the user to invite their roster contacts to enter the chat room", mock.initConverseWithAsync(function (done, _converse) {
  856. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  857. test_utils.createContacts(_converse, 'current'); // We need roster contacts, so that we have someone to invite
  858. // Since we don't actually fetch roster contacts, we need to
  859. // cheat here and emit the event.
  860. _converse.emit('rosterContactsFetched');
  861. spyOn(_converse, 'emit');
  862. spyOn(window, 'prompt').and.callFake(function () {
  863. return "Please join!";
  864. });
  865. var view = _converse.chatboxviews.get('lounge@localhost');
  866. // XXX: cheating a lttle bit, normally this'll be set after
  867. // receiving the features for the room.
  868. view.model.set('open', 'true');
  869. spyOn(view, 'directInvite').and.callThrough();
  870. var $input;
  871. view.$el.find('.chat-area').remove();
  872. test_utils.waitUntil(function () {
  873. return view.$el.find('input.invited-contact').length;
  874. }, 300)
  875. .then(function () {
  876. var $input = view.$el.find('input.invited-contact');
  877. expect($input.attr('placeholder')).toBe('Invite');
  878. $input.val("Felix");
  879. var evt;
  880. // check if Event() is a constructor function
  881. // usage as per the spec, if true
  882. if (typeof(Event) === 'function') {
  883. evt = new Event('input');
  884. } else { // the deprecated way for PhantomJS
  885. evt = document.createEvent('CustomEvent');
  886. evt.initCustomEvent('input', false, false, null);
  887. }
  888. $input[0].dispatchEvent(evt);
  889. var sent_stanza;
  890. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  891. sent_stanza = stanza;
  892. });
  893. var $hint = $input.siblings('ul').children('li');
  894. expect($input.val()).toBe('Felix');
  895. expect($hint[0].textContent).toBe('Felix Amsel');
  896. expect($hint.length).toBe(1);
  897. if (typeof(Event) === 'function') {
  898. // Not working on PhantomJS
  899. evt = new Event('mousedown', {'bubbles': true});
  900. evt.button = 0; // For some reason awesomplete wants this
  901. $hint[0].dispatchEvent(evt);
  902. expect(window.prompt).toHaveBeenCalled();
  903. expect(view.directInvite).toHaveBeenCalled();
  904. expect(sent_stanza.toLocaleString()).toBe(
  905. "<message from='dummy@localhost/resource' to='felix.amsel@localhost' id='" +
  906. sent_stanza.nodeTree.getAttribute('id') +
  907. "' xmlns='jabber:client'>"+
  908. "<x xmlns='jabber:x:conference' jid='lounge@localhost' reason='Please join!'/>"+
  909. "</message>"
  910. );
  911. }
  912. done();
  913. });
  914. }));
  915. it("can be joined automatically, based upon a received invite", mock.initConverse(function (_converse) {
  916. test_utils.createContacts(_converse, 'current'); // We need roster contacts, who can invite us
  917. spyOn(window, 'confirm').and.callFake(function () {
  918. return true;
  919. });
  920. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  921. var view = _converse.chatboxviews.get('lounge@localhost');
  922. view.close();
  923. view.model.destroy(); // Manually calling this, otherwise we have to mock stanzas.
  924. var name = mock.cur_names[0];
  925. var from_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  926. var room_jid = 'lounge@localhost';
  927. var reason = "Please join this chat room";
  928. var message = $(
  929. "<message from='"+from_jid+"' to='"+_converse.bare_jid+"'>" +
  930. "<x xmlns='jabber:x:conference'" +
  931. "jid='"+room_jid+"'" +
  932. "reason='"+reason+"'/>"+
  933. "</message>"
  934. )[0];
  935. expect(_converse.chatboxes.models.length).toBe(1);
  936. expect(_converse.chatboxes.models[0].id).toBe("controlbox");
  937. _converse.onDirectMUCInvitation(message);
  938. expect(window.confirm).toHaveBeenCalledWith(
  939. name + ' has invited you to join a chat room: '+ room_jid +
  940. ', and left the following reason: "'+reason+'"');
  941. expect(_converse.chatboxes.models.length).toBe(2);
  942. expect(_converse.chatboxes.models[0].id).toBe('controlbox');
  943. expect(_converse.chatboxes.models[1].id).toBe(room_jid);
  944. }));
  945. it("shows received groupchat messages", mock.initConverse(function (_converse) {
  946. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  947. spyOn(_converse, 'emit');
  948. var view = _converse.chatboxviews.get('lounge@localhost');
  949. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  950. var nick = mock.chatroom_names[0];
  951. var text = 'This is a received message';
  952. var message = $msg({
  953. from: 'lounge@localhost/'+nick,
  954. id: '1',
  955. to: 'dummy@localhost',
  956. type: 'groupchat'
  957. }).c('body').t(text);
  958. view.onChatRoomMessage(message.nodeTree);
  959. var $chat_content = view.$el.find('.chat-content');
  960. expect($chat_content.find('.chat-message').length).toBe(1);
  961. expect($chat_content.find('.chat-msg-content').text()).toBe(text);
  962. expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
  963. }));
  964. it("shows sent groupchat messages", mock.initConverse(function (_converse) {
  965. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  966. spyOn(_converse, 'emit');
  967. var view = _converse.chatboxviews.get('lounge@localhost');
  968. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  969. var text = 'This is a sent message';
  970. view.$el.find('.chat-textarea').text(text);
  971. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  972. expect(_converse.emit).toHaveBeenCalledWith('messageSend', text);
  973. var $chat_content = view.$el.find('.chat-content');
  974. expect($chat_content.find('.chat-message').length).toBe(1);
  975. // Let's check that if we receive the same message again, it's
  976. // not shown.
  977. var message = $msg({
  978. from: 'lounge@localhost/dummy',
  979. to: 'dummy@localhost.com',
  980. type: 'groupchat',
  981. id: view.model.messages.at(0).get('msgid')
  982. }).c('body').t(text);
  983. view.onChatRoomMessage(message.nodeTree);
  984. expect($chat_content.find('.chat-message').length).toBe(1);
  985. expect($chat_content.find('.chat-msg-content').last().text()).toBe(text);
  986. // We don't emit an event if it's our own message
  987. expect(_converse.emit.calls.count(), 1);
  988. }));
  989. it("will cause the chat area to be scrolled down only if it was at the bottom already", mock.initConverseWithAsync(function (done, _converse) {
  990. var message = 'This message is received while the chat area is scrolled up';
  991. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  992. var view = _converse.chatboxviews.get('lounge@localhost');
  993. spyOn(view, 'scrollDown').and.callThrough();
  994. /* Create enough messages so that there's a
  995. * scrollbar.
  996. */
  997. for (var i=0; i<20; i++) {
  998. view.handleMUCMessage(
  999. $msg({
  1000. from: 'lounge@localhost/someone',
  1001. to: 'dummy@localhost.com',
  1002. type: 'groupchat',
  1003. id: (new Date()).getTime(),
  1004. }).c('body').t('Message: '+i).tree());
  1005. }
  1006. // Give enough time for `markScrolled` to have been called
  1007. setTimeout(function () {
  1008. view.$content.scrollTop(0);
  1009. view.handleMUCMessage(
  1010. $msg({
  1011. from: 'lounge@localhost/someone',
  1012. to: 'dummy@localhost.com',
  1013. type: 'groupchat',
  1014. id: (new Date()).getTime(),
  1015. }).c('body').t(message).tree());
  1016. // Now check that the message appears inside the chatbox in the DOM
  1017. var $chat_content = view.$el.find('.chat-content');
  1018. var msg_txt = $chat_content.find('.chat-message:last').find('.chat-msg-content').text();
  1019. expect(msg_txt).toEqual(message);
  1020. expect(view.$content.scrollTop()).toBe(0);
  1021. done();
  1022. }, 500);
  1023. }));
  1024. it("shows received chatroom subject messages", mock.initConverse(function (_converse) {
  1025. test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc');
  1026. var text = 'Jabber/XMPP Development | RFCs and Extensions: http://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  1027. var stanza = Strophe.xmlHtmlNode(
  1028. '<message xmlns="jabber:client" to="jc@opkode.com/_converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">'+
  1029. ' <subject>'+text+'</subject>'+
  1030. ' <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>'+
  1031. ' <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>'+
  1032. '</message>').firstChild;
  1033. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1034. var view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1035. var $chat_content = view.$el.find('.chat-content');
  1036. expect($chat_content.find('.chat-info:last').text()).toBe('Topic set by ralphm to: '+text);
  1037. }));
  1038. it("escapes the subject before rendering it, to avoid JS-injection attacks", mock.initConverse(function (_converse) {
  1039. test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc');
  1040. spyOn(window, 'alert');
  1041. var subject = '<img src="x" onerror="alert(\'XSS\');"/>';
  1042. var view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1043. view.setChatRoomSubject('ralphm', subject);
  1044. var $chat_content = view.$el.find('.chat-content');
  1045. expect($chat_content.find('.chat-info:last').text()).toBe('Topic set by ralphm to: '+subject);
  1046. }));
  1047. it("informs users if their nicknames has been changed.", mock.initConverse(function (_converse) {
  1048. /* The service then sends two presence stanzas to the full JID
  1049. * of each occupant (including the occupant who is changing his
  1050. * or her room nickname), one of type "unavailable" for the old
  1051. * nickname and one indicating availability for the new
  1052. * nickname.
  1053. *
  1054. * See: http://xmpp.org/extensions/xep-0045.html#changenick
  1055. *
  1056. * <presence
  1057. * from='coven@localhost/thirdwitch'
  1058. * id='DC352437-C019-40EC-B590-AF29E879AF98'
  1059. * to='hag66@shakespeare.lit/pda'
  1060. * type='unavailable'>
  1061. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1062. * <item affiliation='member'
  1063. * jid='hag66@shakespeare.lit/pda'
  1064. * nick='oldhag'
  1065. * role='occupant'/>
  1066. * <status code='303'/>
  1067. * <status code='110'/>
  1068. * </x>
  1069. * </presence>
  1070. *
  1071. * <presence
  1072. * from='coven@localhost/oldhag'
  1073. * id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
  1074. * to='hag66@shakespeare.lit/pda'>
  1075. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1076. * <item affiliation='member'
  1077. * jid='hag66@shakespeare.lit/pda'
  1078. * role='occupant'/>
  1079. * <status code='110'/>
  1080. * </x>
  1081. * </presence>
  1082. */
  1083. var __ = utils.__.bind(_converse);
  1084. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'oldnick');
  1085. var view = _converse.chatboxviews.get('lounge@localhost');
  1086. var $chat_content = view.$el.find('.chat-content');
  1087. // The user has just entered the room and receives their own
  1088. // presence from the server.
  1089. // See example 24:
  1090. // http://xmpp.org/extensions/xep-0045.html#enter-pres
  1091. var presence = $pres({
  1092. to:'dummy@localhost/pda',
  1093. from:'lounge@localhost/oldnick',
  1094. id:'DC352437-C019-40EC-B590-AF29E879AF97'
  1095. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1096. .c('item').attrs({
  1097. affiliation: 'member',
  1098. jid: 'dummy@localhost/pda',
  1099. role: 'occupant'
  1100. }).up()
  1101. .c('status').attrs({code:'110'}).up()
  1102. .c('status').attrs({code:'210'}).nodeTree;
  1103. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1104. var $occupants = view.$('.occupant-list');
  1105. expect($occupants.children().length).toBe(1);
  1106. expect($occupants.children().first(0).text()).toBe("oldnick");
  1107. expect($chat_content.find('div.chat-info').length).toBe(2);
  1108. expect($chat_content.find('div.chat-info:first').html()).toBe("oldnick has joined the room.");
  1109. expect($chat_content.find('div.chat-info:last').html()).toBe(__(_converse.muc.new_nickname_messages["210"], "oldnick"));
  1110. presence = $pres().attrs({
  1111. from:'lounge@localhost/oldnick',
  1112. id:'DC352437-C019-40EC-B590-AF29E879AF98',
  1113. to:'dummy@localhost/pda',
  1114. type:'unavailable'
  1115. })
  1116. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1117. .c('item').attrs({
  1118. affiliation: 'member',
  1119. jid: 'dummy@localhost/pda',
  1120. nick: 'newnick',
  1121. role: 'occupant'
  1122. }).up()
  1123. .c('status').attrs({code:'303'}).up()
  1124. .c('status').attrs({code:'110'}).nodeTree;
  1125. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1126. expect($chat_content.find('div.chat-info').length).toBe(3);
  1127. expect($chat_content.find('div.chat-info').last().html()).toBe(__(_converse.muc.new_nickname_messages["303"], "newnick"));
  1128. $occupants = view.$('.occupant-list');
  1129. expect($occupants.children().length).toBe(0);
  1130. presence = $pres().attrs({
  1131. from:'lounge@localhost/newnick',
  1132. id:'5B4F27A4-25ED-43F7-A699-382C6B4AFC67',
  1133. to:'dummy@localhost/pda'
  1134. })
  1135. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1136. .c('item').attrs({
  1137. affiliation: 'member',
  1138. jid: 'dummy@localhost/pda',
  1139. role: 'occupant'
  1140. }).up()
  1141. .c('status').attrs({code:'110'}).nodeTree;
  1142. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1143. expect($chat_content.find('div.chat-info').length).toBe(4);
  1144. expect($chat_content.find('div.chat-info').get(2).textContent).toBe(__(_converse.muc.new_nickname_messages["303"], "newnick"));
  1145. expect($chat_content.find('div.chat-info').last().html()).toBe("newnick has joined the room.");
  1146. $occupants = view.$('.occupant-list');
  1147. expect($occupants.children().length).toBe(1);
  1148. expect($occupants.children().first(0).text()).toBe("newnick");
  1149. }));
  1150. it("queries for the room information before attempting to join the user", mock.initConverse(function (_converse) {
  1151. var sent_IQ, IQ_id;
  1152. var sendIQ = _converse.connection.sendIQ;
  1153. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1154. sent_IQ = iq;
  1155. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1156. });
  1157. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  1158. // Check that the room queried for the feautures.
  1159. expect(sent_IQ.toLocaleString()).toBe(
  1160. "<iq from='dummy@localhost/resource' to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  1161. "<query xmlns='http://jabber.org/protocol/disco#info'/>"+
  1162. "</iq>");
  1163. /* <iq from='coven@chat.shakespeare.lit'
  1164. * id='ik3vs715'
  1165. * to='hag66@shakespeare.lit/pda'
  1166. * type='result'>
  1167. * <query xmlns='http://jabber.org/protocol/disco#info'>
  1168. * <identity
  1169. * category='conference'
  1170. * name='A Dark Cave'
  1171. * type='text'/>
  1172. * <feature var='http://jabber.org/protocol/muc'/>
  1173. * <feature var='muc_passwordprotected'/>
  1174. * <feature var='muc_hidden'/>
  1175. * <feature var='muc_temporary'/>
  1176. * <feature var='muc_open'/>
  1177. * <feature var='muc_unmoderated'/>
  1178. * <feature var='muc_nonanonymous'/>
  1179. * </query>
  1180. * </iq>
  1181. */
  1182. var features_stanza = $iq({
  1183. from: 'coven@chat.shakespeare.lit',
  1184. 'id': IQ_id,
  1185. 'to': 'dummy@localhost/desktop',
  1186. 'type': 'result'
  1187. })
  1188. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  1189. .c('identity', {
  1190. 'category': 'conference',
  1191. 'name': 'A Dark Cave',
  1192. 'type': 'text'
  1193. }).up()
  1194. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  1195. .c('feature', {'var': 'muc_passwordprotected'}).up()
  1196. .c('feature', {'var': 'muc_hidden'}).up()
  1197. .c('feature', {'var': 'muc_temporary'}).up()
  1198. .c('feature', {'var': 'muc_open'}).up()
  1199. .c('feature', {'var': 'muc_unmoderated'}).up()
  1200. .c('feature', {'var': 'muc_nonanonymous'});
  1201. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1202. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1203. expect(view.model.get('features_fetched')).toBe(true);
  1204. expect(view.model.get('passwordprotected')).toBe(true);
  1205. expect(view.model.get('hidden')).toBe(true);
  1206. expect(view.model.get('temporary')).toBe(true);
  1207. expect(view.model.get('open')).toBe(true);
  1208. expect(view.model.get('unmoderated')).toBe(true);
  1209. expect(view.model.get('nonanonymous')).toBe(true);
  1210. }));
  1211. it("updates the shown features when the room configuration has changed", mock.initConverse(function (_converse) {
  1212. var sent_IQ, IQ_id;
  1213. var sendIQ = _converse.connection.sendIQ;
  1214. test_utils.openAndEnterChatRoom(_converse, 'room', 'conference.example.org', 'dummy');
  1215. var view = _converse.chatboxviews.get('room@conference.example.org');
  1216. view.model.set({
  1217. 'passwordprotected': false,
  1218. 'unsecured': true,
  1219. 'hidden': false,
  1220. 'public': true,
  1221. 'membersonly': false,
  1222. 'open': true,
  1223. 'persistent': false,
  1224. 'temporary': true,
  1225. 'nonanonymous': true,
  1226. 'semianonymous': false,
  1227. 'moderated': false,
  1228. 'unmoderated': true
  1229. });
  1230. expect(view.model.get('persistent')).toBe(false);
  1231. expect(view.model.get('temporary')).toBe(true);
  1232. view.model.set({'persistent': true});
  1233. expect(view.model.get('persistent')).toBe(true);
  1234. expect(view.model.get('temporary')).toBe(false);
  1235. expect(view.model.get('unsecured')).toBe(true);
  1236. expect(view.model.get('passwordprotected')).toBe(false);
  1237. view.model.set({'passwordprotected': true});
  1238. expect(view.model.get('unsecured')).toBe(false);
  1239. expect(view.model.get('passwordprotected')).toBe(true);
  1240. expect(view.model.get('unmoderated')).toBe(true);
  1241. expect(view.model.get('moderated')).toBe(false);
  1242. view.model.set({'moderated': true});
  1243. expect(view.model.get('unmoderated')).toBe(false);
  1244. expect(view.model.get('moderated')).toBe(true);
  1245. expect(view.model.get('nonanonymous')).toBe(true);
  1246. expect(view.model.get('semianonymous')).toBe(false);
  1247. view.model.set({'nonanonymous': false});
  1248. expect(view.model.get('nonanonymous')).toBe(false);
  1249. expect(view.model.get('semianonymous')).toBe(true);
  1250. expect(view.model.get('open')).toBe(true);
  1251. expect(view.model.get('membersonly')).toBe(false);
  1252. view.model.set({'membersonly': true});
  1253. expect(view.model.get('open')).toBe(false);
  1254. expect(view.model.get('membersonly')).toBe(true);
  1255. }));
  1256. it("indicates when a room is no longer anonymous", mock.initConverse(function (_converse) {
  1257. var sent_IQ, IQ_id;
  1258. var sendIQ = _converse.connection.sendIQ;
  1259. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1260. sent_IQ = iq;
  1261. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1262. });
  1263. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  1264. // We pretend this is a new room, so no disco info is returned.
  1265. var features_stanza = $iq({
  1266. from: 'coven@chat.shakespeare.lit',
  1267. 'id': IQ_id,
  1268. 'to': 'dummy@localhost/desktop',
  1269. 'type': 'error'
  1270. }).c('error', {'type': 'cancel'})
  1271. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1272. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1273. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1274. /* <message xmlns="jabber:client"
  1275. * type="groupchat"
  1276. * to="dummy@localhost/_converse.js-27854181"
  1277. * from="coven@chat.shakespeare.lit">
  1278. * <x xmlns="http://jabber.org/protocol/muc#user">
  1279. * <status code="104"/>
  1280. * <status code="172"/>
  1281. * </x>
  1282. * </message>
  1283. */
  1284. var message = $msg({
  1285. type:'groupchat',
  1286. to: 'dummy@localhost/_converse.js-27854181',
  1287. from: 'coven@chat.shakespeare.lit'
  1288. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  1289. .c('status', {code: '104'}).up()
  1290. .c('status', {code: '172'});
  1291. _converse.connection._dataRecv(test_utils.createRequest(message));
  1292. var $chat_body = view.$('.chatroom-body');
  1293. expect($chat_body.html().trim().indexOf(
  1294. '<div class="chat-info">This room is now no longer anonymous</div>'
  1295. )).not.toBe(-1);
  1296. }));
  1297. it("informs users if they have been kicked out of the chat room", mock.initConverse(function (_converse) {
  1298. /* <presence
  1299. * from='harfleur@chat.shakespeare.lit/pistol'
  1300. * to='pistol@shakespeare.lit/harfleur'
  1301. * type='unavailable'>
  1302. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1303. * <item affiliation='none' role='none'>
  1304. * <actor nick='Fluellen'/>
  1305. * <reason>Avaunt, you cullion!</reason>
  1306. * </item>
  1307. * <status code='110'/>
  1308. * <status code='307'/>
  1309. * </x>
  1310. * </presence>
  1311. */
  1312. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1313. var presence = $pres().attrs({
  1314. from:'lounge@localhost/dummy',
  1315. to:'dummy@localhost/pda',
  1316. type:'unavailable'
  1317. })
  1318. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1319. .c('item').attrs({
  1320. affiliation: 'none',
  1321. jid: 'dummy@localhost/pda',
  1322. role: 'none'
  1323. })
  1324. .c('actor').attrs({nick: 'Fluellen'}).up()
  1325. .c('reason').t('Avaunt, you cullion!').up()
  1326. .up()
  1327. .c('status').attrs({code:'110'}).up()
  1328. .c('status').attrs({code:'307'}).nodeTree;
  1329. var view = _converse.chatboxviews.get('lounge@localhost');
  1330. view.onChatRoomPresence(presence);
  1331. expect(view.$('.chat-area').is(':visible')).toBeFalsy();
  1332. expect(view.$('.occupants').is(':visible')).toBeFalsy();
  1333. var $chat_body = view.$('.chatroom-body');
  1334. expect($chat_body.find('.disconnect-msg').text()).toBe(
  1335. 'You have been kicked from this room'+
  1336. 'This action was done by Fluellen.'+
  1337. 'The reason given is: "Avaunt, you cullion!".'
  1338. );
  1339. }));
  1340. it("can be saved to, and retrieved from, browserStorage", mock.initConverse(function (_converse) {
  1341. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1342. // We instantiate a new ChatBoxes collection, which by default
  1343. // will be empty.
  1344. test_utils.openControlBox();
  1345. var newchatboxes = new _converse.ChatBoxes();
  1346. expect(newchatboxes.length).toEqual(0);
  1347. // The chatboxes will then be fetched from browserStorage inside the
  1348. // onConnected method
  1349. newchatboxes.onConnected();
  1350. expect(newchatboxes.length).toEqual(2);
  1351. // Check that the chatrooms retrieved from browserStorage
  1352. // have the same attributes values as the original ones.
  1353. var attrs = ['id', 'box_id', 'visible'];
  1354. var new_attrs, old_attrs;
  1355. for (var i=0; i<attrs.length; i++) {
  1356. new_attrs = _.map(_.map(newchatboxes.models, 'attributes'), attrs[i]);
  1357. old_attrs = _.map(_.map(_converse.chatboxes.models, 'attributes'), attrs[i]);
  1358. // FIXME: should have have to sort here? Order must
  1359. // probably be the same...
  1360. // This should be fixed once the controlbox always opens
  1361. // only on the right.
  1362. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  1363. }
  1364. _converse.rosterview.render();
  1365. }));
  1366. it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'", mock.initConverseWithAsync(function (done, _converse) {
  1367. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1368. var view = _converse.chatboxviews.get('lounge@localhost'),
  1369. trimmed_chatboxes = _converse.minimized_chats;
  1370. spyOn(view, 'minimize').and.callThrough();
  1371. spyOn(view, 'maximize').and.callThrough();
  1372. spyOn(_converse, 'emit');
  1373. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1374. view.$el.find('.toggle-chatbox-button').click();
  1375. expect(view.minimize).toHaveBeenCalled();
  1376. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMinimized', jasmine.any(Object));
  1377. expect(view.$el.is(':visible')).toBeFalsy();
  1378. expect(view.model.get('minimized')).toBeTruthy();
  1379. expect(view.minimize).toHaveBeenCalled();
  1380. var trimmedview = trimmed_chatboxes.get(view.model.get('id'));
  1381. trimmedview.$("a.restore-chat").click();
  1382. test_utils.waitUntil(function () {
  1383. return view.$el.is(':visible');
  1384. }, 300)
  1385. .then(function () {
  1386. expect(view.maximize).toHaveBeenCalled();
  1387. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMaximized', jasmine.any(Object));
  1388. expect(view.$el.is(':visible')).toBeTruthy();
  1389. expect(view.model.get('minimized')).toBeFalsy();
  1390. expect(_converse.emit.calls.count(), 3);
  1391. done();
  1392. });
  1393. }));
  1394. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", mock.initConverse(function (_converse) {
  1395. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1396. var view = _converse.chatboxviews.get('lounge@localhost');
  1397. spyOn(view, 'close').and.callThrough();
  1398. spyOn(_converse, 'emit');
  1399. spyOn(view, 'leave');
  1400. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1401. view.$el.find('.close-chatbox-button').click();
  1402. expect(view.close).toHaveBeenCalled();
  1403. expect(view.leave).toHaveBeenCalled();
  1404. // XXX: After refactoring, the chat box only gets closed
  1405. // once we have confirmation from the server. To test this,
  1406. // we would have to mock the returned presence stanza.
  1407. // See the "leave" method on the ChatRoomView.
  1408. // expect(_converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  1409. }));
  1410. });
  1411. describe("Each chat room can take special commands", function () {
  1412. it("to set the room topic", mock.initConverse(function (_converse) {
  1413. var sent_stanza;
  1414. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1415. var view = _converse.chatboxviews.get('lounge@localhost');
  1416. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1417. spyOn(view, 'clearChatRoomMessages');
  1418. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  1419. sent_stanza = stanza;
  1420. });
  1421. // Check the alias /topic
  1422. var $textarea = view.$el.find('.chat-textarea');
  1423. $textarea.text('/topic This is the room subject');
  1424. $textarea.trigger($.Event('keypress', {keyCode: 13}));
  1425. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1426. expect(_converse.connection.send).toHaveBeenCalled();
  1427. expect(sent_stanza.textContent).toBe('This is the room subject');
  1428. // Check /subject
  1429. $textarea.val('/subject This is a new subject');
  1430. $textarea.trigger($.Event('keypress', {keyCode: 13}));
  1431. expect(sent_stanza.textContent).toBe('This is a new subject');
  1432. // Check case insensitivity
  1433. //
  1434. // XXX: This works in the browser but fails on phantomjs
  1435. // expect(sent_stanza.outerHTML).toBe(
  1436. // '<message to="lounge@localhost" from="dummy@localhost/resource" type="groupchat" xmlns="jabber:client">'+
  1437. // '<subject xmlns="jabber:client">This is yet another subject</subject>'+
  1438. // '</message>');
  1439. $textarea.val('/Subject This is yet another subject');
  1440. $textarea.trigger($.Event('keypress', {keyCode: 13}));
  1441. expect(sent_stanza.textContent).toBe('This is yet another subject');
  1442. }));
  1443. it("to clear messages", mock.initConverse(function (_converse) {
  1444. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1445. var view = _converse.chatboxviews.get('lounge@localhost');
  1446. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1447. spyOn(view, 'clearChatRoomMessages');
  1448. view.$el.find('.chat-textarea').text('/clear');
  1449. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  1450. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1451. expect(view.clearChatRoomMessages).toHaveBeenCalled();
  1452. }));
  1453. it("to make a user an owner", mock.initConverse(function (_converse) {
  1454. var sent_IQ, IQ_id;
  1455. var sendIQ = _converse.connection.sendIQ;
  1456. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1457. sent_IQ = iq;
  1458. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1459. });
  1460. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1461. var view = _converse.chatboxviews.get('lounge@localhost');
  1462. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1463. spyOn(view, 'setAffiliation').and.callThrough();
  1464. spyOn(view, 'showStatusNotification').and.callThrough();
  1465. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  1466. view.$el.find('.chat-textarea').text('/owner');
  1467. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  1468. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1469. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  1470. expect(view.showStatusNotification).toHaveBeenCalledWith(
  1471. "Error: the \"owner\" command takes two arguments, the user's nickname and optionally a reason.",
  1472. true
  1473. );
  1474. expect(view.setAffiliation).not.toHaveBeenCalled();
  1475. // Call now with the correct amount of arguments.
  1476. // XXX: Calling onMessageSubmitted directly, trying
  1477. // again via triggering Event doesn't work for some weird
  1478. // reason.
  1479. view.onMessageSubmitted('/owner annoyingGuy@localhost You\'re annoying');
  1480. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  1481. expect(view.showStatusNotification.calls.count()).toBe(1);
  1482. expect(view.setAffiliation).toHaveBeenCalled();
  1483. // Check that the member list now gets updated
  1484. expect(sent_IQ.toLocaleString()).toBe(
  1485. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  1486. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  1487. "<item affiliation='owner' jid='annoyingGuy@localhost'>"+
  1488. "<reason>You&apos;re annoying</reason>"+
  1489. "</item>"+
  1490. "</query>"+
  1491. "</iq>");
  1492. }));
  1493. it("to ban a user", mock.initConverse(function (_converse) {
  1494. var sent_IQ, IQ_id;
  1495. var sendIQ = _converse.connection.sendIQ;
  1496. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1497. sent_IQ = iq;
  1498. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1499. });
  1500. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1501. var view = _converse.chatboxviews.get('lounge@localhost');
  1502. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1503. spyOn(view, 'setAffiliation').and.callThrough();
  1504. spyOn(view, 'showStatusNotification').and.callThrough();
  1505. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  1506. view.$el.find('.chat-textarea').text('/ban');
  1507. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  1508. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1509. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  1510. expect(view.showStatusNotification).toHaveBeenCalledWith(
  1511. "Error: the \"ban\" command takes two arguments, the user's nickname and optionally a reason.",
  1512. true
  1513. );
  1514. expect(view.setAffiliation).not.toHaveBeenCalled();
  1515. // Call now with the correct amount of arguments.
  1516. // XXX: Calling onMessageSubmitted directly, trying
  1517. // again via triggering Event doesn't work for some weird
  1518. // reason.
  1519. view.onMessageSubmitted('/ban annoyingGuy@localhost You\'re annoying');
  1520. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  1521. expect(view.showStatusNotification.calls.count()).toBe(1);
  1522. expect(view.setAffiliation).toHaveBeenCalled();
  1523. // Check that the member list now gets updated
  1524. expect(sent_IQ.toLocaleString()).toBe(
  1525. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  1526. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  1527. "<item affiliation='outcast' jid='annoyingGuy@localhost'>"+
  1528. "<reason>You&apos;re annoying</reason>"+
  1529. "</item>"+
  1530. "</query>"+
  1531. "</iq>");
  1532. }));
  1533. });
  1534. describe("When attempting to enter a chatroom", function () {
  1535. var submitRoomForm = function (_converse) {
  1536. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  1537. var $input = roomspanel.$el.find('input.new-chatroom-name');
  1538. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  1539. var $server = roomspanel.$el.find('input.new-chatroom-server');
  1540. $input.val('problematic');
  1541. $nick.val('dummy');
  1542. $server.val('muc.localhost');
  1543. roomspanel.$el.find('form').submit();
  1544. };
  1545. it("will show an error message if the room requires a password", mock.initConverse(function (_converse) {
  1546. submitRoomForm(_converse);
  1547. var presence = $pres().attrs({
  1548. from:'lounge@localhost/thirdwitch',
  1549. id:'n13mt3l',
  1550. to:'dummy@localhost/pda',
  1551. type:'error'})
  1552. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  1553. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  1554. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  1555. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  1556. spyOn(view, 'renderPasswordForm').and.callThrough();
  1557. view.onChatRoomPresence(presence);
  1558. var $chat_body = view.$el.find('.chatroom-body');
  1559. expect(view.renderPasswordForm).toHaveBeenCalled();
  1560. expect($chat_body.find('form.chatroom-form').length).toBe(1);
  1561. expect($chat_body.find('legend').text()).toBe('This chatroom requires a password');
  1562. }));
  1563. it("will show an error message if the room is members-only and the user not included", mock.initConverse(function (_converse) {
  1564. submitRoomForm(_converse);
  1565. var presence = $pres().attrs({
  1566. from:'lounge@localhost/thirdwitch',
  1567. id:'n13mt3l',
  1568. to:'dummy@localhost/pda',
  1569. type:'error'})
  1570. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  1571. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  1572. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  1573. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  1574. spyOn(view, 'showErrorMessage').and.callThrough();
  1575. view.onChatRoomPresence(presence);
  1576. expect(view.$el.find('.chatroom-body p:last').text()).toBe('You are not on the member list of this room.');
  1577. }));
  1578. it("will show an error message if the user has been banned", mock.initConverse(function (_converse) {
  1579. submitRoomForm(_converse);
  1580. var presence = $pres().attrs({
  1581. from:'lounge@localhost/thirdwitch',
  1582. id:'n13mt3l',
  1583. to:'dummy@localhost/pda',
  1584. type:'error'})
  1585. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  1586. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  1587. .c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  1588. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  1589. spyOn(view, 'showErrorMessage').and.callThrough();
  1590. view.onChatRoomPresence(presence);
  1591. expect(view.$el.find('.chatroom-body p:last').text()).toBe('You have been banned from this room.');
  1592. }));
  1593. it("will render a nickname form if a nickname conflict happens and muc_nickname_from_jid=false", mock.initConverse(function (_converse) {
  1594. submitRoomForm(_converse);
  1595. var presence = $pres().attrs({
  1596. from:'lounge@localhost/thirdwitch',
  1597. id:'n13mt3l',
  1598. to:'dummy@localhost/pda',
  1599. type:'error'})
  1600. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  1601. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  1602. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  1603. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  1604. spyOn(view, 'showErrorMessage').and.callThrough();
  1605. view.onChatRoomPresence(presence);
  1606. expect(view.$el.find('.chatroom-body form.chatroom-form label:first').text()).toBe('Please choose your nickname');
  1607. }));
  1608. it("will automatically choose a new nickname if a nickname conflict happens and muc_nickname_from_jid=true", mock.initConverse(function (_converse) {
  1609. /* <presence
  1610. * from='coven@chat.shakespeare.lit/thirdwitch'
  1611. * id='n13mt3l'
  1612. * to='hag66@shakespeare.lit/pda'
  1613. * type='error'>
  1614. * <x xmlns='http://jabber.org/protocol/muc'/>
  1615. * <error by='coven@chat.shakespeare.lit' type='cancel'>
  1616. * <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  1617. * </error>
  1618. * </presence>
  1619. */
  1620. submitRoomForm(_converse);
  1621. _converse.muc_nickname_from_jid = true;
  1622. var attrs = {
  1623. from:'lounge@localhost/dummy',
  1624. id:'n13mt3l',
  1625. to:'dummy@localhost/pda',
  1626. type:'error'
  1627. };
  1628. var presence = $pres().attrs(attrs)
  1629. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  1630. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  1631. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  1632. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  1633. spyOn(view, 'showErrorMessage').and.callThrough();
  1634. spyOn(view, 'join').and.callThrough();
  1635. // Simulate repeatedly that there's already someone in the room
  1636. // with that nickname
  1637. view.onChatRoomPresence(presence);
  1638. expect(view.join).toHaveBeenCalledWith('dummy-2');
  1639. attrs.from = 'lounge@localhost/dummy-2';
  1640. presence = $pres().attrs(attrs)
  1641. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  1642. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  1643. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  1644. view.onChatRoomPresence(presence);
  1645. expect(view.join).toHaveBeenCalledWith('dummy-3');
  1646. attrs.from = 'lounge@localhost/dummy-3';
  1647. presence = $pres().attrs(attrs)
  1648. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  1649. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  1650. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  1651. view.onChatRoomPresence(presence);
  1652. expect(view.join).toHaveBeenCalledWith('dummy-4');
  1653. }));
  1654. it("will show an error message if the user is not allowed to have created the room", mock.initConverse(function (_converse) {
  1655. submitRoomForm(_converse);
  1656. var presence = $pres().attrs({
  1657. from:'lounge@localhost/thirdwitch',
  1658. id:'n13mt3l',
  1659. to:'dummy@localhost/pda',
  1660. type:'error'})
  1661. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  1662. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  1663. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  1664. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  1665. spyOn(view, 'showErrorMessage').and.callThrough();
  1666. view.onChatRoomPresence(presence);
  1667. expect(view.$el.find('.chatroom-body p:last').text()).toBe('You are not allowed to create new rooms.');
  1668. }));
  1669. it("will show an error message if the user's nickname doesn't conform to room policy", mock.initConverse(function (_converse) {
  1670. submitRoomForm(_converse);
  1671. var presence = $pres().attrs({
  1672. from:'lounge@localhost/thirdwitch',
  1673. id:'n13mt3l',
  1674. to:'dummy@localhost/pda',
  1675. type:'error'})
  1676. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  1677. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  1678. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  1679. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  1680. spyOn(view, 'showErrorMessage').and.callThrough();
  1681. view.onChatRoomPresence(presence);
  1682. expect(view.$el.find('.chatroom-body p:last').text()).toBe("Your nickname doesn't conform to this room's policies.");
  1683. }));
  1684. it("will show an error message if the room doesn't yet exist", mock.initConverse(function (_converse) {
  1685. submitRoomForm(_converse);
  1686. var presence = $pres().attrs({
  1687. from:'lounge@localhost/thirdwitch',
  1688. id:'n13mt3l',
  1689. to:'dummy@localhost/pda',
  1690. type:'error'})
  1691. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  1692. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  1693. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  1694. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  1695. spyOn(view, 'showErrorMessage').and.callThrough();
  1696. view.onChatRoomPresence(presence);
  1697. expect(view.$el.find('.chatroom-body p:last').text()).toBe("This room does not (yet) exist.");
  1698. }));
  1699. it("will show an error message if the room has reached its maximum number of occupants", mock.initConverse(function (_converse) {
  1700. submitRoomForm(_converse);
  1701. var presence = $pres().attrs({
  1702. from:'lounge@localhost/thirdwitch',
  1703. id:'n13mt3l',
  1704. to:'dummy@localhost/pda',
  1705. type:'error'})
  1706. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  1707. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  1708. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  1709. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  1710. spyOn(view, 'showErrorMessage').and.callThrough();
  1711. view.onChatRoomPresence(presence);
  1712. expect(view.$el.find('.chatroom-body p:last').text()).toBe("This room has reached its maximum number of occupants.");
  1713. }));
  1714. });
  1715. describe("Someone being invited to a chat room", function () {
  1716. it("will first be added to the member list if the chat room is members only", mock.initConverse(function (_converse) {
  1717. var sent_IQs = [], IQ_ids = [];
  1718. var sendIQ = _converse.connection.sendIQ;
  1719. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1720. sent_IQs.push(iq);
  1721. IQ_ids.push(sendIQ.bind(this)(iq, callback, errback));
  1722. });
  1723. test_utils.openChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy');
  1724. // State that the chat is members-only via the features IQ
  1725. var features_stanza = $iq({
  1726. from: 'coven@chat.shakespeare.lit',
  1727. 'id': IQ_ids.pop(),
  1728. 'to': 'dummy@localhost/desktop',
  1729. 'type': 'result'
  1730. })
  1731. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  1732. .c('identity', {
  1733. 'category': 'conference',
  1734. 'name': 'A Dark Cave',
  1735. 'type': 'text'
  1736. }).up()
  1737. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  1738. .c('feature', {'var': 'muc_hidden'}).up()
  1739. .c('feature', {'var': 'muc_temporary'}).up()
  1740. .c('feature', {'var': 'muc_membersonly'}).up();
  1741. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1742. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1743. expect(view.model.get('membersonly')).toBeTruthy();
  1744. test_utils.createContacts(_converse, 'current');
  1745. var sent_stanza,
  1746. sent_id;
  1747. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  1748. if (stanza.nodeTree && stanza.nodeTree.nodeName === 'message') {
  1749. sent_id = stanza.nodeTree.getAttribute('id');
  1750. sent_stanza = stanza;
  1751. }
  1752. });
  1753. var name = mock.cur_names[0];
  1754. var invitee_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  1755. var reason = "Please join this chat room";
  1756. view.directInvite(invitee_jid, reason);
  1757. var admin_iq_id = IQ_ids.pop();
  1758. var owner_iq_id = IQ_ids.pop();
  1759. var member_iq_id = IQ_ids.pop();
  1760. // Check in reverse order that we requested all three lists
  1761. // (member, owner and admin).
  1762. expect(sent_IQs.pop().toLocaleString()).toBe(
  1763. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+admin_iq_id+"'>"+
  1764. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  1765. "<item affiliation='admin'/>"+
  1766. "</query>"+
  1767. "</iq>");
  1768. expect(sent_IQs.pop().toLocaleString()).toBe(
  1769. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+owner_iq_id+"'>"+
  1770. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  1771. "<item affiliation='owner'/>"+
  1772. "</query>"+
  1773. "</iq>");
  1774. expect(sent_IQs.pop().toLocaleString()).toBe(
  1775. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+member_iq_id+"'>"+
  1776. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  1777. "<item affiliation='member'/>"+
  1778. "</query>"+
  1779. "</iq>");
  1780. /* Now the service sends the member list to the user
  1781. *
  1782. * <iq from='coven@chat.shakespeare.lit'
  1783. * id='member3'
  1784. * to='crone1@shakespeare.lit/desktop'
  1785. * type='result'>
  1786. * <query xmlns='http://jabber.org/protocol/muc#admin'>
  1787. * <item affiliation='member'
  1788. * jid='hag66@shakespeare.lit'
  1789. * nick='thirdwitch'
  1790. * role='participant'/>
  1791. * </query>
  1792. * </iq>
  1793. */
  1794. var member_list_stanza = $iq({
  1795. 'from': 'coven@chat.shakespeare.lit',
  1796. 'id': member_iq_id,
  1797. 'to': 'dummy@localhost/resource',
  1798. 'type': 'result'
  1799. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  1800. .c('item', {
  1801. 'affiliation': 'member',
  1802. 'jid': 'hag66@shakespeare.lit',
  1803. 'nick': 'thirdwitch',
  1804. 'role': 'participant'
  1805. });
  1806. _converse.connection._dataRecv(test_utils.createRequest(member_list_stanza));
  1807. var admin_list_stanza = $iq({
  1808. 'from': 'coven@chat.shakespeare.lit',
  1809. 'id': admin_iq_id,
  1810. 'to': 'dummy@localhost/resource',
  1811. 'type': 'result'
  1812. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  1813. .c('item', {
  1814. 'affiliation': 'admin',
  1815. 'jid': 'wiccarocks@shakespeare.lit',
  1816. 'nick': 'secondwitch'
  1817. });
  1818. _converse.connection._dataRecv(test_utils.createRequest(admin_list_stanza));
  1819. var owner_list_stanza = $iq({
  1820. 'from': 'coven@chat.shakespeare.lit',
  1821. 'id': owner_iq_id,
  1822. 'to': 'dummy@localhost/resource',
  1823. 'type': 'result'
  1824. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  1825. .c('item', {
  1826. 'affiliation': 'owner',
  1827. 'jid': 'crone1@shakespeare.lit',
  1828. });
  1829. _converse.connection._dataRecv(test_utils.createRequest(owner_list_stanza));
  1830. // Check that the member list now gets updated
  1831. expect(sent_IQs.pop().toLocaleString()).toBe(
  1832. "<iq to='coven@chat.shakespeare.lit' type='set' xmlns='jabber:client' id='"+IQ_ids.pop()+"'>"+
  1833. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  1834. "<item affiliation='member' jid='"+invitee_jid+"'>"+
  1835. "<reason>Please join this chat room</reason>"+
  1836. "</item>"+
  1837. "</query>"+
  1838. "</iq>");
  1839. // Finally check that the user gets invited.
  1840. expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
  1841. "<message from='dummy@localhost/resource' to='"+invitee_jid+"' id='"+sent_id+"' xmlns='jabber:client'>"+
  1842. "<x xmlns='jabber:x:conference' jid='coven@chat.shakespeare.lit' reason='Please join this chat room'/>"+
  1843. "</message>"
  1844. );
  1845. }));
  1846. });
  1847. describe("The affiliations delta", function () {
  1848. it("can be computed in various ways", mock.initConverse(function (_converse) {
  1849. test_utils.openChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy');
  1850. var roomview = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1851. var exclude_existing = false;
  1852. var remove_absentees = false;
  1853. var new_list = [];
  1854. var old_list = [];
  1855. var delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  1856. expect(delta.length).toBe(0);
  1857. new_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  1858. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  1859. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  1860. expect(delta.length).toBe(0);
  1861. // When remove_absentees is false, then affiliations in the old
  1862. // list which are not in the new one won't be removed.
  1863. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  1864. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  1865. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  1866. expect(delta.length).toBe(0);
  1867. // With exclude_existing set to false, any changed affiliations
  1868. // will be included in the delta (i.e. existing affiliations
  1869. // are included in the comparison).
  1870. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  1871. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  1872. expect(delta.length).toBe(1);
  1873. expect(delta[0].jid).toBe('wiccarocks@shakespeare.lit');
  1874. expect(delta[0].affiliation).toBe('member');
  1875. // To also remove affiliations from the old list which are not
  1876. // in the new list, we set remove_absentees to true
  1877. remove_absentees = true;
  1878. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  1879. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  1880. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  1881. expect(delta.length).toBe(1);
  1882. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  1883. expect(delta[0].affiliation).toBe('none');
  1884. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, [], old_list);
  1885. expect(delta.length).toBe(2);
  1886. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  1887. expect(delta[0].affiliation).toBe('none');
  1888. expect(delta[1].jid).toBe('wiccarocks@shakespeare.lit');
  1889. expect(delta[1].affiliation).toBe('none');
  1890. // To only add a user if they don't already have an
  1891. // affiliation, we set 'exclude_existing' to true
  1892. exclude_existing = true;
  1893. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  1894. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  1895. expect(delta.length).toBe(0);
  1896. }));
  1897. });
  1898. describe("The \"Rooms\" Panel", function () {
  1899. it("is opened by clicking the 'Chatrooms' tab", mock.initConverse(function (_converse) {
  1900. test_utils.openControlBox();
  1901. var cbview = _converse.chatboxviews.get('controlbox');
  1902. var $tabs = cbview.$el.find('#controlbox-tabs');
  1903. var $panels = cbview.$el.find('.controlbox-panes');
  1904. var $contacts = $panels.children().first();
  1905. var $chatrooms = $panels.children().last();
  1906. spyOn(cbview, 'switchTab').and.callThrough();
  1907. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1908. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  1909. expect($contacts.is(':visible')).toBe(false);
  1910. expect($chatrooms.is(':visible')).toBe(true);
  1911. expect(cbview.switchTab).toHaveBeenCalled();
  1912. }));
  1913. it("contains a form through which a new chatroom can be created", mock.initConverse(function (_converse) {
  1914. test_utils.openControlBox();
  1915. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  1916. var $input = roomspanel.$el.find('input.new-chatroom-name');
  1917. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  1918. var $server = roomspanel.$el.find('input.new-chatroom-server');
  1919. expect($input.length).toBe(1);
  1920. expect($server.length).toBe(1);
  1921. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  1922. spyOn(roomspanel, 'createChatRoom').and.callThrough();
  1923. spyOn(_converse.ChatRoomView.prototype, 'getRoomFeatures').and.callFake(function () {
  1924. var deferred = new $.Deferred();
  1925. deferred.resolve();
  1926. return deferred.promise();
  1927. });
  1928. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1929. $input.val('Lounge');
  1930. $nick.val('dummy');
  1931. $server.val('muc.localhost');
  1932. roomspanel.$el.find('form').submit();
  1933. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  1934. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  1935. }));
  1936. it("can list rooms publically available on the server", mock.initConverse(function (_converse) {
  1937. test_utils.openControlBox();
  1938. var panel = _converse.chatboxviews.get('controlbox').roomspanel;
  1939. $(panel.tabs).find('li').last().find('a').click(); // Click the chatrooms tab
  1940. panel.model.set({'muc_domain': 'muc.localhost'}); // Make sure the domain is set
  1941. // See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
  1942. expect($('#available-chatrooms').children('dt').length).toBe(0);
  1943. expect($('#available-chatrooms').children('dd').length).toBe(0);
  1944. var iq = $iq({
  1945. from:'muc.localhost',
  1946. to:'dummy@localhost/pda',
  1947. type:'result'
  1948. }).c('query')
  1949. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  1950. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  1951. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  1952. .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
  1953. panel.onRoomsFound(iq);
  1954. expect(panel.$('#available-chatrooms').children('dt').length).toBe(1);
  1955. expect(panel.$('#available-chatrooms').children('dt').first().text()).toBe("Rooms on muc.localhost");
  1956. expect(panel.$('#available-chatrooms').children('dd').length).toBe(4);
  1957. }));
  1958. it("shows the number of unread mentions received", mock.initConverse(function (_converse) {
  1959. var room_jid = 'kitchen@conference.shakespeare.lit';
  1960. test_utils.openAndEnterChatRoom(
  1961. _converse, 'kitchen', 'conference.shakespeare.lit', 'fires');
  1962. test_utils.openContactsPanel(_converse);
  1963. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  1964. var view = _converse.chatboxviews.get(room_jid);
  1965. view.model.set({'minimized': true});
  1966. var contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
  1967. var message = 'fires: Your attention is required';
  1968. var nick = mock.chatroom_names[0],
  1969. msg = $msg({
  1970. from: room_jid+'/'+nick,
  1971. id: (new Date()).getTime(),
  1972. to: 'dummy@localhost',
  1973. type: 'groupchat'
  1974. }).c('body').t(message).tree();
  1975. view.handleMUCMessage(msg);
  1976. expect(_.includes(roomspanel.tab_el.firstChild.classList, 'unread-msgs')).toBeTruthy();
  1977. expect(roomspanel.tab_el.querySelector('.msgs-indicator').textContent).toBe('1');
  1978. msg = $msg({
  1979. from: room_jid+'/'+nick,
  1980. id: (new Date()).getTime(),
  1981. to: 'dummy@localhost',
  1982. type: 'groupchat'
  1983. }).c('body').t(message).tree();
  1984. view.handleMUCMessage(msg);
  1985. expect(roomspanel.tab_el.querySelector('.msgs-indicator').textContent).toBe('2');
  1986. view.model.set({'minimized': false});
  1987. expect(_.includes(roomspanel.tab_el.firstChild.classList, 'unread-msgs')).toBeFalsy();
  1988. }));
  1989. });
  1990. });
  1991. }));