chatroom.js 132 KB

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