chatroom.js 175 KB

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