chatroom.js 201 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645
  1. (function (root, factory) {
  2. define(["jquery", "jasmine", "mock", "converse-core", "test-utils", "utils" ], factory);
  3. } (this, function ($, jasmine, mock, converse, test_utils, utils) {
  4. var _ = converse.env._;
  5. var $pres = converse.env.$pres;
  6. var $iq = converse.env.$iq;
  7. var $msg = converse.env.$msg;
  8. var Strophe = converse.env.Strophe;
  9. var Promise = converse.env.Promise;
  10. var moment = converse.env.moment;
  11. var sizzle = converse.env.sizzle;
  12. var u = converse.env.utils;
  13. return describe("ChatRooms", function () {
  14. describe("The \"rooms\" API", function () {
  15. it("has a method 'close' which closes rooms by JID or all rooms when called with no arguments",
  16. mock.initConverseWithPromises(
  17. null, ['rosterGroupsFetched'], {},
  18. function (done, _converse) {
  19. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy')
  20. .then(() => test_utils.openAndEnterChatRoom(_converse, 'leisure', 'localhost', 'dummy'))
  21. .then(() => test_utils.openAndEnterChatRoom(_converse, 'news', 'localhost', 'dummy'))
  22. .then(() => {
  23. expect(u.isVisible(_converse.chatboxviews.get('lounge@localhost').el)).toBeTruthy();
  24. expect(u.isVisible(_converse.chatboxviews.get('leisure@localhost').el)).toBeTruthy();
  25. expect(u.isVisible(_converse.chatboxviews.get('news@localhost').el)).toBeTruthy();
  26. // XXX: bit of a cheat here. We want `cleanup()` to be
  27. // called on the room. Either it's this or faking
  28. // `sendPresence`.
  29. _converse.connection.connected = false;
  30. _converse.api.rooms.close('lounge@localhost');
  31. expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
  32. expect(u.isVisible(_converse.chatboxviews.get('leisure@localhost').el)).toBeTruthy();
  33. expect(u.isVisible(_converse.chatboxviews.get('news@localhost').el)).toBeTruthy();
  34. _converse.api.rooms.close(['leisure@localhost', 'news@localhost']);
  35. expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
  36. expect(_converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
  37. expect(_converse.chatboxviews.get('news@localhost')).toBeUndefined();
  38. return test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  39. })
  40. .then(() => test_utils.openAndEnterChatRoom(_converse, 'leisure', 'localhost', 'dummy'))
  41. .then(() => {
  42. expect(u.isVisible(_converse.chatboxviews.get('lounge@localhost').el)).toBeTruthy();
  43. expect(u.isVisible(_converse.chatboxviews.get('leisure@localhost').el)).toBeTruthy();
  44. _converse.api.rooms.close();
  45. expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
  46. expect(_converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
  47. done();
  48. }).catch(_.partial(console.error, _));
  49. }));
  50. it("has a method 'get' which returns a wrapped chat room (if it exists)",
  51. mock.initConverseWithPromises(
  52. null, ['rosterGroupsFetched'], {},
  53. function (done, _converse) {
  54. test_utils.createContacts(_converse, 'current');
  55. test_utils.waitUntil(function () {
  56. return $(_converse.rosterview.el).find('.roster-group .group-toggle').length;
  57. }, 300).then(function () {
  58. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  59. var jid = 'lounge@localhost';
  60. var room = _converse.api.rooms.get(jid);
  61. expect(room instanceof Object).toBeTruthy();
  62. var chatroomview = _converse.chatboxviews.get(jid);
  63. expect(chatroomview.is_chatroom).toBeTruthy();
  64. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  65. chatroomview.close();
  66. // Test with mixed case
  67. test_utils.openAndEnterChatRoom(_converse, 'Leisure', 'localhost', 'dummy').then(function () {
  68. jid = 'Leisure@localhost';
  69. room = _converse.api.rooms.get(jid);
  70. expect(room instanceof Object).toBeTruthy();
  71. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  72. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  73. jid = 'leisure@localhost';
  74. room = _converse.api.rooms.get(jid);
  75. expect(room instanceof Object).toBeTruthy();
  76. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  77. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  78. jid = 'leiSure@localhost';
  79. room = _converse.api.rooms.get(jid);
  80. expect(room instanceof Object).toBeTruthy();
  81. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  82. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  83. chatroomview.close();
  84. // Non-existing room
  85. jid = 'lounge2@localhost';
  86. room = _converse.api.rooms.get(jid);
  87. expect(typeof room === 'undefined').toBeTruthy();
  88. done();
  89. });
  90. });
  91. });
  92. }));
  93. it("has a method 'open' which opens (optionally configures) and returns a wrapped chat box",
  94. mock.initConverseWithPromises(
  95. null, ['rosterGroupsFetched'], {},
  96. function (done, _converse) {
  97. // Mock 'getRoomFeatures', otherwise the room won't be
  98. // displayed as it waits first for the features to be returned
  99. // (when it's a new room being created).
  100. spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(function () {
  101. var deferred = new $.Deferred();
  102. deferred.resolve();
  103. return deferred.promise();
  104. });
  105. test_utils.openControlBox();
  106. test_utils.createContacts(_converse, 'current');
  107. test_utils.waitUntil(function () {
  108. return $(_converse.rosterview.el).find('.roster-group .group-toggle').length;
  109. }, 300).then(function () {
  110. var jid = 'lounge@localhost';
  111. var room = _converse.api.rooms.open(jid);
  112. // Test on chat room that's not yet open
  113. expect(room instanceof Object).toBeTruthy();
  114. var chatroomview = _converse.chatboxviews.get(jid);
  115. expect(chatroomview.is_chatroom).toBeTruthy();
  116. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  117. // Test again, now that the room exists.
  118. room = _converse.api.rooms.open(jid);
  119. expect(room instanceof Object).toBeTruthy();
  120. chatroomview = _converse.chatboxviews.get(jid);
  121. expect(chatroomview.is_chatroom).toBeTruthy();
  122. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  123. chatroomview.close();
  124. // Test with mixed case in JID
  125. jid = 'Leisure@localhost';
  126. room = _converse.api.rooms.open(jid);
  127. expect(room instanceof Object).toBeTruthy();
  128. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  129. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  130. jid = 'leisure@localhost';
  131. room = _converse.api.rooms.open(jid);
  132. expect(room instanceof Object).toBeTruthy();
  133. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  134. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  135. jid = 'leiSure@localhost';
  136. room = _converse.api.rooms.open(jid);
  137. expect(room instanceof Object).toBeTruthy();
  138. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  139. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  140. chatroomview.close();
  141. _converse.muc_instant_rooms = false;
  142. var sent_IQ, IQ_id, sent_IQ_els = [];
  143. var sendIQ = _converse.connection.sendIQ;
  144. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  145. sent_IQ = iq;
  146. sent_IQ_els.push(iq.nodeTree);
  147. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  148. });
  149. // Test with configuration
  150. _converse.api.rooms.open('room@conference.example.org', {
  151. 'nick': 'some1',
  152. 'auto_configure': true,
  153. 'roomconfig': {
  154. 'changesubject': false,
  155. 'membersonly': true,
  156. 'persistentroom': true,
  157. 'publicroom': true,
  158. 'roomdesc': 'Welcome to this room',
  159. 'whois': 'anyone'
  160. }
  161. });
  162. chatroomview = _converse.chatboxviews.get('room@conference.example.org');
  163. // We pretend this is a new room, so no disco info is returned.
  164. var features_stanza = $iq({
  165. from: 'room@conference.example.org',
  166. 'id': IQ_id,
  167. 'to': 'dummy@localhost/desktop',
  168. 'type': 'error'
  169. }).c('error', {'type': 'cancel'})
  170. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  171. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  172. /* <presence xmlns="jabber:client" to="dummy@localhost/pda" from="room@conference.example.org/yo">
  173. * <x xmlns="http://jabber.org/protocol/muc#user">
  174. * <item affiliation="owner" jid="dummy@localhost/pda" role="moderator"/>
  175. * <status code="110"/>
  176. * <status code="201"/>
  177. * </x>
  178. * </presence>
  179. */
  180. var presence = $pres({
  181. from:'room@conference.example.org/some1',
  182. to:'dummy@localhost/pda'
  183. })
  184. .c('x', {xmlns:'http://jabber.org/protocol/muc#user'})
  185. .c('item', {
  186. affiliation: 'owner',
  187. jid: 'dummy@localhost/pda',
  188. role: 'moderator'
  189. }).up()
  190. .c('status', {code:'110'}).up()
  191. .c('status', {code:'201'});
  192. _converse.connection._dataRecv(test_utils.createRequest(presence));
  193. expect(_converse.connection.sendIQ).toHaveBeenCalled();
  194. expect(sent_IQ.toLocaleString()).toBe(
  195. "<iq to='room@conference.example.org' type='get' xmlns='jabber:client' id='"+IQ_id+
  196. "'><query xmlns='http://jabber.org/protocol/muc#owner'/></iq>"
  197. );
  198. var node = Strophe.xmlHtmlNode(
  199. '<iq xmlns="jabber:client"'+
  200. ' type="result"'+
  201. ' to="dummy@localhost/pda"'+
  202. ' from="room@conference.example.org" id="'+IQ_id+'">'+
  203. ' <query xmlns="http://jabber.org/protocol/muc#owner">'+
  204. ' <x xmlns="jabber:x:data" type="form">'+
  205. ' <title>Configuration for room@conference.example.org</title>'+
  206. ' <instructions>Complete and submit this form to configure the room.</instructions>'+
  207. ' <field var="FORM_TYPE" type="hidden">'+
  208. ' <value>http://jabber.org/protocol/muc#roomconfig</value>'+
  209. ' </field>'+
  210. ' <field type="text-single" var="muc#roomconfig_roomname" label="Name">'+
  211. ' <value>Room</value>'+
  212. ' </field>'+
  213. ' <field type="text-single" var="muc#roomconfig_roomdesc" label="Description"><value/></field>'+
  214. ' <field type="boolean" var="muc#roomconfig_persistentroom" label="Make Room Persistent?"/>'+
  215. ' <field type="boolean" var="muc#roomconfig_publicroom" label="Make Room Publicly Searchable?"><value>1</value></field>'+
  216. ' <field type="boolean" var="muc#roomconfig_changesubject" label="Allow Occupants to Change Subject?"/>'+
  217. ' <field type="list-single" var="muc#roomconfig_whois" label="Who May Discover Real JIDs?"><option label="Moderators Only">'+
  218. ' <value>moderators</value></option><option label="Anyone"><value>anyone</value></option>'+
  219. ' </field>'+
  220. ' <field type="text-private" var="muc#roomconfig_roomsecret" label="Password"><value/></field>'+
  221. ' <field type="boolean" var="muc#roomconfig_moderatedroom" label="Make Room Moderated?"/>'+
  222. ' <field type="boolean" var="muc#roomconfig_membersonly" label="Make Room Members-Only?"/>'+
  223. ' <field type="text-single" var="muc#roomconfig_historylength" label="Maximum Number of History Messages Returned by Room">'+
  224. ' <value>20</value></field>'+
  225. ' </x>'+
  226. ' </query>'+
  227. ' </iq>');
  228. spyOn(chatroomview.model, 'sendConfiguration').and.callThrough();
  229. _converse.connection._dataRecv(test_utils.createRequest(node.firstElementChild));
  230. return test_utils.waitUntil(function () {
  231. return chatroomview.model.sendConfiguration.calls.count() === 1;
  232. }, 300).then(function () {
  233. var sent_stanza = sent_IQ_els.pop();
  234. while (sent_stanza.getAttribute('type') !== 'set') {
  235. sent_stanza = sent_IQ_els.pop();
  236. }
  237. expect(sizzle('field[var="muc#roomconfig_roomname"] value', sent_stanza).pop().textContent).toBe('Room');
  238. expect(sizzle('field[var="muc#roomconfig_roomdesc"] value', sent_stanza).pop().textContent).toBe('Welcome to this room');
  239. expect(sizzle('field[var="muc#roomconfig_persistentroom"] value', sent_stanza).pop().textContent).toBe('1');
  240. expect(sizzle('field[var="muc#roomconfig_publicroom"] value ', sent_stanza).pop().textContent).toBe('1');
  241. expect(sizzle('field[var="muc#roomconfig_changesubject"] value', sent_stanza).pop().textContent).toBe('0');
  242. expect(sizzle('field[var="muc#roomconfig_whois"] value ', sent_stanza).pop().textContent).toBe('anyone');
  243. expect(sizzle('field[var="muc#roomconfig_membersonly"] value', sent_stanza).pop().textContent).toBe('1');
  244. expect(sizzle('field[var="muc#roomconfig_historylength"] value', sent_stanza).pop().textContent).toBe('20');
  245. done();
  246. });
  247. });
  248. }));
  249. });
  250. describe("An instant chat room", function () {
  251. it("will be created when muc_instant_rooms is set to true",
  252. mock.initConverseWithPromises(
  253. null, ['rosterGroupsFetched'], {},
  254. function (done, _converse) {
  255. var IQ_stanzas = _converse.connection.IQ_stanzas;
  256. var sent_IQ, IQ_id;
  257. var sendIQ = _converse.connection.sendIQ;
  258. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  259. if (iq.nodeTree.getAttribute('to') === 'lounge@localhost') {
  260. sent_IQ = iq;
  261. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  262. } else {
  263. sendIQ.bind(this)(iq, callback, errback);
  264. }
  265. });
  266. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  267. // We pretend this is a new room, so no disco info is returned.
  268. //
  269. /* <iq from="jordie.langen@chat.example.org/converse.js-11659299" to="myroom@conference.chat.example.org" type="get">
  270. * <query xmlns="http://jabber.org/protocol/disco#info"/>
  271. * </iq>
  272. * <iq xmlns="jabber:client" type="error" to="jordie.langen@chat.example.org/converse.js-11659299" from="myroom@conference.chat.example.org">
  273. * <error type="cancel">
  274. * <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  275. * </error>
  276. * </iq>
  277. */
  278. var features_stanza = $iq({
  279. 'from': 'lounge@localhost',
  280. 'id': IQ_id,
  281. 'to': 'dummy@localhost/desktop',
  282. 'type': 'error'
  283. }).c('error', {'type': 'cancel'})
  284. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  285. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  286. var view = _converse.chatboxviews.get('lounge@localhost');
  287. spyOn(view, 'join').and.callThrough();
  288. spyOn(view, 'submitNickname').and.callThrough();
  289. /* <iq to="myroom@conference.chat.example.org"
  290. * from="jordie.langen@chat.example.org/converse.js-11659299"
  291. * type="get">
  292. * <query xmlns="http://jabber.org/protocol/disco#info"
  293. * node="x-roomuser-item"/>
  294. * </iq>
  295. */
  296. test_utils.waitUntil(function () {
  297. return _.filter(IQ_stanzas, function (iq) {
  298. return iq.nodeTree.querySelector('query[node="x-roomuser-item"]');
  299. }).length > 0;
  300. }, 300).then(function () {
  301. const iq = _.filter(IQ_stanzas, function (iq) {
  302. return iq.nodeTree.querySelector(`query[node="x-roomuser-item"]`);
  303. }).pop();
  304. const id = iq.nodeTree.getAttribute('id');
  305. expect(iq.toLocaleString()).toBe(
  306. "<iq to='lounge@localhost' from='dummy@localhost/resource' "+
  307. "type='get' xmlns='jabber:client' id='"+id+"'>"+
  308. "<query xmlns='http://jabber.org/protocol/disco#info' node='x-roomuser-item'/></iq>");
  309. /* <iq xmlns="jabber:client" type="error" to="jordie.langen@chat.example.org/converse.js-11659299" from="myroom@conference.chat.example.org">
  310. * <error type="cancel">
  311. * <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  312. * </error>
  313. * </iq>
  314. */
  315. var stanza = $iq({
  316. 'type': 'error',
  317. 'id': id,
  318. 'from': view.model.get('jid'),
  319. 'to': _converse.connection.jid
  320. }).c('error', {'type': 'cancel'})
  321. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  322. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  323. var input = view.el.querySelector('input[name="nick"]');
  324. input.value = 'nicky';
  325. view.el.querySelector('input[type=submit]').click();
  326. expect(view.submitNickname).toHaveBeenCalled();
  327. expect(view.join).toHaveBeenCalled();
  328. // The user has just entered the room (because join was called)
  329. // and receives their own presence from the server.
  330. // See example 24:
  331. // http://xmpp.org/extensions/xep-0045.html#enter-pres
  332. //
  333. /* <presence xmlns="jabber:client" to="jordie.langen@chat.example.org/converse.js-11659299" from="myroom@conference.chat.example.org/jc">
  334. * <x xmlns="http://jabber.org/protocol/muc#user">
  335. * <item jid="jordie.langen@chat.example.org/converse.js-11659299" affiliation="owner" role="moderator"/>
  336. * <status code="110"/>
  337. * <status code="201"/>
  338. * </x>
  339. * </presence>
  340. */
  341. var presence = $pres({
  342. to:'dummy@localhost/resource',
  343. from:'lounge@localhost/thirdwitch',
  344. id:'5025e055-036c-4bc5-a227-706e7e352053'
  345. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  346. .c('item').attrs({
  347. affiliation: 'owner',
  348. jid: 'dummy@localhost/resource',
  349. role: 'moderator'
  350. }).up()
  351. .c('status').attrs({code:'110'}).up()
  352. .c('status').attrs({code:'201'}).nodeTree;
  353. _converse.connection._dataRecv(test_utils.createRequest(presence));
  354. var info_text = view.el.querySelector('.chat-content .chat-info').textContent;
  355. expect(info_text).toBe('A new room has been created');
  356. // An instant room is created by saving the default configuratoin.
  357. //
  358. /* <iq to="myroom@conference.chat.example.org" type="set" xmlns="jabber:client" id="5025e055-036c-4bc5-a227-706e7e352053:sendIQ">
  359. * <query xmlns="http://jabber.org/protocol/muc#owner"><x xmlns="jabber:x:data" type="submit"/></query>
  360. * </iq>
  361. */
  362. expect(sent_IQ.toLocaleString()).toBe(
  363. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  364. "<query xmlns='http://jabber.org/protocol/muc#owner'><x xmlns='jabber:x:data' type='submit'/>"+
  365. "</query></iq>");
  366. done();
  367. });
  368. }));
  369. });
  370. describe("A Chat Room", function () {
  371. it("shows join/leave messages when users enter or exit a room",
  372. mock.initConverseWithPromises(
  373. null, ['rosterGroupsFetched'], {},
  374. function (done, _converse) {
  375. test_utils.openChatRoom(_converse, "coven", 'chat.shakespeare.lit', 'some1');
  376. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  377. var $chat_content = $(view.el).find('.chat-content');
  378. /* We don't show join/leave messages for existing occupants. We
  379. * know about them because we receive their presences before we
  380. * receive our own.
  381. */
  382. var presence = $pres({
  383. to: 'dummy@localhost/_converse.js-29092160',
  384. from: 'coven@chat.shakespeare.lit/oldguy'
  385. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  386. .c('item', {
  387. 'affiliation': 'none',
  388. 'jid': 'oldguy@localhost/_converse.js-290929789',
  389. 'role': 'participant'
  390. });
  391. _converse.connection._dataRecv(test_utils.createRequest(presence));
  392. expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(0);
  393. /* <presence to="dummy@localhost/_converse.js-29092160"
  394. * from="coven@chat.shakespeare.lit/some1">
  395. * <x xmlns="http://jabber.org/protocol/muc#user">
  396. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  397. * <status code="110"/>
  398. * </x>
  399. * </presence></body>
  400. */
  401. presence = $pres({
  402. to: 'dummy@localhost/_converse.js-29092160',
  403. from: 'coven@chat.shakespeare.lit/some1'
  404. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  405. .c('item', {
  406. 'affiliation': 'owner',
  407. 'jid': 'dummy@localhost/_converse.js-29092160',
  408. 'role': 'moderator'
  409. }).up()
  410. .c('status', {code: '110'});
  411. _converse.connection._dataRecv(test_utils.createRequest(presence));
  412. expect($chat_content.find('div.chat-info:first').html()).toBe("some1 has entered the room");
  413. presence = $pres({
  414. to: 'dummy@localhost/_converse.js-29092160',
  415. from: 'coven@chat.shakespeare.lit/newguy'
  416. })
  417. .c('x', {xmlns: Strophe.NS.MUC_USER})
  418. .c('item', {
  419. 'affiliation': 'none',
  420. 'jid': 'newguy@localhost/_converse.js-290929789',
  421. 'role': 'participant'
  422. });
  423. _converse.connection._dataRecv(test_utils.createRequest(presence));
  424. expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(2);
  425. expect($chat_content.find('div.chat-info:last').html()).toBe("newguy has entered the room");
  426. // Add another entrant, otherwise the above message will be
  427. // collapsed if "newguy" leaves immediately again
  428. presence = $pres({
  429. to: 'dummy@localhost/_converse.js-29092160',
  430. from: 'coven@chat.shakespeare.lit/newgirl'
  431. })
  432. .c('x', {xmlns: Strophe.NS.MUC_USER})
  433. .c('item', {
  434. 'affiliation': 'none',
  435. 'jid': 'newgirl@localhost/_converse.js-213098781',
  436. 'role': 'participant'
  437. });
  438. _converse.connection._dataRecv(test_utils.createRequest(presence));
  439. expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(3);
  440. expect($chat_content.find('div.chat-info:last').html()).toBe("newgirl has entered the room");
  441. // Don't show duplicate join messages
  442. presence = $pres({
  443. to: 'dummy@localhost/_converse.js-290918392',
  444. from: 'coven@chat.shakespeare.lit/newguy'
  445. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  446. .c('item', {
  447. 'affiliation': 'none',
  448. 'jid': 'newguy@localhost/_converse.js-290929789',
  449. 'role': 'participant'
  450. });
  451. _converse.connection._dataRecv(test_utils.createRequest(presence));
  452. expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(3);
  453. /* <presence
  454. * from='coven@chat.shakespeare.lit/thirdwitch'
  455. * to='crone1@shakespeare.lit/desktop'
  456. * type='unavailable'>
  457. * <status>Disconnected: Replaced by new connection</status>
  458. * <x xmlns='http://jabber.org/protocol/muc#user'>
  459. * <item affiliation='member'
  460. * jid='hag66@shakespeare.lit/pda'
  461. * role='none'/>
  462. * </x>
  463. * </presence>
  464. */
  465. presence = $pres({
  466. to: 'dummy@localhost/_converse.js-29092160',
  467. type: 'unavailable',
  468. from: 'coven@chat.shakespeare.lit/newguy'
  469. })
  470. .c('status', 'Disconnected: Replaced by new connection').up()
  471. .c('x', {xmlns: Strophe.NS.MUC_USER})
  472. .c('item', {
  473. 'affiliation': 'none',
  474. 'jid': 'newguy@localhost/_converse.js-290929789',
  475. 'role': 'none'
  476. });
  477. _converse.connection._dataRecv(test_utils.createRequest(presence));
  478. expect($chat_content.find('div.chat-info').length).toBe(4);
  479. expect($chat_content.find('div.chat-info:last').html()).toBe(
  480. 'newguy has left the room. '+
  481. '"Disconnected: Replaced by new connection"');
  482. // When the user immediately joins again, we collapse the
  483. // multiple join/leave messages.
  484. presence = $pres({
  485. to: 'dummy@localhost/_converse.js-29092160',
  486. from: 'coven@chat.shakespeare.lit/newguy'
  487. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  488. .c('item', {
  489. 'affiliation': 'none',
  490. 'jid': 'newguy@localhost/_converse.js-290929789',
  491. 'role': 'participant'
  492. });
  493. _converse.connection._dataRecv(test_utils.createRequest(presence));
  494. expect($chat_content.find('div.chat-info').length).toBe(4);
  495. var $msg_el = $chat_content.find('div.chat-info:last');
  496. expect($msg_el.html()).toBe("newguy has left and re-entered the room");
  497. expect($msg_el.data('leavejoin')).toBe('"newguy"');
  498. presence = $pres({
  499. to: 'dummy@localhost/_converse.js-29092160',
  500. type: 'unavailable',
  501. from: 'coven@chat.shakespeare.lit/newguy'
  502. })
  503. .c('x', {xmlns: Strophe.NS.MUC_USER})
  504. .c('item', {
  505. 'affiliation': 'none',
  506. 'jid': 'newguy@localhost/_converse.js-290929789',
  507. 'role': 'none'
  508. });
  509. _converse.connection._dataRecv(test_utils.createRequest(presence));
  510. expect($chat_content.find('div.chat-info').length).toBe(4);
  511. $msg_el = $chat_content.find('div.chat-info:last');
  512. expect($msg_el.html()).toBe('newguy has left the room');
  513. expect($msg_el.data('leave')).toBe('"newguy"');
  514. presence = $pres({
  515. to: 'dummy@localhost/_converse.js-29092160',
  516. from: 'coven@chat.shakespeare.lit/nomorenicks'
  517. })
  518. .c('x', {xmlns: Strophe.NS.MUC_USER})
  519. .c('item', {
  520. 'affiliation': 'none',
  521. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  522. 'role': 'participant'
  523. });
  524. _converse.connection._dataRecv(test_utils.createRequest(presence));
  525. expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(5);
  526. expect($chat_content.find('div.chat-info:last').html()).toBe("nomorenicks has entered the room");
  527. presence = $pres({
  528. to: 'dummy@localhost/_converse.js-290918392',
  529. type: 'unavailable',
  530. from: 'coven@chat.shakespeare.lit/nomorenicks'
  531. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  532. .c('item', {
  533. 'affiliation': 'none',
  534. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  535. 'role': 'none'
  536. });
  537. _converse.connection._dataRecv(test_utils.createRequest(presence));
  538. expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(5);
  539. expect($chat_content.find('div.chat-info:last').html()).toBe("nomorenicks has entered and left the room");
  540. presence = $pres({
  541. to: 'dummy@localhost/_converse.js-29092160',
  542. from: 'coven@chat.shakespeare.lit/nomorenicks'
  543. })
  544. .c('x', {xmlns: Strophe.NS.MUC_USER})
  545. .c('item', {
  546. 'affiliation': 'none',
  547. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  548. 'role': 'participant'
  549. });
  550. _converse.connection._dataRecv(test_utils.createRequest(presence));
  551. expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(5);
  552. expect($chat_content.find('div.chat-info:last').html()).toBe("nomorenicks has entered the room");
  553. done();
  554. }));
  555. it("shows a new day indicator if a join/leave message is received on a new day",
  556. mock.initConverseWithPromises(
  557. null, ['rosterGroupsFetched'], {},
  558. function (done, _converse) {
  559. test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy').then(function () {
  560. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  561. var chat_content = view.el.querySelector('.chat-content');
  562. var $chat_content = $(chat_content);
  563. var indicator = chat_content.querySelector('.date-separator');
  564. expect(indicator).not.toBe(null);
  565. expect(indicator.getAttribute('class')).toEqual('message date-separator');
  566. expect(indicator.getAttribute('data-isodate')).toEqual(moment().startOf('day').format());
  567. expect(indicator.querySelector('time').textContent).toEqual(moment().startOf('day').format("dddd MMM Do YYYY"));
  568. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(1);
  569. expect(chat_content.querySelector('div.chat-info').textContent).toBe(
  570. "dummy has entered the room"
  571. );
  572. var baseTime = new Date();
  573. jasmine.clock().install();
  574. jasmine.clock().mockDate(baseTime);
  575. var ONE_DAY_LATER = 86400000;
  576. jasmine.clock().tick(ONE_DAY_LATER);
  577. /* <presence to="dummy@localhost/_converse.js-29092160"
  578. * from="coven@chat.shakespeare.lit/some1">
  579. * <x xmlns="http://jabber.org/protocol/muc#user">
  580. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  581. * <status code="110"/>
  582. * </x>
  583. * </presence></body>
  584. */
  585. var presence = $pres({
  586. to: 'dummy@localhost/_converse.js-29092160',
  587. from: 'coven@chat.shakespeare.lit/some1'
  588. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  589. .c('item', {
  590. 'affiliation': 'owner',
  591. 'jid': 'some1@localhost/_converse.js-290929789',
  592. 'role': 'moderator'
  593. });
  594. _converse.connection._dataRecv(test_utils.createRequest(presence));
  595. indicator = chat_content.querySelector('.date-separator[data-isodate="'+moment().startOf('day').format()+'"]');
  596. expect(indicator).not.toBe(null);
  597. expect(indicator.getAttribute('class')).toEqual('message date-separator');
  598. expect(indicator.getAttribute('data-isodate')).toEqual(moment().startOf('day').format());
  599. expect(indicator.querySelector('time').getAttribute('class')).toEqual('separator-text');
  600. expect(indicator.querySelector('time').textContent).toEqual(moment().startOf('day').format("dddd MMM Do YYYY"));
  601. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  602. expect(chat_content.querySelector('div.chat-info:last-child').textContent).toBe(
  603. "some1 has entered the room"
  604. );
  605. jasmine.clock().tick(ONE_DAY_LATER);
  606. // Test a user leaving a chat room
  607. presence = $pres({
  608. to: 'dummy@localhost/_converse.js-29092160',
  609. type: 'unavailable',
  610. from: 'coven@chat.shakespeare.lit/some1'
  611. })
  612. .c('status', 'Disconnected: Replaced by new connection').up()
  613. .c('x', {xmlns: Strophe.NS.MUC_USER})
  614. .c('item', {
  615. 'affiliation': 'owner',
  616. 'jid': 'some1@localhost/_converse.js-290929789',
  617. 'role': 'moderator'
  618. });
  619. _converse.connection._dataRecv(test_utils.createRequest(presence));
  620. indicator = chat_content.querySelector('.date-separator[data-isodate="'+moment().startOf('day').format()+'"]');
  621. expect(indicator).not.toBe(null);
  622. expect(indicator.getAttribute('class')).toEqual('message date-separator');
  623. expect(indicator.getAttribute('data-isodate')).toEqual(moment().startOf('day').format());
  624. expect(indicator.querySelector('time').textContent).toEqual(moment().startOf('day').format("dddd MMM Do YYYY"));
  625. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  626. expect($(chat_content).find('div.chat-info:last').html()).toBe(
  627. 'some1 has left the room. '+
  628. '"Disconnected: Replaced by new connection"');
  629. jasmine.clock().tick(ONE_DAY_LATER);
  630. var stanza = Strophe.xmlHtmlNode(
  631. '<message xmlns="jabber:client"' +
  632. ' to="dummy@localhost/_converse.js-290929789"' +
  633. ' type="groupchat"' +
  634. ' from="coven@chat.shakespeare.lit/some1">'+
  635. ' <body>hello world</body>'+
  636. ' <delay xmlns="urn:xmpp:delay" stamp="'+moment().format()+'" from="some1@localhost"/>'+
  637. '</message>').firstChild;
  638. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  639. presence = $pres({
  640. to: 'dummy@localhost/_converse.js-29092160',
  641. from: 'coven@chat.shakespeare.lit/newguy'
  642. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  643. .c('item', {
  644. 'affiliation': 'none',
  645. 'jid': 'newguy@localhost/_converse.js-290929789',
  646. 'role': 'participant'
  647. });
  648. _converse.connection._dataRecv(test_utils.createRequest(presence));
  649. var $time = $chat_content.find('time');
  650. expect($time.length).toEqual(4);
  651. var $indicator = $chat_content.find('.date-separator:eq(3)');
  652. expect($indicator.attr('class')).toEqual('message date-separator');
  653. expect($indicator.data('isodate')).toEqual(moment().startOf('day').format());
  654. expect($indicator.find('time').text()).toEqual(moment().startOf('day').format("dddd MMM Do YYYY"));
  655. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(4);
  656. expect($chat_content.find('div.chat-info:last').html()).toBe("newguy has entered the room");
  657. jasmine.clock().tick(ONE_DAY_LATER);
  658. stanza = Strophe.xmlHtmlNode(
  659. '<message xmlns="jabber:client"' +
  660. ' to="dummy@localhost/_converse.js-290929789"' +
  661. ' type="groupchat"' +
  662. ' from="coven@chat.shakespeare.lit/some1">'+
  663. ' <body>hello world</body>'+
  664. ' <delay xmlns="urn:xmpp:delay" stamp="'+moment().format()+'" from="some1@localhost"/>'+
  665. '</message>').firstChild;
  666. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  667. jasmine.clock().tick(ONE_DAY_LATER);
  668. // Test a user leaving a chat room
  669. presence = $pres({
  670. to: 'dummy@localhost/_converse.js-29092160',
  671. type: 'unavailable',
  672. from: 'coven@chat.shakespeare.lit/newguy'
  673. })
  674. .c('status', 'Disconnected: Replaced by new connection').up()
  675. .c('x', {xmlns: Strophe.NS.MUC_USER})
  676. .c('item', {
  677. 'affiliation': 'none',
  678. 'jid': 'newguy@localhost/_converse.js-290929789',
  679. 'role': 'none'
  680. });
  681. _converse.connection._dataRecv(test_utils.createRequest(presence));
  682. $time = $chat_content.find('time');
  683. expect($time.length).toEqual(6);
  684. $indicator = $chat_content.find('.date-separator:eq(5)');
  685. expect($indicator.attr('class')).toEqual('message date-separator');
  686. expect($indicator.data('isodate')).toEqual(moment().startOf('day').format());
  687. expect($indicator.find('time').text()).toEqual(moment().startOf('day').format("dddd MMM Do YYYY"));
  688. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(5);
  689. expect($chat_content.find('div.chat-info:last').html()).toBe(
  690. 'newguy has left the room. '+
  691. '"Disconnected: Replaced by new connection"');
  692. jasmine.clock().uninstall();
  693. done();
  694. return;
  695. });
  696. }));
  697. it("shows its description in the chat heading",
  698. mock.initConverseWithPromises(
  699. null, ['rosterGroupsFetched'], {},
  700. function (done, _converse) {
  701. var sent_IQ, IQ_id;
  702. var sendIQ = _converse.connection.sendIQ;
  703. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  704. sent_IQ = iq;
  705. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  706. });
  707. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  708. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  709. var features_stanza = $iq({
  710. from: 'coven@chat.shakespeare.lit',
  711. 'id': IQ_id,
  712. 'to': 'dummy@localhost/desktop',
  713. 'type': 'result'
  714. })
  715. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  716. .c('identity', {
  717. 'category': 'conference',
  718. 'name': 'A Dark Cave',
  719. 'type': 'text'
  720. }).up()
  721. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  722. .c('feature', {'var': 'muc_passwordprotected'}).up()
  723. .c('feature', {'var': 'muc_hidden'}).up()
  724. .c('feature', {'var': 'muc_temporary'}).up()
  725. .c('feature', {'var': 'muc_open'}).up()
  726. .c('feature', {'var': 'muc_unmoderated'}).up()
  727. .c('feature', {'var': 'muc_nonanonymous'}).up()
  728. .c('feature', {'var': 'urn:xmpp:mam:0'}).up()
  729. .c('x', { 'xmlns':'jabber:x:data', 'type':'result'})
  730. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  731. .c('value').t('http://jabber.org/protocol/muc#roominfo').up().up()
  732. .c('field', {'type':'text-single', 'var':'muc#roominfo_description', 'label':'Description'})
  733. .c('value').t('This is the description').up().up()
  734. .c('field', {'type':'text-single', 'var':'muc#roominfo_occupants', 'label':'Number of occupants'})
  735. .c('value').t(0);
  736. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  737. test_utils.waitUntil(function () {
  738. return _.get(view.el.querySelector('.chatroom-description'), 'textContent');
  739. }).then(function () {
  740. expect($(view.el.querySelector('.chatroom-description')).text()).toBe('This is the description');
  741. done();
  742. });
  743. }));
  744. it("will specially mark messages in which you are mentioned",
  745. mock.initConverseWithPromises(
  746. null, ['rosterGroupsFetched'], {},
  747. function (done, _converse) {
  748. test_utils.createContacts(_converse, 'current');
  749. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  750. var view = _converse.chatboxviews.get('lounge@localhost');
  751. if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
  752. var message = 'dummy: Your attention is required';
  753. var nick = mock.chatroom_names[0],
  754. msg = $msg({
  755. from: 'lounge@localhost/'+nick,
  756. id: (new Date()).getTime(),
  757. to: 'dummy@localhost',
  758. type: 'groupchat'
  759. }).c('body').t(message).tree();
  760. view.model.onMessage(msg);
  761. expect($(view.el).find('.chat-msg').hasClass('mentioned')).toBeTruthy();
  762. done();
  763. });
  764. }));
  765. it("supports the /me command",
  766. mock.initConverseWithPromises(
  767. null, ['rosterGroupsFetched'], {},
  768. function (done, _converse) {
  769. test_utils.waitUntilDiscoConfirmed(_converse, 'localhost', [], ['vcard-temp'])
  770. .then(function () {
  771. return test_utils.waitUntil(() => _converse.xmppstatus.vcard.get('fullname'))
  772. }).then(function () {
  773. test_utils.createContacts(_converse, 'current');
  774. return test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  775. }).then(function () {
  776. var view = _converse.chatboxviews.get('lounge@localhost');
  777. if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
  778. var message = '/me is tired';
  779. var nick = mock.chatroom_names[0],
  780. msg = $msg({
  781. 'from': 'lounge@localhost/'+nick,
  782. 'id': (new Date()).getTime(),
  783. 'to': 'dummy@localhost',
  784. 'type': 'groupchat'
  785. }).c('body').t(message).tree();
  786. view.model.onMessage(msg);
  787. expect(_.includes($(view.el).find('.chat-msg-author').text(), '**Dyon van de Wege')).toBeTruthy();
  788. expect($(view.el).find('.chat-msg-text').text()).toBe(' is tired');
  789. message = '/me is as well';
  790. msg = $msg({
  791. from: 'lounge@localhost/Max Mustermann',
  792. id: (new Date()).getTime(),
  793. to: 'dummy@localhost',
  794. type: 'groupchat'
  795. }).c('body').t(message).tree();
  796. view.model.onMessage(msg);
  797. expect(_.includes($(view.el).find('.chat-msg-author:last').text(), '**Max Mustermann')).toBeTruthy();
  798. expect($(view.el).find('.chat-msg-text:last').text()).toBe(' is as well');
  799. done();
  800. });
  801. }));
  802. it("can be configured if you're its owner",
  803. mock.initConverseWithPromises(
  804. null, ['rosterGroupsFetched'], {},
  805. function (done, _converse) {
  806. var view;
  807. var sent_IQ, IQ_id;
  808. var sendIQ = _converse.connection.sendIQ;
  809. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  810. sent_IQ = iq;
  811. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  812. });
  813. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  814. view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  815. spyOn(view.model, 'saveAffiliationAndRole').and.callThrough();
  816. // We pretend this is a new room, so no disco info is returned.
  817. var features_stanza = $iq({
  818. from: 'coven@chat.shakespeare.lit',
  819. 'id': IQ_id,
  820. 'to': 'dummy@localhost/desktop',
  821. 'type': 'error'
  822. }).c('error', {'type': 'cancel'})
  823. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  824. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  825. /* <presence to="dummy@localhost/_converse.js-29092160"
  826. * from="coven@chat.shakespeare.lit/some1">
  827. * <x xmlns="http://jabber.org/protocol/muc#user">
  828. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  829. * <status code="110"/>
  830. * </x>
  831. * </presence></body>
  832. */
  833. var presence = $pres({
  834. to: 'dummy@localhost/_converse.js-29092160',
  835. from: 'coven@chat.shakespeare.lit/some1'
  836. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  837. .c('item', {
  838. 'affiliation': 'owner',
  839. 'jid': 'dummy@localhost/_converse.js-29092160',
  840. 'role': 'moderator'
  841. }).up()
  842. .c('status', {code: '110'});
  843. _converse.connection._dataRecv(test_utils.createRequest(presence));
  844. expect(view.model.saveAffiliationAndRole).toHaveBeenCalled();
  845. expect($(view.el.querySelector('.toggle-chatbox-button')).is(':visible')).toBeTruthy();
  846. test_utils.waitUntil(function () {
  847. return !_.isNull(view.el.querySelector('.configure-chatroom-button'));
  848. }, 300).then(function () {
  849. expect($(view.el.querySelector('.configure-chatroom-button')).is(':visible')).toBeTruthy();
  850. view.el.querySelector('.configure-chatroom-button').click();
  851. /* Check that an IQ is sent out, asking for the
  852. * configuration form.
  853. * See: // http://xmpp.org/extensions/xep-0045.html#example-163
  854. *
  855. * <iq from='crone1@shakespeare.lit/desktop'
  856. * id='config1'
  857. * to='coven@chat.shakespeare.lit'
  858. * type='get'>
  859. * <query xmlns='http://jabber.org/protocol/muc#owner'/>
  860. * </iq>
  861. */
  862. expect(sent_IQ.toLocaleString()).toBe(
  863. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  864. "<query xmlns='http://jabber.org/protocol/muc#owner'/>"+
  865. "</iq>");
  866. /* Server responds with the configuration form.
  867. * See: // http://xmpp.org/extensions/xep-0045.html#example-165
  868. */
  869. var config_stanza = $iq({from: 'coven@chat.shakespeare.lit',
  870. 'id': IQ_id,
  871. 'to': 'dummy@localhost/desktop',
  872. 'type': 'result'})
  873. .c('query', { 'xmlns': 'http://jabber.org/protocol/muc#owner'})
  874. .c('x', { 'xmlns': 'jabber:x:data', 'type': 'form'})
  875. .c('title').t('Configuration for "coven" Room').up()
  876. .c('instructions').t('Complete this form to modify the configuration of your room.').up()
  877. .c('field', {'type': 'hidden', 'var': 'FORM_TYPE'})
  878. .c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up()
  879. .c('field', {
  880. 'label': 'Natural-Language Room Name',
  881. 'type': 'text-single',
  882. 'var': 'muc#roomconfig_roomname'})
  883. .c('value').t('A Dark Cave').up().up()
  884. .c('field', {
  885. 'label': 'Short Description of Room',
  886. 'type': 'text-single',
  887. 'var': 'muc#roomconfig_roomdesc'})
  888. .c('value').t('The place for all good witches!').up().up()
  889. .c('field', {
  890. 'label': 'Enable Public Logging?',
  891. 'type': 'boolean',
  892. 'var': 'muc#roomconfig_enablelogging'})
  893. .c('value').t(0).up().up()
  894. .c('field', {
  895. 'label': 'Allow Occupants to Change Subject?',
  896. 'type': 'boolean',
  897. 'var': 'muc#roomconfig_changesubject'})
  898. .c('value').t(0).up().up()
  899. .c('field', {
  900. 'label': 'Allow Occupants to Invite Others?',
  901. 'type': 'boolean',
  902. 'var': 'muc#roomconfig_allowinvites'})
  903. .c('value').t(0).up().up()
  904. .c('field', {
  905. 'label': 'Who Can Send Private Messages?',
  906. 'type': 'list-single',
  907. 'var': 'muc#roomconfig_allowpm'})
  908. .c('value').t('anyone').up()
  909. .c('option', {'label': 'Anyone'})
  910. .c('value').t('anyone').up().up()
  911. .c('option', {'label': 'Anyone with Voice'})
  912. .c('value').t('participants').up().up()
  913. .c('option', {'label': 'Moderators Only'})
  914. .c('value').t('moderators').up().up()
  915. .c('option', {'label': 'Nobody'})
  916. .c('value').t('none').up().up().up()
  917. .c('field', {
  918. 'label': 'Roles for which Presence is Broadcasted',
  919. 'type': 'list-multi',
  920. 'var': 'muc#roomconfig_presencebroadcast'})
  921. .c('value').t('moderator').up()
  922. .c('value').t('participant').up()
  923. .c('value').t('visitor').up()
  924. .c('option', {'label': 'Moderator'})
  925. .c('value').t('moderator').up().up()
  926. .c('option', {'label': 'Participant'})
  927. .c('value').t('participant').up().up()
  928. .c('option', {'label': 'Visitor'})
  929. .c('value').t('visitor').up().up().up()
  930. .c('field', {
  931. 'label': 'Roles and Affiliations that May Retrieve Member List',
  932. 'type': 'list-multi',
  933. 'var': 'muc#roomconfig_getmemberlist'})
  934. .c('value').t('moderator').up()
  935. .c('value').t('participant').up()
  936. .c('value').t('visitor').up()
  937. .c('option', {'label': 'Moderator'})
  938. .c('value').t('moderator').up().up()
  939. .c('option', {'label': 'Participant'})
  940. .c('value').t('participant').up().up()
  941. .c('option', {'label': 'Visitor'})
  942. .c('value').t('visitor').up().up().up()
  943. .c('field', {
  944. 'label': 'Make Room Publicly Searchable?',
  945. 'type': 'boolean',
  946. 'var': 'muc#roomconfig_publicroom'})
  947. .c('value').t(0).up().up()
  948. .c('field', {
  949. 'label': 'Make Room Publicly Searchable?',
  950. 'type': 'boolean',
  951. 'var': 'muc#roomconfig_publicroom'})
  952. .c('value').t(0).up().up()
  953. .c('field', {
  954. 'label': 'Make Room Persistent?',
  955. 'type': 'boolean',
  956. 'var': 'muc#roomconfig_persistentroom'})
  957. .c('value').t(0).up().up()
  958. .c('field', {
  959. 'label': 'Make Room Moderated?',
  960. 'type': 'boolean',
  961. 'var': 'muc#roomconfig_moderatedroom'})
  962. .c('value').t(0).up().up()
  963. .c('field', {
  964. 'label': 'Make Room Members Only?',
  965. 'type': 'boolean',
  966. 'var': 'muc#roomconfig_membersonly'})
  967. .c('value').t(0).up().up()
  968. .c('field', {
  969. 'label': 'Password Required for Entry?',
  970. 'type': 'boolean',
  971. 'var': 'muc#roomconfig_passwordprotectedroom'})
  972. .c('value').t(1).up().up()
  973. .c('field', {'type': 'fixed'})
  974. .c('value').t('If a password is required to enter this room,'+
  975. 'you must specify the password below.').up().up()
  976. .c('field', {
  977. 'label': 'Password',
  978. 'type': 'text-private',
  979. 'var': 'muc#roomconfig_roomsecret'})
  980. .c('value').t('cauldronburn');
  981. _converse.connection._dataRecv(test_utils.createRequest(config_stanza));
  982. test_utils.waitUntil(function () {
  983. return $(view.el.querySelector('form.chatroom-form')).length;
  984. }, 300).then(function () {
  985. expect($(view.el.querySelector('form.chatroom-form')).length).toBe(1);
  986. expect(view.el.querySelectorAll('form.chatroom-form fieldset').length).toBe(2);
  987. var $membersonly = $(view.el.querySelector('input[name="muc#roomconfig_membersonly"]'));
  988. expect($membersonly.length).toBe(1);
  989. expect($membersonly.attr('type')).toBe('checkbox');
  990. $membersonly.prop('checked', true);
  991. var $moderated = $(view.el.querySelector('input[name="muc#roomconfig_moderatedroom"]'));
  992. expect($moderated.length).toBe(1);
  993. expect($moderated.attr('type')).toBe('checkbox');
  994. $moderated.prop('checked', true);
  995. var $password = $(view.el.querySelector('input[name="muc#roomconfig_roomsecret"]'));
  996. expect($password.length).toBe(1);
  997. expect($password.attr('type')).toBe('password');
  998. var $allowpm = $(view.el.querySelector('select[name="muc#roomconfig_allowpm"]'));
  999. expect($allowpm.length).toBe(1);
  1000. $allowpm.val('moderators');
  1001. var $presencebroadcast = $(view.el.querySelector('select[name="muc#roomconfig_presencebroadcast"]'));
  1002. expect($presencebroadcast.length).toBe(1);
  1003. $presencebroadcast.val(['moderator']);
  1004. view.el.querySelector('input[type="submit"]').click();
  1005. var $sent_stanza = $(sent_IQ.toLocaleString());
  1006. expect($sent_stanza.find('field[var="muc#roomconfig_membersonly"] value').text()).toBe('1');
  1007. expect($sent_stanza.find('field[var="muc#roomconfig_moderatedroom"] value').text()).toBe('1');
  1008. expect($sent_stanza.find('field[var="muc#roomconfig_allowpm"] value').text()).toBe('moderators');
  1009. expect($sent_stanza.find('field[var="muc#roomconfig_presencebroadcast"] value').text()).toBe('moderator');
  1010. done();
  1011. });
  1012. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  1013. }));
  1014. it("shows all members even if they're not currently present in the room",
  1015. mock.initConverseWithPromises(
  1016. null, ['rosterGroupsFetched'], {},
  1017. function (done, _converse) {
  1018. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function() {
  1019. var name;
  1020. var view = _converse.chatboxviews.get('lounge@localhost'),
  1021. occupants = view.el.querySelector('.occupant-list');
  1022. var presence, role, jid, model;
  1023. for (var i=0; i<mock.chatroom_names.length; i++) {
  1024. name = mock.chatroom_names[i];
  1025. role = mock.chatroom_roles[name].role;
  1026. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  1027. jid =
  1028. presence = $pres({
  1029. to:'dummy@localhost/pda',
  1030. from:'lounge@localhost/'+name
  1031. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1032. .c('item').attrs({
  1033. affiliation: mock.chatroom_roles[name].affiliation,
  1034. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  1035. role: role
  1036. }).up()
  1037. .c('status').attrs({code:'110'}).nodeTree;
  1038. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1039. expect(occupants.querySelectorAll('li').length).toBe(2+i);
  1040. model = view.occupantsview.model.where({'nick': name})[0];
  1041. var index = view.occupantsview.model.indexOf(model);
  1042. expect(occupants.querySelectorAll('li .occupant-nick')[index].textContent.trim()).toBe(mock.chatroom_names[i]);
  1043. }
  1044. // Test users leaving the room
  1045. // http://xmpp.org/extensions/xep-0045.html#exit
  1046. for (i=mock.chatroom_names.length-1; i>-1; i--) {
  1047. name = mock.chatroom_names[i];
  1048. role = mock.chatroom_roles[name].role;
  1049. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  1050. presence = $pres({
  1051. to:'dummy@localhost/pda',
  1052. from:'lounge@localhost/'+name,
  1053. type: 'unavailable'
  1054. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1055. .c('item').attrs({
  1056. affiliation: mock.chatroom_roles[name].affiliation,
  1057. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  1058. role: 'none'
  1059. }).nodeTree;
  1060. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1061. expect(occupants.querySelectorAll('li').length).toBe(7);
  1062. }
  1063. done();
  1064. }).catch(_.partial(console.error, _));
  1065. }));
  1066. it("shows users currently present in the room",
  1067. mock.initConverseWithPromises(
  1068. null, ['rosterGroupsFetched'], {},
  1069. function (done, _converse) {
  1070. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function() {
  1071. var name;
  1072. var view = _converse.chatboxviews.get('lounge@localhost'),
  1073. occupants = view.el.querySelector('.occupant-list');
  1074. var presence, role, jid, model;
  1075. for (var i=0; i<mock.chatroom_names.length; i++) {
  1076. name = mock.chatroom_names[i];
  1077. role = mock.chatroom_roles[name].role;
  1078. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  1079. jid =
  1080. presence = $pres({
  1081. to:'dummy@localhost/pda',
  1082. from:'lounge@localhost/'+name
  1083. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1084. .c('item').attrs({
  1085. affiliation: 'none',
  1086. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  1087. role: role
  1088. }).up()
  1089. .c('status').attrs({code:'110'}).nodeTree;
  1090. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1091. expect(occupants.querySelectorAll('li').length).toBe(2+i);
  1092. model = view.occupantsview.model.where({'nick': name})[0];
  1093. var index = view.occupantsview.model.indexOf(model);
  1094. expect(occupants.querySelectorAll('li .occupant-nick')[index].textContent.trim()).toBe(mock.chatroom_names[i]);
  1095. }
  1096. // Test users leaving the room
  1097. // http://xmpp.org/extensions/xep-0045.html#exit
  1098. for (i=mock.chatroom_names.length-1; i>-1; i--) {
  1099. name = mock.chatroom_names[i];
  1100. role = mock.chatroom_roles[name].role;
  1101. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  1102. presence = $pres({
  1103. to:'dummy@localhost/pda',
  1104. from:'lounge@localhost/'+name,
  1105. type: 'unavailable'
  1106. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1107. .c('item').attrs({
  1108. affiliation: mock.chatroom_roles[name].affiliation,
  1109. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  1110. role: 'none'
  1111. }).nodeTree;
  1112. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1113. expect(occupants.querySelectorAll('li').length).toBe(i+1);
  1114. }
  1115. done();
  1116. }).catch(_.partial(console.error, _));
  1117. }));
  1118. it("escapes occupant nicknames when rendering them, to avoid JS-injection attacks",
  1119. mock.initConverseWithPromises(null, ['rosterGroupsFetched'], {}, function (done, _converse) {
  1120. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1121. /* <presence xmlns="jabber:client" to="jc@chat.example.org/converse.js-17184538"
  1122. * from="oo@conference.chat.example.org/&lt;img src=&quot;x&quot; onerror=&quot;alert(123)&quot;/&gt;">
  1123. * <x xmlns="http://jabber.org/protocol/muc#user">
  1124. * <item jid="jc@chat.example.org/converse.js-17184538" affiliation="owner" role="moderator"/>
  1125. * <status code="110"/>
  1126. * </x>
  1127. * </presence>"
  1128. */
  1129. var presence = $pres({
  1130. to:'dummy@localhost/pda',
  1131. from:"lounge@localhost/&lt;img src=&quot;x&quot; onerror=&quot;alert(123)&quot;/&gt;"
  1132. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1133. .c('item').attrs({
  1134. jid: 'someone@localhost',
  1135. role: 'moderator',
  1136. }).up()
  1137. .c('status').attrs({code:'110'}).nodeTree;
  1138. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1139. var view = _converse.chatboxviews.get('lounge@localhost');
  1140. var occupants = view.el.querySelector('.occupant-list').querySelectorAll('li .occupant-nick');
  1141. expect(occupants.length).toBe(2);
  1142. expect($(occupants).first().text().trim()).toBe("&lt;img src=&quot;x&quot; onerror=&quot;alert(123)&quot;/&gt;");
  1143. done();
  1144. });
  1145. }));
  1146. it("indicates moderators and visitors by means of a special css class and tooltip",
  1147. mock.initConverseWithPromises(
  1148. null, ['rosterGroupsFetched'], {},
  1149. function (done, _converse) {
  1150. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1151. var view = _converse.chatboxviews.get('lounge@localhost');
  1152. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  1153. var occupants = view.el.querySelector('.occupant-list').querySelectorAll('li');
  1154. expect(occupants.length).toBe(1);
  1155. expect($(occupants).first().find('.occupant-nick').text().trim()).toBe("dummy");
  1156. expect($(occupants).first().find('.badge').length).toBe(1);
  1157. expect($(occupants).first().find('.badge').first().text()).toBe('Member');
  1158. var presence = $pres({
  1159. to:'dummy@localhost/pda',
  1160. from:'lounge@localhost/moderatorman'
  1161. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1162. .c('item').attrs({
  1163. affiliation: 'admin',
  1164. jid: contact_jid,
  1165. role: 'moderator',
  1166. }).up()
  1167. .c('status').attrs({code:'110'}).nodeTree;
  1168. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1169. occupants = view.el.querySelector('.occupant-list').querySelectorAll('li');
  1170. expect(occupants.length).toBe(2);
  1171. expect($(occupants).first().find('.occupant-nick').text().trim()).toBe("moderatorman");
  1172. expect($(occupants).last().find('.occupant-nick').text().trim()).toBe("dummy");
  1173. expect($(occupants).first().find('.badge').length).toBe(2);
  1174. expect($(occupants).first().find('.badge').first().text()).toBe('Admin');
  1175. expect($(occupants).first().find('.badge').last().text()).toBe('Moderator');
  1176. expect($(occupants).first().attr('title')).toBe(
  1177. contact_jid + ' This user is a moderator. Click to mention moderatorman in your message.'
  1178. );
  1179. contact_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost';
  1180. presence = $pres({
  1181. to:'dummy@localhost/pda',
  1182. from:'lounge@localhost/visitorwoman'
  1183. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1184. .c('item').attrs({
  1185. jid: contact_jid,
  1186. role: 'visitor',
  1187. }).up()
  1188. .c('status').attrs({code:'110'}).nodeTree;
  1189. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1190. occupants = view.el.querySelector('.occupant-list').querySelectorAll('li');
  1191. expect($(occupants).last().find('.occupant-nick').text().trim()).toBe("visitorwoman");
  1192. expect($(occupants).last().find('.badge').length).toBe(1);
  1193. expect($(occupants).last().find('.badge').last().text()).toBe('Visitor');
  1194. expect($(occupants).last().attr('title')).toBe(
  1195. contact_jid + ' This user can NOT send messages in this room. Click to mention visitorwoman in your message.'
  1196. );
  1197. done();
  1198. }).catch(_.partial(console.error, _));
  1199. }));
  1200. it("will use the user's reserved nickname, if it exists",
  1201. mock.initConverseWithPromises(
  1202. null, ['rosterGroupsFetched'], {},
  1203. function (done, _converse) {
  1204. var IQ_stanzas = _converse.connection.IQ_stanzas;
  1205. var sent_IQ, IQ_id;
  1206. var sendIQ = _converse.connection.sendIQ;
  1207. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1208. if (iq.nodeTree.getAttribute('to') === 'lounge@localhost') {
  1209. sent_IQ = iq;
  1210. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1211. } else {
  1212. sendIQ.bind(this)(iq, callback, errback);
  1213. }
  1214. });
  1215. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1216. // We pretend this is a new room, so no disco info is returned.
  1217. var features_stanza = $iq({
  1218. from: 'lounge@localhost',
  1219. 'id': IQ_id,
  1220. 'to': 'dummy@localhost/desktop',
  1221. 'type': 'error'
  1222. }).c('error', {'type': 'cancel'})
  1223. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1224. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1225. var view = _converse.chatboxviews.get('lounge@localhost');
  1226. spyOn(view, 'join').and.callThrough();
  1227. /* <iq from='hag66@shakespeare.lit/pda'
  1228. * id='getnick1'
  1229. * to='coven@chat.shakespeare.lit'
  1230. * type='get'>
  1231. * <query xmlns='http://jabber.org/protocol/disco#info'
  1232. * node='x-roomuser-item'/>
  1233. * </iq>
  1234. */
  1235. test_utils.waitUntil(function () {
  1236. return _.filter(IQ_stanzas, function (iq) {
  1237. return iq.nodeTree.querySelector('query[node="x-roomuser-item"]');
  1238. }).length > 0;
  1239. }, 300).then(function () {
  1240. const iq = _.filter(IQ_stanzas, function (iq) {
  1241. return iq.nodeTree.querySelector(`query[node="x-roomuser-item"]`);
  1242. }).pop();
  1243. const id = iq.nodeTree.getAttribute('id');
  1244. expect(iq.toLocaleString()).toBe(
  1245. "<iq to='lounge@localhost' from='dummy@localhost/resource' "+
  1246. "type='get' xmlns='jabber:client' id='"+id+"'>"+
  1247. "<query xmlns='http://jabber.org/protocol/disco#info' node='x-roomuser-item'/></iq>");
  1248. /* <iq from='coven@chat.shakespeare.lit'
  1249. * id='getnick1'
  1250. * to='hag66@shakespeare.lit/pda'
  1251. * type='result'>
  1252. * <query xmlns='http://jabber.org/protocol/disco#info'
  1253. * node='x-roomuser-item'>
  1254. * <identity
  1255. * category='conference'
  1256. * name='thirdwitch'
  1257. * type='text'/>
  1258. * </query>
  1259. * </iq>
  1260. */
  1261. var stanza = $iq({
  1262. 'type': 'result',
  1263. 'id': IQ_id,
  1264. 'from': view.model.get('jid'),
  1265. 'to': _converse.connection.jid
  1266. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info', 'node': 'x-roomuser-item'})
  1267. .c('identity', {'category': 'conference', 'name': 'thirdwitch', 'type': 'text'});
  1268. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1269. expect(view.join).toHaveBeenCalled();
  1270. // The user has just entered the room (because join was called)
  1271. // and receives their own presence from the server.
  1272. // See example 24:
  1273. // http://xmpp.org/extensions/xep-0045.html#enter-pres
  1274. var presence = $pres({
  1275. to:'dummy@localhost/resource',
  1276. from:'lounge@localhost/thirdwitch',
  1277. id:'DC352437-C019-40EC-B590-AF29E879AF97'
  1278. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1279. .c('item').attrs({
  1280. affiliation: 'member',
  1281. jid: 'dummy@localhost/resource',
  1282. role: 'participant'
  1283. }).up()
  1284. .c('status').attrs({code:'110'}).up()
  1285. .c('status').attrs({code:'210'}).nodeTree;
  1286. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1287. var info_text = $(view.el).find('.chat-content .chat-info:first').text();
  1288. expect(info_text).toBe('Your nickname has been automatically set to thirdwitch');
  1289. done();
  1290. });
  1291. }));
  1292. it("allows the user to invite their roster contacts to enter the chat room",
  1293. mock.initConverseWithPromises(
  1294. null, ['rosterGroupsFetched'], {},
  1295. function (done, _converse) {
  1296. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1297. test_utils.createContacts(_converse, 'current'); // We need roster contacts, so that we have someone to invite
  1298. // Since we don't actually fetch roster contacts, we need to
  1299. // cheat here and emit the event.
  1300. _converse.emit('rosterContactsFetched');
  1301. spyOn(_converse, 'emit');
  1302. spyOn(window, 'prompt').and.callFake(function () {
  1303. return "Please join!";
  1304. });
  1305. var view = _converse.chatboxviews.get('lounge@localhost');
  1306. // XXX: cheating a lttle bit, normally this'll be set after
  1307. // receiving the features for the room.
  1308. view.model.set('open', 'true');
  1309. spyOn(view.model, 'directInvite').and.callThrough();
  1310. var $input;
  1311. $(view.el).find('.chat-area').remove();
  1312. test_utils.waitUntil(function () {
  1313. return $(view.el).find('input.invited-contact').length;
  1314. }, 300).then(function () {
  1315. var $input = $(view.el).find('input.invited-contact');
  1316. expect($input.attr('placeholder')).toBe('Invite');
  1317. $input.val("Felix");
  1318. var evt = new Event('input');
  1319. $input[0].dispatchEvent(evt);
  1320. var sent_stanza;
  1321. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  1322. sent_stanza = stanza;
  1323. });
  1324. var $hint = $input.siblings('ul').children('li');
  1325. expect($input.val()).toBe('Felix');
  1326. expect($hint[0].textContent).toBe('Felix Amsel');
  1327. expect($hint.length).toBe(1);
  1328. if (typeof(Event) === 'function') {
  1329. // Not working on PhantomJS
  1330. evt = new Event('mousedown', {'bubbles': true});
  1331. evt.button = 0; // For some reason awesomplete wants this
  1332. $hint[0].dispatchEvent(evt);
  1333. expect(window.prompt).toHaveBeenCalled();
  1334. expect(view.model.directInvite).toHaveBeenCalled();
  1335. expect(sent_stanza.toLocaleString()).toBe(
  1336. "<message from='dummy@localhost/resource' to='felix.amsel@localhost' id='" +
  1337. sent_stanza.nodeTree.getAttribute('id') +
  1338. "' xmlns='jabber:client'>"+
  1339. "<x xmlns='jabber:x:conference' jid='lounge@localhost' reason='Please join!'/>"+
  1340. "</message>"
  1341. );
  1342. }
  1343. done();
  1344. });
  1345. }));
  1346. it("can be joined automatically, based upon a received invite",
  1347. mock.initConverseWithPromises(
  1348. null, ['rosterGroupsFetched'], {},
  1349. function (done, _converse) {
  1350. test_utils.createContacts(_converse, 'current'); // We need roster contacts, who can invite us
  1351. spyOn(window, 'confirm').and.callFake(function () {
  1352. return true;
  1353. });
  1354. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1355. var view = _converse.chatboxviews.get('lounge@localhost');
  1356. view.close(); // Hack, otherwise we have to mock stanzas.
  1357. var name = mock.cur_names[0];
  1358. var from_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  1359. var room_jid = 'lounge@localhost';
  1360. var reason = "Please join this chat room";
  1361. expect(_converse.chatboxes.models.length).toBe(1);
  1362. expect(_converse.chatboxes.models[0].id).toBe("controlbox");
  1363. var stanza = Strophe.xmlHtmlNode(
  1364. '<message xmlns="jabber:client" to="'+_converse.bare_jid+'" from="'+from_jid+'" id="9bceb415-f34b-4fa4-80d5-c0d076a24231">'+
  1365. '<x xmlns="jabber:x:conference" jid="'+room_jid+'" reason="'+reason+'"/>'+
  1366. '</message>').firstChild;
  1367. _converse.onDirectMUCInvitation(stanza);
  1368. expect(window.confirm).toHaveBeenCalledWith(
  1369. name + ' has invited you to join a chat room: '+ room_jid +
  1370. ', and left the following reason: "'+reason+'"');
  1371. expect(_converse.chatboxes.models.length).toBe(2);
  1372. expect(_converse.chatboxes.models[0].id).toBe('controlbox');
  1373. expect(_converse.chatboxes.models[1].id).toBe(room_jid);
  1374. done();
  1375. });
  1376. }));
  1377. it("shows received groupchat messages",
  1378. mock.initConverseWithPromises(
  1379. null, ['rosterGroupsFetched'], {},
  1380. function (done, _converse) {
  1381. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1382. spyOn(_converse, 'emit');
  1383. var view = _converse.chatboxviews.get('lounge@localhost');
  1384. if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
  1385. var nick = mock.chatroom_names[0];
  1386. view.model.occupants.create({
  1387. 'nick': nick,
  1388. 'muc_jid': `${view.model.get('jid')}/${nick}`
  1389. });
  1390. var text = 'This is a received message';
  1391. var message = $msg({
  1392. from: 'lounge@localhost/'+nick,
  1393. id: '1',
  1394. to: 'dummy@localhost',
  1395. type: 'groupchat'
  1396. }).c('body').t(text);
  1397. view.model.onMessage(message.nodeTree);
  1398. var $chat_content = $(view.el).find('.chat-content');
  1399. expect($chat_content.find('.chat-msg').length).toBe(1);
  1400. expect($chat_content.find('.chat-msg-text').text()).toBe(text);
  1401. expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
  1402. done();
  1403. });
  1404. }));
  1405. it("shows sent groupchat messages",
  1406. mock.initConverseWithPromises(
  1407. null, ['rosterGroupsFetched'], {},
  1408. function (done, _converse) {
  1409. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1410. spyOn(_converse, 'emit');
  1411. var view = _converse.chatboxviews.get('lounge@localhost');
  1412. if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
  1413. var text = 'This is a sent message';
  1414. var textarea = view.el.querySelector('.chat-textarea');
  1415. textarea.value = text;
  1416. view.keyPressed({
  1417. target: textarea,
  1418. preventDefault: _.noop,
  1419. keyCode: 13
  1420. });
  1421. expect(_converse.emit).toHaveBeenCalledWith('messageSend', text);
  1422. var $chat_content = $(view.el).find('.chat-content');
  1423. expect($chat_content.find('.chat-msg').length).toBe(1);
  1424. // Let's check that if we receive the same message again, it's
  1425. // not shown.
  1426. var message = $msg({
  1427. from: 'lounge@localhost/dummy',
  1428. to: 'dummy@localhost.com',
  1429. type: 'groupchat',
  1430. id: view.model.messages.at(0).get('msgid')
  1431. }).c('body').t(text);
  1432. view.model.onMessage(message.nodeTree);
  1433. expect($chat_content.find('.chat-msg').length).toBe(1);
  1434. expect($chat_content.find('.chat-msg-text').last().text()).toBe(text);
  1435. // We don't emit an event if it's our own message
  1436. expect(_converse.emit.calls.count(), 1);
  1437. done();
  1438. });
  1439. }));
  1440. it("will cause the chat area to be scrolled down only if it was at the bottom already",
  1441. mock.initConverseWithPromises(
  1442. null, ['rosterGroupsFetched'], {},
  1443. function (done, _converse) {
  1444. var message = 'This message is received while the chat area is scrolled up';
  1445. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1446. var view = _converse.chatboxviews.get('lounge@localhost');
  1447. spyOn(view, 'scrollDown').and.callThrough();
  1448. /* Create enough messages so that there's a
  1449. * scrollbar.
  1450. */
  1451. for (var i=0; i<20; i++) {
  1452. view.model.onMessage(
  1453. $msg({
  1454. from: 'lounge@localhost/someone',
  1455. to: 'dummy@localhost.com',
  1456. type: 'groupchat',
  1457. id: (new Date()).getTime(),
  1458. }).c('body').t('Message: '+i).tree());
  1459. }
  1460. // Give enough time for `markScrolled` to have been called
  1461. setTimeout(function () {
  1462. view.content.scrollTop = 0;
  1463. view.model.onMessage(
  1464. $msg({
  1465. from: 'lounge@localhost/someone',
  1466. to: 'dummy@localhost.com',
  1467. type: 'groupchat',
  1468. id: (new Date()).getTime(),
  1469. }).c('body').t(message).tree());
  1470. // Now check that the message appears inside the chatbox in the DOM
  1471. var $chat_content = $(view.el).find('.chat-content');
  1472. var msg_txt = $chat_content.find('.chat-msg:last').find('.chat-msg-text').text();
  1473. expect(msg_txt).toEqual(message);
  1474. expect(view.content.scrollTop).toBe(0);
  1475. done();
  1476. }, 500);
  1477. });
  1478. }));
  1479. it("shows received chatroom subject messages",
  1480. mock.initConverseWithPromises(
  1481. null, ['rosterGroupsFetched'], {},
  1482. function (done, _converse) {
  1483. test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc').then(function () {
  1484. var text = 'Jabber/XMPP Development | RFCs and Extensions: http://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  1485. var stanza = Strophe.xmlHtmlNode(
  1486. '<message xmlns="jabber:client" to="jc@opkode.com/_converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">'+
  1487. ' <subject>'+text+'</subject>'+
  1488. ' <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>'+
  1489. ' <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>'+
  1490. '</message>').firstChild;
  1491. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1492. var view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1493. var chat_content = view.el.querySelector('.chat-content');
  1494. expect($(chat_content).find('.chat-event:last').text()).toBe('Topic set by ralphm');
  1495. expect($(chat_content).find('.chat-topic:last').text()).toBe(text);
  1496. done();
  1497. });
  1498. }));
  1499. it("escapes the subject before rendering it, to avoid JS-injection attacks",
  1500. mock.initConverseWithPromises(
  1501. null, ['rosterGroupsFetched'], {},
  1502. function (done, _converse) {
  1503. test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc').then(function () {
  1504. spyOn(window, 'alert');
  1505. var subject = '<img src="x" onerror="alert(\'XSS\');"/>';
  1506. var view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1507. view.model.set({'subject': {
  1508. 'text': subject,
  1509. 'author': 'ralphm'
  1510. }});
  1511. var chat_content = view.el.querySelector('.chat-content');
  1512. expect($(chat_content).find('.chat-event:last').text()).toBe('Topic set by ralphm');
  1513. expect($(chat_content).find('.chat-topic:last').text()).toBe(subject);
  1514. done();
  1515. });
  1516. }));
  1517. it("informs users if their nicknames has been changed.",
  1518. mock.initConverseWithPromises(
  1519. null, ['rosterGroupsFetched'], {},
  1520. function (done, _converse) {
  1521. /* The service then sends two presence stanzas to the full JID
  1522. * of each occupant (including the occupant who is changing his
  1523. * or her room nickname), one of type "unavailable" for the old
  1524. * nickname and one indicating availability for the new
  1525. * nickname.
  1526. *
  1527. * See: http://xmpp.org/extensions/xep-0045.html#changenick
  1528. *
  1529. * <presence
  1530. * from='coven@localhost/thirdwitch'
  1531. * id='DC352437-C019-40EC-B590-AF29E879AF98'
  1532. * to='hag66@shakespeare.lit/pda'
  1533. * type='unavailable'>
  1534. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1535. * <item affiliation='member'
  1536. * jid='hag66@shakespeare.lit/pda'
  1537. * nick='oldhag'
  1538. * role='participant'/>
  1539. * <status code='303'/>
  1540. * <status code='110'/>
  1541. * </x>
  1542. * </presence>
  1543. *
  1544. * <presence
  1545. * from='coven@localhost/oldhag'
  1546. * id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
  1547. * to='hag66@shakespeare.lit/pda'>
  1548. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1549. * <item affiliation='member'
  1550. * jid='hag66@shakespeare.lit/pda'
  1551. * role='participant'/>
  1552. * <status code='110'/>
  1553. * </x>
  1554. * </presence>
  1555. */
  1556. var __ = _converse.__;
  1557. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'oldnick').then(function () {
  1558. var view = _converse.chatboxviews.get('lounge@localhost');
  1559. var $chat_content = $(view.el).find('.chat-content');
  1560. var $occupants = $(view.el.querySelector('.occupant-list'));
  1561. expect($occupants.children().length).toBe(1);
  1562. expect($occupants.children().first(0).find('.occupant-nick').text().trim()).toBe("oldnick");
  1563. expect($chat_content.find('div.chat-info').length).toBe(1);
  1564. expect($chat_content.find('div.chat-info:first').html()).toBe("oldnick has entered the room");
  1565. var presence = $pres().attrs({
  1566. from:'lounge@localhost/oldnick',
  1567. id:'DC352437-C019-40EC-B590-AF29E879AF98',
  1568. to:'dummy@localhost/pda',
  1569. type:'unavailable'
  1570. })
  1571. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1572. .c('item').attrs({
  1573. affiliation: 'member',
  1574. jid: 'dummy@localhost/pda',
  1575. nick: 'newnick',
  1576. role: 'participant'
  1577. }).up()
  1578. .c('status').attrs({code:'303'}).up()
  1579. .c('status').attrs({code:'110'}).nodeTree;
  1580. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1581. expect($chat_content.find('div.chat-info').length).toBe(2);
  1582. expect($chat_content.find('div.chat-info').last().html()).toBe(
  1583. __(_converse.muc.new_nickname_messages["303"], "newnick")
  1584. );
  1585. $occupants = $(view.el.querySelector('.occupant-list'));
  1586. expect($occupants.children().length).toBe(1);
  1587. presence = $pres().attrs({
  1588. from:'lounge@localhost/newnick',
  1589. id:'5B4F27A4-25ED-43F7-A699-382C6B4AFC67',
  1590. to:'dummy@localhost/pda'
  1591. })
  1592. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1593. .c('item').attrs({
  1594. affiliation: 'member',
  1595. jid: 'dummy@localhost/pda',
  1596. role: 'participant'
  1597. }).up()
  1598. .c('status').attrs({code:'110'}).nodeTree;
  1599. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1600. // XXX: currently we still have an additional "has entered the room"
  1601. // notification for the new nickname. Ideally we'd not have
  1602. // that, but that's probably not possible without some
  1603. // significant refactoring.
  1604. expect($chat_content.find('div.chat-info').length).toBe(3);
  1605. expect($chat_content.find('div.chat-info').get(1).textContent).toBe(
  1606. __(_converse.muc.new_nickname_messages["303"], "newnick")
  1607. );
  1608. $occupants = $(view.el.querySelector('.occupant-list'));
  1609. expect($occupants.children().length).toBe(1);
  1610. expect($occupants.children().find('.occupant-nick').first(0).text()).toBe("newnick");
  1611. done();
  1612. });
  1613. }));
  1614. it("queries for the room information before attempting to join the user",
  1615. mock.initConverseWithPromises(
  1616. null, ['rosterGroupsFetched'], {},
  1617. function (done, _converse) {
  1618. var sent_IQ, IQ_id;
  1619. var sendIQ = _converse.connection.sendIQ;
  1620. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1621. sent_IQ = iq;
  1622. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1623. });
  1624. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  1625. // Check that the room queried for the feautures.
  1626. expect(sent_IQ.toLocaleString()).toBe(
  1627. "<iq from='dummy@localhost/resource' to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  1628. "<query xmlns='http://jabber.org/protocol/disco#info'/>"+
  1629. "</iq>");
  1630. /* <iq from='coven@chat.shakespeare.lit'
  1631. * id='ik3vs715'
  1632. * to='hag66@shakespeare.lit/pda'
  1633. * type='result'>
  1634. * <query xmlns='http://jabber.org/protocol/disco#info'>
  1635. * <identity
  1636. * category='conference'
  1637. * name='A Dark Cave'
  1638. * type='text'/>
  1639. * <feature var='http://jabber.org/protocol/muc'/>
  1640. * <feature var='muc_passwordprotected'/>
  1641. * <feature var='muc_hidden'/>
  1642. * <feature var='muc_temporary'/>
  1643. * <feature var='muc_open'/>
  1644. * <feature var='muc_unmoderated'/>
  1645. * <feature var='muc_nonanonymous'/>
  1646. * </query>
  1647. * </iq>
  1648. */
  1649. var features_stanza = $iq({
  1650. from: 'coven@chat.shakespeare.lit',
  1651. 'id': IQ_id,
  1652. 'to': 'dummy@localhost/desktop',
  1653. 'type': 'result'
  1654. })
  1655. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  1656. .c('identity', {
  1657. 'category': 'conference',
  1658. 'name': 'A Dark Cave',
  1659. 'type': 'text'
  1660. }).up()
  1661. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  1662. .c('feature', {'var': 'muc_passwordprotected'}).up()
  1663. .c('feature', {'var': 'muc_hidden'}).up()
  1664. .c('feature', {'var': 'muc_temporary'}).up()
  1665. .c('feature', {'var': 'muc_open'}).up()
  1666. .c('feature', {'var': 'muc_unmoderated'}).up()
  1667. .c('feature', {'var': 'muc_nonanonymous'});
  1668. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1669. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1670. expect(view.model.get('features_fetched')).toBe(true);
  1671. expect(view.model.get('passwordprotected')).toBe(true);
  1672. expect(view.model.get('hidden')).toBe(true);
  1673. expect(view.model.get('temporary')).toBe(true);
  1674. expect(view.model.get('open')).toBe(true);
  1675. expect(view.model.get('unmoderated')).toBe(true);
  1676. expect(view.model.get('nonanonymous')).toBe(true);
  1677. done();
  1678. }));
  1679. it("updates the shown features when the room configuration has changed",
  1680. mock.initConverseWithPromises(
  1681. null, ['rosterGroupsFetched'], {},
  1682. function (done, _converse) {
  1683. var sent_IQ, IQ_id;
  1684. var sendIQ = _converse.connection.sendIQ;
  1685. test_utils.openAndEnterChatRoom(_converse, 'room', 'conference.example.org', 'dummy').then(function () {
  1686. var view = _converse.chatboxviews.get('room@conference.example.org');
  1687. view.model.set({
  1688. 'passwordprotected': false,
  1689. 'unsecured': true,
  1690. 'hidden': false,
  1691. 'public': true,
  1692. 'membersonly': false,
  1693. 'open': true,
  1694. 'persistent': false,
  1695. 'temporary': true,
  1696. 'nonanonymous': true,
  1697. 'semianonymous': false,
  1698. 'moderated': false,
  1699. 'unmoderated': true
  1700. });
  1701. expect(view.model.get('persistent')).toBe(false);
  1702. expect(view.model.get('temporary')).toBe(true);
  1703. view.model.set({'persistent': true});
  1704. expect(view.model.get('persistent')).toBe(true);
  1705. expect(view.model.get('temporary')).toBe(false);
  1706. expect(view.model.get('unsecured')).toBe(true);
  1707. expect(view.model.get('passwordprotected')).toBe(false);
  1708. view.model.set({'passwordprotected': true});
  1709. expect(view.model.get('unsecured')).toBe(false);
  1710. expect(view.model.get('passwordprotected')).toBe(true);
  1711. expect(view.model.get('unmoderated')).toBe(true);
  1712. expect(view.model.get('moderated')).toBe(false);
  1713. view.model.set({'moderated': true});
  1714. expect(view.model.get('unmoderated')).toBe(false);
  1715. expect(view.model.get('moderated')).toBe(true);
  1716. expect(view.model.get('nonanonymous')).toBe(true);
  1717. expect(view.model.get('semianonymous')).toBe(false);
  1718. view.model.set({'nonanonymous': false});
  1719. expect(view.model.get('nonanonymous')).toBe(false);
  1720. expect(view.model.get('semianonymous')).toBe(true);
  1721. expect(view.model.get('open')).toBe(true);
  1722. expect(view.model.get('membersonly')).toBe(false);
  1723. view.model.set({'membersonly': true});
  1724. expect(view.model.get('open')).toBe(false);
  1725. expect(view.model.get('membersonly')).toBe(true);
  1726. done();
  1727. }).catch(_.partial(console.error, _));
  1728. }));
  1729. it("indicates when a room is no longer anonymous",
  1730. mock.initConverseWithPromises(
  1731. null, ['rosterGroupsFetched'], {},
  1732. function (done, _converse) {
  1733. var sent_IQ, IQ_id;
  1734. var sendIQ = _converse.connection.sendIQ;
  1735. test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'some1').then(function () {
  1736. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1737. sent_IQ = iq;
  1738. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1739. });
  1740. // We pretend this is a new room, so no disco info is returned.
  1741. var features_stanza = $iq({
  1742. from: 'coven@chat.shakespeare.lit',
  1743. 'id': IQ_id,
  1744. 'to': 'dummy@localhost/desktop',
  1745. 'type': 'error'
  1746. }).c('error', {'type': 'cancel'})
  1747. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1748. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1749. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1750. /* <message xmlns="jabber:client"
  1751. * type="groupchat"
  1752. * to="dummy@localhost/_converse.js-27854181"
  1753. * from="coven@chat.shakespeare.lit">
  1754. * <x xmlns="http://jabber.org/protocol/muc#user">
  1755. * <status code="104"/>
  1756. * <status code="172"/>
  1757. * </x>
  1758. * </message>
  1759. */
  1760. var message = $msg({
  1761. type:'groupchat',
  1762. to: 'dummy@localhost/_converse.js-27854181',
  1763. from: 'coven@chat.shakespeare.lit'
  1764. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  1765. .c('status', {code: '104'}).up()
  1766. .c('status', {code: '172'});
  1767. _converse.connection._dataRecv(test_utils.createRequest(message));
  1768. var $chat_body = $(view.el.querySelector('.chatroom-body'));
  1769. expect($chat_body.find('.message:last').text()).toBe('This room is now no longer anonymous');
  1770. done();
  1771. });
  1772. }));
  1773. it("informs users if they have been kicked out of the chat room",
  1774. mock.initConverseWithPromises(
  1775. null, ['rosterGroupsFetched'], {},
  1776. function (done, _converse) {
  1777. /* <presence
  1778. * from='harfleur@chat.shakespeare.lit/pistol'
  1779. * to='pistol@shakespeare.lit/harfleur'
  1780. * type='unavailable'>
  1781. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1782. * <item affiliation='none' role='none'>
  1783. * <actor nick='Fluellen'/>
  1784. * <reason>Avaunt, you cullion!</reason>
  1785. * </item>
  1786. * <status code='110'/>
  1787. * <status code='307'/>
  1788. * </x>
  1789. * </presence>
  1790. */
  1791. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1792. var presence = $pres().attrs({
  1793. from:'lounge@localhost/dummy',
  1794. to:'dummy@localhost/pda',
  1795. type:'unavailable'
  1796. })
  1797. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1798. .c('item').attrs({
  1799. affiliation: 'none',
  1800. jid: 'dummy@localhost/pda',
  1801. role: 'none'
  1802. })
  1803. .c('actor').attrs({nick: 'Fluellen'}).up()
  1804. .c('reason').t('Avaunt, you cullion!').up()
  1805. .up()
  1806. .c('status').attrs({code:'110'}).up()
  1807. .c('status').attrs({code:'307'}).nodeTree;
  1808. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1809. var view = _converse.chatboxviews.get('lounge@localhost');
  1810. expect($(view.el.querySelector('.chat-area')).is(':visible')).toBeFalsy();
  1811. expect($(view.el.querySelector('.occupants')).is(':visible')).toBeFalsy();
  1812. var $chat_body = $(view.el.querySelector('.chatroom-body'));
  1813. expect($chat_body.find('.disconnect-msg').text()).toBe(
  1814. 'You have been kicked from this room'+
  1815. 'This action was done by Fluellen.'+
  1816. 'The reason given is: "Avaunt, you cullion!".'
  1817. );
  1818. done();
  1819. });
  1820. }));
  1821. it("can be saved to, and retrieved from, browserStorage",
  1822. mock.initConverseWithPromises(
  1823. null, ['rosterGroupsFetched'], {},
  1824. function (done, _converse) {
  1825. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1826. // We instantiate a new ChatBoxes collection, which by default
  1827. // will be empty.
  1828. test_utils.openControlBox();
  1829. var newchatboxes = new _converse.ChatBoxes();
  1830. expect(newchatboxes.length).toEqual(0);
  1831. // The chatboxes will then be fetched from browserStorage inside the
  1832. // onConnected method
  1833. newchatboxes.onConnected();
  1834. expect(newchatboxes.length).toEqual(2);
  1835. // Check that the chatrooms retrieved from browserStorage
  1836. // have the same attributes values as the original ones.
  1837. var attrs = ['id', 'box_id', 'visible'];
  1838. var new_attrs, old_attrs;
  1839. for (var i=0; i<attrs.length; i++) {
  1840. new_attrs = _.map(_.map(newchatboxes.models, 'attributes'), attrs[i]);
  1841. old_attrs = _.map(_.map(_converse.chatboxes.models, 'attributes'), attrs[i]);
  1842. // FIXME: should have have to sort here? Order must
  1843. // probably be the same...
  1844. // This should be fixed once the controlbox always opens
  1845. // only on the right.
  1846. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  1847. }
  1848. _converse.rosterview.render();
  1849. done();
  1850. }));
  1851. it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'",
  1852. mock.initConverseWithPromises(
  1853. null, ['rosterGroupsFetched'], {},
  1854. function (done, _converse) {
  1855. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1856. var view = _converse.chatboxviews.get('lounge@localhost'),
  1857. trimmed_chatboxes = _converse.minimized_chats;
  1858. spyOn(view, 'minimize').and.callThrough();
  1859. spyOn(view, 'maximize').and.callThrough();
  1860. spyOn(_converse, 'emit');
  1861. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1862. view.el.querySelector('.toggle-chatbox-button').click();
  1863. expect(view.minimize).toHaveBeenCalled();
  1864. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMinimized', jasmine.any(Object));
  1865. expect(u.isVisible(view.el)).toBeFalsy();
  1866. expect(view.model.get('minimized')).toBeTruthy();
  1867. expect(view.minimize).toHaveBeenCalled();
  1868. var trimmedview = trimmed_chatboxes.get(view.model.get('id'));
  1869. trimmedview.el.querySelector("a.restore-chat").click();
  1870. expect(view.maximize).toHaveBeenCalled();
  1871. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMaximized', jasmine.any(Object));
  1872. expect(view.model.get('minimized')).toBeFalsy();
  1873. expect(_converse.emit.calls.count(), 3);
  1874. done();
  1875. }));
  1876. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'",
  1877. mock.initConverseWithPromises(
  1878. null, ['rosterGroupsFetched'], {},
  1879. function (done, _converse) {
  1880. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1881. var view = _converse.chatboxviews.get('lounge@localhost');
  1882. spyOn(view, 'close').and.callThrough();
  1883. spyOn(_converse, 'emit');
  1884. spyOn(view.model, 'leave');
  1885. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1886. view.el.querySelector('.close-chatbox-button').click();
  1887. expect(view.close).toHaveBeenCalled();
  1888. expect(view.model.leave).toHaveBeenCalled();
  1889. expect(_converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  1890. done();
  1891. }));
  1892. });
  1893. describe("Each chat room can take special commands", function () {
  1894. it("/help to show the available commands",
  1895. mock.initConverseWithPromises(
  1896. null, ['rosterGroupsFetched'], {},
  1897. function (done, _converse) {
  1898. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1899. var view = _converse.chatboxviews.get('lounge@localhost');
  1900. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1901. var textarea = view.el.querySelector('.chat-textarea');
  1902. textarea.value = '/help This is the room subject';
  1903. view.keyPressed({
  1904. target: textarea,
  1905. preventDefault: _.noop,
  1906. keyCode: 13
  1907. });
  1908. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1909. const info_messages = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  1910. expect(info_messages.length).toBe(17);
  1911. expect(info_messages.pop().textContent).toBe('/voice: Allow muted user to post messages');
  1912. expect(info_messages.pop().textContent).toBe('/topic: Set room subject (alias for /subject)');
  1913. expect(info_messages.pop().textContent).toBe('/subject: Set room subject');
  1914. expect(info_messages.pop().textContent).toBe('/revoke: Revoke user\'s membership');
  1915. expect(info_messages.pop().textContent).toBe('/owner: Grant ownership of this room');
  1916. expect(info_messages.pop().textContent).toBe('/op: Grant moderator role to user');
  1917. expect(info_messages.pop().textContent).toBe('/nick: Change your nickname');
  1918. expect(info_messages.pop().textContent).toBe('/mute: Remove user\'s ability to post messages');
  1919. expect(info_messages.pop().textContent).toBe('/member: Grant membership to a user');
  1920. expect(info_messages.pop().textContent).toBe('/me: Write in 3rd person');
  1921. expect(info_messages.pop().textContent).toBe('/kick: Kick user from room');
  1922. expect(info_messages.pop().textContent).toBe('/help: Show this menu');
  1923. expect(info_messages.pop().textContent).toBe('/deop: Change user role to participant');
  1924. expect(info_messages.pop().textContent).toBe('/clear: Remove messages');
  1925. expect(info_messages.pop().textContent).toBe('/ban: Ban user from room');
  1926. expect(info_messages.pop().textContent).toBe('/admin: Change user\'s affiliation to admin');
  1927. done();
  1928. });
  1929. }));
  1930. it("/topic to set the room topic",
  1931. mock.initConverseWithPromises(
  1932. null, ['rosterGroupsFetched'], {},
  1933. function (done, _converse) {
  1934. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1935. var sent_stanza;
  1936. var view = _converse.chatboxviews.get('lounge@localhost');
  1937. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1938. spyOn(view, 'clearMessages');
  1939. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  1940. sent_stanza = stanza;
  1941. });
  1942. // Check the alias /topic
  1943. var textarea = view.el.querySelector('.chat-textarea');
  1944. textarea.value = '/topic This is the room subject';
  1945. view.keyPressed({
  1946. target: textarea,
  1947. preventDefault: _.noop,
  1948. keyCode: 13
  1949. });
  1950. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1951. expect(_converse.connection.send).toHaveBeenCalled();
  1952. expect(sent_stanza.textContent).toBe('This is the room subject');
  1953. // Check /subject
  1954. textarea.value = '/subject This is a new subject';
  1955. view.keyPressed({
  1956. target: textarea,
  1957. preventDefault: _.noop,
  1958. keyCode: 13
  1959. });
  1960. expect(sent_stanza.textContent).toBe('This is a new subject');
  1961. expect(sent_stanza.outerHTML).toBe(
  1962. '<message to="lounge@localhost" from="dummy@localhost/resource" type="groupchat" xmlns="jabber:client">'+
  1963. '<subject xmlns="jabber:client">This is a new subject</subject>'+
  1964. '</message>');
  1965. // Check case insensitivity
  1966. textarea.value = '/Subject This is yet another subject';
  1967. view.keyPressed({
  1968. target: textarea,
  1969. preventDefault: _.noop,
  1970. keyCode: 13
  1971. });
  1972. expect(sent_stanza.textContent).toBe('This is yet another subject');
  1973. expect(sent_stanza.outerHTML).toBe(
  1974. '<message to="lounge@localhost" from="dummy@localhost/resource" type="groupchat" xmlns="jabber:client">'+
  1975. '<subject xmlns="jabber:client">This is yet another subject</subject>'+
  1976. '</message>');
  1977. done();
  1978. }).catch(_.partial(console.error, _));
  1979. }));
  1980. it("/clear to clear messages",
  1981. mock.initConverseWithPromises(
  1982. null, ['rosterGroupsFetched'], {},
  1983. function (done, _converse) {
  1984. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1985. var view = _converse.chatboxviews.get('lounge@localhost');
  1986. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1987. spyOn(view, 'clearMessages');
  1988. var textarea = view.el.querySelector('.chat-textarea')
  1989. textarea.value = '/clear';
  1990. view.keyPressed({
  1991. target: textarea,
  1992. preventDefault: _.noop,
  1993. keyCode: 13
  1994. });
  1995. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1996. expect(view.clearMessages).toHaveBeenCalled();
  1997. done();
  1998. }).catch(_.partial(console.error, _));
  1999. }));
  2000. it("/owner to make a user an owner",
  2001. mock.initConverseWithPromises(
  2002. null, ['rosterGroupsFetched'], {},
  2003. function (done, _converse) {
  2004. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2005. var sent_IQ, IQ_id;
  2006. var sendIQ = _converse.connection.sendIQ;
  2007. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2008. sent_IQ = iq;
  2009. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2010. });
  2011. var view = _converse.chatboxviews.get('lounge@localhost');
  2012. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2013. spyOn(view.model, 'setAffiliation').and.callThrough();
  2014. spyOn(view, 'showErrorMessage').and.callThrough();
  2015. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2016. var textarea = view.el.querySelector('.chat-textarea')
  2017. textarea.value = '/owner';
  2018. view.keyPressed({
  2019. target: textarea,
  2020. preventDefault: _.noop,
  2021. keyCode: 13
  2022. });
  2023. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2024. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2025. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2026. "Error: the \"owner\" command takes two arguments, the user's nickname and optionally a reason.",
  2027. true
  2028. );
  2029. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2030. // Call now with the correct amount of arguments.
  2031. // XXX: Calling onMessageSubmitted directly, trying
  2032. // again via triggering Event doesn't work for some weird
  2033. // reason.
  2034. view.onMessageSubmitted('/owner annoyingGuy@localhost You\'re responsible');
  2035. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2036. expect(view.showErrorMessage.calls.count()).toBe(1);
  2037. expect(view.model.setAffiliation).toHaveBeenCalled();
  2038. // Check that the member list now gets updated
  2039. expect(sent_IQ.toLocaleString()).toBe(
  2040. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2041. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2042. "<item affiliation='owner' jid='annoyingGuy@localhost'>"+
  2043. "<reason>You&apos;re responsible</reason>"+
  2044. "</item>"+
  2045. "</query>"+
  2046. "</iq>");
  2047. done();
  2048. }).catch(_.partial(console.error, _));
  2049. }));
  2050. it("/ban to ban a user",
  2051. mock.initConverseWithPromises(
  2052. null, ['rosterGroupsFetched'], {},
  2053. function (done, _converse) {
  2054. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2055. var sent_IQ, IQ_id;
  2056. var sendIQ = _converse.connection.sendIQ;
  2057. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2058. sent_IQ = iq;
  2059. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2060. });
  2061. var view = _converse.chatboxviews.get('lounge@localhost');
  2062. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2063. spyOn(view.model, 'setAffiliation').and.callThrough();
  2064. spyOn(view, 'showErrorMessage').and.callThrough();
  2065. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2066. var textarea = view.el.querySelector('.chat-textarea')
  2067. textarea.value = '/ban';
  2068. view.keyPressed({
  2069. target: textarea,
  2070. preventDefault: _.noop,
  2071. keyCode: 13
  2072. });
  2073. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2074. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2075. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2076. "Error: the \"ban\" command takes two arguments, the user's nickname and optionally a reason.",
  2077. true
  2078. );
  2079. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2080. // Call now with the correct amount of arguments.
  2081. // XXX: Calling onMessageSubmitted directly, trying
  2082. // again via triggering Event doesn't work for some weird
  2083. // reason.
  2084. view.onMessageSubmitted('/ban annoyingGuy@localhost You\'re annoying');
  2085. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2086. expect(view.showErrorMessage.calls.count()).toBe(1);
  2087. expect(view.model.setAffiliation).toHaveBeenCalled();
  2088. // Check that the member list now gets updated
  2089. expect(sent_IQ.toLocaleString()).toBe(
  2090. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2091. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2092. "<item affiliation='outcast' jid='annoyingGuy@localhost'>"+
  2093. "<reason>You&apos;re annoying</reason>"+
  2094. "</item>"+
  2095. "</query>"+
  2096. "</iq>");
  2097. done();
  2098. }).catch(_.partial(console.error, _));
  2099. }));
  2100. it("/kick to kick a user",
  2101. mock.initConverseWithPromises(
  2102. null, ['rosterGroupsFetched'], {},
  2103. function (done, _converse) {
  2104. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2105. var sent_IQ, IQ_id;
  2106. var sendIQ = _converse.connection.sendIQ;
  2107. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2108. sent_IQ = iq;
  2109. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2110. });
  2111. var view = _converse.chatboxviews.get('lounge@localhost');
  2112. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2113. spyOn(view, 'modifyRole').and.callThrough();
  2114. spyOn(view, 'showErrorMessage').and.callThrough();
  2115. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2116. var textarea = view.el.querySelector('.chat-textarea')
  2117. textarea.value = '/kick';
  2118. view.keyPressed({
  2119. target: textarea,
  2120. preventDefault: _.noop,
  2121. keyCode: 13
  2122. });
  2123. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2124. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2125. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2126. "Error: the \"kick\" command takes two arguments, the user's nickname and optionally a reason.",
  2127. true
  2128. );
  2129. expect(view.modifyRole).not.toHaveBeenCalled();
  2130. // Call now with the correct amount of arguments.
  2131. // XXX: Calling onMessageSubmitted directly, trying
  2132. // again via triggering Event doesn't work for some weird
  2133. // reason.
  2134. view.onMessageSubmitted('/kick annoyingGuy You\'re annoying');
  2135. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2136. expect(view.showErrorMessage.calls.count()).toBe(1);
  2137. expect(view.modifyRole).toHaveBeenCalled();
  2138. expect(sent_IQ.toLocaleString()).toBe(
  2139. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2140. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2141. "<item nick='annoyingGuy' role='none'>"+
  2142. "<reason>You&apos;re annoying</reason>"+
  2143. "</item>"+
  2144. "</query>"+
  2145. "</iq>");
  2146. /* <presence
  2147. * from='harfleur@chat.shakespeare.lit/pistol'
  2148. * to='gower@shakespeare.lit/cell'
  2149. * type='unavailable'>
  2150. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2151. * <item affiliation='none' role='none'/>
  2152. * <status code='307'/>
  2153. * </x>
  2154. * </presence>
  2155. */
  2156. var presence = $pres({
  2157. 'from': 'lounge@localhost/annoyingGuy',
  2158. 'to': 'dummy@localhost/desktop',
  2159. 'type': 'unavailable'
  2160. })
  2161. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2162. .c('item', {
  2163. 'affiliation': 'none',
  2164. 'role': 'none'
  2165. }).up()
  2166. .c('status', {'code': '307'});
  2167. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2168. expect(
  2169. view.el.querySelectorAll('.chat-info')[2].textContent).toBe(
  2170. "annoyingGuy has been kicked out");
  2171. done();
  2172. }).catch(_.partial(console.error, _));
  2173. }));
  2174. it("/op and /deop to make a user a moderator or not",
  2175. mock.initConverseWithPromises(
  2176. null, ['rosterGroupsFetched'], {},
  2177. function (done, _converse) {
  2178. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2179. var sent_IQ, IQ_id;
  2180. var sendIQ = _converse.connection.sendIQ;
  2181. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2182. sent_IQ = iq;
  2183. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2184. });
  2185. var view = _converse.chatboxviews.get('lounge@localhost');
  2186. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2187. spyOn(view, 'modifyRole').and.callThrough();
  2188. spyOn(view, 'showErrorMessage').and.callThrough();
  2189. spyOn(view, 'showChatEvent').and.callThrough();
  2190. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2191. // New user enters the room
  2192. /* <presence
  2193. * from='coven@chat.shakespeare.lit/thirdwitch'
  2194. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  2195. * to='crone1@shakespeare.lit/desktop'>
  2196. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2197. * <item affiliation='member' role='moderator'/>
  2198. * </x>
  2199. * </presence>
  2200. */
  2201. var presence = $pres({
  2202. 'from': 'lounge@localhost/trustworthyguy',
  2203. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2204. 'to': 'dummy@localhost/desktop'
  2205. })
  2206. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2207. .c('item', {
  2208. 'jid': 'trustworthyguy@localhost',
  2209. 'affiliation': 'member',
  2210. 'role': 'participant'
  2211. });
  2212. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2213. var info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2214. expect(info_msgs.pop().textContent).toBe("trustworthyguy has entered the room");
  2215. var textarea = view.el.querySelector('.chat-textarea')
  2216. textarea.value = '/op';
  2217. view.keyPressed({
  2218. target: textarea,
  2219. preventDefault: _.noop,
  2220. keyCode: 13
  2221. });
  2222. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2223. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2224. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2225. "Error: the \"op\" command takes two arguments, the user's nickname and optionally a reason.",
  2226. true
  2227. );
  2228. expect(view.modifyRole).not.toHaveBeenCalled();
  2229. // Call now with the correct amount of arguments.
  2230. // XXX: Calling onMessageSubmitted directly, trying
  2231. // again via triggering Event doesn't work for some weird
  2232. // reason.
  2233. view.onMessageSubmitted('/op trustworthyguy You\'re trustworthy');
  2234. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2235. expect(view.showErrorMessage.calls.count()).toBe(1);
  2236. expect(view.modifyRole).toHaveBeenCalled();
  2237. expect(sent_IQ.toLocaleString()).toBe(
  2238. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2239. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2240. "<item nick='trustworthyguy' role='moderator'>"+
  2241. "<reason>You&apos;re trustworthy</reason>"+
  2242. "</item>"+
  2243. "</query>"+
  2244. "</iq>");
  2245. /* <presence
  2246. * from='coven@chat.shakespeare.lit/thirdwitch'
  2247. * to='crone1@shakespeare.lit/desktop'>
  2248. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2249. * <item affiliation='member'
  2250. * jid='hag66@shakespeare.lit/pda'
  2251. * role='moderator'/>
  2252. * </x>
  2253. * </presence>
  2254. */
  2255. presence = $pres({
  2256. 'from': 'lounge@localhost/trustworthyguy',
  2257. 'to': 'dummy@localhost/desktop'
  2258. })
  2259. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2260. .c('item', {
  2261. 'jid': 'trustworthyguy@localhost',
  2262. 'affiliation': 'member',
  2263. 'role': 'moderator'
  2264. });
  2265. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2266. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2267. expect(info_msgs.pop().textContent).toBe("trustworthyguy is now a moderator");
  2268. view.onMessageSubmitted('/deop trustworthyguy Perhaps not');
  2269. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2270. expect(view.showChatEvent.calls.count()).toBe(1);
  2271. expect(view.modifyRole).toHaveBeenCalled();
  2272. expect(sent_IQ.toLocaleString()).toBe(
  2273. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2274. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2275. "<item nick='trustworthyguy' role='participant'>"+
  2276. "<reason>Perhaps not</reason>"+
  2277. "</item>"+
  2278. "</query>"+
  2279. "</iq>");
  2280. /* <presence
  2281. * from='coven@chat.shakespeare.lit/thirdwitch'
  2282. * to='crone1@shakespeare.lit/desktop'>
  2283. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2284. * <item affiliation='member'
  2285. * jid='hag66@shakespeare.lit/pda'
  2286. * role='participant'/>
  2287. * </x>
  2288. * </presence>
  2289. */
  2290. presence = $pres({
  2291. 'from': 'lounge@localhost/trustworthyguy',
  2292. 'to': 'dummy@localhost/desktop'
  2293. }).c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2294. .c('item', {
  2295. 'jid': 'trustworthyguy@localhost',
  2296. 'affiliation': 'member',
  2297. 'role': 'participant'
  2298. });
  2299. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2300. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2301. expect(info_msgs.pop().textContent).toBe("trustworthyguy is no longer a moderator");
  2302. done();
  2303. }).catch(_.partial(console.error, _));
  2304. }));
  2305. it("/mute and /voice to mute and unmute a user",
  2306. mock.initConverseWithPromises(
  2307. null, ['rosterGroupsFetched'], {},
  2308. function (done, _converse) {
  2309. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2310. var sent_IQ, IQ_id;
  2311. var sendIQ = _converse.connection.sendIQ;
  2312. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2313. sent_IQ = iq;
  2314. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2315. });
  2316. var view = _converse.chatboxviews.get('lounge@localhost');
  2317. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2318. spyOn(view, 'modifyRole').and.callThrough();
  2319. spyOn(view, 'showErrorMessage').and.callThrough();
  2320. spyOn(view, 'showChatEvent').and.callThrough();
  2321. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2322. // New user enters the room
  2323. /* <presence
  2324. * from='coven@chat.shakespeare.lit/thirdwitch'
  2325. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  2326. * to='crone1@shakespeare.lit/desktop'>
  2327. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2328. * <item affiliation='member' role='participant'/>
  2329. * </x>
  2330. * </presence>
  2331. */
  2332. var presence = $pres({
  2333. 'from': 'lounge@localhost/annoyingGuy',
  2334. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2335. 'to': 'dummy@localhost/desktop'
  2336. })
  2337. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2338. .c('item', {
  2339. 'jid': 'annoyingguy@localhost',
  2340. 'affiliation': 'member',
  2341. 'role': 'participant'
  2342. });
  2343. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2344. var info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2345. expect(info_msgs.pop().textContent).toBe("annoyingGuy has entered the room");
  2346. var textarea = view.el.querySelector('.chat-textarea')
  2347. textarea.value = '/mute';
  2348. view.keyPressed({
  2349. target: textarea,
  2350. preventDefault: _.noop,
  2351. keyCode: 13
  2352. });
  2353. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2354. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2355. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2356. "Error: the \"mute\" command takes two arguments, the user's nickname and optionally a reason.",
  2357. true
  2358. );
  2359. expect(view.modifyRole).not.toHaveBeenCalled();
  2360. // Call now with the correct amount of arguments.
  2361. // XXX: Calling onMessageSubmitted directly, trying
  2362. // again via triggering Event doesn't work for some weird
  2363. // reason.
  2364. view.onMessageSubmitted('/mute annoyingGuy You\'re annoying');
  2365. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2366. expect(view.showErrorMessage.calls.count()).toBe(1);
  2367. expect(view.modifyRole).toHaveBeenCalled();
  2368. expect(sent_IQ.toLocaleString()).toBe(
  2369. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2370. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2371. "<item nick='annoyingGuy' role='visitor'>"+
  2372. "<reason>You&apos;re annoying</reason>"+
  2373. "</item>"+
  2374. "</query>"+
  2375. "</iq>");
  2376. /* <presence
  2377. * from='coven@chat.shakespeare.lit/thirdwitch'
  2378. * to='crone1@shakespeare.lit/desktop'>
  2379. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2380. * <item affiliation='member'
  2381. * jid='hag66@shakespeare.lit/pda'
  2382. * role='visitor'/>
  2383. * </x>
  2384. * </presence>
  2385. */
  2386. presence = $pres({
  2387. 'from': 'lounge@localhost/annoyingGuy',
  2388. 'to': 'dummy@localhost/desktop'
  2389. })
  2390. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2391. .c('item', {
  2392. 'jid': 'annoyingguy@localhost',
  2393. 'affiliation': 'member',
  2394. 'role': 'visitor'
  2395. });
  2396. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2397. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2398. expect(info_msgs.pop().textContent).toBe("annoyingGuy has been muted");
  2399. view.onMessageSubmitted('/voice annoyingGuy Now you can talk again');
  2400. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2401. expect(view.showChatEvent.calls.count()).toBe(1);
  2402. expect(view.modifyRole).toHaveBeenCalled();
  2403. expect(sent_IQ.toLocaleString()).toBe(
  2404. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2405. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2406. "<item nick='annoyingGuy' role='participant'>"+
  2407. "<reason>Now you can talk again</reason>"+
  2408. "</item>"+
  2409. "</query>"+
  2410. "</iq>");
  2411. /* <presence
  2412. * from='coven@chat.shakespeare.lit/thirdwitch'
  2413. * to='crone1@shakespeare.lit/desktop'>
  2414. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2415. * <item affiliation='member'
  2416. * jid='hag66@shakespeare.lit/pda'
  2417. * role='visitor'/>
  2418. * </x>
  2419. * </presence>
  2420. */
  2421. presence = $pres({
  2422. 'from': 'lounge@localhost/annoyingGuy',
  2423. 'to': 'dummy@localhost/desktop'
  2424. })
  2425. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2426. .c('item', {
  2427. 'jid': 'annoyingguy@localhost',
  2428. 'affiliation': 'member',
  2429. 'role': 'participant'
  2430. });
  2431. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2432. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2433. expect(info_msgs.pop().textContent).toBe("annoyingGuy has been given a voice again");
  2434. done();
  2435. }).catch(_.partial(console.error, _));
  2436. }));
  2437. });
  2438. describe("When attempting to enter a chatroom", function () {
  2439. it("will show an error message if the room requires a password",
  2440. mock.initConverseWithPromises(
  2441. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  2442. function (done, _converse) {
  2443. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2444. .then(function () {
  2445. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2446. spyOn(view, 'renderPasswordForm').and.callThrough();
  2447. var presence = $pres().attrs({
  2448. from:'problematic@muc.localhost/dummy',
  2449. id:'n13mt3l',
  2450. to:'dummy@localhost/pda',
  2451. type:'error'})
  2452. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2453. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  2454. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'});
  2455. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2456. var $chat_body = $(view.el).find('.chatroom-body');
  2457. expect(view.renderPasswordForm).toHaveBeenCalled();
  2458. expect($chat_body.find('form.chatroom-form').length).toBe(1);
  2459. expect($chat_body.find('legend').text()).toBe('This chatroom requires a password');
  2460. // Let's submit the form
  2461. spyOn(view, 'join');
  2462. var input_el = view.el.querySelector('[name="password"]');
  2463. input_el.value = 'secret';
  2464. view.el.querySelector('input[type=submit]').click();
  2465. expect(view.join).toHaveBeenCalledWith('dummy', 'secret');
  2466. done();
  2467. }).catch(_.partial(console.error, _));
  2468. }));
  2469. it("will show an error message if the room is members-only and the user not included",
  2470. mock.initConverseWithPromises(
  2471. null, ['rosterGroupsFetched'], {},
  2472. function (done, _converse) {
  2473. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2474. .then(function () {
  2475. var presence = $pres().attrs({
  2476. from:'problematic@muc.localhost/dummy',
  2477. id:'n13mt3l',
  2478. to:'dummy@localhost/pda',
  2479. type:'error'})
  2480. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2481. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  2482. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2483. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2484. spyOn(view, 'showErrorMessage').and.callThrough();
  2485. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2486. expect($(view.el).find('.chatroom-body p:last').text()).toBe('You are not on the member list of this room.');
  2487. done();
  2488. }).catch(_.partial(console.error, _));
  2489. }));
  2490. it("will show an error message if the user has been banned",
  2491. mock.initConverseWithPromises(
  2492. null, ['rosterGroupsFetched'], {},
  2493. function (done, _converse) {
  2494. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2495. .then(function () {
  2496. var presence = $pres().attrs({
  2497. from:'problematic@muc.localhost/dummy',
  2498. id:'n13mt3l',
  2499. to:'dummy@localhost/pda',
  2500. type:'error'})
  2501. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2502. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  2503. .c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2504. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2505. spyOn(view, 'showErrorMessage').and.callThrough();
  2506. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2507. expect($(view.el).find('.chatroom-body p:last').text()).toBe('You have been banned from this room.');
  2508. done();
  2509. }).catch(_.partial(console.error, _));
  2510. }));
  2511. it("will render a nickname form if a nickname conflict happens and muc_nickname_from_jid=false",
  2512. mock.initConverseWithPromises(
  2513. null, ['rosterGroupsFetched'], {},
  2514. function (done, _converse) {
  2515. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2516. .then(function () {
  2517. var presence = $pres().attrs({
  2518. from:'problematic@muc.localhost/dummy',
  2519. id:'n13mt3l',
  2520. to:'dummy@localhost/pda',
  2521. type:'error'})
  2522. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2523. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2524. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2525. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2526. spyOn(view, 'showErrorMessage').and.callThrough();
  2527. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2528. expect($(view.el).find('.chatroom-body form.chatroom-form label:first').text()).toBe('Please choose your nickname');
  2529. var $input = $(view.el).find('.chatroom-body form.chatroom-form input:first');
  2530. $input.val('nicky');
  2531. view.el.querySelector('input[type=submit]').click();
  2532. done();
  2533. }).catch(_.partial(console.error, _));
  2534. }));
  2535. it("will automatically choose a new nickname if a nickname conflict happens and muc_nickname_from_jid=true",
  2536. mock.initConverseWithPromises(
  2537. null, ['rosterGroupsFetched'], {},
  2538. function (done, _converse) {
  2539. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2540. .then(function () {
  2541. /* <presence
  2542. * from='coven@chat.shakespeare.lit/thirdwitch'
  2543. * id='n13mt3l'
  2544. * to='hag66@shakespeare.lit/pda'
  2545. * type='error'>
  2546. * <x xmlns='http://jabber.org/protocol/muc'/>
  2547. * <error by='coven@chat.shakespeare.lit' type='cancel'>
  2548. * <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  2549. * </error>
  2550. * </presence>
  2551. */
  2552. _converse.muc_nickname_from_jid = true;
  2553. var attrs = {
  2554. from:'problematic@muc.localhost/dummy',
  2555. to:'dummy@localhost/pda',
  2556. type:'error'
  2557. };
  2558. attrs.id = new Date().getTime();
  2559. var presence = $pres().attrs(attrs)
  2560. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2561. .c('error').attrs({by:'problematic@muc.localhost', type:'cancel'})
  2562. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2563. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2564. spyOn(view, 'showErrorMessage').and.callThrough();
  2565. spyOn(view, 'join').and.callThrough();
  2566. // Simulate repeatedly that there's already someone in the room
  2567. // with that nickname
  2568. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2569. expect(view.join).toHaveBeenCalledWith('dummy-2');
  2570. attrs.from = 'problematic@muc.localhost/dummy-2';
  2571. attrs.id = new Date().getTime();
  2572. presence = $pres().attrs(attrs)
  2573. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2574. .c('error').attrs({by:'problematic@muc.localhost', type:'cancel'})
  2575. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2576. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2577. expect(view.join).toHaveBeenCalledWith('dummy-3');
  2578. attrs.from = 'problematic@muc.localhost/dummy-3';
  2579. attrs.id = new Date().getTime();
  2580. presence = $pres().attrs(attrs)
  2581. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2582. .c('error').attrs({by:'problematic@muc.localhost', type:'cancel'})
  2583. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2584. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2585. expect(view.join).toHaveBeenCalledWith('dummy-4');
  2586. done();
  2587. }).catch(_.partial(console.error, _));
  2588. }));
  2589. it("will show an error message if the user is not allowed to have created the room",
  2590. mock.initConverseWithPromises(
  2591. null, ['rosterGroupsFetched'], {},
  2592. function (done, _converse) {
  2593. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2594. .then(function () {
  2595. var presence = $pres().attrs({
  2596. from:'problematic@muc.localhost/dummy',
  2597. id:'n13mt3l',
  2598. to:'dummy@localhost/pda',
  2599. type:'error'})
  2600. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2601. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2602. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2603. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2604. spyOn(view, 'showErrorMessage').and.callThrough();
  2605. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2606. expect($(view.el).find('.chatroom-body p:last').text()).toBe('You are not allowed to create new rooms.');
  2607. done();
  2608. }).catch(_.partial(console.error, _));
  2609. }));
  2610. it("will show an error message if the user's nickname doesn't conform to room policy",
  2611. mock.initConverseWithPromises(
  2612. null, ['rosterGroupsFetched'], {},
  2613. function (done, _converse) {
  2614. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2615. .then(function () {
  2616. var presence = $pres().attrs({
  2617. from:'problematic@muc.localhost/dummy',
  2618. id:'n13mt3l',
  2619. to:'dummy@localhost/pda',
  2620. type:'error'})
  2621. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2622. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2623. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2624. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2625. spyOn(view, 'showErrorMessage').and.callThrough();
  2626. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2627. expect($(view.el).find('.chatroom-body p:last').text()).toBe("Your nickname doesn't conform to this room's policies.");
  2628. done();
  2629. }).catch(_.partial(console.error, _));
  2630. }));
  2631. it("will show an error message if the room doesn't yet exist",
  2632. mock.initConverseWithPromises(
  2633. null, ['rosterGroupsFetched'], {},
  2634. function (done, _converse) {
  2635. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2636. .then(function () {
  2637. var presence = $pres().attrs({
  2638. from:'problematic@muc.localhost/dummy',
  2639. id:'n13mt3l',
  2640. to:'dummy@localhost/pda',
  2641. type:'error'})
  2642. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2643. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2644. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2645. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2646. spyOn(view, 'showErrorMessage').and.callThrough();
  2647. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2648. expect($(view.el).find('.chatroom-body p:last').text()).toBe("This room does not (yet) exist.");
  2649. done();
  2650. }).catch(_.partial(console.error, _));
  2651. }));
  2652. it("will show an error message if the room has reached its maximum number of occupants",
  2653. mock.initConverseWithPromises(
  2654. null, ['rosterGroupsFetched'], {},
  2655. function (done, _converse) {
  2656. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2657. .then(function () {
  2658. var presence = $pres().attrs({
  2659. from:'problematic@muc.localhost/dummy',
  2660. id:'n13mt3l',
  2661. to:'dummy@localhost/pda',
  2662. type:'error'})
  2663. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2664. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2665. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2666. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2667. spyOn(view, 'showErrorMessage').and.callThrough();
  2668. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2669. expect($(view.el).find('.chatroom-body p:last').text()).toBe("This room has reached its maximum number of occupants.");
  2670. done();
  2671. }).catch(_.partial(console.error, _));
  2672. }));
  2673. });
  2674. describe("Someone being invited to a chat room", function () {
  2675. it("will first be added to the member list if the chat room is members only",
  2676. mock.initConverseWithPromises(
  2677. null, ['rosterGroupsFetched'], {},
  2678. function (done, _converse) {
  2679. var sent_IQs = [], IQ_ids = [];
  2680. var sendIQ = _converse.connection.sendIQ;
  2681. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2682. sent_IQs.push(iq);
  2683. IQ_ids.push(sendIQ.bind(this)(iq, callback, errback));
  2684. });
  2685. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'dummy'});
  2686. // State that the chat is members-only via the features IQ
  2687. var features_stanza = $iq({
  2688. from: 'coven@chat.shakespeare.lit',
  2689. 'id': IQ_ids.pop(),
  2690. 'to': 'dummy@localhost/desktop',
  2691. 'type': 'result'
  2692. })
  2693. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  2694. .c('identity', {
  2695. 'category': 'conference',
  2696. 'name': 'A Dark Cave',
  2697. 'type': 'text'
  2698. }).up()
  2699. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  2700. .c('feature', {'var': 'muc_hidden'}).up()
  2701. .c('feature', {'var': 'muc_temporary'}).up()
  2702. .c('feature', {'var': 'muc_membersonly'}).up();
  2703. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  2704. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2705. expect(view.model.get('membersonly')).toBeTruthy();
  2706. test_utils.createContacts(_converse, 'current');
  2707. var sent_stanza, sent_id;
  2708. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  2709. if (stanza.nodeTree && stanza.nodeTree.nodeName === 'message') {
  2710. sent_id = stanza.nodeTree.getAttribute('id');
  2711. sent_stanza = stanza;
  2712. }
  2713. });
  2714. var name = mock.cur_names[0];
  2715. var invitee_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  2716. var reason = "Please join this chat room";
  2717. view.model.directInvite(invitee_jid, reason);
  2718. // Check in reverse order that we requested all three lists
  2719. // (member, owner and admin).
  2720. var admin_iq_id = IQ_ids.pop();
  2721. var owner_iq_id = IQ_ids.pop();
  2722. var member_iq_id = IQ_ids.pop();
  2723. expect(sent_IQs.pop().toLocaleString()).toBe(
  2724. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+admin_iq_id+"'>"+
  2725. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2726. "<item affiliation='admin'/>"+
  2727. "</query>"+
  2728. "</iq>");
  2729. expect(sent_IQs.pop().toLocaleString()).toBe(
  2730. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+owner_iq_id+"'>"+
  2731. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2732. "<item affiliation='owner'/>"+
  2733. "</query>"+
  2734. "</iq>");
  2735. expect(sent_IQs.pop().toLocaleString()).toBe(
  2736. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+member_iq_id+"'>"+
  2737. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2738. "<item affiliation='member'/>"+
  2739. "</query>"+
  2740. "</iq>");
  2741. /* Now the service sends the member list to the user
  2742. *
  2743. * <iq from='coven@chat.shakespeare.lit'
  2744. * id='member3'
  2745. * to='crone1@shakespeare.lit/desktop'
  2746. * type='result'>
  2747. * <query xmlns='http://jabber.org/protocol/muc#admin'>
  2748. * <item affiliation='member'
  2749. * jid='hag66@shakespeare.lit'
  2750. * nick='thirdwitch'
  2751. * role='participant'/>
  2752. * </query>
  2753. * </iq>
  2754. */
  2755. var member_list_stanza = $iq({
  2756. 'from': 'coven@chat.shakespeare.lit',
  2757. 'id': member_iq_id,
  2758. 'to': 'dummy@localhost/resource',
  2759. 'type': 'result'
  2760. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  2761. .c('item', {
  2762. 'affiliation': 'member',
  2763. 'jid': 'hag66@shakespeare.lit',
  2764. 'nick': 'thirdwitch',
  2765. 'role': 'participant'
  2766. });
  2767. _converse.connection._dataRecv(test_utils.createRequest(member_list_stanza));
  2768. var admin_list_stanza = $iq({
  2769. 'from': 'coven@chat.shakespeare.lit',
  2770. 'id': admin_iq_id,
  2771. 'to': 'dummy@localhost/resource',
  2772. 'type': 'result'
  2773. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  2774. .c('item', {
  2775. 'affiliation': 'admin',
  2776. 'jid': 'wiccarocks@shakespeare.lit',
  2777. 'nick': 'secondwitch'
  2778. });
  2779. _converse.connection._dataRecv(test_utils.createRequest(admin_list_stanza));
  2780. var owner_list_stanza = $iq({
  2781. 'from': 'coven@chat.shakespeare.lit',
  2782. 'id': owner_iq_id,
  2783. 'to': 'dummy@localhost/resource',
  2784. 'type': 'result'
  2785. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  2786. .c('item', {
  2787. 'affiliation': 'owner',
  2788. 'jid': 'crone1@shakespeare.lit',
  2789. });
  2790. _converse.connection._dataRecv(test_utils.createRequest(owner_list_stanza));
  2791. test_utils.waitUntil(function () {
  2792. return IQ_ids.length;
  2793. }, 300).then(function () {
  2794. // Check that the member list now gets updated
  2795. var iq = "<iq to='coven@chat.shakespeare.lit' type='set' xmlns='jabber:client' id='"+IQ_ids.pop()+"'>"+
  2796. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2797. "<item affiliation='member' jid='"+invitee_jid+"'>"+
  2798. "<reason>Please join this chat room</reason>"+
  2799. "</item>"+
  2800. "</query>"+
  2801. "</iq>";
  2802. test_utils.waitUntil(function () {
  2803. return _.includes(_.invokeMap(sent_IQs, Object.prototype.toLocaleString), iq);
  2804. }, 300).then(function () {
  2805. // Finally check that the user gets invited.
  2806. expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
  2807. "<message from='dummy@localhost/resource' to='"+invitee_jid+"' id='"+sent_id+"' xmlns='jabber:client'>"+
  2808. "<x xmlns='jabber:x:conference' jid='coven@chat.shakespeare.lit' reason='Please join this chat room'/>"+
  2809. "</message>"
  2810. );
  2811. done();
  2812. });
  2813. });
  2814. }));
  2815. });
  2816. describe("The affiliations delta", function () {
  2817. it("can be computed in various ways",
  2818. mock.initConverseWithPromises(
  2819. null, ['rosterGroupsFetched'], {},
  2820. function (done, _converse) {
  2821. test_utils.openChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy');
  2822. var roomview = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2823. var exclude_existing = false;
  2824. var remove_absentees = false;
  2825. var new_list = [];
  2826. var old_list = [];
  2827. var delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2828. expect(delta.length).toBe(0);
  2829. new_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2830. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2831. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2832. expect(delta.length).toBe(0);
  2833. // When remove_absentees is false, then affiliations in the old
  2834. // list which are not in the new one won't be removed.
  2835. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  2836. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2837. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2838. expect(delta.length).toBe(0);
  2839. // With exclude_existing set to false, any changed affiliations
  2840. // will be included in the delta (i.e. existing affiliations
  2841. // are included in the comparison).
  2842. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  2843. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2844. expect(delta.length).toBe(1);
  2845. expect(delta[0].jid).toBe('wiccarocks@shakespeare.lit');
  2846. expect(delta[0].affiliation).toBe('member');
  2847. // To also remove affiliations from the old list which are not
  2848. // in the new list, we set remove_absentees to true
  2849. remove_absentees = true;
  2850. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  2851. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2852. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2853. expect(delta.length).toBe(1);
  2854. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  2855. expect(delta[0].affiliation).toBe('none');
  2856. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, [], old_list);
  2857. expect(delta.length).toBe(2);
  2858. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  2859. expect(delta[0].affiliation).toBe('none');
  2860. expect(delta[1].jid).toBe('wiccarocks@shakespeare.lit');
  2861. expect(delta[1].affiliation).toBe('none');
  2862. // To only add a user if they don't already have an
  2863. // affiliation, we set 'exclude_existing' to true
  2864. exclude_existing = true;
  2865. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  2866. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2867. expect(delta.length).toBe(0);
  2868. done();
  2869. }));
  2870. });
  2871. describe("The \"Chatrooms\" section", function () {
  2872. it("contains a link to a modal through which a new chatroom can be created",
  2873. mock.initConverseWithPromises(
  2874. null, ['rosterGroupsFetched'], {},
  2875. function (done, _converse) {
  2876. test_utils.openControlBox();
  2877. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  2878. roomspanel.el.querySelector('.show-add-muc-modal').click();
  2879. test_utils.closeControlBox(_converse);
  2880. const modal = roomspanel.add_room_modal;
  2881. test_utils.waitUntil(function () {
  2882. return u.isVisible(modal.el);
  2883. }, 1000).then(function () {
  2884. spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(function () {
  2885. var deferred = new $.Deferred();
  2886. deferred.resolve();
  2887. return deferred.promise();
  2888. });
  2889. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2890. modal.el.querySelector('input[name="chatroom"]').value = 'lounce@muc.localhost';
  2891. modal.el.querySelector('form input[type="submit"]').click();
  2892. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  2893. done();
  2894. }).catch(_.partial(console.error, _));
  2895. }));
  2896. it("contains a link to a modal which can list rooms publically available on the server",
  2897. mock.initConverseWithPromises(
  2898. null, ['rosterGroupsFetched'], {},
  2899. function (done, _converse) {
  2900. var sendIQ = _converse.connection.sendIQ;
  2901. var sent_stanza, IQ_id;
  2902. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2903. sent_stanza = iq;
  2904. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2905. });
  2906. test_utils.openControlBox();
  2907. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  2908. roomspanel.el.querySelector('.show-list-muc-modal').click();
  2909. test_utils.closeControlBox(_converse);
  2910. const modal = roomspanel.list_rooms_modal;
  2911. test_utils.waitUntil(function () {
  2912. return u.isVisible(modal.el);
  2913. }, 1000).then(function () {
  2914. spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(function () {
  2915. var deferred = new $.Deferred();
  2916. deferred.resolve();
  2917. return deferred.promise();
  2918. });
  2919. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2920. // See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
  2921. expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(0);
  2922. const input = modal.el.querySelector('input[name="server"]').value = 'chat.shakespear.lit';
  2923. modal.el.querySelector('input[type="submit"]').click();
  2924. expect(sent_stanza.toLocaleString()).toBe(
  2925. "<iq to='chat.shakespear.lit' from='dummy@localhost/resource' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2926. "<query xmlns='http://jabber.org/protocol/disco#items'/>"+
  2927. "</iq>"
  2928. );
  2929. var iq = $iq({
  2930. from:'muc.localhost',
  2931. to:'dummy@localhost/pda',
  2932. id: IQ_id,
  2933. type:'result'
  2934. }).c('query')
  2935. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  2936. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  2937. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  2938. .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
  2939. _converse.connection._dataRecv(test_utils.createRequest(iq));
  2940. expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(5);
  2941. const rooms = modal.el.querySelectorAll('.available-chatrooms li');
  2942. expect(rooms[0].textContent.trim()).toBe("Rooms found:");
  2943. expect(rooms[1].textContent.trim()).toBe("A Lonely Heath");
  2944. expect(rooms[2].textContent.trim()).toBe("A Dark Cave");
  2945. expect(rooms[3].textContent.trim()).toBe("The Palace");
  2946. expect(rooms[4].textContent.trim()).toBe("Macbeth's Castle");
  2947. rooms[4].querySelector('.open-room').click();
  2948. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  2949. var view = _converse.chatboxviews.get('inverness@chat.shakespeare.lit');
  2950. expect(view.el.querySelector('.chat-head-chatroom').textContent.trim()).toBe("Macbeth's Castle");
  2951. done();
  2952. }).catch(_.partial(console.error, _));
  2953. }));
  2954. it("shows the number of unread mentions received",
  2955. mock.initConverseWithPromises(
  2956. null, ['rosterGroupsFetched'], {'allow_bookmarks': false},
  2957. function (done, _converse) {
  2958. // XXX: we set `allow_bookmarks` to false, so that the rooms
  2959. // list gets rendered. Otherwise we would have to mock
  2960. // the bookmark stanza exchange.
  2961. test_utils.openControlBox();
  2962. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  2963. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(0);
  2964. var room_jid = 'kitchen@conference.shakespeare.lit';
  2965. test_utils.openAndEnterChatRoom(
  2966. _converse, 'kitchen', 'conference.shakespeare.lit', 'fires').then(function () {
  2967. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  2968. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
  2969. var view = _converse.chatboxviews.get(room_jid);
  2970. view.model.set({'minimized': true});
  2971. var contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
  2972. var message = 'fires: Your attention is required';
  2973. var nick = mock.chatroom_names[0];
  2974. view.model.onMessage($msg({
  2975. from: room_jid+'/'+nick,
  2976. id: (new Date()).getTime(),
  2977. to: 'dummy@localhost',
  2978. type: 'groupchat'
  2979. }).c('body').t(message).tree());
  2980. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  2981. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
  2982. expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('1');
  2983. view.model.onMessage($msg({
  2984. 'from': room_jid+'/'+nick,
  2985. 'id': (new Date()).getTime(),
  2986. 'to': 'dummy@localhost',
  2987. 'type': 'groupchat'
  2988. }).c('body').t(message).tree());
  2989. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  2990. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
  2991. expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('2');
  2992. view.model.set({'minimized': false});
  2993. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  2994. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
  2995. done();
  2996. });
  2997. }));
  2998. describe("A Chat Status Notification", function () {
  2999. describe("A composing notification", function () {
  3000. it("will be shown if received",
  3001. mock.initConverseWithPromises(
  3002. null, ['rosterGroupsFetched'], {},
  3003. function (done, _converse) {
  3004. test_utils.openAndEnterChatRoom(
  3005. _converse, 'coven', 'chat.shakespeare.lit', 'some1').then(function () {
  3006. var room_jid = 'coven@chat.shakespeare.lit';
  3007. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3008. var $chat_content = $(view.el).find('.chat-content');
  3009. /* <presence to="dummy@localhost/_converse.js-29092160"
  3010. * from="coven@chat.shakespeare.lit/some1">
  3011. * <x xmlns="http://jabber.org/protocol/muc#user">
  3012. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  3013. * <status code="110"/>
  3014. * </x>
  3015. * </presence></body>
  3016. */
  3017. var presence = $pres({
  3018. to: 'dummy@localhost/_converse.js-29092160',
  3019. from: 'coven@chat.shakespeare.lit/some1'
  3020. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  3021. .c('item', {
  3022. 'affiliation': 'owner',
  3023. 'jid': 'dummy@localhost/_converse.js-29092160',
  3024. 'role': 'moderator'
  3025. }).up()
  3026. .c('status', {code: '110'});
  3027. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3028. expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(2);
  3029. expect($chat_content.find('div.chat-info:first').html()).toBe("some1 has entered the room");
  3030. expect($chat_content.find('div.chat-info:last').html()).toBe("some1 is now a moderator");
  3031. presence = $pres({
  3032. to: 'dummy@localhost/_converse.js-29092160',
  3033. from: 'coven@chat.shakespeare.lit/newguy'
  3034. })
  3035. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3036. .c('item', {
  3037. 'affiliation': 'none',
  3038. 'jid': 'newguy@localhost/_converse.js-290929789',
  3039. 'role': 'participant'
  3040. });
  3041. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3042. expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(3);
  3043. expect($chat_content.find('div.chat-info:last').html()).toBe("newguy has entered the room");
  3044. presence = $pres({
  3045. to: 'dummy@localhost/_converse.js-29092160',
  3046. from: 'coven@chat.shakespeare.lit/nomorenicks'
  3047. })
  3048. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3049. .c('item', {
  3050. 'affiliation': 'none',
  3051. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  3052. 'role': 'participant'
  3053. });
  3054. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3055. expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(4);
  3056. expect($chat_content.find('div.chat-info:last').html()).toBe("nomorenicks has entered the room");
  3057. // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
  3058. // <composing> state
  3059. var msg = $msg({
  3060. from: room_jid+'/newguy',
  3061. id: (new Date()).getTime(),
  3062. to: 'dummy@localhost',
  3063. type: 'groupchat'
  3064. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3065. view.model.onMessage(msg);
  3066. // Check that the notification appears inside the chatbox in the DOM
  3067. var events = view.el.querySelectorAll('.chat-event');
  3068. expect(events.length).toBe(4);
  3069. expect(events[0].textContent).toEqual('some1 has entered the room');
  3070. expect(events[1].textContent).toEqual('some1 is now a moderator');
  3071. expect(events[2].textContent).toEqual('newguy has entered the room');
  3072. expect(events[3].textContent).toEqual('nomorenicks has entered the room');
  3073. var notifications = view.el.querySelectorAll('.chat-state-notification');
  3074. expect(notifications.length).toBe(1);
  3075. expect(notifications[0].textContent).toEqual('newguy is typing');
  3076. const timeout_functions = [];
  3077. spyOn(window, 'setTimeout').and.callFake(function (func, delay) {
  3078. timeout_functions.push(func);
  3079. });
  3080. // Check that it doesn't appear twice
  3081. msg = $msg({
  3082. from: room_jid+'/newguy',
  3083. id: (new Date()).getTime(),
  3084. to: 'dummy@localhost',
  3085. type: 'groupchat'
  3086. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3087. view.model.onMessage(msg);
  3088. events = view.el.querySelectorAll('.chat-event');
  3089. expect(events.length).toBe(4);
  3090. expect(events[0].textContent).toEqual('some1 has entered the room');
  3091. expect(events[1].textContent).toEqual('some1 is now a moderator');
  3092. expect(events[2].textContent).toEqual('newguy has entered the room');
  3093. expect(events[3].textContent).toEqual('nomorenicks has entered the room');
  3094. notifications = view.el.querySelectorAll('.chat-state-notification');
  3095. expect(notifications.length).toBe(1);
  3096. expect(notifications[0].textContent).toEqual('newguy is typing');
  3097. expect(timeout_functions.length).toBe(1);
  3098. // <composing> state for a different occupant
  3099. msg = $msg({
  3100. from: room_jid+'/nomorenicks',
  3101. id: (new Date()).getTime(),
  3102. to: 'dummy@localhost',
  3103. type: 'groupchat'
  3104. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3105. view.model.onMessage(msg);
  3106. events = view.el.querySelectorAll('.chat-event');
  3107. expect(events.length).toBe(4);
  3108. expect(events[0].textContent).toEqual('some1 has entered the room');
  3109. expect(events[1].textContent).toEqual('some1 is now a moderator');
  3110. expect(events[2].textContent).toEqual('newguy has entered the room');
  3111. expect(events[3].textContent).toEqual('nomorenicks has entered the room');
  3112. notifications = view.el.querySelectorAll('.chat-state-notification');
  3113. expect(notifications.length).toBe(2);
  3114. expect(notifications[0].textContent).toEqual('newguy is typing');
  3115. expect(notifications[1].textContent).toEqual('nomorenicks is typing');
  3116. expect(timeout_functions.length).toBe(2);
  3117. // Check that new messages appear under the chat state
  3118. // notifications
  3119. msg = $msg({
  3120. from: 'lounge@localhost/some1',
  3121. id: (new Date()).getTime(),
  3122. to: 'dummy@localhost',
  3123. type: 'groupchat'
  3124. }).c('body').t('hello world').tree();
  3125. view.model.onMessage(msg);
  3126. var messages = view.el.querySelectorAll('.message');
  3127. expect(messages.length).toBe(8);
  3128. expect(view.el.querySelectorAll('.chat-msg').length).toBe(1);
  3129. expect(view.el.querySelector('.chat-msg .chat-msg-text').textContent).toBe('hello world');
  3130. // Test that the composing notifications get removed
  3131. // via timeout.
  3132. timeout_functions[0]();
  3133. events = view.el.querySelectorAll('.chat-event');
  3134. expect(events.length).toBe(4);
  3135. expect(events[0].textContent).toEqual('some1 has entered the room');
  3136. expect(events[1].textContent).toEqual('some1 is now a moderator');
  3137. expect(events[2].textContent).toEqual('newguy has entered the room');
  3138. expect(events[3].textContent).toEqual('nomorenicks has entered the room');
  3139. notifications = view.el.querySelectorAll('.chat-state-notification');
  3140. expect(notifications.length).toBe(1);
  3141. expect(notifications[0].textContent).toEqual('nomorenicks is typing');
  3142. timeout_functions[1]();
  3143. events = view.el.querySelectorAll('.chat-event');
  3144. expect(events.length).toBe(4);
  3145. expect(events[0].textContent).toEqual('some1 has entered the room');
  3146. expect(events[1].textContent).toEqual('some1 is now a moderator');
  3147. expect(events[2].textContent).toEqual('newguy has entered the room');
  3148. expect(events[3].textContent).toEqual('nomorenicks has entered the room');
  3149. notifications = view.el.querySelectorAll('.chat-state-notification');
  3150. expect(notifications.length).toBe(0);
  3151. done();
  3152. });
  3153. }));
  3154. });
  3155. describe("A paused notification", function () {
  3156. it("will be shown if received",
  3157. mock.initConverseWithPromises(
  3158. null, ['rosterGroupsFetched'], {},
  3159. function (done, _converse) {
  3160. test_utils.openChatRoom(_converse, "coven", 'chat.shakespeare.lit', 'some1');
  3161. var room_jid = 'coven@chat.shakespeare.lit';
  3162. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3163. var $chat_content = $(view.el).find('.chat-content');
  3164. /* <presence to="dummy@localhost/_converse.js-29092160"
  3165. * from="coven@chat.shakespeare.lit/some1">
  3166. * <x xmlns="http://jabber.org/protocol/muc#user">
  3167. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  3168. * <status code="110"/>
  3169. * </x>
  3170. * </presence></body>
  3171. */
  3172. var presence = $pres({
  3173. to: 'dummy@localhost/_converse.js-29092160',
  3174. from: 'coven@chat.shakespeare.lit/some1'
  3175. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  3176. .c('item', {
  3177. 'affiliation': 'owner',
  3178. 'jid': 'dummy@localhost/_converse.js-29092160',
  3179. 'role': 'moderator'
  3180. }).up()
  3181. .c('status', {code: '110'});
  3182. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3183. expect($chat_content.find('div.chat-info:first').html()).toBe("some1 has entered the room");
  3184. presence = $pres({
  3185. to: 'dummy@localhost/_converse.js-29092160',
  3186. from: 'coven@chat.shakespeare.lit/newguy'
  3187. })
  3188. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3189. .c('item', {
  3190. 'affiliation': 'none',
  3191. 'jid': 'newguy@localhost/_converse.js-290929789',
  3192. 'role': 'participant'
  3193. });
  3194. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3195. expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(2);
  3196. expect($chat_content.find('div.chat-info:last').html()).toBe("newguy has entered the room");
  3197. presence = $pres({
  3198. to: 'dummy@localhost/_converse.js-29092160',
  3199. from: 'coven@chat.shakespeare.lit/nomorenicks'
  3200. })
  3201. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3202. .c('item', {
  3203. 'affiliation': 'none',
  3204. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  3205. 'role': 'participant'
  3206. });
  3207. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3208. expect($chat_content[0].querySelectorAll('div.chat-info').length).toBe(3);
  3209. expect($chat_content.find('div.chat-info:last').html()).toBe("nomorenicks has entered the room");
  3210. // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
  3211. // <composing> state
  3212. var msg = $msg({
  3213. from: room_jid+'/newguy',
  3214. id: (new Date()).getTime(),
  3215. to: 'dummy@localhost',
  3216. type: 'groupchat'
  3217. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3218. view.model.onMessage(msg);
  3219. // Check that the notification appears inside the chatbox in the DOM
  3220. var events = view.el.querySelectorAll('.chat-event');
  3221. expect(events.length).toBe(3);
  3222. expect(events[0].textContent).toEqual('some1 has entered the room');
  3223. expect(events[1].textContent).toEqual('newguy has entered the room');
  3224. expect(events[2].textContent).toEqual('nomorenicks has entered the room');
  3225. var notifications = view.el.querySelectorAll('.chat-state-notification');
  3226. expect(notifications.length).toBe(1);
  3227. expect(notifications[0].textContent).toEqual('newguy is typing');
  3228. // Check that it doesn't appear twice
  3229. msg = $msg({
  3230. from: room_jid+'/newguy',
  3231. id: (new Date()).getTime(),
  3232. to: 'dummy@localhost',
  3233. type: 'groupchat'
  3234. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3235. view.model.onMessage(msg);
  3236. events = view.el.querySelectorAll('.chat-event');
  3237. expect(events.length).toBe(3);
  3238. expect(events[0].textContent).toEqual('some1 has entered the room');
  3239. expect(events[1].textContent).toEqual('newguy has entered the room');
  3240. expect(events[2].textContent).toEqual('nomorenicks has entered the room');
  3241. notifications = view.el.querySelectorAll('.chat-state-notification');
  3242. expect(notifications.length).toBe(1);
  3243. expect(notifications[0].textContent).toEqual('newguy is typing');
  3244. // <composing> state for a different occupant
  3245. msg = $msg({
  3246. from: room_jid+'/nomorenicks',
  3247. id: (new Date()).getTime(),
  3248. to: 'dummy@localhost',
  3249. type: 'groupchat'
  3250. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3251. view.model.onMessage(msg);
  3252. events = view.el.querySelectorAll('.chat-event');
  3253. expect(events.length).toBe(3);
  3254. expect(events[0].textContent).toEqual('some1 has entered the room');
  3255. expect(events[1].textContent).toEqual('newguy has entered the room');
  3256. expect(events[2].textContent).toEqual('nomorenicks has entered the room');
  3257. notifications = view.el.querySelectorAll('.chat-state-notification');
  3258. expect(notifications.length).toBe(2);
  3259. expect(notifications[0].textContent).toEqual('newguy is typing');
  3260. expect(notifications[1].textContent).toEqual('nomorenicks is typing');
  3261. // <paused> state from occupant who typed first
  3262. msg = $msg({
  3263. from: room_jid+'/newguy',
  3264. id: (new Date()).getTime(),
  3265. to: 'dummy@localhost',
  3266. type: 'groupchat'
  3267. }).c('body').c('paused', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3268. view.model.onMessage(msg);
  3269. events = view.el.querySelectorAll('.chat-event');
  3270. expect(events.length).toBe(3);
  3271. expect(events[0].textContent).toEqual('some1 has entered the room');
  3272. expect(events[1].textContent).toEqual('newguy has entered the room');
  3273. expect(events[2].textContent).toEqual('nomorenicks has entered the room');
  3274. notifications = view.el.querySelectorAll('.chat-state-notification');
  3275. expect(notifications.length).toBe(2);
  3276. expect(notifications[0].textContent).toEqual('nomorenicks is typing');
  3277. expect(notifications[1].textContent).toEqual('newguy has stopped typing');
  3278. done();
  3279. }));
  3280. });
  3281. });
  3282. });
  3283. });
  3284. }));