chatroom.js 175 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242
  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. expect(_converse.chatboxes.models.length).toBe(1);
  1300. expect(_converse.chatboxes.models[0].id).toBe("controlbox");
  1301. var stanza = Strophe.xmlHtmlNode(
  1302. '<message xmlns="jabber:client" to="'+_converse.bare_jid+'" from="'+from_jid+'" id="9bceb415-f34b-4fa4-80d5-c0d076a24231">'+
  1303. '<x xmlns="jabber:x:conference" jid="'+room_jid+'" reason="'+reason+'"/>'+
  1304. '</message>').firstChild;
  1305. _converse.onDirectMUCInvitation(stanza);
  1306. expect(window.confirm).toHaveBeenCalledWith(
  1307. name + ' has invited you to join a chat room: '+ room_jid +
  1308. ', and left the following reason: "'+reason+'"');
  1309. expect(_converse.chatboxes.models.length).toBe(2);
  1310. expect(_converse.chatboxes.models[0].id).toBe('controlbox');
  1311. expect(_converse.chatboxes.models[1].id).toBe(room_jid);
  1312. done();
  1313. });
  1314. }));
  1315. it("shows received groupchat messages",
  1316. mock.initConverseWithPromises(
  1317. null, ['rosterGroupsFetched'], {},
  1318. function (done, _converse) {
  1319. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1320. spyOn(_converse, 'emit');
  1321. var view = _converse.chatboxviews.get('lounge@localhost');
  1322. if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
  1323. var nick = mock.chatroom_names[0];
  1324. var text = 'This is a received message';
  1325. var message = $msg({
  1326. from: 'lounge@localhost/'+nick,
  1327. id: '1',
  1328. to: 'dummy@localhost',
  1329. type: 'groupchat'
  1330. }).c('body').t(text);
  1331. view.onChatRoomMessage(message.nodeTree);
  1332. var $chat_content = $(view.el).find('.chat-content');
  1333. expect($chat_content.find('.chat-message').length).toBe(1);
  1334. expect($chat_content.find('.chat-msg-content').text()).toBe(text);
  1335. expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
  1336. done();
  1337. }));
  1338. it("shows sent groupchat messages",
  1339. mock.initConverseWithPromises(
  1340. null, ['rosterGroupsFetched'], {},
  1341. function (done, _converse) {
  1342. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1343. spyOn(_converse, 'emit');
  1344. var view = _converse.chatboxviews.get('lounge@localhost');
  1345. if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
  1346. var text = 'This is a sent message';
  1347. var textarea = view.el.querySelector('.chat-textarea');
  1348. textarea.value = text;
  1349. view.keyPressed({
  1350. target: textarea,
  1351. preventDefault: _.noop,
  1352. keyCode: 13
  1353. });
  1354. expect(_converse.emit).toHaveBeenCalledWith('messageSend', text);
  1355. var $chat_content = $(view.el).find('.chat-content');
  1356. expect($chat_content.find('.chat-message').length).toBe(1);
  1357. // Let's check that if we receive the same message again, it's
  1358. // not shown.
  1359. var message = $msg({
  1360. from: 'lounge@localhost/dummy',
  1361. to: 'dummy@localhost.com',
  1362. type: 'groupchat',
  1363. id: view.model.messages.at(0).get('msgid')
  1364. }).c('body').t(text);
  1365. view.onChatRoomMessage(message.nodeTree);
  1366. expect($chat_content.find('.chat-message').length).toBe(1);
  1367. expect($chat_content.find('.chat-msg-content').last().text()).toBe(text);
  1368. // We don't emit an event if it's our own message
  1369. expect(_converse.emit.calls.count(), 1);
  1370. done();
  1371. });
  1372. }));
  1373. it("will cause the chat area to be scrolled down only if it was at the bottom already",
  1374. mock.initConverseWithPromises(
  1375. null, ['rosterGroupsFetched'], {},
  1376. function (done, _converse) {
  1377. var message = 'This message is received while the chat area is scrolled up';
  1378. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1379. var view = _converse.chatboxviews.get('lounge@localhost');
  1380. spyOn(view, 'scrollDown').and.callThrough();
  1381. /* Create enough messages so that there's a
  1382. * scrollbar.
  1383. */
  1384. for (var i=0; i<20; i++) {
  1385. view.handleMUCMessage(
  1386. $msg({
  1387. from: 'lounge@localhost/someone',
  1388. to: 'dummy@localhost.com',
  1389. type: 'groupchat',
  1390. id: (new Date()).getTime(),
  1391. }).c('body').t('Message: '+i).tree());
  1392. }
  1393. // Give enough time for `markScrolled` to have been called
  1394. setTimeout(function () {
  1395. view.content.scrollTop = 0;
  1396. view.handleMUCMessage(
  1397. $msg({
  1398. from: 'lounge@localhost/someone',
  1399. to: 'dummy@localhost.com',
  1400. type: 'groupchat',
  1401. id: (new Date()).getTime(),
  1402. }).c('body').t(message).tree());
  1403. // Now check that the message appears inside the chatbox in the DOM
  1404. var $chat_content = $(view.el).find('.chat-content');
  1405. var msg_txt = $chat_content.find('.chat-message:last').find('.chat-msg-content').text();
  1406. expect(msg_txt).toEqual(message);
  1407. expect(view.content.scrollTop).toBe(0);
  1408. done();
  1409. }, 500);
  1410. });
  1411. }));
  1412. it("shows received chatroom subject messages",
  1413. mock.initConverseWithPromises(
  1414. null, ['rosterGroupsFetched'], {},
  1415. function (done, _converse) {
  1416. test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc').then(function () {
  1417. var text = 'Jabber/XMPP Development | RFCs and Extensions: http://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  1418. var stanza = Strophe.xmlHtmlNode(
  1419. '<message xmlns="jabber:client" to="jc@opkode.com/_converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">'+
  1420. ' <subject>'+text+'</subject>'+
  1421. ' <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>'+
  1422. ' <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>'+
  1423. '</message>').firstChild;
  1424. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1425. var view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1426. var $chat_content = $(view.el).find('.chat-content');
  1427. expect($chat_content.find('.chat-info:last').text()).toBe('Topic set by ralphm to: '+text);
  1428. done();
  1429. });
  1430. }));
  1431. it("escapes the subject before rendering it, to avoid JS-injection attacks",
  1432. mock.initConverseWithPromises(
  1433. null, ['rosterGroupsFetched'], {},
  1434. function (done, _converse) {
  1435. test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc').then(function () {
  1436. spyOn(window, 'alert');
  1437. var subject = '<img src="x" onerror="alert(\'XSS\');"/>';
  1438. var view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1439. view.setChatRoomSubject('ralphm', subject);
  1440. var $chat_content = $(view.el).find('.chat-content');
  1441. expect($chat_content.find('.chat-info:last').text()).toBe('Topic set by ralphm to: '+subject);
  1442. done();
  1443. });
  1444. }));
  1445. it("informs users if their nicknames has been changed.",
  1446. mock.initConverseWithPromises(
  1447. null, ['rosterGroupsFetched'], {},
  1448. function (done, _converse) {
  1449. /* The service then sends two presence stanzas to the full JID
  1450. * of each occupant (including the occupant who is changing his
  1451. * or her room nickname), one of type "unavailable" for the old
  1452. * nickname and one indicating availability for the new
  1453. * nickname.
  1454. *
  1455. * See: http://xmpp.org/extensions/xep-0045.html#changenick
  1456. *
  1457. * <presence
  1458. * from='coven@localhost/thirdwitch'
  1459. * id='DC352437-C019-40EC-B590-AF29E879AF98'
  1460. * to='hag66@shakespeare.lit/pda'
  1461. * type='unavailable'>
  1462. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1463. * <item affiliation='member'
  1464. * jid='hag66@shakespeare.lit/pda'
  1465. * nick='oldhag'
  1466. * role='participant'/>
  1467. * <status code='303'/>
  1468. * <status code='110'/>
  1469. * </x>
  1470. * </presence>
  1471. *
  1472. * <presence
  1473. * from='coven@localhost/oldhag'
  1474. * id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
  1475. * to='hag66@shakespeare.lit/pda'>
  1476. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1477. * <item affiliation='member'
  1478. * jid='hag66@shakespeare.lit/pda'
  1479. * role='participant'/>
  1480. * <status code='110'/>
  1481. * </x>
  1482. * </presence>
  1483. */
  1484. var __ = _converse.__;
  1485. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'oldnick').then(function () {
  1486. var view = _converse.chatboxviews.get('lounge@localhost');
  1487. var $chat_content = $(view.el).find('.chat-content');
  1488. // The user has just entered the room and receives their own
  1489. // presence from the server.
  1490. // See example 24:
  1491. // http://xmpp.org/extensions/xep-0045.html#enter-pres
  1492. var presence = $pres({
  1493. to:'dummy@localhost/pda',
  1494. from:'lounge@localhost/oldnick',
  1495. id:'DC352437-C019-40EC-B590-AF29E879AF97'
  1496. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1497. .c('item').attrs({
  1498. affiliation: 'member',
  1499. jid: 'dummy@localhost/pda',
  1500. role: 'participant'
  1501. }).up()
  1502. .c('status').attrs({code:'110'}).up()
  1503. .c('status').attrs({code:'210'}).nodeTree;
  1504. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1505. var $occupants = $(view.el.querySelector('.occupant-list'));
  1506. expect($occupants.children().length).toBe(1);
  1507. expect($occupants.children().first(0).text()).toBe("oldnick");
  1508. expect($chat_content.find('div.chat-info').length).toBe(2);
  1509. expect($chat_content.find('div.chat-info:first').html()).toBe("oldnick has entered the room.");
  1510. expect($chat_content.find('div.chat-info:last').html()).toBe(
  1511. __(_converse.muc.new_nickname_messages["210"], "oldnick")
  1512. );
  1513. presence = $pres().attrs({
  1514. from:'lounge@localhost/oldnick',
  1515. id:'DC352437-C019-40EC-B590-AF29E879AF98',
  1516. to:'dummy@localhost/pda',
  1517. type:'unavailable'
  1518. })
  1519. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1520. .c('item').attrs({
  1521. affiliation: 'member',
  1522. jid: 'dummy@localhost/pda',
  1523. nick: 'newnick',
  1524. role: 'participant'
  1525. }).up()
  1526. .c('status').attrs({code:'303'}).up()
  1527. .c('status').attrs({code:'110'}).nodeTree;
  1528. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1529. expect($chat_content.find('div.chat-info').length).toBe(3);
  1530. expect($chat_content.find('div.chat-info').last().html()).toBe(
  1531. __(_converse.muc.new_nickname_messages["303"], "newnick")
  1532. );
  1533. $occupants = $(view.el.querySelector('.occupant-list'));
  1534. expect($occupants.children().length).toBe(0);
  1535. presence = $pres().attrs({
  1536. from:'lounge@localhost/newnick',
  1537. id:'5B4F27A4-25ED-43F7-A699-382C6B4AFC67',
  1538. to:'dummy@localhost/pda'
  1539. })
  1540. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1541. .c('item').attrs({
  1542. affiliation: 'member',
  1543. jid: 'dummy@localhost/pda',
  1544. role: 'participant'
  1545. }).up()
  1546. .c('status').attrs({code:'110'}).nodeTree;
  1547. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1548. expect($chat_content.find('div.chat-info').length).toBe(4);
  1549. expect($chat_content.find('div.chat-info').get(2).textContent).toBe(
  1550. __(_converse.muc.new_nickname_messages["303"], "newnick")
  1551. );
  1552. expect($chat_content.find('div.chat-info').last().html()).toBe(
  1553. "newnick has entered the room.");
  1554. $occupants = $(view.el.querySelector('.occupant-list'));
  1555. expect($occupants.children().length).toBe(1);
  1556. expect($occupants.children().first(0).text()).toBe("newnick");
  1557. done();
  1558. });
  1559. }));
  1560. it("queries for the room information before attempting to join the user",
  1561. mock.initConverseWithPromises(
  1562. null, ['rosterGroupsFetched'], {},
  1563. function (done, _converse) {
  1564. var sent_IQ, IQ_id;
  1565. var sendIQ = _converse.connection.sendIQ;
  1566. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1567. sent_IQ = iq;
  1568. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1569. });
  1570. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  1571. // Check that the room queried for the feautures.
  1572. expect(sent_IQ.toLocaleString()).toBe(
  1573. "<iq from='dummy@localhost/resource' to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  1574. "<query xmlns='http://jabber.org/protocol/disco#info'/>"+
  1575. "</iq>");
  1576. /* <iq from='coven@chat.shakespeare.lit'
  1577. * id='ik3vs715'
  1578. * to='hag66@shakespeare.lit/pda'
  1579. * type='result'>
  1580. * <query xmlns='http://jabber.org/protocol/disco#info'>
  1581. * <identity
  1582. * category='conference'
  1583. * name='A Dark Cave'
  1584. * type='text'/>
  1585. * <feature var='http://jabber.org/protocol/muc'/>
  1586. * <feature var='muc_passwordprotected'/>
  1587. * <feature var='muc_hidden'/>
  1588. * <feature var='muc_temporary'/>
  1589. * <feature var='muc_open'/>
  1590. * <feature var='muc_unmoderated'/>
  1591. * <feature var='muc_nonanonymous'/>
  1592. * </query>
  1593. * </iq>
  1594. */
  1595. var features_stanza = $iq({
  1596. from: 'coven@chat.shakespeare.lit',
  1597. 'id': IQ_id,
  1598. 'to': 'dummy@localhost/desktop',
  1599. 'type': 'result'
  1600. })
  1601. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  1602. .c('identity', {
  1603. 'category': 'conference',
  1604. 'name': 'A Dark Cave',
  1605. 'type': 'text'
  1606. }).up()
  1607. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  1608. .c('feature', {'var': 'muc_passwordprotected'}).up()
  1609. .c('feature', {'var': 'muc_hidden'}).up()
  1610. .c('feature', {'var': 'muc_temporary'}).up()
  1611. .c('feature', {'var': 'muc_open'}).up()
  1612. .c('feature', {'var': 'muc_unmoderated'}).up()
  1613. .c('feature', {'var': 'muc_nonanonymous'});
  1614. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1615. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1616. expect(view.model.get('features_fetched')).toBe(true);
  1617. expect(view.model.get('passwordprotected')).toBe(true);
  1618. expect(view.model.get('hidden')).toBe(true);
  1619. expect(view.model.get('temporary')).toBe(true);
  1620. expect(view.model.get('open')).toBe(true);
  1621. expect(view.model.get('unmoderated')).toBe(true);
  1622. expect(view.model.get('nonanonymous')).toBe(true);
  1623. done();
  1624. }));
  1625. it("updates the shown features when the room configuration has changed",
  1626. mock.initConverseWithPromises(
  1627. null, ['rosterGroupsFetched'], {},
  1628. function (done, _converse) {
  1629. var sent_IQ, IQ_id;
  1630. var sendIQ = _converse.connection.sendIQ;
  1631. test_utils.openAndEnterChatRoom(_converse, 'room', 'conference.example.org', 'dummy').then(function () {
  1632. var view = _converse.chatboxviews.get('room@conference.example.org');
  1633. view.model.set({
  1634. 'passwordprotected': false,
  1635. 'unsecured': true,
  1636. 'hidden': false,
  1637. 'public': true,
  1638. 'membersonly': false,
  1639. 'open': true,
  1640. 'persistent': false,
  1641. 'temporary': true,
  1642. 'nonanonymous': true,
  1643. 'semianonymous': false,
  1644. 'moderated': false,
  1645. 'unmoderated': true
  1646. });
  1647. expect(view.model.get('persistent')).toBe(false);
  1648. expect(view.model.get('temporary')).toBe(true);
  1649. view.model.set({'persistent': true});
  1650. expect(view.model.get('persistent')).toBe(true);
  1651. expect(view.model.get('temporary')).toBe(false);
  1652. expect(view.model.get('unsecured')).toBe(true);
  1653. expect(view.model.get('passwordprotected')).toBe(false);
  1654. view.model.set({'passwordprotected': true});
  1655. expect(view.model.get('unsecured')).toBe(false);
  1656. expect(view.model.get('passwordprotected')).toBe(true);
  1657. expect(view.model.get('unmoderated')).toBe(true);
  1658. expect(view.model.get('moderated')).toBe(false);
  1659. view.model.set({'moderated': true});
  1660. expect(view.model.get('unmoderated')).toBe(false);
  1661. expect(view.model.get('moderated')).toBe(true);
  1662. expect(view.model.get('nonanonymous')).toBe(true);
  1663. expect(view.model.get('semianonymous')).toBe(false);
  1664. view.model.set({'nonanonymous': false});
  1665. expect(view.model.get('nonanonymous')).toBe(false);
  1666. expect(view.model.get('semianonymous')).toBe(true);
  1667. expect(view.model.get('open')).toBe(true);
  1668. expect(view.model.get('membersonly')).toBe(false);
  1669. view.model.set({'membersonly': true});
  1670. expect(view.model.get('open')).toBe(false);
  1671. expect(view.model.get('membersonly')).toBe(true);
  1672. done();
  1673. });
  1674. }));
  1675. it("indicates when a room is no longer anonymous",
  1676. mock.initConverseWithPromises(
  1677. null, ['rosterGroupsFetched'], {},
  1678. function (done, _converse) {
  1679. var sent_IQ, IQ_id;
  1680. var sendIQ = _converse.connection.sendIQ;
  1681. test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'some1').then(function () {
  1682. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1683. sent_IQ = iq;
  1684. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1685. });
  1686. // We pretend this is a new room, so no disco info is returned.
  1687. var features_stanza = $iq({
  1688. from: 'coven@chat.shakespeare.lit',
  1689. 'id': IQ_id,
  1690. 'to': 'dummy@localhost/desktop',
  1691. 'type': 'error'
  1692. }).c('error', {'type': 'cancel'})
  1693. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1694. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1695. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1696. /* <message xmlns="jabber:client"
  1697. * type="groupchat"
  1698. * to="dummy@localhost/_converse.js-27854181"
  1699. * from="coven@chat.shakespeare.lit">
  1700. * <x xmlns="http://jabber.org/protocol/muc#user">
  1701. * <status code="104"/>
  1702. * <status code="172"/>
  1703. * </x>
  1704. * </message>
  1705. */
  1706. var message = $msg({
  1707. type:'groupchat',
  1708. to: 'dummy@localhost/_converse.js-27854181',
  1709. from: 'coven@chat.shakespeare.lit'
  1710. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  1711. .c('status', {code: '104'}).up()
  1712. .c('status', {code: '172'});
  1713. _converse.connection._dataRecv(test_utils.createRequest(message));
  1714. var $chat_body = $(view.el.querySelector('.chatroom-body'));
  1715. expect($chat_body.find('.message:last').text()).toBe('This room is now no longer anonymous');
  1716. done();
  1717. });
  1718. }));
  1719. it("informs users if they have been kicked out of the chat room",
  1720. mock.initConverseWithPromises(
  1721. null, ['rosterGroupsFetched'], {},
  1722. function (done, _converse) {
  1723. /* <presence
  1724. * from='harfleur@chat.shakespeare.lit/pistol'
  1725. * to='pistol@shakespeare.lit/harfleur'
  1726. * type='unavailable'>
  1727. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1728. * <item affiliation='none' role='none'>
  1729. * <actor nick='Fluellen'/>
  1730. * <reason>Avaunt, you cullion!</reason>
  1731. * </item>
  1732. * <status code='110'/>
  1733. * <status code='307'/>
  1734. * </x>
  1735. * </presence>
  1736. */
  1737. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1738. var presence = $pres().attrs({
  1739. from:'lounge@localhost/dummy',
  1740. to:'dummy@localhost/pda',
  1741. type:'unavailable'
  1742. })
  1743. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1744. .c('item').attrs({
  1745. affiliation: 'none',
  1746. jid: 'dummy@localhost/pda',
  1747. role: 'none'
  1748. })
  1749. .c('actor').attrs({nick: 'Fluellen'}).up()
  1750. .c('reason').t('Avaunt, you cullion!').up()
  1751. .up()
  1752. .c('status').attrs({code:'110'}).up()
  1753. .c('status').attrs({code:'307'}).nodeTree;
  1754. var view = _converse.chatboxviews.get('lounge@localhost');
  1755. view.onChatRoomPresence(presence);
  1756. expect($(view.el.querySelector('.chat-area')).is(':visible')).toBeFalsy();
  1757. expect($(view.el.querySelector('.occupants')).is(':visible')).toBeFalsy();
  1758. var $chat_body = $(view.el.querySelector('.chatroom-body'));
  1759. expect($chat_body.find('.disconnect-msg').text()).toBe(
  1760. 'You have been kicked from this room'+
  1761. 'This action was done by Fluellen.'+
  1762. 'The reason given is: "Avaunt, you cullion!".'
  1763. );
  1764. done();
  1765. });
  1766. }));
  1767. it("can be saved to, and retrieved from, browserStorage",
  1768. mock.initConverseWithPromises(
  1769. null, ['rosterGroupsFetched'], {},
  1770. function (done, _converse) {
  1771. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1772. // We instantiate a new ChatBoxes collection, which by default
  1773. // will be empty.
  1774. test_utils.openControlBox();
  1775. var newchatboxes = new _converse.ChatBoxes();
  1776. expect(newchatboxes.length).toEqual(0);
  1777. // The chatboxes will then be fetched from browserStorage inside the
  1778. // onConnected method
  1779. newchatboxes.onConnected();
  1780. expect(newchatboxes.length).toEqual(2);
  1781. // Check that the chatrooms retrieved from browserStorage
  1782. // have the same attributes values as the original ones.
  1783. var attrs = ['id', 'box_id', 'visible'];
  1784. var new_attrs, old_attrs;
  1785. for (var i=0; i<attrs.length; i++) {
  1786. new_attrs = _.map(_.map(newchatboxes.models, 'attributes'), attrs[i]);
  1787. old_attrs = _.map(_.map(_converse.chatboxes.models, 'attributes'), attrs[i]);
  1788. // FIXME: should have have to sort here? Order must
  1789. // probably be the same...
  1790. // This should be fixed once the controlbox always opens
  1791. // only on the right.
  1792. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  1793. }
  1794. _converse.rosterview.render();
  1795. done();
  1796. }));
  1797. it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'",
  1798. mock.initConverseWithPromises(
  1799. null, ['rosterGroupsFetched'], {},
  1800. function (done, _converse) {
  1801. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1802. var view = _converse.chatboxviews.get('lounge@localhost'),
  1803. trimmed_chatboxes = _converse.minimized_chats;
  1804. spyOn(view, 'minimize').and.callThrough();
  1805. spyOn(view, 'maximize').and.callThrough();
  1806. spyOn(_converse, 'emit');
  1807. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1808. view.el.querySelector('.toggle-chatbox-button').click();
  1809. expect(view.minimize).toHaveBeenCalled();
  1810. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMinimized', jasmine.any(Object));
  1811. expect(u.isVisible(view.el)).toBeFalsy();
  1812. expect(view.model.get('minimized')).toBeTruthy();
  1813. expect(view.minimize).toHaveBeenCalled();
  1814. var trimmedview = trimmed_chatboxes.get(view.model.get('id'));
  1815. trimmedview.el.querySelector("a.restore-chat").click();
  1816. expect(view.maximize).toHaveBeenCalled();
  1817. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMaximized', jasmine.any(Object));
  1818. expect(view.model.get('minimized')).toBeFalsy();
  1819. expect(_converse.emit.calls.count(), 3);
  1820. done();
  1821. }));
  1822. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'",
  1823. mock.initConverseWithPromises(
  1824. null, ['rosterGroupsFetched'], {},
  1825. function (done, _converse) {
  1826. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1827. var view = _converse.chatboxviews.get('lounge@localhost');
  1828. spyOn(view, 'close').and.callThrough();
  1829. spyOn(_converse, 'emit');
  1830. spyOn(view, 'leave');
  1831. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1832. view.el.querySelector('.close-chatbox-button').click();
  1833. expect(view.close).toHaveBeenCalled();
  1834. expect(view.leave).toHaveBeenCalled();
  1835. // XXX: After refactoring, the chat box only gets closed
  1836. // once we have confirmation from the server. To test this,
  1837. // we would have to mock the returned presence stanza.
  1838. // See the "leave" method on the ChatRoomView.
  1839. // expect(_converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  1840. done();
  1841. }));
  1842. });
  1843. describe("Each chat room can take special commands", function () {
  1844. it("/help to show the available commands",
  1845. mock.initConverseWithPromises(
  1846. null, ['rosterGroupsFetched'], {},
  1847. function (done, _converse) {
  1848. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1849. var view = _converse.chatboxviews.get('lounge@localhost');
  1850. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1851. var textarea = view.el.querySelector('.chat-textarea');
  1852. textarea.value = '/help This is the room subject';
  1853. view.keyPressed({
  1854. target: textarea,
  1855. preventDefault: _.noop,
  1856. keyCode: 13
  1857. });
  1858. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1859. const info_messages = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info:not(.chat-date)'), 0);
  1860. expect(info_messages.length).toBe(17);
  1861. expect(info_messages.pop().textContent).toBe('/voice: Allow muted user to post messages');
  1862. expect(info_messages.pop().textContent).toBe('/topic: Set room subject (alias for /subject)');
  1863. expect(info_messages.pop().textContent).toBe('/subject: Set room subject');
  1864. expect(info_messages.pop().textContent).toBe('/revoke: Revoke user\'s membership');
  1865. expect(info_messages.pop().textContent).toBe('/owner: Grant ownership of this room');
  1866. expect(info_messages.pop().textContent).toBe('/op: Grant moderator role to user');
  1867. expect(info_messages.pop().textContent).toBe('/nick: Change your nickname');
  1868. expect(info_messages.pop().textContent).toBe('/mute: Remove user\'s ability to post messages');
  1869. expect(info_messages.pop().textContent).toBe('/member: Grant membership to a user');
  1870. expect(info_messages.pop().textContent).toBe('/me: Write in 3rd person');
  1871. expect(info_messages.pop().textContent).toBe('/kick: Kick user from room');
  1872. expect(info_messages.pop().textContent).toBe('/help: Show this menu');
  1873. expect(info_messages.pop().textContent).toBe('/deop: Change user role to participant');
  1874. expect(info_messages.pop().textContent).toBe('/clear: Remove messages');
  1875. expect(info_messages.pop().textContent).toBe('/ban: Ban user from room');
  1876. expect(info_messages.pop().textContent).toBe('/admin: Change user\'s affiliation to admin');
  1877. done();
  1878. });
  1879. }));
  1880. it("/topic to set the room topic",
  1881. mock.initConverseWithPromises(
  1882. null, ['rosterGroupsFetched'], {},
  1883. function (done, _converse) {
  1884. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1885. var sent_stanza;
  1886. var view = _converse.chatboxviews.get('lounge@localhost');
  1887. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1888. spyOn(view, 'clearChatRoomMessages');
  1889. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  1890. sent_stanza = stanza;
  1891. });
  1892. // Check the alias /topic
  1893. var textarea = view.el.querySelector('.chat-textarea');
  1894. textarea.value = '/topic This is the room subject';
  1895. view.keyPressed({
  1896. target: textarea,
  1897. preventDefault: _.noop,
  1898. keyCode: 13
  1899. });
  1900. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1901. expect(_converse.connection.send).toHaveBeenCalled();
  1902. expect(sent_stanza.textContent).toBe('This is the room subject');
  1903. // Check /subject
  1904. textarea.value = '/subject This is a new subject';
  1905. view.keyPressed({
  1906. target: textarea,
  1907. preventDefault: _.noop,
  1908. keyCode: 13
  1909. });
  1910. expect(sent_stanza.textContent).toBe('This is a new subject');
  1911. expect(sent_stanza.outerHTML).toBe(
  1912. '<message to="lounge@localhost" from="dummy@localhost/resource" type="groupchat" xmlns="jabber:client">'+
  1913. '<subject xmlns="jabber:client">This is a new subject</subject>'+
  1914. '</message>');
  1915. // Check case insensitivity
  1916. textarea.value = '/Subject This is yet another subject';
  1917. view.keyPressed({
  1918. target: textarea,
  1919. preventDefault: _.noop,
  1920. keyCode: 13
  1921. });
  1922. expect(sent_stanza.textContent).toBe('This is yet another subject');
  1923. expect(sent_stanza.outerHTML).toBe(
  1924. '<message to="lounge@localhost" from="dummy@localhost/resource" type="groupchat" xmlns="jabber:client">'+
  1925. '<subject xmlns="jabber:client">This is yet another subject</subject>'+
  1926. '</message>');
  1927. done();
  1928. });
  1929. }));
  1930. it("/clear to clear messages",
  1931. mock.initConverseWithPromises(
  1932. null, ['rosterGroupsFetched'], {},
  1933. function (done, _converse) {
  1934. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1935. var view = _converse.chatboxviews.get('lounge@localhost');
  1936. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1937. spyOn(view, 'clearChatRoomMessages');
  1938. var textarea = view.el.querySelector('.chat-textarea')
  1939. textarea.value = '/clear';
  1940. view.keyPressed({
  1941. target: textarea,
  1942. preventDefault: _.noop,
  1943. keyCode: 13
  1944. });
  1945. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1946. expect(view.clearChatRoomMessages).toHaveBeenCalled();
  1947. done();
  1948. });
  1949. }));
  1950. it("/owner to make a user an owner",
  1951. mock.initConverseWithPromises(
  1952. null, ['rosterGroupsFetched'], {},
  1953. function (done, _converse) {
  1954. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1955. var sent_IQ, IQ_id;
  1956. var sendIQ = _converse.connection.sendIQ;
  1957. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1958. sent_IQ = iq;
  1959. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1960. });
  1961. var view = _converse.chatboxviews.get('lounge@localhost');
  1962. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1963. spyOn(view, 'setAffiliation').and.callThrough();
  1964. spyOn(view, 'showStatusNotification').and.callThrough();
  1965. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  1966. var textarea = view.el.querySelector('.chat-textarea')
  1967. textarea.value = '/owner';
  1968. view.keyPressed({
  1969. target: textarea,
  1970. preventDefault: _.noop,
  1971. keyCode: 13
  1972. });
  1973. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1974. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  1975. expect(view.showStatusNotification).toHaveBeenCalledWith(
  1976. "Error: the \"owner\" command takes two arguments, the user's nickname and optionally a reason.",
  1977. true
  1978. );
  1979. expect(view.setAffiliation).not.toHaveBeenCalled();
  1980. // Call now with the correct amount of arguments.
  1981. // XXX: Calling onMessageSubmitted directly, trying
  1982. // again via triggering Event doesn't work for some weird
  1983. // reason.
  1984. view.onMessageSubmitted('/owner annoyingGuy@localhost You\'re responsible');
  1985. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  1986. expect(view.showStatusNotification.calls.count()).toBe(1);
  1987. expect(view.setAffiliation).toHaveBeenCalled();
  1988. // Check that the member list now gets updated
  1989. expect(sent_IQ.toLocaleString()).toBe(
  1990. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  1991. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  1992. "<item affiliation='owner' jid='annoyingGuy@localhost'>"+
  1993. "<reason>You&apos;re responsible</reason>"+
  1994. "</item>"+
  1995. "</query>"+
  1996. "</iq>");
  1997. done();
  1998. });
  1999. }));
  2000. it("/ban to ban a user",
  2001. mock.initConverseWithPromises(
  2002. null, ['rosterGroupsFetched'], {},
  2003. function (done, _converse) {
  2004. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2005. var sent_IQ, IQ_id;
  2006. var sendIQ = _converse.connection.sendIQ;
  2007. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2008. sent_IQ = iq;
  2009. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2010. });
  2011. var view = _converse.chatboxviews.get('lounge@localhost');
  2012. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2013. spyOn(view, 'setAffiliation').and.callThrough();
  2014. spyOn(view, 'showStatusNotification').and.callThrough();
  2015. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2016. var textarea = view.el.querySelector('.chat-textarea')
  2017. textarea.value = '/ban';
  2018. view.keyPressed({
  2019. target: textarea,
  2020. preventDefault: _.noop,
  2021. keyCode: 13
  2022. });
  2023. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2024. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2025. expect(view.showStatusNotification).toHaveBeenCalledWith(
  2026. "Error: the \"ban\" command takes two arguments, the user's nickname and optionally a reason.",
  2027. true
  2028. );
  2029. expect(view.setAffiliation).not.toHaveBeenCalled();
  2030. // Call now with the correct amount of arguments.
  2031. // XXX: Calling onMessageSubmitted directly, trying
  2032. // again via triggering Event doesn't work for some weird
  2033. // reason.
  2034. view.onMessageSubmitted('/ban annoyingGuy@localhost You\'re annoying');
  2035. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2036. expect(view.showStatusNotification.calls.count()).toBe(1);
  2037. expect(view.setAffiliation).toHaveBeenCalled();
  2038. // Check that the member list now gets updated
  2039. expect(sent_IQ.toLocaleString()).toBe(
  2040. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2041. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2042. "<item affiliation='outcast' jid='annoyingGuy@localhost'>"+
  2043. "<reason>You&apos;re annoying</reason>"+
  2044. "</item>"+
  2045. "</query>"+
  2046. "</iq>");
  2047. done();
  2048. });
  2049. }));
  2050. it("/kick to kick a user",
  2051. mock.initConverseWithPromises(
  2052. null, ['rosterGroupsFetched'], {},
  2053. function (done, _converse) {
  2054. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2055. var sent_IQ, IQ_id;
  2056. var sendIQ = _converse.connection.sendIQ;
  2057. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2058. sent_IQ = iq;
  2059. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2060. });
  2061. var view = _converse.chatboxviews.get('lounge@localhost');
  2062. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2063. spyOn(view, 'modifyRole').and.callThrough();
  2064. spyOn(view, 'showStatusNotification').and.callThrough();
  2065. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2066. var textarea = view.el.querySelector('.chat-textarea')
  2067. textarea.value = '/kick';
  2068. view.keyPressed({
  2069. target: textarea,
  2070. preventDefault: _.noop,
  2071. keyCode: 13
  2072. });
  2073. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2074. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2075. expect(view.showStatusNotification).toHaveBeenCalledWith(
  2076. "Error: the \"kick\" command takes two arguments, the user's nickname and optionally a reason.",
  2077. true
  2078. );
  2079. expect(view.modifyRole).not.toHaveBeenCalled();
  2080. // Call now with the correct amount of arguments.
  2081. // XXX: Calling onMessageSubmitted directly, trying
  2082. // again via triggering Event doesn't work for some weird
  2083. // reason.
  2084. view.onMessageSubmitted('/kick annoyingGuy You\'re annoying');
  2085. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2086. expect(view.showStatusNotification.calls.count()).toBe(1);
  2087. expect(view.modifyRole).toHaveBeenCalled();
  2088. expect(sent_IQ.toLocaleString()).toBe(
  2089. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2090. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2091. "<item nick='annoyingGuy' role='none'>"+
  2092. "<reason>You&apos;re annoying</reason>"+
  2093. "</item>"+
  2094. "</query>"+
  2095. "</iq>");
  2096. /* <presence
  2097. * from='harfleur@chat.shakespeare.lit/pistol'
  2098. * to='gower@shakespeare.lit/cell'
  2099. * type='unavailable'>
  2100. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2101. * <item affiliation='none' role='none'/>
  2102. * <status code='307'/>
  2103. * </x>
  2104. * </presence>
  2105. */
  2106. var presence = $pres({
  2107. 'from': 'lounge@localhost/annoyingGuy',
  2108. 'to': 'dummy@localhost/desktop',
  2109. 'type': 'unavailable'
  2110. })
  2111. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2112. .c('item', {
  2113. 'affiliation': 'none',
  2114. 'role': 'none'
  2115. }).up()
  2116. .c('status', {'code': '307'});
  2117. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2118. expect(
  2119. view.el.querySelectorAll('.chat-info')[3].textContent).toBe(
  2120. "annoyingGuy has been kicked out");
  2121. done();
  2122. });
  2123. }));
  2124. it("/op and /deop to make a user a moderator or not",
  2125. mock.initConverseWithPromises(
  2126. null, ['rosterGroupsFetched'], {},
  2127. function (done, _converse) {
  2128. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2129. var sent_IQ, IQ_id;
  2130. var sendIQ = _converse.connection.sendIQ;
  2131. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2132. sent_IQ = iq;
  2133. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2134. });
  2135. var view = _converse.chatboxviews.get('lounge@localhost');
  2136. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2137. spyOn(view, 'modifyRole').and.callThrough();
  2138. spyOn(view, 'showStatusNotification').and.callThrough();
  2139. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2140. // New user enters the room
  2141. /* <presence
  2142. * from='coven@chat.shakespeare.lit/thirdwitch'
  2143. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  2144. * to='crone1@shakespeare.lit/desktop'>
  2145. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2146. * <item affiliation='member' role='moderator'/>
  2147. * </x>
  2148. * </presence>
  2149. */
  2150. var presence = $pres({
  2151. 'from': 'lounge@localhost/trustworthyguy',
  2152. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2153. 'to': 'dummy@localhost/desktop'
  2154. })
  2155. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2156. .c('item', {
  2157. 'jid': 'trustworthyguy@localhost',
  2158. 'affiliation': 'member',
  2159. 'role': 'participant'
  2160. });
  2161. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2162. var info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2163. expect(info_msgs.pop().textContent).toBe("trustworthyguy has entered the room.");
  2164. var textarea = view.el.querySelector('.chat-textarea')
  2165. textarea.value = '/op';
  2166. view.keyPressed({
  2167. target: textarea,
  2168. preventDefault: _.noop,
  2169. keyCode: 13
  2170. });
  2171. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2172. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2173. expect(view.showStatusNotification).toHaveBeenCalledWith(
  2174. "Error: the \"op\" command takes two arguments, the user's nickname and optionally a reason.",
  2175. true
  2176. );
  2177. expect(view.modifyRole).not.toHaveBeenCalled();
  2178. // Call now with the correct amount of arguments.
  2179. // XXX: Calling onMessageSubmitted directly, trying
  2180. // again via triggering Event doesn't work for some weird
  2181. // reason.
  2182. view.onMessageSubmitted('/op trustworthyguy You\'re trustworthy');
  2183. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2184. expect(view.showStatusNotification.calls.count()).toBe(1);
  2185. expect(view.modifyRole).toHaveBeenCalled();
  2186. expect(sent_IQ.toLocaleString()).toBe(
  2187. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2188. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2189. "<item nick='trustworthyguy' role='moderator'>"+
  2190. "<reason>You&apos;re trustworthy</reason>"+
  2191. "</item>"+
  2192. "</query>"+
  2193. "</iq>");
  2194. /* <presence
  2195. * from='coven@chat.shakespeare.lit/thirdwitch'
  2196. * to='crone1@shakespeare.lit/desktop'>
  2197. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2198. * <item affiliation='member'
  2199. * jid='hag66@shakespeare.lit/pda'
  2200. * role='moderator'/>
  2201. * </x>
  2202. * </presence>
  2203. */
  2204. presence = $pres({
  2205. 'from': 'lounge@localhost/trustworthyguy',
  2206. 'to': 'dummy@localhost/desktop'
  2207. })
  2208. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2209. .c('item', {
  2210. 'jid': 'trustworthyguy@localhost',
  2211. 'affiliation': 'member',
  2212. 'role': 'moderator'
  2213. });
  2214. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2215. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2216. expect(info_msgs.pop().textContent).toBe("trustworthyguy is now a moderator.");
  2217. view.onMessageSubmitted('/deop trustworthyguy Perhaps not');
  2218. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2219. expect(view.showStatusNotification.calls.count()).toBe(2);
  2220. expect(view.modifyRole).toHaveBeenCalled();
  2221. expect(sent_IQ.toLocaleString()).toBe(
  2222. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2223. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2224. "<item nick='trustworthyguy' role='participant'>"+
  2225. "<reason>Perhaps not</reason>"+
  2226. "</item>"+
  2227. "</query>"+
  2228. "</iq>");
  2229. /* <presence
  2230. * from='coven@chat.shakespeare.lit/thirdwitch'
  2231. * to='crone1@shakespeare.lit/desktop'>
  2232. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2233. * <item affiliation='member'
  2234. * jid='hag66@shakespeare.lit/pda'
  2235. * role='participant'/>
  2236. * </x>
  2237. * </presence>
  2238. */
  2239. presence = $pres({
  2240. 'from': 'lounge@localhost/trustworthyguy',
  2241. 'to': 'dummy@localhost/desktop'
  2242. }).c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2243. .c('item', {
  2244. 'jid': 'trustworthyguy@localhost',
  2245. 'affiliation': 'member',
  2246. 'role': 'participant'
  2247. });
  2248. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2249. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2250. expect(info_msgs.pop().textContent).toBe("trustworthyguy is no longer a moderator.");
  2251. done();
  2252. });
  2253. }));
  2254. it("/mute and /voice to mute and unmute a user",
  2255. mock.initConverseWithPromises(
  2256. null, ['rosterGroupsFetched'], {},
  2257. function (done, _converse) {
  2258. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2259. var sent_IQ, IQ_id;
  2260. var sendIQ = _converse.connection.sendIQ;
  2261. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2262. sent_IQ = iq;
  2263. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2264. });
  2265. var view = _converse.chatboxviews.get('lounge@localhost');
  2266. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2267. spyOn(view, 'modifyRole').and.callThrough();
  2268. spyOn(view, 'showStatusNotification').and.callThrough();
  2269. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2270. // New user enters the room
  2271. /* <presence
  2272. * from='coven@chat.shakespeare.lit/thirdwitch'
  2273. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  2274. * to='crone1@shakespeare.lit/desktop'>
  2275. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2276. * <item affiliation='member' role='participant'/>
  2277. * </x>
  2278. * </presence>
  2279. */
  2280. var presence = $pres({
  2281. 'from': 'lounge@localhost/annoyingGuy',
  2282. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2283. 'to': 'dummy@localhost/desktop'
  2284. })
  2285. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2286. .c('item', {
  2287. 'jid': 'annoyingguy@localhost',
  2288. 'affiliation': 'member',
  2289. 'role': 'participant'
  2290. });
  2291. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2292. var info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2293. expect(info_msgs.pop().textContent).toBe("annoyingGuy has entered the room.");
  2294. var textarea = view.el.querySelector('.chat-textarea')
  2295. textarea.value = '/mute';
  2296. view.keyPressed({
  2297. target: textarea,
  2298. preventDefault: _.noop,
  2299. keyCode: 13
  2300. });
  2301. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2302. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2303. expect(view.showStatusNotification).toHaveBeenCalledWith(
  2304. "Error: the \"mute\" command takes two arguments, the user's nickname and optionally a reason.",
  2305. true
  2306. );
  2307. expect(view.modifyRole).not.toHaveBeenCalled();
  2308. // Call now with the correct amount of arguments.
  2309. // XXX: Calling onMessageSubmitted directly, trying
  2310. // again via triggering Event doesn't work for some weird
  2311. // reason.
  2312. view.onMessageSubmitted('/mute annoyingGuy You\'re annoying');
  2313. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2314. expect(view.showStatusNotification.calls.count()).toBe(1);
  2315. expect(view.modifyRole).toHaveBeenCalled();
  2316. expect(sent_IQ.toLocaleString()).toBe(
  2317. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2318. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2319. "<item nick='annoyingGuy' role='visitor'>"+
  2320. "<reason>You&apos;re annoying</reason>"+
  2321. "</item>"+
  2322. "</query>"+
  2323. "</iq>");
  2324. /* <presence
  2325. * from='coven@chat.shakespeare.lit/thirdwitch'
  2326. * to='crone1@shakespeare.lit/desktop'>
  2327. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2328. * <item affiliation='member'
  2329. * jid='hag66@shakespeare.lit/pda'
  2330. * role='visitor'/>
  2331. * </x>
  2332. * </presence>
  2333. */
  2334. presence = $pres({
  2335. 'from': 'lounge@localhost/annoyingGuy',
  2336. 'to': 'dummy@localhost/desktop'
  2337. })
  2338. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2339. .c('item', {
  2340. 'jid': 'annoyingguy@localhost',
  2341. 'affiliation': 'member',
  2342. 'role': 'visitor'
  2343. });
  2344. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2345. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2346. expect(info_msgs.pop().textContent).toBe("annoyingGuy has been muted.");
  2347. view.onMessageSubmitted('/voice annoyingGuy Now you can talk again');
  2348. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2349. expect(view.showStatusNotification.calls.count()).toBe(2);
  2350. expect(view.modifyRole).toHaveBeenCalled();
  2351. expect(sent_IQ.toLocaleString()).toBe(
  2352. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2353. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2354. "<item nick='annoyingGuy' role='participant'>"+
  2355. "<reason>Now you can talk again</reason>"+
  2356. "</item>"+
  2357. "</query>"+
  2358. "</iq>");
  2359. /* <presence
  2360. * from='coven@chat.shakespeare.lit/thirdwitch'
  2361. * to='crone1@shakespeare.lit/desktop'>
  2362. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2363. * <item affiliation='member'
  2364. * jid='hag66@shakespeare.lit/pda'
  2365. * role='visitor'/>
  2366. * </x>
  2367. * </presence>
  2368. */
  2369. presence = $pres({
  2370. 'from': 'lounge@localhost/annoyingGuy',
  2371. 'to': 'dummy@localhost/desktop'
  2372. })
  2373. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2374. .c('item', {
  2375. 'jid': 'annoyingguy@localhost',
  2376. 'affiliation': 'member',
  2377. 'role': 'participant'
  2378. });
  2379. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2380. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2381. expect(info_msgs.pop().textContent).toBe("annoyingGuy has been given a voice again.");
  2382. done();
  2383. });
  2384. }));
  2385. });
  2386. describe("When attempting to enter a chatroom", function () {
  2387. var submitRoomForm = function (_converse) {
  2388. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  2389. var input = roomspanel.el.querySelector('input.new-chatroom-name');
  2390. var nick = roomspanel.el.querySelector('input.new-chatroom-nick');
  2391. var server = roomspanel.el.querySelector('input.new-chatroom-server');
  2392. input.value = 'problematic';
  2393. if (nick) {
  2394. nick.value = 'dummy';
  2395. }
  2396. server.value = 'muc.localhost';
  2397. roomspanel.el.querySelector('form [type="submit"]').click();
  2398. };
  2399. it("will show an error message if the room requires a password",
  2400. mock.initConverseWithPromises(
  2401. null, ['rosterGroupsFetched'], {},
  2402. function (done, _converse) {
  2403. submitRoomForm(_converse);
  2404. var presence = $pres().attrs({
  2405. from:'lounge@localhost/thirdwitch',
  2406. id:'n13mt3l',
  2407. to:'dummy@localhost/pda',
  2408. type:'error'})
  2409. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2410. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  2411. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2412. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2413. spyOn(view, 'renderPasswordForm').and.callThrough();
  2414. view.onChatRoomPresence(presence);
  2415. var $chat_body = $(view.el).find('.chatroom-body');
  2416. expect(view.renderPasswordForm).toHaveBeenCalled();
  2417. expect($chat_body.find('form.chatroom-form').length).toBe(1);
  2418. expect($chat_body.find('legend').text()).toBe('This chatroom requires a password');
  2419. // Let's submit the form
  2420. spyOn(view, 'join');
  2421. var input_el = view.el.querySelector('[name="password"]');
  2422. input_el.value = 'secret';
  2423. view.el.querySelector('input[type=submit]').click();
  2424. expect(view.join).toHaveBeenCalledWith(undefined, "secret");
  2425. done();
  2426. }));
  2427. it("will show an error message if the room is members-only and the user not included",
  2428. mock.initConverseWithPromises(
  2429. null, ['rosterGroupsFetched'], {},
  2430. function (done, _converse) {
  2431. submitRoomForm(_converse);
  2432. var presence = $pres().attrs({
  2433. from:'lounge@localhost/thirdwitch',
  2434. id:'n13mt3l',
  2435. to:'dummy@localhost/pda',
  2436. type:'error'})
  2437. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2438. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  2439. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2440. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2441. spyOn(view, 'showErrorMessage').and.callThrough();
  2442. view.onChatRoomPresence(presence);
  2443. expect($(view.el).find('.chatroom-body p:last').text()).toBe('You are not on the member list of this room.');
  2444. done();
  2445. }));
  2446. it("will show an error message if the user has been banned",
  2447. mock.initConverseWithPromises(
  2448. null, ['rosterGroupsFetched'], {},
  2449. function (done, _converse) {
  2450. submitRoomForm(_converse);
  2451. var presence = $pres().attrs({
  2452. from:'lounge@localhost/thirdwitch',
  2453. id:'n13mt3l',
  2454. to:'dummy@localhost/pda',
  2455. type:'error'})
  2456. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2457. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  2458. .c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2459. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2460. spyOn(view, 'showErrorMessage').and.callThrough();
  2461. view.onChatRoomPresence(presence);
  2462. expect($(view.el).find('.chatroom-body p:last').text()).toBe('You have been banned from this room.');
  2463. done();
  2464. }));
  2465. it("will render a nickname form if a nickname conflict happens and muc_nickname_from_jid=false",
  2466. mock.initConverseWithPromises(
  2467. null, ['rosterGroupsFetched'], {},
  2468. function (done, _converse) {
  2469. submitRoomForm(_converse);
  2470. var presence = $pres().attrs({
  2471. from:'lounge@localhost/thirdwitch',
  2472. id:'n13mt3l',
  2473. to:'dummy@localhost/pda',
  2474. type:'error'})
  2475. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2476. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2477. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2478. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2479. spyOn(view, 'showErrorMessage').and.callThrough();
  2480. view.onChatRoomPresence(presence);
  2481. expect($(view.el).find('.chatroom-body form.chatroom-form label:first').text()).toBe('Please choose your nickname');
  2482. var $input = $(view.el).find('.chatroom-body form.chatroom-form input:first');
  2483. $input.val('nicky');
  2484. view.el.querySelector('input[type=submit]').click();
  2485. done();
  2486. }));
  2487. it("will automatically choose a new nickname if a nickname conflict happens and muc_nickname_from_jid=true",
  2488. mock.initConverseWithPromises(
  2489. null, ['rosterGroupsFetched'], {},
  2490. function (done, _converse) {
  2491. /* <presence
  2492. * from='coven@chat.shakespeare.lit/thirdwitch'
  2493. * id='n13mt3l'
  2494. * to='hag66@shakespeare.lit/pda'
  2495. * type='error'>
  2496. * <x xmlns='http://jabber.org/protocol/muc'/>
  2497. * <error by='coven@chat.shakespeare.lit' type='cancel'>
  2498. * <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  2499. * </error>
  2500. * </presence>
  2501. */
  2502. submitRoomForm(_converse);
  2503. _converse.muc_nickname_from_jid = true;
  2504. var attrs = {
  2505. from:'lounge@localhost/dummy',
  2506. id:'n13mt3l',
  2507. to:'dummy@localhost/pda',
  2508. type:'error'
  2509. };
  2510. var presence = $pres().attrs(attrs)
  2511. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2512. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2513. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2514. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2515. spyOn(view, 'showErrorMessage').and.callThrough();
  2516. spyOn(view, 'join').and.callThrough();
  2517. // Simulate repeatedly that there's already someone in the room
  2518. // with that nickname
  2519. view.onChatRoomPresence(presence);
  2520. expect(view.join).toHaveBeenCalledWith('dummy-2');
  2521. attrs.from = 'lounge@localhost/dummy-2';
  2522. presence = $pres().attrs(attrs)
  2523. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2524. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2525. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2526. view.onChatRoomPresence(presence);
  2527. expect(view.join).toHaveBeenCalledWith('dummy-3');
  2528. attrs.from = 'lounge@localhost/dummy-3';
  2529. presence = $pres().attrs(attrs)
  2530. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2531. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2532. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2533. view.onChatRoomPresence(presence);
  2534. expect(view.join).toHaveBeenCalledWith('dummy-4');
  2535. done();
  2536. }));
  2537. it("will show an error message if the user is not allowed to have created the room",
  2538. mock.initConverseWithPromises(
  2539. null, ['rosterGroupsFetched'], {},
  2540. function (done, _converse) {
  2541. submitRoomForm(_converse);
  2542. var presence = $pres().attrs({
  2543. from:'lounge@localhost/thirdwitch',
  2544. id:'n13mt3l',
  2545. to:'dummy@localhost/pda',
  2546. type:'error'})
  2547. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2548. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2549. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2550. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2551. spyOn(view, 'showErrorMessage').and.callThrough();
  2552. view.onChatRoomPresence(presence);
  2553. expect($(view.el).find('.chatroom-body p:last').text()).toBe('You are not allowed to create new rooms.');
  2554. done();
  2555. }));
  2556. it("will show an error message if the user's nickname doesn't conform to room policy",
  2557. mock.initConverseWithPromises(
  2558. null, ['rosterGroupsFetched'], {},
  2559. function (done, _converse) {
  2560. submitRoomForm(_converse);
  2561. var presence = $pres().attrs({
  2562. from:'lounge@localhost/thirdwitch',
  2563. id:'n13mt3l',
  2564. to:'dummy@localhost/pda',
  2565. type:'error'})
  2566. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2567. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2568. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2569. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2570. spyOn(view, 'showErrorMessage').and.callThrough();
  2571. view.onChatRoomPresence(presence);
  2572. expect($(view.el).find('.chatroom-body p:last').text()).toBe("Your nickname doesn't conform to this room's policies.");
  2573. done();
  2574. }));
  2575. it("will show an error message if the room doesn't yet exist",
  2576. mock.initConverseWithPromises(
  2577. null, ['rosterGroupsFetched'], {},
  2578. function (done, _converse) {
  2579. submitRoomForm(_converse);
  2580. var presence = $pres().attrs({
  2581. from:'lounge@localhost/thirdwitch',
  2582. id:'n13mt3l',
  2583. to:'dummy@localhost/pda',
  2584. type:'error'})
  2585. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2586. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2587. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2588. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2589. spyOn(view, 'showErrorMessage').and.callThrough();
  2590. view.onChatRoomPresence(presence);
  2591. expect($(view.el).find('.chatroom-body p:last').text()).toBe("This room does not (yet) exist.");
  2592. done();
  2593. }));
  2594. it("will show an error message if the room has reached its maximum number of occupants",
  2595. mock.initConverseWithPromises(
  2596. null, ['rosterGroupsFetched'], {},
  2597. function (done, _converse) {
  2598. submitRoomForm(_converse);
  2599. var presence = $pres().attrs({
  2600. from:'lounge@localhost/thirdwitch',
  2601. id:'n13mt3l',
  2602. to:'dummy@localhost/pda',
  2603. type:'error'})
  2604. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2605. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2606. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2607. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2608. spyOn(view, 'showErrorMessage').and.callThrough();
  2609. view.onChatRoomPresence(presence);
  2610. expect($(view.el).find('.chatroom-body p:last').text()).toBe("This room has reached its maximum number of occupants.");
  2611. done();
  2612. }));
  2613. });
  2614. describe("Someone being invited to a chat room", function () {
  2615. it("will first be added to the member list if the chat room is members only",
  2616. mock.initConverseWithPromises(
  2617. null, ['rosterGroupsFetched'], {},
  2618. function (done, _converse) {
  2619. var sent_IQs = [], IQ_ids = [];
  2620. var sendIQ = _converse.connection.sendIQ;
  2621. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2622. sent_IQs.push(iq);
  2623. IQ_ids.push(sendIQ.bind(this)(iq, callback, errback));
  2624. });
  2625. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'dummy'});
  2626. // State that the chat is members-only via the features IQ
  2627. var features_stanza = $iq({
  2628. from: 'coven@chat.shakespeare.lit',
  2629. 'id': IQ_ids.pop(),
  2630. 'to': 'dummy@localhost/desktop',
  2631. 'type': 'result'
  2632. })
  2633. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  2634. .c('identity', {
  2635. 'category': 'conference',
  2636. 'name': 'A Dark Cave',
  2637. 'type': 'text'
  2638. }).up()
  2639. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  2640. .c('feature', {'var': 'muc_hidden'}).up()
  2641. .c('feature', {'var': 'muc_temporary'}).up()
  2642. .c('feature', {'var': 'muc_membersonly'}).up();
  2643. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  2644. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2645. expect(view.model.get('membersonly')).toBeTruthy();
  2646. test_utils.createContacts(_converse, 'current');
  2647. var sent_stanza, sent_id;
  2648. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  2649. if (stanza.nodeTree && stanza.nodeTree.nodeName === 'message') {
  2650. sent_id = stanza.nodeTree.getAttribute('id');
  2651. sent_stanza = stanza;
  2652. }
  2653. });
  2654. var name = mock.cur_names[0];
  2655. var invitee_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  2656. var reason = "Please join this chat room";
  2657. view.directInvite(invitee_jid, reason);
  2658. // Check in reverse order that we requested all three lists
  2659. // (member, owner and admin).
  2660. var admin_iq_id = IQ_ids.pop();
  2661. var owner_iq_id = IQ_ids.pop();
  2662. var member_iq_id = IQ_ids.pop();
  2663. expect(sent_IQs.pop().toLocaleString()).toBe(
  2664. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+admin_iq_id+"'>"+
  2665. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2666. "<item affiliation='admin'/>"+
  2667. "</query>"+
  2668. "</iq>");
  2669. expect(sent_IQs.pop().toLocaleString()).toBe(
  2670. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+owner_iq_id+"'>"+
  2671. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2672. "<item affiliation='owner'/>"+
  2673. "</query>"+
  2674. "</iq>");
  2675. expect(sent_IQs.pop().toLocaleString()).toBe(
  2676. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+member_iq_id+"'>"+
  2677. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2678. "<item affiliation='member'/>"+
  2679. "</query>"+
  2680. "</iq>");
  2681. /* Now the service sends the member list to the user
  2682. *
  2683. * <iq from='coven@chat.shakespeare.lit'
  2684. * id='member3'
  2685. * to='crone1@shakespeare.lit/desktop'
  2686. * type='result'>
  2687. * <query xmlns='http://jabber.org/protocol/muc#admin'>
  2688. * <item affiliation='member'
  2689. * jid='hag66@shakespeare.lit'
  2690. * nick='thirdwitch'
  2691. * role='participant'/>
  2692. * </query>
  2693. * </iq>
  2694. */
  2695. var member_list_stanza = $iq({
  2696. 'from': 'coven@chat.shakespeare.lit',
  2697. 'id': member_iq_id,
  2698. 'to': 'dummy@localhost/resource',
  2699. 'type': 'result'
  2700. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  2701. .c('item', {
  2702. 'affiliation': 'member',
  2703. 'jid': 'hag66@shakespeare.lit',
  2704. 'nick': 'thirdwitch',
  2705. 'role': 'participant'
  2706. });
  2707. _converse.connection._dataRecv(test_utils.createRequest(member_list_stanza));
  2708. var admin_list_stanza = $iq({
  2709. 'from': 'coven@chat.shakespeare.lit',
  2710. 'id': admin_iq_id,
  2711. 'to': 'dummy@localhost/resource',
  2712. 'type': 'result'
  2713. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  2714. .c('item', {
  2715. 'affiliation': 'admin',
  2716. 'jid': 'wiccarocks@shakespeare.lit',
  2717. 'nick': 'secondwitch'
  2718. });
  2719. _converse.connection._dataRecv(test_utils.createRequest(admin_list_stanza));
  2720. var owner_list_stanza = $iq({
  2721. 'from': 'coven@chat.shakespeare.lit',
  2722. 'id': owner_iq_id,
  2723. 'to': 'dummy@localhost/resource',
  2724. 'type': 'result'
  2725. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  2726. .c('item', {
  2727. 'affiliation': 'owner',
  2728. 'jid': 'crone1@shakespeare.lit',
  2729. });
  2730. _converse.connection._dataRecv(test_utils.createRequest(owner_list_stanza));
  2731. test_utils.waitUntil(function () {
  2732. return IQ_ids.length;
  2733. }, 300).then(function () {
  2734. // Check that the member list now gets updated
  2735. var iq = "<iq to='coven@chat.shakespeare.lit' type='set' xmlns='jabber:client' id='"+IQ_ids.pop()+"'>"+
  2736. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2737. "<item affiliation='member' jid='"+invitee_jid+"'>"+
  2738. "<reason>Please join this chat room</reason>"+
  2739. "</item>"+
  2740. "</query>"+
  2741. "</iq>";
  2742. test_utils.waitUntil(function () {
  2743. return _.includes(_.invokeMap(sent_IQs, Object.prototype.toLocaleString), iq);
  2744. }, 300).then(function () {
  2745. // Finally check that the user gets invited.
  2746. expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
  2747. "<message from='dummy@localhost/resource' to='"+invitee_jid+"' id='"+sent_id+"' xmlns='jabber:client'>"+
  2748. "<x xmlns='jabber:x:conference' jid='coven@chat.shakespeare.lit' reason='Please join this chat room'/>"+
  2749. "</message>"
  2750. );
  2751. done();
  2752. });
  2753. });
  2754. }));
  2755. });
  2756. describe("The affiliations delta", function () {
  2757. it("can be computed in various ways",
  2758. mock.initConverseWithPromises(
  2759. null, ['rosterGroupsFetched'], {},
  2760. function (done, _converse) {
  2761. test_utils.openChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy');
  2762. var roomview = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2763. var exclude_existing = false;
  2764. var remove_absentees = false;
  2765. var new_list = [];
  2766. var old_list = [];
  2767. var delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2768. expect(delta.length).toBe(0);
  2769. new_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2770. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2771. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2772. expect(delta.length).toBe(0);
  2773. // When remove_absentees is false, then affiliations in the old
  2774. // list which are not in the new one won't be removed.
  2775. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  2776. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2777. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2778. expect(delta.length).toBe(0);
  2779. // With exclude_existing set to false, any changed affiliations
  2780. // will be included in the delta (i.e. existing affiliations
  2781. // are included in the comparison).
  2782. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  2783. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2784. expect(delta.length).toBe(1);
  2785. expect(delta[0].jid).toBe('wiccarocks@shakespeare.lit');
  2786. expect(delta[0].affiliation).toBe('member');
  2787. // To also remove affiliations from the old list which are not
  2788. // in the new list, we set remove_absentees to true
  2789. remove_absentees = true;
  2790. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  2791. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2792. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2793. expect(delta.length).toBe(1);
  2794. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  2795. expect(delta[0].affiliation).toBe('none');
  2796. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, [], old_list);
  2797. expect(delta.length).toBe(2);
  2798. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  2799. expect(delta[0].affiliation).toBe('none');
  2800. expect(delta[1].jid).toBe('wiccarocks@shakespeare.lit');
  2801. expect(delta[1].affiliation).toBe('none');
  2802. // To only add a user if they don't already have an
  2803. // affiliation, we set 'exclude_existing' to true
  2804. exclude_existing = true;
  2805. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  2806. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2807. expect(delta.length).toBe(0);
  2808. done();
  2809. }));
  2810. });
  2811. describe("The \"Rooms\" Panel", function () {
  2812. it("is opened by clicking the 'Chatrooms' tab",
  2813. mock.initConverseWithPromises(
  2814. null, ['rosterGroupsFetched'], {},
  2815. function (done, _converse) {
  2816. test_utils.openControlBox();
  2817. var cbview = _converse.chatboxviews.get('controlbox');
  2818. var $tabs = $(cbview.el).find('#controlbox-tabs');
  2819. var $panels = $(cbview.el).find('.controlbox-panes');
  2820. var $contacts = $panels.children().first();
  2821. var $chatrooms = $panels.children().last();
  2822. spyOn(cbview, 'switchTab').and.callThrough();
  2823. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2824. $tabs.find('li').last().find('a')[0].click(); // Clicks the chatrooms tab
  2825. expect(u.isVisible($contacts[0])).toBe(false);
  2826. expect(u.isVisible($chatrooms[0])).toBe(true);
  2827. expect(cbview.switchTab).toHaveBeenCalled();
  2828. done();
  2829. }));
  2830. it("contains a form through which a new chatroom can be created",
  2831. mock.initConverseWithPromises(
  2832. null, ['rosterGroupsFetched'], {},
  2833. function (done, _converse) {
  2834. test_utils.openControlBox();
  2835. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  2836. var $input = $(roomspanel.el).find('input.new-chatroom-name');
  2837. var $nick = $(roomspanel.el).find('input.new-chatroom-nick');
  2838. var $server = $(roomspanel.el).find('input.new-chatroom-server');
  2839. expect($input.length).toBe(1);
  2840. expect($server.length).toBe(1);
  2841. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  2842. spyOn(roomspanel, 'openChatRoom').and.callThrough();
  2843. spyOn(_converse.ChatRoomView.prototype, 'getRoomFeatures').and.callFake(function () {
  2844. var deferred = new $.Deferred();
  2845. deferred.resolve();
  2846. return deferred.promise();
  2847. });
  2848. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2849. $input.val('Lounge');
  2850. $nick.val('dummy');
  2851. $server.val('muc.localhost');
  2852. roomspanel.el.querySelector('form [type="submit"]').click();
  2853. expect(roomspanel.openChatRoom).toHaveBeenCalled();
  2854. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  2855. done();
  2856. }));
  2857. it("can list rooms publically available on the server",
  2858. mock.initConverseWithPromises(
  2859. null, ['rosterGroupsFetched'], {},
  2860. function (done, _converse) {
  2861. var sendIQ = _converse.connection.sendIQ;
  2862. var sent_stanza, IQ_id;
  2863. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2864. sent_stanza = iq;
  2865. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2866. });
  2867. test_utils.openControlBox();
  2868. var panel = _converse.chatboxviews.get('controlbox').roomspanel;
  2869. $(panel.tabs).find('li').last().find('a')[0].click(); // Click the chatrooms tab
  2870. panel.model.set({'muc_domain': 'muc.localhost'}); // Make sure the domain is set
  2871. // See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
  2872. expect(document.querySelectorAll('#available-chatrooms dt').length).toBe(0);
  2873. expect(document.querySelectorAll('#available-chatrooms dd').length).toBe(0);
  2874. document.querySelector('input#show-rooms').click();
  2875. expect(sent_stanza.toLocaleString()).toBe(
  2876. "<iq to='muc.localhost' from='dummy@localhost/resource' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2877. "<query xmlns='http://jabber.org/protocol/disco#items'/>"+
  2878. "</iq>"
  2879. );
  2880. var iq = $iq({
  2881. from:'muc.localhost',
  2882. to:'dummy@localhost/pda',
  2883. id: IQ_id,
  2884. type:'result'
  2885. }).c('query')
  2886. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  2887. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  2888. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  2889. .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
  2890. _converse.connection._dataRecv(test_utils.createRequest(iq));
  2891. expect(document.querySelectorAll('#available-chatrooms dt').length).toBe(1);
  2892. expect($(panel.el.querySelector('#available-chatrooms')).children('dt').first().text()).toBe("Rooms found");
  2893. expect($(panel.el.querySelector('#available-chatrooms')).children('dd').length).toBe(4);
  2894. done();
  2895. }));
  2896. });
  2897. describe("The \"Rooms\" Panel", function () {
  2898. it("shows the number of unread mentions received",
  2899. mock.initConverseWithPromises(
  2900. null, ['rosterGroupsFetched'], {},
  2901. function (done, _converse) {
  2902. var room_jid = 'kitchen@conference.shakespeare.lit';
  2903. test_utils.openAndEnterChatRoom(
  2904. _converse, 'kitchen', 'conference.shakespeare.lit', 'fires').then(function () {
  2905. test_utils.openContactsPanel(_converse);
  2906. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  2907. expect(_.isNull(roomspanel.tab_el.querySelector('.msgs-indicator'))).toBeTruthy();
  2908. var view = _converse.chatboxviews.get(room_jid);
  2909. view.model.set({'minimized': true});
  2910. var contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
  2911. var message = 'fires: Your attention is required';
  2912. var nick = mock.chatroom_names[0],
  2913. msg = $msg({
  2914. from: room_jid+'/'+nick,
  2915. id: (new Date()).getTime(),
  2916. to: 'dummy@localhost',
  2917. type: 'groupchat'
  2918. }).c('body').t(message).tree();
  2919. view.handleMUCMessage(msg);
  2920. test_utils.waitUntil(function () {
  2921. return _.includes(roomspanel.tab_el.firstChild.classList, 'unread-msgs');
  2922. }, 300).then(function () {
  2923. expect(_.includes(roomspanel.tab_el.firstChild.classList, 'unread-msgs')).toBeTruthy();
  2924. expect(roomspanel.tab_el.querySelector('.msgs-indicator').textContent).toBe('1');
  2925. msg = $msg({
  2926. from: room_jid+'/'+nick,
  2927. id: (new Date()).getTime(),
  2928. to: 'dummy@localhost',
  2929. type: 'groupchat'
  2930. }).c('body').t(message).tree();
  2931. view.handleMUCMessage(msg);
  2932. expect(roomspanel.tab_el.querySelector('.msgs-indicator').textContent).toBe('2');
  2933. var contacts_panel = _converse.chatboxviews.get('controlbox').contactspanel;
  2934. expect(_.isNull(contacts_panel.tab_el.querySelector('.msgs-indicator'))).toBeTruthy();
  2935. view.model.set({'minimized': false});
  2936. expect(_.includes(roomspanel.tab_el.firstChild.classList, 'unread-msgs')).toBeFalsy();
  2937. expect(_.isNull(roomspanel.tab_el.querySelector('.msgs-indicator'))).toBeTruthy();
  2938. done();
  2939. });
  2940. });
  2941. }));
  2942. });
  2943. });
  2944. }));