2
0

chatroom.js 202 KB

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