chatroom.js 175 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233
  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. var chatroomview = _converse.chatboxviews.get(jid);
  75. expect(chatroomview.is_chatroom).toBeTruthy();
  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. var chatroomview = _converse.chatboxviews.get(jid);
  127. expect(chatroomview.is_chatroom).toBeTruthy();
  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. chatroomview = _converse.chatboxviews.get(jid);
  133. expect(chatroomview.is_chatroom).toBeTruthy();
  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[name="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 badge badge-info');
  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 badge badge-info');
  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 badge badge-info');
  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 badge badge-info');
  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. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  694. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  695. var features_stanza = $iq({
  696. from: 'coven@chat.shakespeare.lit',
  697. 'id': IQ_id,
  698. 'to': 'dummy@localhost/desktop',
  699. 'type': 'result'
  700. })
  701. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  702. .c('identity', {
  703. 'category': 'conference',
  704. 'name': 'A Dark Cave',
  705. 'type': 'text'
  706. }).up()
  707. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  708. .c('feature', {'var': 'muc_passwordprotected'}).up()
  709. .c('feature', {'var': 'muc_hidden'}).up()
  710. .c('feature', {'var': 'muc_temporary'}).up()
  711. .c('feature', {'var': 'muc_open'}).up()
  712. .c('feature', {'var': 'muc_unmoderated'}).up()
  713. .c('feature', {'var': 'muc_nonanonymous'}).up()
  714. .c('feature', {'var': 'urn:xmpp:mam:0'}).up()
  715. .c('x', { 'xmlns':'jabber:x:data', 'type':'result'})
  716. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  717. .c('value').t('http://jabber.org/protocol/muc#roominfo').up().up()
  718. .c('field', {'type':'text-single', 'var':'muc#roominfo_description', 'label':'Description'})
  719. .c('value').t('This is the description').up().up()
  720. .c('field', {'type':'text-single', 'var':'muc#roominfo_occupants', 'label':'Number of occupants'})
  721. .c('value').t(0);
  722. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  723. test_utils.waitUntil(function () {
  724. return _.get(view.el.querySelector('.chatroom-description'), 'textContent');
  725. }).then(function () {
  726. expect($(view.el.querySelector('.chatroom-description')).text()).toBe('This is the description');
  727. done();
  728. });
  729. }));
  730. it("will specially mark messages in which you are mentioned",
  731. mock.initConverseWithPromises(
  732. null, ['rosterGroupsFetched'], {},
  733. function (done, _converse) {
  734. test_utils.createContacts(_converse, 'current');
  735. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  736. var view = _converse.chatboxviews.get('lounge@localhost');
  737. if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
  738. var message = 'dummy: Your attention is required';
  739. var nick = mock.chatroom_names[0],
  740. msg = $msg({
  741. from: 'lounge@localhost/'+nick,
  742. id: (new Date()).getTime(),
  743. to: 'dummy@localhost',
  744. type: 'groupchat'
  745. }).c('body').t(message).tree();
  746. view.handleMUCMessage(msg);
  747. expect($(view.el).find('.chat-message').hasClass('mentioned')).toBeTruthy();
  748. done();
  749. });
  750. }));
  751. it("supports the /me command",
  752. mock.initConverseWithPromises(
  753. null, ['rosterGroupsFetched'], {},
  754. function (done, _converse) {
  755. test_utils.waitUntilDiscoConfirmed(_converse, 'localhost', [], ['vcard-temp'])
  756. .then(function () {
  757. return test_utils.waitUntil(function () {
  758. return _converse.xmppstatus.get('fullname');
  759. }, 300);
  760. }).then(function () {
  761. test_utils.createContacts(_converse, 'current');
  762. return test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  763. }).then(function () {
  764. var view = _converse.chatboxviews.get('lounge@localhost');
  765. if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
  766. var message = '/me is tired';
  767. var nick = mock.chatroom_names[0],
  768. msg = $msg({
  769. from: 'lounge@localhost/'+nick,
  770. id: (new Date()).getTime(),
  771. to: 'dummy@localhost',
  772. type: 'groupchat'
  773. }).c('body').t(message).tree();
  774. view.handleMUCMessage(msg);
  775. expect(_.includes($(view.el).find('.chat-msg-author').text(), '**Dyon van de Wege')).toBeTruthy();
  776. expect($(view.el).find('.chat-msg-content').text()).toBe(' is tired');
  777. message = '/me is as well';
  778. msg = $msg({
  779. from: 'lounge@localhost/dummy',
  780. id: (new Date()).getTime(),
  781. to: 'dummy@localhost',
  782. type: 'groupchat'
  783. }).c('body').t(message).tree();
  784. view.handleMUCMessage(msg);
  785. expect(_.includes($(view.el).find('.chat-msg-author:last').text(), '**Max Mustermann')).toBeTruthy();
  786. expect($(view.el).find('.chat-msg-content:last').text()).toBe(' is as well');
  787. done();
  788. });
  789. }));
  790. it("can be configured if you're its owner",
  791. mock.initConverseWithPromises(
  792. null, ['rosterGroupsFetched'], {},
  793. function (done, _converse) {
  794. var view;
  795. var sent_IQ, IQ_id;
  796. var sendIQ = _converse.connection.sendIQ;
  797. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  798. sent_IQ = iq;
  799. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  800. });
  801. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  802. view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  803. spyOn(view, 'saveAffiliationAndRole').and.callThrough();
  804. // We pretend this is a new room, so no disco info is returned.
  805. var features_stanza = $iq({
  806. from: 'coven@chat.shakespeare.lit',
  807. 'id': IQ_id,
  808. 'to': 'dummy@localhost/desktop',
  809. 'type': 'error'
  810. }).c('error', {'type': 'cancel'})
  811. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  812. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  813. /* <presence to="dummy@localhost/_converse.js-29092160"
  814. * from="coven@chat.shakespeare.lit/some1">
  815. * <x xmlns="http://jabber.org/protocol/muc#user">
  816. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  817. * <status code="110"/>
  818. * </x>
  819. * </presence></body>
  820. */
  821. var presence = $pres({
  822. to: 'dummy@localhost/_converse.js-29092160',
  823. from: 'coven@chat.shakespeare.lit/some1'
  824. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  825. .c('item', {
  826. 'affiliation': 'owner',
  827. 'jid': 'dummy@localhost/_converse.js-29092160',
  828. 'role': 'moderator'
  829. }).up()
  830. .c('status', {code: '110'});
  831. _converse.connection._dataRecv(test_utils.createRequest(presence));
  832. expect(view.saveAffiliationAndRole).toHaveBeenCalled();
  833. expect($(view.el.querySelector('.toggle-chatbox-button')).is(':visible')).toBeTruthy();
  834. test_utils.waitUntil(function () {
  835. return !_.isNull(view.el.querySelector('.configure-chatroom-button'));
  836. }, 300).then(function () {
  837. expect($(view.el.querySelector('.configure-chatroom-button')).is(':visible')).toBeTruthy();
  838. view.el.querySelector('.configure-chatroom-button').click();
  839. /* Check that an IQ is sent out, asking for the
  840. * configuration form.
  841. * See: // http://xmpp.org/extensions/xep-0045.html#example-163
  842. *
  843. * <iq from='crone1@shakespeare.lit/desktop'
  844. * id='config1'
  845. * to='coven@chat.shakespeare.lit'
  846. * type='get'>
  847. * <query xmlns='http://jabber.org/protocol/muc#owner'/>
  848. * </iq>
  849. */
  850. expect(sent_IQ.toLocaleString()).toBe(
  851. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  852. "<query xmlns='http://jabber.org/protocol/muc#owner'/>"+
  853. "</iq>");
  854. /* Server responds with the configuration form.
  855. * See: // http://xmpp.org/extensions/xep-0045.html#example-165
  856. */
  857. var config_stanza = $iq({from: 'coven@chat.shakespeare.lit',
  858. 'id': IQ_id,
  859. 'to': 'dummy@localhost/desktop',
  860. 'type': 'result'})
  861. .c('query', { 'xmlns': 'http://jabber.org/protocol/muc#owner'})
  862. .c('x', { 'xmlns': 'jabber:x:data', 'type': 'form'})
  863. .c('title').t('Configuration for "coven" Room').up()
  864. .c('instructions').t('Complete this form to modify the configuration of your room.').up()
  865. .c('field', {'type': 'hidden', 'var': 'FORM_TYPE'})
  866. .c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up()
  867. .c('field', {
  868. 'label': 'Natural-Language Room Name',
  869. 'type': 'text-single',
  870. 'var': 'muc#roomconfig_roomname'})
  871. .c('value').t('A Dark Cave').up().up()
  872. .c('field', {
  873. 'label': 'Short Description of Room',
  874. 'type': 'text-single',
  875. 'var': 'muc#roomconfig_roomdesc'})
  876. .c('value').t('The place for all good witches!').up().up()
  877. .c('field', {
  878. 'label': 'Enable Public Logging?',
  879. 'type': 'boolean',
  880. 'var': 'muc#roomconfig_enablelogging'})
  881. .c('value').t(0).up().up()
  882. .c('field', {
  883. 'label': 'Allow Occupants to Change Subject?',
  884. 'type': 'boolean',
  885. 'var': 'muc#roomconfig_changesubject'})
  886. .c('value').t(0).up().up()
  887. .c('field', {
  888. 'label': 'Allow Occupants to Invite Others?',
  889. 'type': 'boolean',
  890. 'var': 'muc#roomconfig_allowinvites'})
  891. .c('value').t(0).up().up()
  892. .c('field', {
  893. 'label': 'Who Can Send Private Messages?',
  894. 'type': 'list-single',
  895. 'var': 'muc#roomconfig_allowpm'})
  896. .c('value').t('anyone').up()
  897. .c('option', {'label': 'Anyone'})
  898. .c('value').t('anyone').up().up()
  899. .c('option', {'label': 'Anyone with Voice'})
  900. .c('value').t('participants').up().up()
  901. .c('option', {'label': 'Moderators Only'})
  902. .c('value').t('moderators').up().up()
  903. .c('option', {'label': 'Nobody'})
  904. .c('value').t('none').up().up().up()
  905. .c('field', {
  906. 'label': 'Roles for which Presence is Broadcasted',
  907. 'type': 'list-multi',
  908. 'var': 'muc#roomconfig_presencebroadcast'})
  909. .c('value').t('moderator').up()
  910. .c('value').t('participant').up()
  911. .c('value').t('visitor').up()
  912. .c('option', {'label': 'Moderator'})
  913. .c('value').t('moderator').up().up()
  914. .c('option', {'label': 'Participant'})
  915. .c('value').t('participant').up().up()
  916. .c('option', {'label': 'Visitor'})
  917. .c('value').t('visitor').up().up().up()
  918. .c('field', {
  919. 'label': 'Roles and Affiliations that May Retrieve Member List',
  920. 'type': 'list-multi',
  921. 'var': 'muc#roomconfig_getmemberlist'})
  922. .c('value').t('moderator').up()
  923. .c('value').t('participant').up()
  924. .c('value').t('visitor').up()
  925. .c('option', {'label': 'Moderator'})
  926. .c('value').t('moderator').up().up()
  927. .c('option', {'label': 'Participant'})
  928. .c('value').t('participant').up().up()
  929. .c('option', {'label': 'Visitor'})
  930. .c('value').t('visitor').up().up().up()
  931. .c('field', {
  932. 'label': 'Make Room Publicly Searchable?',
  933. 'type': 'boolean',
  934. 'var': 'muc#roomconfig_publicroom'})
  935. .c('value').t(0).up().up()
  936. .c('field', {
  937. 'label': 'Make Room Publicly Searchable?',
  938. 'type': 'boolean',
  939. 'var': 'muc#roomconfig_publicroom'})
  940. .c('value').t(0).up().up()
  941. .c('field', {
  942. 'label': 'Make Room Persistent?',
  943. 'type': 'boolean',
  944. 'var': 'muc#roomconfig_persistentroom'})
  945. .c('value').t(0).up().up()
  946. .c('field', {
  947. 'label': 'Make Room Moderated?',
  948. 'type': 'boolean',
  949. 'var': 'muc#roomconfig_moderatedroom'})
  950. .c('value').t(0).up().up()
  951. .c('field', {
  952. 'label': 'Make Room Members Only?',
  953. 'type': 'boolean',
  954. 'var': 'muc#roomconfig_membersonly'})
  955. .c('value').t(0).up().up()
  956. .c('field', {
  957. 'label': 'Password Required for Entry?',
  958. 'type': 'boolean',
  959. 'var': 'muc#roomconfig_passwordprotectedroom'})
  960. .c('value').t(1).up().up()
  961. .c('field', {'type': 'fixed'})
  962. .c('value').t('If a password is required to enter this room,'+
  963. 'you must specify the password below.').up().up()
  964. .c('field', {
  965. 'label': 'Password',
  966. 'type': 'text-private',
  967. 'var': 'muc#roomconfig_roomsecret'})
  968. .c('value').t('cauldronburn');
  969. _converse.connection._dataRecv(test_utils.createRequest(config_stanza));
  970. test_utils.waitUntil(function () {
  971. return $(view.el.querySelector('form.chatroom-form')).length;
  972. }, 300).then(function () {
  973. expect($(view.el.querySelector('form.chatroom-form')).length).toBe(1);
  974. expect(view.el.querySelectorAll('form.chatroom-form fieldset').length).toBe(2);
  975. var $membersonly = $(view.el.querySelector('input[name="muc#roomconfig_membersonly"]'));
  976. expect($membersonly.length).toBe(1);
  977. expect($membersonly.attr('type')).toBe('checkbox');
  978. $membersonly.prop('checked', true);
  979. var $moderated = $(view.el.querySelector('input[name="muc#roomconfig_moderatedroom"]'));
  980. expect($moderated.length).toBe(1);
  981. expect($moderated.attr('type')).toBe('checkbox');
  982. $moderated.prop('checked', true);
  983. var $password = $(view.el.querySelector('input[name="muc#roomconfig_roomsecret"]'));
  984. expect($password.length).toBe(1);
  985. expect($password.attr('type')).toBe('password');
  986. var $allowpm = $(view.el.querySelector('select[name="muc#roomconfig_allowpm"]'));
  987. expect($allowpm.length).toBe(1);
  988. $allowpm.val('moderators');
  989. var $presencebroadcast = $(view.el.querySelector('select[name="muc#roomconfig_presencebroadcast"]'));
  990. expect($presencebroadcast.length).toBe(1);
  991. $presencebroadcast.val(['moderator']);
  992. view.el.querySelector('input[type="submit"]').click();
  993. var $sent_stanza = $(sent_IQ.toLocaleString());
  994. expect($sent_stanza.find('field[var="muc#roomconfig_membersonly"] value').text()).toBe('1');
  995. expect($sent_stanza.find('field[var="muc#roomconfig_moderatedroom"] value').text()).toBe('1');
  996. expect($sent_stanza.find('field[var="muc#roomconfig_allowpm"] value').text()).toBe('moderators');
  997. expect($sent_stanza.find('field[var="muc#roomconfig_presencebroadcast"] value').text()).toBe('moderator');
  998. done();
  999. });
  1000. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  1001. }));
  1002. it("shows users currently present in the room",
  1003. mock.initConverseWithPromises(
  1004. null, ['rosterGroupsFetched'], {},
  1005. function (done, _converse) {
  1006. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function() {
  1007. var name;
  1008. var view = _converse.chatboxviews.get('lounge@localhost'),
  1009. occupants = view.el.querySelector('.occupant-list');
  1010. var presence, role, jid, model;
  1011. for (var i=0; i<mock.chatroom_names.length; i++) {
  1012. name = mock.chatroom_names[i];
  1013. role = mock.chatroom_roles[name].role;
  1014. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  1015. jid =
  1016. presence = $pres({
  1017. to:'dummy@localhost/pda',
  1018. from:'lounge@localhost/'+name
  1019. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1020. .c('item').attrs({
  1021. affiliation: mock.chatroom_roles[name].affiliation,
  1022. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  1023. role: role
  1024. }).up()
  1025. .c('status').attrs({code:'110'}).nodeTree;
  1026. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1027. expect(occupants.querySelectorAll('li').length).toBe(2+i);
  1028. model = view.occupantsview.model.where({'nick': name})[0];
  1029. var index = view.occupantsview.model.indexOf(model);
  1030. expect(occupants.querySelectorAll('li')[index].textContent).toBe(mock.chatroom_names[i]);
  1031. expect($(occupants.querySelectorAll('li')[index]).hasClass('moderator')).toBe(role === "moderator");
  1032. }
  1033. // Test users leaving the room
  1034. // http://xmpp.org/extensions/xep-0045.html#exit
  1035. for (i=mock.chatroom_names.length-1; i>-1; i--) {
  1036. name = mock.chatroom_names[i];
  1037. role = mock.chatroom_roles[name].role;
  1038. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  1039. presence = $pres({
  1040. to:'dummy@localhost/pda',
  1041. from:'lounge@localhost/'+name,
  1042. type: 'unavailable'
  1043. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1044. .c('item').attrs({
  1045. affiliation: mock.chatroom_roles[name].affiliation,
  1046. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  1047. role: 'none'
  1048. }).nodeTree;
  1049. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1050. expect(occupants.querySelectorAll('li').length).toBe(i+1);
  1051. }
  1052. done();
  1053. });
  1054. }));
  1055. it("escapes occupant nicknames when rendering them, to avoid JS-injection attacks",
  1056. mock.initConverseWithPromises(null, ['rosterGroupsFetched'], {}, function (done, _converse) {
  1057. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1058. /* <presence xmlns="jabber:client" to="jc@chat.example.org/converse.js-17184538"
  1059. * from="oo@conference.chat.example.org/&lt;img src=&quot;x&quot; onerror=&quot;alert(123)&quot;/&gt;">
  1060. * <x xmlns="http://jabber.org/protocol/muc#user">
  1061. * <item jid="jc@chat.example.org/converse.js-17184538" affiliation="owner" role="moderator"/>
  1062. * <status code="110"/>
  1063. * </x>
  1064. * </presence>"
  1065. */
  1066. var presence = $pres({
  1067. to:'dummy@localhost/pda',
  1068. from:"lounge@localhost/&lt;img src=&quot;x&quot; onerror=&quot;alert(123)&quot;/&gt;"
  1069. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1070. .c('item').attrs({
  1071. jid: 'someone@localhost',
  1072. role: 'moderator',
  1073. }).up()
  1074. .c('status').attrs({code:'110'}).nodeTree;
  1075. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1076. var view = _converse.chatboxviews.get('lounge@localhost');
  1077. var occupants = view.el.querySelector('.occupant-list').querySelectorAll('li');
  1078. expect(occupants.length).toBe(2);
  1079. expect($(occupants).first().text()).toBe("&lt;img src=&quot;x&quot; onerror=&quot;alert(123)&quot;/&gt;");
  1080. done();
  1081. });
  1082. }));
  1083. it("indicates moderators and visitors by means of a special css class and tooltip",
  1084. mock.initConverseWithPromises(
  1085. null, ['rosterGroupsFetched'], {},
  1086. function (done, _converse) {
  1087. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1088. var view = _converse.chatboxviews.get('lounge@localhost');
  1089. var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  1090. var presence = $pres({
  1091. to:'dummy@localhost/pda',
  1092. from:'lounge@localhost/moderatorman'
  1093. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1094. .c('item').attrs({
  1095. affiliation: 'admin',
  1096. jid: contact_jid,
  1097. role: 'moderator',
  1098. }).up()
  1099. .c('status').attrs({code:'110'}).nodeTree;
  1100. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1101. var occupants = view.el.querySelector('.occupant-list').querySelectorAll('li');
  1102. expect(occupants.length).toBe(2);
  1103. expect($(occupants).first().text()).toBe("moderatorman");
  1104. expect($(occupants).last().text()).toBe("dummy");
  1105. expect($(occupants).first().attr('class').indexOf('moderator')).not.toBe(-1);
  1106. expect($(occupants).first().attr('title')).toBe(
  1107. contact_jid + ' This user is a moderator. Click to mention moderatorman in your message.'
  1108. );
  1109. contact_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost';
  1110. presence = $pres({
  1111. to:'dummy@localhost/pda',
  1112. from:'lounge@localhost/visitorwoman'
  1113. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1114. .c('item').attrs({
  1115. jid: contact_jid,
  1116. role: 'visitor',
  1117. }).up()
  1118. .c('status').attrs({code:'110'}).nodeTree;
  1119. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1120. occupants = view.el.querySelector('.occupant-list').querySelectorAll('li');
  1121. expect($(occupants).last().text()).toBe("visitorwoman");
  1122. expect($(occupants).last().attr('class').indexOf('visitor')).not.toBe(-1);
  1123. expect($(occupants).last().attr('title')).toBe(
  1124. contact_jid + ' This user can NOT send messages in this room. Click to mention visitorwoman in your message.'
  1125. );
  1126. done();
  1127. });
  1128. }));
  1129. it("will use the user's reserved nickname, if it exists",
  1130. mock.initConverseWithPromises(
  1131. null, ['rosterGroupsFetched'], {},
  1132. function (done, _converse) {
  1133. var sent_IQ, IQ_id;
  1134. var sendIQ = _converse.connection.sendIQ;
  1135. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1136. if (iq.nodeTree.getAttribute('to') === 'lounge@localhost') {
  1137. sent_IQ = iq;
  1138. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1139. } else {
  1140. sendIQ.bind(this)(iq, callback, errback);
  1141. }
  1142. });
  1143. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1144. // We pretend this is a new room, so no disco info is returned.
  1145. var features_stanza = $iq({
  1146. from: 'lounge@localhost',
  1147. 'id': IQ_id,
  1148. 'to': 'dummy@localhost/desktop',
  1149. 'type': 'error'
  1150. }).c('error', {'type': 'cancel'})
  1151. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1152. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1153. var view = _converse.chatboxviews.get('lounge@localhost');
  1154. spyOn(view, 'join').and.callThrough();
  1155. /* <iq from='hag66@shakespeare.lit/pda'
  1156. * id='getnick1'
  1157. * to='coven@chat.shakespeare.lit'
  1158. * type='get'>
  1159. * <query xmlns='http://jabber.org/protocol/disco#info'
  1160. * node='x-roomuser-item'/>
  1161. * </iq>
  1162. */
  1163. test_utils.waitUntil(function () {
  1164. return sent_IQ.toLocaleString() ===
  1165. "<iq to='lounge@localhost' from='dummy@localhost/resource' "+
  1166. "type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  1167. "<query xmlns='http://jabber.org/protocol/disco#info' node='x-roomuser-item'/></iq>";
  1168. }, 300).then(function () {
  1169. /* <iq from='coven@chat.shakespeare.lit'
  1170. * id='getnick1'
  1171. * to='hag66@shakespeare.lit/pda'
  1172. * type='result'>
  1173. * <query xmlns='http://jabber.org/protocol/disco#info'
  1174. * node='x-roomuser-item'>
  1175. * <identity
  1176. * category='conference'
  1177. * name='thirdwitch'
  1178. * type='text'/>
  1179. * </query>
  1180. * </iq>
  1181. */
  1182. var stanza = $iq({
  1183. 'type': 'result',
  1184. 'id': IQ_id,
  1185. 'from': view.model.get('jid'),
  1186. 'to': _converse.connection.jid
  1187. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info', 'node': 'x-roomuser-item'})
  1188. .c('identity', {'category': 'conference', 'name': 'thirdwitch', 'type': 'text'});
  1189. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1190. expect(view.join).toHaveBeenCalled();
  1191. // The user has just entered the room (because join was called)
  1192. // and receives their own presence from the server.
  1193. // See example 24:
  1194. // http://xmpp.org/extensions/xep-0045.html#enter-pres
  1195. var presence = $pres({
  1196. to:'dummy@localhost/resource',
  1197. from:'lounge@localhost/thirdwitch',
  1198. id:'DC352437-C019-40EC-B590-AF29E879AF97'
  1199. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1200. .c('item').attrs({
  1201. affiliation: 'member',
  1202. jid: 'dummy@localhost/resource',
  1203. role: 'participant'
  1204. }).up()
  1205. .c('status').attrs({code:'110'}).up()
  1206. .c('status').attrs({code:'210'}).nodeTree;
  1207. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1208. var info_text = $(view.el).find('.chat-content .chat-info').text();
  1209. expect(info_text).toBe('Your nickname has been automatically set to thirdwitch');
  1210. done();
  1211. });
  1212. }));
  1213. it("allows the user to invite their roster contacts to enter the chat room",
  1214. mock.initConverseWithPromises(
  1215. null, ['rosterGroupsFetched'], {},
  1216. function (done, _converse) {
  1217. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1218. test_utils.createContacts(_converse, 'current'); // We need roster contacts, so that we have someone to invite
  1219. // Since we don't actually fetch roster contacts, we need to
  1220. // cheat here and emit the event.
  1221. _converse.emit('rosterContactsFetched');
  1222. spyOn(_converse, 'emit');
  1223. spyOn(window, 'prompt').and.callFake(function () {
  1224. return "Please join!";
  1225. });
  1226. var view = _converse.chatboxviews.get('lounge@localhost');
  1227. // XXX: cheating a lttle bit, normally this'll be set after
  1228. // receiving the features for the room.
  1229. view.model.set('open', 'true');
  1230. spyOn(view, 'directInvite').and.callThrough();
  1231. var $input;
  1232. $(view.el).find('.chat-area').remove();
  1233. test_utils.waitUntil(function () {
  1234. return $(view.el).find('input.invited-contact').length;
  1235. }, 300).then(function () {
  1236. var $input = $(view.el).find('input.invited-contact');
  1237. expect($input.attr('placeholder')).toBe('Invite');
  1238. $input.val("Felix");
  1239. var evt = new Event('input');
  1240. $input[0].dispatchEvent(evt);
  1241. var sent_stanza;
  1242. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  1243. sent_stanza = stanza;
  1244. });
  1245. var $hint = $input.siblings('ul').children('li');
  1246. expect($input.val()).toBe('Felix');
  1247. expect($hint[0].textContent).toBe('Felix Amsel');
  1248. expect($hint.length).toBe(1);
  1249. if (typeof(Event) === 'function') {
  1250. // Not working on PhantomJS
  1251. evt = new Event('mousedown', {'bubbles': true});
  1252. evt.button = 0; // For some reason awesomplete wants this
  1253. $hint[0].dispatchEvent(evt);
  1254. expect(window.prompt).toHaveBeenCalled();
  1255. expect(view.directInvite).toHaveBeenCalled();
  1256. expect(sent_stanza.toLocaleString()).toBe(
  1257. "<message from='dummy@localhost/resource' to='felix.amsel@localhost' id='" +
  1258. sent_stanza.nodeTree.getAttribute('id') +
  1259. "' xmlns='jabber:client'>"+
  1260. "<x xmlns='jabber:x:conference' jid='lounge@localhost' reason='Please join!'/>"+
  1261. "</message>"
  1262. );
  1263. }
  1264. done();
  1265. });
  1266. }));
  1267. it("can be joined automatically, based upon a received invite",
  1268. mock.initConverseWithPromises(
  1269. null, ['rosterGroupsFetched'], {},
  1270. function (done, _converse) {
  1271. test_utils.createContacts(_converse, 'current'); // We need roster contacts, who can invite us
  1272. spyOn(window, 'confirm').and.callFake(function () {
  1273. return true;
  1274. });
  1275. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1276. var view = _converse.chatboxviews.get('lounge@localhost');
  1277. view.close(); // Hack, otherwise we have to mock stanzas.
  1278. var name = mock.cur_names[0];
  1279. var from_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  1280. var room_jid = 'lounge@localhost';
  1281. var reason = "Please join this chat room";
  1282. expect(_converse.chatboxes.models.length).toBe(1);
  1283. expect(_converse.chatboxes.models[0].id).toBe("controlbox");
  1284. var stanza = Strophe.xmlHtmlNode(
  1285. '<message xmlns="jabber:client" to="'+_converse.bare_jid+'" from="'+from_jid+'" id="9bceb415-f34b-4fa4-80d5-c0d076a24231">'+
  1286. '<x xmlns="jabber:x:conference" jid="'+room_jid+'" reason="'+reason+'"/>'+
  1287. '</message>').firstChild;
  1288. _converse.onDirectMUCInvitation(stanza);
  1289. expect(window.confirm).toHaveBeenCalledWith(
  1290. name + ' has invited you to join a chat room: '+ room_jid +
  1291. ', and left the following reason: "'+reason+'"');
  1292. expect(_converse.chatboxes.models.length).toBe(2);
  1293. expect(_converse.chatboxes.models[0].id).toBe('controlbox');
  1294. expect(_converse.chatboxes.models[1].id).toBe(room_jid);
  1295. done();
  1296. });
  1297. }));
  1298. it("shows received groupchat messages",
  1299. mock.initConverseWithPromises(
  1300. null, ['rosterGroupsFetched'], {},
  1301. function (done, _converse) {
  1302. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1303. spyOn(_converse, 'emit');
  1304. var view = _converse.chatboxviews.get('lounge@localhost');
  1305. if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
  1306. var nick = mock.chatroom_names[0];
  1307. var text = 'This is a received message';
  1308. var message = $msg({
  1309. from: 'lounge@localhost/'+nick,
  1310. id: '1',
  1311. to: 'dummy@localhost',
  1312. type: 'groupchat'
  1313. }).c('body').t(text);
  1314. view.onChatRoomMessage(message.nodeTree);
  1315. var $chat_content = $(view.el).find('.chat-content');
  1316. expect($chat_content.find('.chat-message').length).toBe(1);
  1317. expect($chat_content.find('.chat-msg-content').text()).toBe(text);
  1318. expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
  1319. done();
  1320. }));
  1321. it("shows sent groupchat messages",
  1322. mock.initConverseWithPromises(
  1323. null, ['rosterGroupsFetched'], {},
  1324. function (done, _converse) {
  1325. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1326. spyOn(_converse, 'emit');
  1327. var view = _converse.chatboxviews.get('lounge@localhost');
  1328. if (!$(view.el).find('.chat-area').length) { view.renderChatArea(); }
  1329. var text = 'This is a sent message';
  1330. var textarea = view.el.querySelector('.chat-textarea');
  1331. textarea.value = text;
  1332. view.keyPressed({
  1333. target: textarea,
  1334. preventDefault: _.noop,
  1335. keyCode: 13
  1336. });
  1337. expect(_converse.emit).toHaveBeenCalledWith('messageSend', text);
  1338. var $chat_content = $(view.el).find('.chat-content');
  1339. expect($chat_content.find('.chat-message').length).toBe(1);
  1340. // Let's check that if we receive the same message again, it's
  1341. // not shown.
  1342. var message = $msg({
  1343. from: 'lounge@localhost/dummy',
  1344. to: 'dummy@localhost.com',
  1345. type: 'groupchat',
  1346. id: view.model.messages.at(0).get('msgid')
  1347. }).c('body').t(text);
  1348. view.onChatRoomMessage(message.nodeTree);
  1349. expect($chat_content.find('.chat-message').length).toBe(1);
  1350. expect($chat_content.find('.chat-msg-content').last().text()).toBe(text);
  1351. // We don't emit an event if it's our own message
  1352. expect(_converse.emit.calls.count(), 1);
  1353. done();
  1354. });
  1355. }));
  1356. it("will cause the chat area to be scrolled down only if it was at the bottom already",
  1357. mock.initConverseWithPromises(
  1358. null, ['rosterGroupsFetched'], {},
  1359. function (done, _converse) {
  1360. var message = 'This message is received while the chat area is scrolled up';
  1361. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1362. var view = _converse.chatboxviews.get('lounge@localhost');
  1363. spyOn(view, 'scrollDown').and.callThrough();
  1364. /* Create enough messages so that there's a
  1365. * scrollbar.
  1366. */
  1367. for (var i=0; i<20; i++) {
  1368. view.handleMUCMessage(
  1369. $msg({
  1370. from: 'lounge@localhost/someone',
  1371. to: 'dummy@localhost.com',
  1372. type: 'groupchat',
  1373. id: (new Date()).getTime(),
  1374. }).c('body').t('Message: '+i).tree());
  1375. }
  1376. // Give enough time for `markScrolled` to have been called
  1377. setTimeout(function () {
  1378. view.content.scrollTop = 0;
  1379. view.handleMUCMessage(
  1380. $msg({
  1381. from: 'lounge@localhost/someone',
  1382. to: 'dummy@localhost.com',
  1383. type: 'groupchat',
  1384. id: (new Date()).getTime(),
  1385. }).c('body').t(message).tree());
  1386. // Now check that the message appears inside the chatbox in the DOM
  1387. var $chat_content = $(view.el).find('.chat-content');
  1388. var msg_txt = $chat_content.find('.chat-message:last').find('.chat-msg-content').text();
  1389. expect(msg_txt).toEqual(message);
  1390. expect(view.content.scrollTop).toBe(0);
  1391. done();
  1392. }, 500);
  1393. });
  1394. }));
  1395. it("shows received chatroom subject messages",
  1396. mock.initConverseWithPromises(
  1397. null, ['rosterGroupsFetched'], {},
  1398. function (done, _converse) {
  1399. test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc').then(function () {
  1400. var text = 'Jabber/XMPP Development | RFCs and Extensions: http://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  1401. var stanza = Strophe.xmlHtmlNode(
  1402. '<message xmlns="jabber:client" to="jc@opkode.com/_converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">'+
  1403. ' <subject>'+text+'</subject>'+
  1404. ' <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>'+
  1405. ' <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>'+
  1406. '</message>').firstChild;
  1407. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1408. var view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1409. var chat_content = view.el.querySelector('.chat-content');
  1410. expect($(chat_content).find('.chat-event:last').text()).toBe('Topic set by ralphm');
  1411. expect($(chat_content).find('.chat-topic:last').text()).toBe(text);
  1412. done();
  1413. });
  1414. }));
  1415. it("escapes the subject before rendering it, to avoid JS-injection attacks",
  1416. mock.initConverseWithPromises(
  1417. null, ['rosterGroupsFetched'], {},
  1418. function (done, _converse) {
  1419. test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc').then(function () {
  1420. spyOn(window, 'alert');
  1421. var subject = '<img src="x" onerror="alert(\'XSS\');"/>';
  1422. var view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1423. view.setChatRoomSubject('ralphm', subject);
  1424. var chat_content = view.el.querySelector('.chat-content');
  1425. expect($(chat_content).find('.chat-event:last').text()).toBe('Topic set by ralphm');
  1426. expect($(chat_content).find('.chat-topic:last').text()).toBe(subject);
  1427. done();
  1428. });
  1429. }));
  1430. it("informs users if their nicknames has been changed.",
  1431. mock.initConverseWithPromises(
  1432. null, ['rosterGroupsFetched'], {},
  1433. function (done, _converse) {
  1434. /* The service then sends two presence stanzas to the full JID
  1435. * of each occupant (including the occupant who is changing his
  1436. * or her room nickname), one of type "unavailable" for the old
  1437. * nickname and one indicating availability for the new
  1438. * nickname.
  1439. *
  1440. * See: http://xmpp.org/extensions/xep-0045.html#changenick
  1441. *
  1442. * <presence
  1443. * from='coven@localhost/thirdwitch'
  1444. * id='DC352437-C019-40EC-B590-AF29E879AF98'
  1445. * to='hag66@shakespeare.lit/pda'
  1446. * type='unavailable'>
  1447. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1448. * <item affiliation='member'
  1449. * jid='hag66@shakespeare.lit/pda'
  1450. * nick='oldhag'
  1451. * role='participant'/>
  1452. * <status code='303'/>
  1453. * <status code='110'/>
  1454. * </x>
  1455. * </presence>
  1456. *
  1457. * <presence
  1458. * from='coven@localhost/oldhag'
  1459. * id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
  1460. * to='hag66@shakespeare.lit/pda'>
  1461. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1462. * <item affiliation='member'
  1463. * jid='hag66@shakespeare.lit/pda'
  1464. * role='participant'/>
  1465. * <status code='110'/>
  1466. * </x>
  1467. * </presence>
  1468. */
  1469. var __ = _converse.__;
  1470. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'oldnick').then(function () {
  1471. var view = _converse.chatboxviews.get('lounge@localhost');
  1472. var $chat_content = $(view.el).find('.chat-content');
  1473. // The user has just entered the room and receives their own
  1474. // presence from the server.
  1475. // See example 24:
  1476. // http://xmpp.org/extensions/xep-0045.html#enter-pres
  1477. var presence = $pres({
  1478. to:'dummy@localhost/pda',
  1479. from:'lounge@localhost/oldnick',
  1480. id:'DC352437-C019-40EC-B590-AF29E879AF97'
  1481. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1482. .c('item').attrs({
  1483. affiliation: 'member',
  1484. jid: 'dummy@localhost/pda',
  1485. role: 'participant'
  1486. }).up()
  1487. .c('status').attrs({code:'110'}).up()
  1488. .c('status').attrs({code:'210'}).nodeTree;
  1489. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1490. var $occupants = $(view.el.querySelector('.occupant-list'));
  1491. expect($occupants.children().length).toBe(1);
  1492. expect($occupants.children().first(0).text()).toBe("oldnick");
  1493. expect($chat_content.find('div.chat-info').length).toBe(2);
  1494. expect($chat_content.find('div.chat-info:first').html()).toBe("oldnick has entered the room");
  1495. expect($chat_content.find('div.chat-info:last').html()).toBe(
  1496. __(_converse.muc.new_nickname_messages["210"], "oldnick")
  1497. );
  1498. presence = $pres().attrs({
  1499. from:'lounge@localhost/oldnick',
  1500. id:'DC352437-C019-40EC-B590-AF29E879AF98',
  1501. to:'dummy@localhost/pda',
  1502. type:'unavailable'
  1503. })
  1504. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1505. .c('item').attrs({
  1506. affiliation: 'member',
  1507. jid: 'dummy@localhost/pda',
  1508. nick: 'newnick',
  1509. role: 'participant'
  1510. }).up()
  1511. .c('status').attrs({code:'303'}).up()
  1512. .c('status').attrs({code:'110'}).nodeTree;
  1513. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1514. expect($chat_content.find('div.chat-info').length).toBe(3);
  1515. expect($chat_content.find('div.chat-info').last().html()).toBe(
  1516. __(_converse.muc.new_nickname_messages["303"], "newnick")
  1517. );
  1518. $occupants = $(view.el.querySelector('.occupant-list'));
  1519. expect($occupants.children().length).toBe(0);
  1520. presence = $pres().attrs({
  1521. from:'lounge@localhost/newnick',
  1522. id:'5B4F27A4-25ED-43F7-A699-382C6B4AFC67',
  1523. to:'dummy@localhost/pda'
  1524. })
  1525. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1526. .c('item').attrs({
  1527. affiliation: 'member',
  1528. jid: 'dummy@localhost/pda',
  1529. role: 'participant'
  1530. }).up()
  1531. .c('status').attrs({code:'110'}).nodeTree;
  1532. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1533. expect($chat_content.find('div.chat-info').length).toBe(4);
  1534. expect($chat_content.find('div.chat-info').get(2).textContent).toBe(
  1535. __(_converse.muc.new_nickname_messages["303"], "newnick")
  1536. );
  1537. expect($chat_content.find('div.chat-info').last().html()).toBe(
  1538. "newnick has entered the room");
  1539. $occupants = $(view.el.querySelector('.occupant-list'));
  1540. expect($occupants.children().length).toBe(1);
  1541. expect($occupants.children().first(0).text()).toBe("newnick");
  1542. done();
  1543. });
  1544. }));
  1545. it("queries for the room information before attempting to join the user",
  1546. mock.initConverseWithPromises(
  1547. null, ['rosterGroupsFetched'], {},
  1548. function (done, _converse) {
  1549. var sent_IQ, IQ_id;
  1550. var sendIQ = _converse.connection.sendIQ;
  1551. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1552. sent_IQ = iq;
  1553. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1554. });
  1555. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  1556. // Check that the room queried for the feautures.
  1557. expect(sent_IQ.toLocaleString()).toBe(
  1558. "<iq from='dummy@localhost/resource' to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  1559. "<query xmlns='http://jabber.org/protocol/disco#info'/>"+
  1560. "</iq>");
  1561. /* <iq from='coven@chat.shakespeare.lit'
  1562. * id='ik3vs715'
  1563. * to='hag66@shakespeare.lit/pda'
  1564. * type='result'>
  1565. * <query xmlns='http://jabber.org/protocol/disco#info'>
  1566. * <identity
  1567. * category='conference'
  1568. * name='A Dark Cave'
  1569. * type='text'/>
  1570. * <feature var='http://jabber.org/protocol/muc'/>
  1571. * <feature var='muc_passwordprotected'/>
  1572. * <feature var='muc_hidden'/>
  1573. * <feature var='muc_temporary'/>
  1574. * <feature var='muc_open'/>
  1575. * <feature var='muc_unmoderated'/>
  1576. * <feature var='muc_nonanonymous'/>
  1577. * </query>
  1578. * </iq>
  1579. */
  1580. var features_stanza = $iq({
  1581. from: 'coven@chat.shakespeare.lit',
  1582. 'id': IQ_id,
  1583. 'to': 'dummy@localhost/desktop',
  1584. 'type': 'result'
  1585. })
  1586. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  1587. .c('identity', {
  1588. 'category': 'conference',
  1589. 'name': 'A Dark Cave',
  1590. 'type': 'text'
  1591. }).up()
  1592. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  1593. .c('feature', {'var': 'muc_passwordprotected'}).up()
  1594. .c('feature', {'var': 'muc_hidden'}).up()
  1595. .c('feature', {'var': 'muc_temporary'}).up()
  1596. .c('feature', {'var': 'muc_open'}).up()
  1597. .c('feature', {'var': 'muc_unmoderated'}).up()
  1598. .c('feature', {'var': 'muc_nonanonymous'});
  1599. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1600. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1601. expect(view.model.get('features_fetched')).toBe(true);
  1602. expect(view.model.get('passwordprotected')).toBe(true);
  1603. expect(view.model.get('hidden')).toBe(true);
  1604. expect(view.model.get('temporary')).toBe(true);
  1605. expect(view.model.get('open')).toBe(true);
  1606. expect(view.model.get('unmoderated')).toBe(true);
  1607. expect(view.model.get('nonanonymous')).toBe(true);
  1608. done();
  1609. }));
  1610. it("updates the shown features when the room configuration has changed",
  1611. mock.initConverseWithPromises(
  1612. null, ['rosterGroupsFetched'], {},
  1613. function (done, _converse) {
  1614. var sent_IQ, IQ_id;
  1615. var sendIQ = _converse.connection.sendIQ;
  1616. test_utils.openAndEnterChatRoom(_converse, 'room', 'conference.example.org', 'dummy').then(function () {
  1617. var view = _converse.chatboxviews.get('room@conference.example.org');
  1618. view.model.set({
  1619. 'passwordprotected': false,
  1620. 'unsecured': true,
  1621. 'hidden': false,
  1622. 'public': true,
  1623. 'membersonly': false,
  1624. 'open': true,
  1625. 'persistent': false,
  1626. 'temporary': true,
  1627. 'nonanonymous': true,
  1628. 'semianonymous': false,
  1629. 'moderated': false,
  1630. 'unmoderated': true
  1631. });
  1632. expect(view.model.get('persistent')).toBe(false);
  1633. expect(view.model.get('temporary')).toBe(true);
  1634. view.model.set({'persistent': true});
  1635. expect(view.model.get('persistent')).toBe(true);
  1636. expect(view.model.get('temporary')).toBe(false);
  1637. expect(view.model.get('unsecured')).toBe(true);
  1638. expect(view.model.get('passwordprotected')).toBe(false);
  1639. view.model.set({'passwordprotected': true});
  1640. expect(view.model.get('unsecured')).toBe(false);
  1641. expect(view.model.get('passwordprotected')).toBe(true);
  1642. expect(view.model.get('unmoderated')).toBe(true);
  1643. expect(view.model.get('moderated')).toBe(false);
  1644. view.model.set({'moderated': true});
  1645. expect(view.model.get('unmoderated')).toBe(false);
  1646. expect(view.model.get('moderated')).toBe(true);
  1647. expect(view.model.get('nonanonymous')).toBe(true);
  1648. expect(view.model.get('semianonymous')).toBe(false);
  1649. view.model.set({'nonanonymous': false});
  1650. expect(view.model.get('nonanonymous')).toBe(false);
  1651. expect(view.model.get('semianonymous')).toBe(true);
  1652. expect(view.model.get('open')).toBe(true);
  1653. expect(view.model.get('membersonly')).toBe(false);
  1654. view.model.set({'membersonly': true});
  1655. expect(view.model.get('open')).toBe(false);
  1656. expect(view.model.get('membersonly')).toBe(true);
  1657. done();
  1658. });
  1659. }));
  1660. it("indicates when a room is no longer anonymous",
  1661. mock.initConverseWithPromises(
  1662. null, ['rosterGroupsFetched'], {},
  1663. function (done, _converse) {
  1664. var sent_IQ, IQ_id;
  1665. var sendIQ = _converse.connection.sendIQ;
  1666. test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'some1').then(function () {
  1667. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1668. sent_IQ = iq;
  1669. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1670. });
  1671. // We pretend this is a new room, so no disco info is returned.
  1672. var features_stanza = $iq({
  1673. from: 'coven@chat.shakespeare.lit',
  1674. 'id': IQ_id,
  1675. 'to': 'dummy@localhost/desktop',
  1676. 'type': 'error'
  1677. }).c('error', {'type': 'cancel'})
  1678. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1679. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1680. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1681. /* <message xmlns="jabber:client"
  1682. * type="groupchat"
  1683. * to="dummy@localhost/_converse.js-27854181"
  1684. * from="coven@chat.shakespeare.lit">
  1685. * <x xmlns="http://jabber.org/protocol/muc#user">
  1686. * <status code="104"/>
  1687. * <status code="172"/>
  1688. * </x>
  1689. * </message>
  1690. */
  1691. var message = $msg({
  1692. type:'groupchat',
  1693. to: 'dummy@localhost/_converse.js-27854181',
  1694. from: 'coven@chat.shakespeare.lit'
  1695. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  1696. .c('status', {code: '104'}).up()
  1697. .c('status', {code: '172'});
  1698. _converse.connection._dataRecv(test_utils.createRequest(message));
  1699. var $chat_body = $(view.el.querySelector('.chatroom-body'));
  1700. expect($chat_body.find('.message:last').text()).toBe('This room is now no longer anonymous');
  1701. done();
  1702. });
  1703. }));
  1704. it("informs users if they have been kicked out of the chat room",
  1705. mock.initConverseWithPromises(
  1706. null, ['rosterGroupsFetched'], {},
  1707. function (done, _converse) {
  1708. /* <presence
  1709. * from='harfleur@chat.shakespeare.lit/pistol'
  1710. * to='pistol@shakespeare.lit/harfleur'
  1711. * type='unavailable'>
  1712. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1713. * <item affiliation='none' role='none'>
  1714. * <actor nick='Fluellen'/>
  1715. * <reason>Avaunt, you cullion!</reason>
  1716. * </item>
  1717. * <status code='110'/>
  1718. * <status code='307'/>
  1719. * </x>
  1720. * </presence>
  1721. */
  1722. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1723. var presence = $pres().attrs({
  1724. from:'lounge@localhost/dummy',
  1725. to:'dummy@localhost/pda',
  1726. type:'unavailable'
  1727. })
  1728. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1729. .c('item').attrs({
  1730. affiliation: 'none',
  1731. jid: 'dummy@localhost/pda',
  1732. role: 'none'
  1733. })
  1734. .c('actor').attrs({nick: 'Fluellen'}).up()
  1735. .c('reason').t('Avaunt, you cullion!').up()
  1736. .up()
  1737. .c('status').attrs({code:'110'}).up()
  1738. .c('status').attrs({code:'307'}).nodeTree;
  1739. var view = _converse.chatboxviews.get('lounge@localhost');
  1740. view.onChatRoomPresence(presence);
  1741. expect($(view.el.querySelector('.chat-area')).is(':visible')).toBeFalsy();
  1742. expect($(view.el.querySelector('.occupants')).is(':visible')).toBeFalsy();
  1743. var $chat_body = $(view.el.querySelector('.chatroom-body'));
  1744. expect($chat_body.find('.disconnect-msg').text()).toBe(
  1745. 'You have been kicked from this room'+
  1746. 'This action was done by Fluellen.'+
  1747. 'The reason given is: "Avaunt, you cullion!".'
  1748. );
  1749. done();
  1750. });
  1751. }));
  1752. it("can be saved to, and retrieved from, browserStorage",
  1753. mock.initConverseWithPromises(
  1754. null, ['rosterGroupsFetched'], {},
  1755. function (done, _converse) {
  1756. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1757. // We instantiate a new ChatBoxes collection, which by default
  1758. // will be empty.
  1759. test_utils.openControlBox();
  1760. var newchatboxes = new _converse.ChatBoxes();
  1761. expect(newchatboxes.length).toEqual(0);
  1762. // The chatboxes will then be fetched from browserStorage inside the
  1763. // onConnected method
  1764. newchatboxes.onConnected();
  1765. expect(newchatboxes.length).toEqual(2);
  1766. // Check that the chatrooms retrieved from browserStorage
  1767. // have the same attributes values as the original ones.
  1768. var attrs = ['id', 'box_id', 'visible'];
  1769. var new_attrs, old_attrs;
  1770. for (var i=0; i<attrs.length; i++) {
  1771. new_attrs = _.map(_.map(newchatboxes.models, 'attributes'), attrs[i]);
  1772. old_attrs = _.map(_.map(_converse.chatboxes.models, 'attributes'), attrs[i]);
  1773. // FIXME: should have have to sort here? Order must
  1774. // probably be the same...
  1775. // This should be fixed once the controlbox always opens
  1776. // only on the right.
  1777. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  1778. }
  1779. _converse.rosterview.render();
  1780. done();
  1781. }));
  1782. it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'",
  1783. mock.initConverseWithPromises(
  1784. null, ['rosterGroupsFetched'], {},
  1785. function (done, _converse) {
  1786. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1787. var view = _converse.chatboxviews.get('lounge@localhost'),
  1788. trimmed_chatboxes = _converse.minimized_chats;
  1789. spyOn(view, 'minimize').and.callThrough();
  1790. spyOn(view, 'maximize').and.callThrough();
  1791. spyOn(_converse, 'emit');
  1792. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1793. view.el.querySelector('.toggle-chatbox-button').click();
  1794. expect(view.minimize).toHaveBeenCalled();
  1795. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMinimized', jasmine.any(Object));
  1796. expect(u.isVisible(view.el)).toBeFalsy();
  1797. expect(view.model.get('minimized')).toBeTruthy();
  1798. expect(view.minimize).toHaveBeenCalled();
  1799. var trimmedview = trimmed_chatboxes.get(view.model.get('id'));
  1800. trimmedview.el.querySelector("a.restore-chat").click();
  1801. expect(view.maximize).toHaveBeenCalled();
  1802. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMaximized', jasmine.any(Object));
  1803. expect(view.model.get('minimized')).toBeFalsy();
  1804. expect(_converse.emit.calls.count(), 3);
  1805. done();
  1806. }));
  1807. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'",
  1808. mock.initConverseWithPromises(
  1809. null, ['rosterGroupsFetched'], {},
  1810. function (done, _converse) {
  1811. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1812. var view = _converse.chatboxviews.get('lounge@localhost');
  1813. spyOn(view, 'close').and.callThrough();
  1814. spyOn(_converse, 'emit');
  1815. spyOn(view, 'leave');
  1816. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1817. view.el.querySelector('.close-chatbox-button').click();
  1818. expect(view.close).toHaveBeenCalled();
  1819. expect(view.leave).toHaveBeenCalled();
  1820. // XXX: After refactoring, the chat box only gets closed
  1821. // once we have confirmation from the server. To test this,
  1822. // we would have to mock the returned presence stanza.
  1823. // See the "leave" method on the ChatRoomView.
  1824. // expect(_converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  1825. done();
  1826. }));
  1827. });
  1828. describe("Each chat room can take special commands", function () {
  1829. it("/help to show the available commands",
  1830. mock.initConverseWithPromises(
  1831. null, ['rosterGroupsFetched'], {},
  1832. function (done, _converse) {
  1833. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1834. var view = _converse.chatboxviews.get('lounge@localhost');
  1835. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1836. var textarea = view.el.querySelector('.chat-textarea');
  1837. textarea.value = '/help This is the room subject';
  1838. view.keyPressed({
  1839. target: textarea,
  1840. preventDefault: _.noop,
  1841. keyCode: 13
  1842. });
  1843. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1844. const info_messages = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info:not(.chat-date)'), 0);
  1845. expect(info_messages.length).toBe(17);
  1846. expect(info_messages.pop().textContent).toBe('/voice: Allow muted user to post messages');
  1847. expect(info_messages.pop().textContent).toBe('/topic: Set room subject (alias for /subject)');
  1848. expect(info_messages.pop().textContent).toBe('/subject: Set room subject');
  1849. expect(info_messages.pop().textContent).toBe('/revoke: Revoke user\'s membership');
  1850. expect(info_messages.pop().textContent).toBe('/owner: Grant ownership of this room');
  1851. expect(info_messages.pop().textContent).toBe('/op: Grant moderator role to user');
  1852. expect(info_messages.pop().textContent).toBe('/nick: Change your nickname');
  1853. expect(info_messages.pop().textContent).toBe('/mute: Remove user\'s ability to post messages');
  1854. expect(info_messages.pop().textContent).toBe('/member: Grant membership to a user');
  1855. expect(info_messages.pop().textContent).toBe('/me: Write in 3rd person');
  1856. expect(info_messages.pop().textContent).toBe('/kick: Kick user from room');
  1857. expect(info_messages.pop().textContent).toBe('/help: Show this menu');
  1858. expect(info_messages.pop().textContent).toBe('/deop: Change user role to participant');
  1859. expect(info_messages.pop().textContent).toBe('/clear: Remove messages');
  1860. expect(info_messages.pop().textContent).toBe('/ban: Ban user from room');
  1861. expect(info_messages.pop().textContent).toBe('/admin: Change user\'s affiliation to admin');
  1862. done();
  1863. });
  1864. }));
  1865. it("/topic to set the room topic",
  1866. mock.initConverseWithPromises(
  1867. null, ['rosterGroupsFetched'], {},
  1868. function (done, _converse) {
  1869. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1870. var sent_stanza;
  1871. var view = _converse.chatboxviews.get('lounge@localhost');
  1872. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1873. spyOn(view, 'clearChatRoomMessages');
  1874. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  1875. sent_stanza = stanza;
  1876. });
  1877. // Check the alias /topic
  1878. var textarea = view.el.querySelector('.chat-textarea');
  1879. textarea.value = '/topic This is the room subject';
  1880. view.keyPressed({
  1881. target: textarea,
  1882. preventDefault: _.noop,
  1883. keyCode: 13
  1884. });
  1885. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1886. expect(_converse.connection.send).toHaveBeenCalled();
  1887. expect(sent_stanza.textContent).toBe('This is the room subject');
  1888. // Check /subject
  1889. textarea.value = '/subject This is a new subject';
  1890. view.keyPressed({
  1891. target: textarea,
  1892. preventDefault: _.noop,
  1893. keyCode: 13
  1894. });
  1895. expect(sent_stanza.textContent).toBe('This is a new subject');
  1896. expect(sent_stanza.outerHTML).toBe(
  1897. '<message to="lounge@localhost" from="dummy@localhost/resource" type="groupchat" xmlns="jabber:client">'+
  1898. '<subject xmlns="jabber:client">This is a new subject</subject>'+
  1899. '</message>');
  1900. // Check case insensitivity
  1901. textarea.value = '/Subject This is yet another subject';
  1902. view.keyPressed({
  1903. target: textarea,
  1904. preventDefault: _.noop,
  1905. keyCode: 13
  1906. });
  1907. expect(sent_stanza.textContent).toBe('This is yet another subject');
  1908. expect(sent_stanza.outerHTML).toBe(
  1909. '<message to="lounge@localhost" from="dummy@localhost/resource" type="groupchat" xmlns="jabber:client">'+
  1910. '<subject xmlns="jabber:client">This is yet another subject</subject>'+
  1911. '</message>');
  1912. done();
  1913. });
  1914. }));
  1915. it("/clear to clear messages",
  1916. mock.initConverseWithPromises(
  1917. null, ['rosterGroupsFetched'], {},
  1918. function (done, _converse) {
  1919. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1920. var view = _converse.chatboxviews.get('lounge@localhost');
  1921. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1922. spyOn(view, 'clearChatRoomMessages');
  1923. var textarea = view.el.querySelector('.chat-textarea')
  1924. textarea.value = '/clear';
  1925. view.keyPressed({
  1926. target: textarea,
  1927. preventDefault: _.noop,
  1928. keyCode: 13
  1929. });
  1930. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1931. expect(view.clearChatRoomMessages).toHaveBeenCalled();
  1932. done();
  1933. });
  1934. }));
  1935. it("/owner to make a user an owner",
  1936. mock.initConverseWithPromises(
  1937. null, ['rosterGroupsFetched'], {},
  1938. function (done, _converse) {
  1939. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1940. var sent_IQ, IQ_id;
  1941. var sendIQ = _converse.connection.sendIQ;
  1942. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1943. sent_IQ = iq;
  1944. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1945. });
  1946. var view = _converse.chatboxviews.get('lounge@localhost');
  1947. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1948. spyOn(view, 'setAffiliation').and.callThrough();
  1949. spyOn(view, 'showStatusNotification').and.callThrough();
  1950. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  1951. var textarea = view.el.querySelector('.chat-textarea')
  1952. textarea.value = '/owner';
  1953. view.keyPressed({
  1954. target: textarea,
  1955. preventDefault: _.noop,
  1956. keyCode: 13
  1957. });
  1958. expect(view.onMessageSubmitted).toHaveBeenCalled();
  1959. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  1960. expect(view.showStatusNotification).toHaveBeenCalledWith(
  1961. "Error: the \"owner\" command takes two arguments, the user's nickname and optionally a reason.",
  1962. true
  1963. );
  1964. expect(view.setAffiliation).not.toHaveBeenCalled();
  1965. // Call now with the correct amount of arguments.
  1966. // XXX: Calling onMessageSubmitted directly, trying
  1967. // again via triggering Event doesn't work for some weird
  1968. // reason.
  1969. view.onMessageSubmitted('/owner annoyingGuy@localhost You\'re responsible');
  1970. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  1971. expect(view.showStatusNotification.calls.count()).toBe(1);
  1972. expect(view.setAffiliation).toHaveBeenCalled();
  1973. // Check that the member list now gets updated
  1974. expect(sent_IQ.toLocaleString()).toBe(
  1975. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  1976. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  1977. "<item affiliation='owner' jid='annoyingGuy@localhost'>"+
  1978. "<reason>You&apos;re responsible</reason>"+
  1979. "</item>"+
  1980. "</query>"+
  1981. "</iq>");
  1982. done();
  1983. });
  1984. }));
  1985. it("/ban to ban a user",
  1986. mock.initConverseWithPromises(
  1987. null, ['rosterGroupsFetched'], {},
  1988. function (done, _converse) {
  1989. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1990. var sent_IQ, IQ_id;
  1991. var sendIQ = _converse.connection.sendIQ;
  1992. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1993. sent_IQ = iq;
  1994. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1995. });
  1996. var view = _converse.chatboxviews.get('lounge@localhost');
  1997. spyOn(view, 'onMessageSubmitted').and.callThrough();
  1998. spyOn(view, 'setAffiliation').and.callThrough();
  1999. spyOn(view, 'showStatusNotification').and.callThrough();
  2000. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2001. var textarea = view.el.querySelector('.chat-textarea')
  2002. textarea.value = '/ban';
  2003. view.keyPressed({
  2004. target: textarea,
  2005. preventDefault: _.noop,
  2006. keyCode: 13
  2007. });
  2008. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2009. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2010. expect(view.showStatusNotification).toHaveBeenCalledWith(
  2011. "Error: the \"ban\" command takes two arguments, the user's nickname and optionally a reason.",
  2012. true
  2013. );
  2014. expect(view.setAffiliation).not.toHaveBeenCalled();
  2015. // Call now with the correct amount of arguments.
  2016. // XXX: Calling onMessageSubmitted directly, trying
  2017. // again via triggering Event doesn't work for some weird
  2018. // reason.
  2019. view.onMessageSubmitted('/ban annoyingGuy@localhost You\'re annoying');
  2020. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2021. expect(view.showStatusNotification.calls.count()).toBe(1);
  2022. expect(view.setAffiliation).toHaveBeenCalled();
  2023. // Check that the member list now gets updated
  2024. expect(sent_IQ.toLocaleString()).toBe(
  2025. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2026. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2027. "<item affiliation='outcast' jid='annoyingGuy@localhost'>"+
  2028. "<reason>You&apos;re annoying</reason>"+
  2029. "</item>"+
  2030. "</query>"+
  2031. "</iq>");
  2032. done();
  2033. });
  2034. }));
  2035. it("/kick to kick a user",
  2036. mock.initConverseWithPromises(
  2037. null, ['rosterGroupsFetched'], {},
  2038. function (done, _converse) {
  2039. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2040. var sent_IQ, IQ_id;
  2041. var sendIQ = _converse.connection.sendIQ;
  2042. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2043. sent_IQ = iq;
  2044. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2045. });
  2046. var view = _converse.chatboxviews.get('lounge@localhost');
  2047. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2048. spyOn(view, 'modifyRole').and.callThrough();
  2049. spyOn(view, 'showStatusNotification').and.callThrough();
  2050. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2051. var textarea = view.el.querySelector('.chat-textarea')
  2052. textarea.value = '/kick';
  2053. view.keyPressed({
  2054. target: textarea,
  2055. preventDefault: _.noop,
  2056. keyCode: 13
  2057. });
  2058. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2059. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2060. expect(view.showStatusNotification).toHaveBeenCalledWith(
  2061. "Error: the \"kick\" command takes two arguments, the user's nickname and optionally a reason.",
  2062. true
  2063. );
  2064. expect(view.modifyRole).not.toHaveBeenCalled();
  2065. // Call now with the correct amount of arguments.
  2066. // XXX: Calling onMessageSubmitted directly, trying
  2067. // again via triggering Event doesn't work for some weird
  2068. // reason.
  2069. view.onMessageSubmitted('/kick annoyingGuy You\'re annoying');
  2070. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2071. expect(view.showStatusNotification.calls.count()).toBe(1);
  2072. expect(view.modifyRole).toHaveBeenCalled();
  2073. expect(sent_IQ.toLocaleString()).toBe(
  2074. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2075. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2076. "<item nick='annoyingGuy' role='none'>"+
  2077. "<reason>You&apos;re annoying</reason>"+
  2078. "</item>"+
  2079. "</query>"+
  2080. "</iq>");
  2081. /* <presence
  2082. * from='harfleur@chat.shakespeare.lit/pistol'
  2083. * to='gower@shakespeare.lit/cell'
  2084. * type='unavailable'>
  2085. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2086. * <item affiliation='none' role='none'/>
  2087. * <status code='307'/>
  2088. * </x>
  2089. * </presence>
  2090. */
  2091. var presence = $pres({
  2092. 'from': 'lounge@localhost/annoyingGuy',
  2093. 'to': 'dummy@localhost/desktop',
  2094. 'type': 'unavailable'
  2095. })
  2096. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2097. .c('item', {
  2098. 'affiliation': 'none',
  2099. 'role': 'none'
  2100. }).up()
  2101. .c('status', {'code': '307'});
  2102. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2103. expect(
  2104. view.el.querySelectorAll('.chat-info')[3].textContent).toBe(
  2105. "annoyingGuy has been kicked out");
  2106. done();
  2107. });
  2108. }));
  2109. it("/op and /deop to make a user a moderator or not",
  2110. mock.initConverseWithPromises(
  2111. null, ['rosterGroupsFetched'], {},
  2112. function (done, _converse) {
  2113. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2114. var sent_IQ, IQ_id;
  2115. var sendIQ = _converse.connection.sendIQ;
  2116. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2117. sent_IQ = iq;
  2118. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2119. });
  2120. var view = _converse.chatboxviews.get('lounge@localhost');
  2121. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2122. spyOn(view, 'modifyRole').and.callThrough();
  2123. spyOn(view, 'showStatusNotification').and.callThrough();
  2124. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2125. // New user enters the room
  2126. /* <presence
  2127. * from='coven@chat.shakespeare.lit/thirdwitch'
  2128. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  2129. * to='crone1@shakespeare.lit/desktop'>
  2130. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2131. * <item affiliation='member' role='moderator'/>
  2132. * </x>
  2133. * </presence>
  2134. */
  2135. var presence = $pres({
  2136. 'from': 'lounge@localhost/trustworthyguy',
  2137. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2138. 'to': 'dummy@localhost/desktop'
  2139. })
  2140. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2141. .c('item', {
  2142. 'jid': 'trustworthyguy@localhost',
  2143. 'affiliation': 'member',
  2144. 'role': 'participant'
  2145. });
  2146. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2147. var info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2148. expect(info_msgs.pop().textContent).toBe("trustworthyguy has entered the room");
  2149. var textarea = view.el.querySelector('.chat-textarea')
  2150. textarea.value = '/op';
  2151. view.keyPressed({
  2152. target: textarea,
  2153. preventDefault: _.noop,
  2154. keyCode: 13
  2155. });
  2156. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2157. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2158. expect(view.showStatusNotification).toHaveBeenCalledWith(
  2159. "Error: the \"op\" command takes two arguments, the user's nickname and optionally a reason.",
  2160. true
  2161. );
  2162. expect(view.modifyRole).not.toHaveBeenCalled();
  2163. // Call now with the correct amount of arguments.
  2164. // XXX: Calling onMessageSubmitted directly, trying
  2165. // again via triggering Event doesn't work for some weird
  2166. // reason.
  2167. view.onMessageSubmitted('/op trustworthyguy You\'re trustworthy');
  2168. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2169. expect(view.showStatusNotification.calls.count()).toBe(1);
  2170. expect(view.modifyRole).toHaveBeenCalled();
  2171. expect(sent_IQ.toLocaleString()).toBe(
  2172. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2173. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2174. "<item nick='trustworthyguy' role='moderator'>"+
  2175. "<reason>You&apos;re trustworthy</reason>"+
  2176. "</item>"+
  2177. "</query>"+
  2178. "</iq>");
  2179. /* <presence
  2180. * from='coven@chat.shakespeare.lit/thirdwitch'
  2181. * to='crone1@shakespeare.lit/desktop'>
  2182. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2183. * <item affiliation='member'
  2184. * jid='hag66@shakespeare.lit/pda'
  2185. * role='moderator'/>
  2186. * </x>
  2187. * </presence>
  2188. */
  2189. presence = $pres({
  2190. 'from': 'lounge@localhost/trustworthyguy',
  2191. 'to': 'dummy@localhost/desktop'
  2192. })
  2193. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2194. .c('item', {
  2195. 'jid': 'trustworthyguy@localhost',
  2196. 'affiliation': 'member',
  2197. 'role': 'moderator'
  2198. });
  2199. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2200. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2201. expect(info_msgs.pop().textContent).toBe("trustworthyguy is now a moderator.");
  2202. view.onMessageSubmitted('/deop trustworthyguy Perhaps not');
  2203. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2204. expect(view.showStatusNotification.calls.count()).toBe(2);
  2205. expect(view.modifyRole).toHaveBeenCalled();
  2206. expect(sent_IQ.toLocaleString()).toBe(
  2207. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2208. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2209. "<item nick='trustworthyguy' role='participant'>"+
  2210. "<reason>Perhaps not</reason>"+
  2211. "</item>"+
  2212. "</query>"+
  2213. "</iq>");
  2214. /* <presence
  2215. * from='coven@chat.shakespeare.lit/thirdwitch'
  2216. * to='crone1@shakespeare.lit/desktop'>
  2217. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2218. * <item affiliation='member'
  2219. * jid='hag66@shakespeare.lit/pda'
  2220. * role='participant'/>
  2221. * </x>
  2222. * </presence>
  2223. */
  2224. presence = $pres({
  2225. 'from': 'lounge@localhost/trustworthyguy',
  2226. 'to': 'dummy@localhost/desktop'
  2227. }).c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2228. .c('item', {
  2229. 'jid': 'trustworthyguy@localhost',
  2230. 'affiliation': 'member',
  2231. 'role': 'participant'
  2232. });
  2233. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2234. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2235. expect(info_msgs.pop().textContent).toBe("trustworthyguy is no longer a moderator.");
  2236. done();
  2237. });
  2238. }));
  2239. it("/mute and /voice to mute and unmute a user",
  2240. mock.initConverseWithPromises(
  2241. null, ['rosterGroupsFetched'], {},
  2242. function (done, _converse) {
  2243. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2244. var sent_IQ, IQ_id;
  2245. var sendIQ = _converse.connection.sendIQ;
  2246. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2247. sent_IQ = iq;
  2248. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2249. });
  2250. var view = _converse.chatboxviews.get('lounge@localhost');
  2251. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2252. spyOn(view, 'modifyRole').and.callThrough();
  2253. spyOn(view, 'showStatusNotification').and.callThrough();
  2254. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2255. // New user enters the room
  2256. /* <presence
  2257. * from='coven@chat.shakespeare.lit/thirdwitch'
  2258. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  2259. * to='crone1@shakespeare.lit/desktop'>
  2260. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2261. * <item affiliation='member' role='participant'/>
  2262. * </x>
  2263. * </presence>
  2264. */
  2265. var presence = $pres({
  2266. 'from': 'lounge@localhost/annoyingGuy',
  2267. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2268. 'to': 'dummy@localhost/desktop'
  2269. })
  2270. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2271. .c('item', {
  2272. 'jid': 'annoyingguy@localhost',
  2273. 'affiliation': 'member',
  2274. 'role': 'participant'
  2275. });
  2276. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2277. var info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2278. expect(info_msgs.pop().textContent).toBe("annoyingGuy has entered the room");
  2279. var textarea = view.el.querySelector('.chat-textarea')
  2280. textarea.value = '/mute';
  2281. view.keyPressed({
  2282. target: textarea,
  2283. preventDefault: _.noop,
  2284. keyCode: 13
  2285. });
  2286. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2287. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2288. expect(view.showStatusNotification).toHaveBeenCalledWith(
  2289. "Error: the \"mute\" command takes two arguments, the user's nickname and optionally a reason.",
  2290. true
  2291. );
  2292. expect(view.modifyRole).not.toHaveBeenCalled();
  2293. // Call now with the correct amount of arguments.
  2294. // XXX: Calling onMessageSubmitted directly, trying
  2295. // again via triggering Event doesn't work for some weird
  2296. // reason.
  2297. view.onMessageSubmitted('/mute annoyingGuy You\'re annoying');
  2298. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2299. expect(view.showStatusNotification.calls.count()).toBe(1);
  2300. expect(view.modifyRole).toHaveBeenCalled();
  2301. expect(sent_IQ.toLocaleString()).toBe(
  2302. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2303. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2304. "<item nick='annoyingGuy' role='visitor'>"+
  2305. "<reason>You&apos;re annoying</reason>"+
  2306. "</item>"+
  2307. "</query>"+
  2308. "</iq>");
  2309. /* <presence
  2310. * from='coven@chat.shakespeare.lit/thirdwitch'
  2311. * to='crone1@shakespeare.lit/desktop'>
  2312. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2313. * <item affiliation='member'
  2314. * jid='hag66@shakespeare.lit/pda'
  2315. * role='visitor'/>
  2316. * </x>
  2317. * </presence>
  2318. */
  2319. presence = $pres({
  2320. 'from': 'lounge@localhost/annoyingGuy',
  2321. 'to': 'dummy@localhost/desktop'
  2322. })
  2323. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2324. .c('item', {
  2325. 'jid': 'annoyingguy@localhost',
  2326. 'affiliation': 'member',
  2327. 'role': 'visitor'
  2328. });
  2329. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2330. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2331. expect(info_msgs.pop().textContent).toBe("annoyingGuy has been muted.");
  2332. view.onMessageSubmitted('/voice annoyingGuy Now you can talk again');
  2333. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2334. expect(view.showStatusNotification.calls.count()).toBe(2);
  2335. expect(view.modifyRole).toHaveBeenCalled();
  2336. expect(sent_IQ.toLocaleString()).toBe(
  2337. "<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2338. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2339. "<item nick='annoyingGuy' role='participant'>"+
  2340. "<reason>Now you can talk again</reason>"+
  2341. "</item>"+
  2342. "</query>"+
  2343. "</iq>");
  2344. /* <presence
  2345. * from='coven@chat.shakespeare.lit/thirdwitch'
  2346. * to='crone1@shakespeare.lit/desktop'>
  2347. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2348. * <item affiliation='member'
  2349. * jid='hag66@shakespeare.lit/pda'
  2350. * role='visitor'/>
  2351. * </x>
  2352. * </presence>
  2353. */
  2354. presence = $pres({
  2355. 'from': 'lounge@localhost/annoyingGuy',
  2356. 'to': 'dummy@localhost/desktop'
  2357. })
  2358. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2359. .c('item', {
  2360. 'jid': 'annoyingguy@localhost',
  2361. 'affiliation': 'member',
  2362. 'role': 'participant'
  2363. });
  2364. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2365. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2366. expect(info_msgs.pop().textContent).toBe("annoyingGuy has been given a voice again.");
  2367. done();
  2368. });
  2369. }));
  2370. });
  2371. describe("When attempting to enter a chatroom", function () {
  2372. it("will show an error message if the room requires a password",
  2373. mock.initConverseWithPromises(
  2374. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  2375. function (done, _converse) {
  2376. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2377. .then(function () {
  2378. var presence = $pres().attrs({
  2379. from:'lounge@localhost/thirdwitch',
  2380. id:'n13mt3l',
  2381. to:'dummy@localhost/pda',
  2382. type:'error'})
  2383. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2384. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  2385. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2386. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2387. spyOn(view, 'renderPasswordForm').and.callThrough();
  2388. view.onChatRoomPresence(presence);
  2389. var $chat_body = $(view.el).find('.chatroom-body');
  2390. expect(view.renderPasswordForm).toHaveBeenCalled();
  2391. expect($chat_body.find('form.chatroom-form').length).toBe(1);
  2392. expect($chat_body.find('legend').text()).toBe('This chatroom requires a password');
  2393. // Let's submit the form
  2394. spyOn(view, 'join');
  2395. var input_el = view.el.querySelector('[name="password"]');
  2396. input_el.value = 'secret';
  2397. view.el.querySelector('input[type=submit]').click();
  2398. expect(view.join).toHaveBeenCalledWith('dummy', 'secret');
  2399. done();
  2400. });
  2401. }));
  2402. it("will show an error message if the room is members-only and the user not included",
  2403. mock.initConverseWithPromises(
  2404. null, ['rosterGroupsFetched'], {},
  2405. function (done, _converse) {
  2406. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2407. .then(function () {
  2408. var presence = $pres().attrs({
  2409. from:'lounge@localhost/thirdwitch',
  2410. id:'n13mt3l',
  2411. to:'dummy@localhost/pda',
  2412. type:'error'})
  2413. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2414. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  2415. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2416. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2417. spyOn(view, 'showErrorMessage').and.callThrough();
  2418. view.onChatRoomPresence(presence);
  2419. expect($(view.el).find('.chatroom-body p:last').text()).toBe('You are not on the member list of this room.');
  2420. done();
  2421. });
  2422. }));
  2423. it("will show an error message if the user has been banned",
  2424. mock.initConverseWithPromises(
  2425. null, ['rosterGroupsFetched'], {},
  2426. function (done, _converse) {
  2427. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2428. .then(function () {
  2429. var presence = $pres().attrs({
  2430. from:'lounge@localhost/thirdwitch',
  2431. id:'n13mt3l',
  2432. to:'dummy@localhost/pda',
  2433. type:'error'})
  2434. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2435. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  2436. .c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2437. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2438. spyOn(view, 'showErrorMessage').and.callThrough();
  2439. view.onChatRoomPresence(presence);
  2440. expect($(view.el).find('.chatroom-body p:last').text()).toBe('You have been banned from this room.');
  2441. done();
  2442. });
  2443. }));
  2444. it("will render a nickname form if a nickname conflict happens and muc_nickname_from_jid=false",
  2445. mock.initConverseWithPromises(
  2446. null, ['rosterGroupsFetched'], {},
  2447. function (done, _converse) {
  2448. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2449. .then(function () {
  2450. var presence = $pres().attrs({
  2451. from:'lounge@localhost/thirdwitch',
  2452. id:'n13mt3l',
  2453. to:'dummy@localhost/pda',
  2454. type:'error'})
  2455. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2456. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2457. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2458. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2459. spyOn(view, 'showErrorMessage').and.callThrough();
  2460. view.onChatRoomPresence(presence);
  2461. expect($(view.el).find('.chatroom-body form.chatroom-form label:first').text()).toBe('Please choose your nickname');
  2462. var $input = $(view.el).find('.chatroom-body form.chatroom-form input:first');
  2463. $input.val('nicky');
  2464. view.el.querySelector('input[type=submit]').click();
  2465. done();
  2466. });
  2467. }));
  2468. it("will automatically choose a new nickname if a nickname conflict happens and muc_nickname_from_jid=true",
  2469. mock.initConverseWithPromises(
  2470. null, ['rosterGroupsFetched'], {},
  2471. function (done, _converse) {
  2472. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2473. .then(function () {
  2474. /* <presence
  2475. * from='coven@chat.shakespeare.lit/thirdwitch'
  2476. * id='n13mt3l'
  2477. * to='hag66@shakespeare.lit/pda'
  2478. * type='error'>
  2479. * <x xmlns='http://jabber.org/protocol/muc'/>
  2480. * <error by='coven@chat.shakespeare.lit' type='cancel'>
  2481. * <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  2482. * </error>
  2483. * </presence>
  2484. */
  2485. _converse.muc_nickname_from_jid = true;
  2486. var attrs = {
  2487. from:'lounge@localhost/dummy',
  2488. id:'n13mt3l',
  2489. to:'dummy@localhost/pda',
  2490. type:'error'
  2491. };
  2492. var presence = $pres().attrs(attrs)
  2493. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2494. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2495. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2496. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2497. spyOn(view, 'showErrorMessage').and.callThrough();
  2498. spyOn(view, 'join').and.callThrough();
  2499. // Simulate repeatedly that there's already someone in the room
  2500. // with that nickname
  2501. view.onChatRoomPresence(presence);
  2502. expect(view.join).toHaveBeenCalledWith('dummy-2');
  2503. attrs.from = 'lounge@localhost/dummy-2';
  2504. presence = $pres().attrs(attrs)
  2505. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2506. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2507. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2508. view.onChatRoomPresence(presence);
  2509. expect(view.join).toHaveBeenCalledWith('dummy-3');
  2510. attrs.from = 'lounge@localhost/dummy-3';
  2511. presence = $pres().attrs(attrs)
  2512. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2513. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2514. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2515. view.onChatRoomPresence(presence);
  2516. expect(view.join).toHaveBeenCalledWith('dummy-4');
  2517. done();
  2518. });
  2519. }));
  2520. it("will show an error message if the user is not allowed to have created the room",
  2521. mock.initConverseWithPromises(
  2522. null, ['rosterGroupsFetched'], {},
  2523. function (done, _converse) {
  2524. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2525. .then(function () {
  2526. var presence = $pres().attrs({
  2527. from:'lounge@localhost/thirdwitch',
  2528. id:'n13mt3l',
  2529. to:'dummy@localhost/pda',
  2530. type:'error'})
  2531. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2532. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2533. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2534. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2535. spyOn(view, 'showErrorMessage').and.callThrough();
  2536. view.onChatRoomPresence(presence);
  2537. expect($(view.el).find('.chatroom-body p:last').text()).toBe('You are not allowed to create new rooms.');
  2538. done();
  2539. });
  2540. }));
  2541. it("will show an error message if the user's nickname doesn't conform to room policy",
  2542. mock.initConverseWithPromises(
  2543. null, ['rosterGroupsFetched'], {},
  2544. function (done, _converse) {
  2545. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2546. .then(function () {
  2547. var presence = $pres().attrs({
  2548. from:'lounge@localhost/thirdwitch',
  2549. id:'n13mt3l',
  2550. to:'dummy@localhost/pda',
  2551. type:'error'})
  2552. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2553. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2554. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2555. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2556. spyOn(view, 'showErrorMessage').and.callThrough();
  2557. view.onChatRoomPresence(presence);
  2558. expect($(view.el).find('.chatroom-body p:last').text()).toBe("Your nickname doesn't conform to this room's policies.");
  2559. done();
  2560. });
  2561. }));
  2562. it("will show an error message if the room doesn't yet exist",
  2563. mock.initConverseWithPromises(
  2564. null, ['rosterGroupsFetched'], {},
  2565. function (done, _converse) {
  2566. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2567. .then(function () {
  2568. var presence = $pres().attrs({
  2569. from:'lounge@localhost/thirdwitch',
  2570. id:'n13mt3l',
  2571. to:'dummy@localhost/pda',
  2572. type:'error'})
  2573. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2574. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2575. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2576. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2577. spyOn(view, 'showErrorMessage').and.callThrough();
  2578. view.onChatRoomPresence(presence);
  2579. expect($(view.el).find('.chatroom-body p:last').text()).toBe("This room does not (yet) exist.");
  2580. done();
  2581. });
  2582. }));
  2583. it("will show an error message if the room has reached its maximum number of occupants",
  2584. mock.initConverseWithPromises(
  2585. null, ['rosterGroupsFetched'], {},
  2586. function (done, _converse) {
  2587. test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  2588. .then(function () {
  2589. var presence = $pres().attrs({
  2590. from:'lounge@localhost/thirdwitch',
  2591. id:'n13mt3l',
  2592. to:'dummy@localhost/pda',
  2593. type:'error'})
  2594. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2595. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  2596. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2597. var view = _converse.chatboxviews.get('problematic@muc.localhost');
  2598. spyOn(view, 'showErrorMessage').and.callThrough();
  2599. view.onChatRoomPresence(presence);
  2600. expect($(view.el).find('.chatroom-body p:last').text()).toBe("This room has reached its maximum number of occupants.");
  2601. done();
  2602. });
  2603. }));
  2604. });
  2605. describe("Someone being invited to a chat room", function () {
  2606. it("will first be added to the member list if the chat room is members only",
  2607. mock.initConverseWithPromises(
  2608. null, ['rosterGroupsFetched'], {},
  2609. function (done, _converse) {
  2610. var sent_IQs = [], IQ_ids = [];
  2611. var sendIQ = _converse.connection.sendIQ;
  2612. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2613. sent_IQs.push(iq);
  2614. IQ_ids.push(sendIQ.bind(this)(iq, callback, errback));
  2615. });
  2616. _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'dummy'});
  2617. // State that the chat is members-only via the features IQ
  2618. var features_stanza = $iq({
  2619. from: 'coven@chat.shakespeare.lit',
  2620. 'id': IQ_ids.pop(),
  2621. 'to': 'dummy@localhost/desktop',
  2622. 'type': 'result'
  2623. })
  2624. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  2625. .c('identity', {
  2626. 'category': 'conference',
  2627. 'name': 'A Dark Cave',
  2628. 'type': 'text'
  2629. }).up()
  2630. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  2631. .c('feature', {'var': 'muc_hidden'}).up()
  2632. .c('feature', {'var': 'muc_temporary'}).up()
  2633. .c('feature', {'var': 'muc_membersonly'}).up();
  2634. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  2635. var view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2636. expect(view.model.get('membersonly')).toBeTruthy();
  2637. test_utils.createContacts(_converse, 'current');
  2638. var sent_stanza, sent_id;
  2639. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  2640. if (stanza.nodeTree && stanza.nodeTree.nodeName === 'message') {
  2641. sent_id = stanza.nodeTree.getAttribute('id');
  2642. sent_stanza = stanza;
  2643. }
  2644. });
  2645. var name = mock.cur_names[0];
  2646. var invitee_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  2647. var reason = "Please join this chat room";
  2648. view.directInvite(invitee_jid, reason);
  2649. // Check in reverse order that we requested all three lists
  2650. // (member, owner and admin).
  2651. var admin_iq_id = IQ_ids.pop();
  2652. var owner_iq_id = IQ_ids.pop();
  2653. var member_iq_id = IQ_ids.pop();
  2654. expect(sent_IQs.pop().toLocaleString()).toBe(
  2655. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+admin_iq_id+"'>"+
  2656. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2657. "<item affiliation='admin'/>"+
  2658. "</query>"+
  2659. "</iq>");
  2660. expect(sent_IQs.pop().toLocaleString()).toBe(
  2661. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+owner_iq_id+"'>"+
  2662. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2663. "<item affiliation='owner'/>"+
  2664. "</query>"+
  2665. "</iq>");
  2666. expect(sent_IQs.pop().toLocaleString()).toBe(
  2667. "<iq to='coven@chat.shakespeare.lit' type='get' xmlns='jabber:client' id='"+member_iq_id+"'>"+
  2668. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2669. "<item affiliation='member'/>"+
  2670. "</query>"+
  2671. "</iq>");
  2672. /* Now the service sends the member list to the user
  2673. *
  2674. * <iq from='coven@chat.shakespeare.lit'
  2675. * id='member3'
  2676. * to='crone1@shakespeare.lit/desktop'
  2677. * type='result'>
  2678. * <query xmlns='http://jabber.org/protocol/muc#admin'>
  2679. * <item affiliation='member'
  2680. * jid='hag66@shakespeare.lit'
  2681. * nick='thirdwitch'
  2682. * role='participant'/>
  2683. * </query>
  2684. * </iq>
  2685. */
  2686. var member_list_stanza = $iq({
  2687. 'from': 'coven@chat.shakespeare.lit',
  2688. 'id': member_iq_id,
  2689. 'to': 'dummy@localhost/resource',
  2690. 'type': 'result'
  2691. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  2692. .c('item', {
  2693. 'affiliation': 'member',
  2694. 'jid': 'hag66@shakespeare.lit',
  2695. 'nick': 'thirdwitch',
  2696. 'role': 'participant'
  2697. });
  2698. _converse.connection._dataRecv(test_utils.createRequest(member_list_stanza));
  2699. var admin_list_stanza = $iq({
  2700. 'from': 'coven@chat.shakespeare.lit',
  2701. 'id': admin_iq_id,
  2702. 'to': 'dummy@localhost/resource',
  2703. 'type': 'result'
  2704. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  2705. .c('item', {
  2706. 'affiliation': 'admin',
  2707. 'jid': 'wiccarocks@shakespeare.lit',
  2708. 'nick': 'secondwitch'
  2709. });
  2710. _converse.connection._dataRecv(test_utils.createRequest(admin_list_stanza));
  2711. var owner_list_stanza = $iq({
  2712. 'from': 'coven@chat.shakespeare.lit',
  2713. 'id': owner_iq_id,
  2714. 'to': 'dummy@localhost/resource',
  2715. 'type': 'result'
  2716. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  2717. .c('item', {
  2718. 'affiliation': 'owner',
  2719. 'jid': 'crone1@shakespeare.lit',
  2720. });
  2721. _converse.connection._dataRecv(test_utils.createRequest(owner_list_stanza));
  2722. test_utils.waitUntil(function () {
  2723. return IQ_ids.length;
  2724. }, 300).then(function () {
  2725. // Check that the member list now gets updated
  2726. var iq = "<iq to='coven@chat.shakespeare.lit' type='set' xmlns='jabber:client' id='"+IQ_ids.pop()+"'>"+
  2727. "<query xmlns='http://jabber.org/protocol/muc#admin'>"+
  2728. "<item affiliation='member' jid='"+invitee_jid+"'>"+
  2729. "<reason>Please join this chat room</reason>"+
  2730. "</item>"+
  2731. "</query>"+
  2732. "</iq>";
  2733. test_utils.waitUntil(function () {
  2734. return _.includes(_.invokeMap(sent_IQs, Object.prototype.toLocaleString), iq);
  2735. }, 300).then(function () {
  2736. // Finally check that the user gets invited.
  2737. expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
  2738. "<message from='dummy@localhost/resource' to='"+invitee_jid+"' id='"+sent_id+"' xmlns='jabber:client'>"+
  2739. "<x xmlns='jabber:x:conference' jid='coven@chat.shakespeare.lit' reason='Please join this chat room'/>"+
  2740. "</message>"
  2741. );
  2742. done();
  2743. });
  2744. });
  2745. }));
  2746. });
  2747. describe("The affiliations delta", function () {
  2748. it("can be computed in various ways",
  2749. mock.initConverseWithPromises(
  2750. null, ['rosterGroupsFetched'], {},
  2751. function (done, _converse) {
  2752. test_utils.openChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy');
  2753. var roomview = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2754. var exclude_existing = false;
  2755. var remove_absentees = false;
  2756. var new_list = [];
  2757. var old_list = [];
  2758. var delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2759. expect(delta.length).toBe(0);
  2760. new_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2761. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2762. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2763. expect(delta.length).toBe(0);
  2764. // When remove_absentees is false, then affiliations in the old
  2765. // list which are not in the new one won't be removed.
  2766. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  2767. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2768. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2769. expect(delta.length).toBe(0);
  2770. // With exclude_existing set to false, any changed affiliations
  2771. // will be included in the delta (i.e. existing affiliations
  2772. // are included in the comparison).
  2773. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  2774. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2775. expect(delta.length).toBe(1);
  2776. expect(delta[0].jid).toBe('wiccarocks@shakespeare.lit');
  2777. expect(delta[0].affiliation).toBe('member');
  2778. // To also remove affiliations from the old list which are not
  2779. // in the new list, we set remove_absentees to true
  2780. remove_absentees = true;
  2781. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  2782. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2783. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2784. expect(delta.length).toBe(1);
  2785. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  2786. expect(delta[0].affiliation).toBe('none');
  2787. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, [], old_list);
  2788. expect(delta.length).toBe(2);
  2789. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  2790. expect(delta[0].affiliation).toBe('none');
  2791. expect(delta[1].jid).toBe('wiccarocks@shakespeare.lit');
  2792. expect(delta[1].affiliation).toBe('none');
  2793. // To only add a user if they don't already have an
  2794. // affiliation, we set 'exclude_existing' to true
  2795. exclude_existing = true;
  2796. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  2797. delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2798. expect(delta.length).toBe(0);
  2799. done();
  2800. }));
  2801. });
  2802. describe("The \"Chatrooms\" section", function () {
  2803. it("contains a link to a modal through which a new chatroom can be created",
  2804. mock.initConverseWithPromises(
  2805. null, ['rosterGroupsFetched'], {},
  2806. function (done, _converse) {
  2807. test_utils.openControlBox();
  2808. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  2809. roomspanel.el.querySelector('.trigger-add-chatrooms-modal').click();
  2810. test_utils.closeControlBox(_converse);
  2811. const modal = roomspanel.add_room_modal;
  2812. test_utils.waitUntil(function () {
  2813. return u.isVisible(modal.el);
  2814. }, 1000).then(function () {
  2815. spyOn(_converse.ChatRoomView.prototype, 'getRoomFeatures').and.callFake(function () {
  2816. var deferred = new $.Deferred();
  2817. deferred.resolve();
  2818. return deferred.promise();
  2819. });
  2820. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2821. modal.el.querySelector('input[name="chatroom"]').value = 'lounce@muc.localhost';
  2822. modal.el.querySelector('form input[type="submit"]').click();
  2823. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  2824. done();
  2825. }).catch(_.partial(console.error, _));
  2826. }));
  2827. it("contains a link to a modal which can list rooms publically available on the server",
  2828. mock.initConverseWithPromises(
  2829. null, ['rosterGroupsFetched'], {},
  2830. function (done, _converse) {
  2831. var sendIQ = _converse.connection.sendIQ;
  2832. var sent_stanza, IQ_id;
  2833. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2834. sent_stanza = iq;
  2835. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2836. });
  2837. test_utils.openControlBox();
  2838. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  2839. roomspanel.el.querySelector('.trigger-list-chatrooms-modal').click();
  2840. test_utils.closeControlBox(_converse);
  2841. const modal = roomspanel.list_rooms_modal;
  2842. test_utils.waitUntil(function () {
  2843. return u.isVisible(modal.el);
  2844. }, 1000).then(function () {
  2845. spyOn(_converse.ChatRoomView.prototype, 'getRoomFeatures').and.callFake(function () {
  2846. var deferred = new $.Deferred();
  2847. deferred.resolve();
  2848. return deferred.promise();
  2849. });
  2850. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2851. // See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
  2852. expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(0);
  2853. const input = modal.el.querySelector('input[name="server"]').value = 'chat.shakespear.lit';
  2854. modal.el.querySelector('input[type="submit"]').click();
  2855. expect(sent_stanza.toLocaleString()).toBe(
  2856. "<iq to='chat.shakespear.lit' from='dummy@localhost/resource' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
  2857. "<query xmlns='http://jabber.org/protocol/disco#items'/>"+
  2858. "</iq>"
  2859. );
  2860. var iq = $iq({
  2861. from:'muc.localhost',
  2862. to:'dummy@localhost/pda',
  2863. id: IQ_id,
  2864. type:'result'
  2865. }).c('query')
  2866. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  2867. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  2868. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  2869. .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
  2870. _converse.connection._dataRecv(test_utils.createRequest(iq));
  2871. expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(5);
  2872. const rooms = modal.el.querySelectorAll('.available-chatrooms li');
  2873. expect(rooms[0].textContent.trim()).toBe("Rooms found:");
  2874. expect(rooms[1].textContent.trim()).toBe("A Lonely Heath");
  2875. expect(rooms[2].textContent.trim()).toBe("A Dark Cave");
  2876. expect(rooms[3].textContent.trim()).toBe("The Palace");
  2877. expect(rooms[4].textContent.trim()).toBe("Macbeth's Castle");
  2878. rooms[4].querySelector('.open-room').click();
  2879. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  2880. var view = _converse.chatboxviews.get('inverness@chat.shakespeare.lit');
  2881. expect(view.el.querySelector('.chat-head-chatroom').textContent.trim()).toBe("Macbeth's Castle");
  2882. done();
  2883. }).catch(_.partial(console.error, _));
  2884. }));
  2885. it("shows the number of unread mentions received",
  2886. mock.initConverseWithPromises(
  2887. null, ['rosterGroupsFetched'], {'allow_bookmarks': false},
  2888. function (done, _converse) {
  2889. // XXX: we set `allow_bookmarks` to false, so that the rooms
  2890. // list gets rendered. Otherwise we would have to mock
  2891. // the bookmark stanza exchange.
  2892. test_utils.openControlBox();
  2893. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  2894. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(0);
  2895. var room_jid = 'kitchen@conference.shakespeare.lit';
  2896. test_utils.openAndEnterChatRoom(
  2897. _converse, 'kitchen', 'conference.shakespeare.lit', 'fires').then(function () {
  2898. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  2899. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
  2900. var view = _converse.chatboxviews.get(room_jid);
  2901. view.model.set({'minimized': true});
  2902. var contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
  2903. var message = 'fires: Your attention is required';
  2904. var nick = mock.chatroom_names[0],
  2905. msg = $msg({
  2906. from: room_jid+'/'+nick,
  2907. id: (new Date()).getTime(),
  2908. to: 'dummy@localhost',
  2909. type: 'groupchat'
  2910. }).c('body').t(message).tree();
  2911. view.handleMUCMessage(msg);
  2912. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  2913. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
  2914. expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('1');
  2915. msg = $msg({
  2916. from: room_jid+'/'+nick,
  2917. id: (new Date()).getTime(),
  2918. to: 'dummy@localhost',
  2919. type: 'groupchat'
  2920. }).c('body').t(message).tree();
  2921. view.handleMUCMessage(msg);
  2922. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  2923. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
  2924. expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('2');
  2925. view.model.set({'minimized': false});
  2926. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  2927. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
  2928. done();
  2929. });
  2930. }));
  2931. });
  2932. });
  2933. }));