chatroom.js 162 KB

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