chatroom.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*global converse */
  2. (function (root, factory) {
  3. define([
  4. "jquery",
  5. "underscore",
  6. "mock",
  7. "test_utils",
  8. "utils"
  9. ], function ($, _, mock, test_utils, utils) {
  10. return factory($, _, mock, test_utils, utils);
  11. }
  12. );
  13. } (this, function ($, _, mock, test_utils, utils) {
  14. var $pres = converse_api.env.$pres;
  15. var $msg = converse_api.env.$msg;
  16. var Strophe = converse_api.env.Strophe;
  17. return describe("ChatRooms", function (mock, test_utils) {
  18. beforeEach(function () {
  19. runs(function () {
  20. test_utils.closeAllChatBoxes();
  21. test_utils.clearBrowserStorage();
  22. });
  23. });
  24. describe("The \"rooms\" API", function () {
  25. beforeEach(function () {
  26. test_utils.closeAllChatBoxes();
  27. test_utils.clearBrowserStorage();
  28. converse.rosterview.model.reset();
  29. test_utils.createContacts('current');
  30. });
  31. it("has a method 'close' which closes rooms by JID or all rooms when called with no arguments", function () {
  32. runs(function () {
  33. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  34. test_utils.openChatRoom('leisure', 'localhost', 'dummy');
  35. test_utils.openChatRoom('news', 'localhost', 'dummy');
  36. expect(converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy();
  37. expect(converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
  38. expect(converse.chatboxviews.get('news@localhost').$el.is(':visible')).toBeTruthy();
  39. });
  40. waits('100');
  41. runs(function () {
  42. converse_api.rooms.close('lounge@localhost');
  43. expect(converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
  44. expect(converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
  45. expect(converse.chatboxviews.get('news@localhost').$el.is(':visible')).toBeTruthy();
  46. converse_api.rooms.close(['leisure@localhost', 'news@localhost']);
  47. expect(converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
  48. expect(converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
  49. expect(converse.chatboxviews.get('news@localhost')).toBeUndefined();
  50. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  51. test_utils.openChatRoom('leisure', 'localhost', 'dummy');
  52. expect(converse.chatboxviews.get('lounge@localhost').$el.is(':visible')).toBeTruthy();
  53. expect(converse.chatboxviews.get('leisure@localhost').$el.is(':visible')).toBeTruthy();
  54. });
  55. waits('100');
  56. runs(function () {
  57. converse_api.rooms.close();
  58. expect(converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
  59. expect(converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
  60. });
  61. });
  62. it("has a method 'get' which returns a wrapped chat room (if it exists)", function () {
  63. waits('300'); // ChatBox.show() is debounced for 250ms
  64. runs(function () {
  65. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  66. var jid = 'lounge@localhost';
  67. var room = converse_api.rooms.get(jid);
  68. expect(room instanceof Object).toBeTruthy();
  69. expect(room.is_chatroom).toBeTruthy();
  70. var chatroomview = converse.chatboxviews.get(jid);
  71. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  72. chatroomview.close();
  73. });
  74. waits('300'); // ChatBox.show() is debounced for 250ms
  75. runs(function () {
  76. // Test with mixed case
  77. test_utils.openChatRoom('Leisure', 'localhost', 'dummy');
  78. var jid = 'Leisure@localhost';
  79. var room = converse_api.rooms.get(jid);
  80. expect(room instanceof Object).toBeTruthy();
  81. var chatroomview = converse.chatboxviews.get(jid.toLowerCase());
  82. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  83. });
  84. waits('300'); // ChatBox.show() is debounced for 250ms
  85. runs(function () {
  86. var jid = 'leisure@localhost';
  87. var room = converse_api.rooms.get(jid);
  88. expect(room instanceof Object).toBeTruthy();
  89. var chatroomview = converse.chatboxviews.get(jid.toLowerCase());
  90. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  91. jid = 'leiSure@localhost';
  92. room = converse_api.rooms.get(jid);
  93. expect(room instanceof Object).toBeTruthy();
  94. chatroomview = converse.chatboxviews.get(jid.toLowerCase());
  95. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  96. chatroomview.close();
  97. // Non-existing room
  98. jid = 'lounge2@localhost';
  99. room = converse_api.rooms.get(jid);
  100. expect(typeof room === 'undefined').toBeTruthy();
  101. });
  102. });
  103. it("has a method 'open' which opens and returns a wrapped chat box", function () {
  104. var chatroomview;
  105. var jid = 'lounge@localhost';
  106. var room = converse_api.rooms.open(jid);
  107. runs(function () {
  108. // Test on chat room that doesn't exist.
  109. expect(room instanceof Object).toBeTruthy();
  110. expect(room.is_chatroom).toBeTruthy();
  111. chatroomview = converse.chatboxviews.get(jid);
  112. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  113. });
  114. waits('300'); // ChatBox.show() is debounced for 250ms
  115. runs(function () {
  116. // Test again, now that the room exists.
  117. room = converse_api.rooms.open(jid);
  118. expect(room instanceof Object).toBeTruthy();
  119. expect(room.is_chatroom).toBeTruthy();
  120. chatroomview = converse.chatboxviews.get(jid);
  121. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  122. });
  123. waits('300'); // ChatBox.show() is debounced for 250ms
  124. runs(function () {
  125. // Test with mixed case in JID
  126. jid = 'Leisure@localhost';
  127. room = converse_api.rooms.open(jid);
  128. expect(room instanceof Object).toBeTruthy();
  129. chatroomview = converse.chatboxviews.get(jid.toLowerCase());
  130. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  131. jid = 'leisure@localhost';
  132. room = converse_api.rooms.open(jid);
  133. expect(room instanceof Object).toBeTruthy();
  134. chatroomview = converse.chatboxviews.get(jid.toLowerCase());
  135. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  136. jid = 'leiSure@localhost';
  137. room = converse_api.rooms.open(jid);
  138. expect(room instanceof Object).toBeTruthy();
  139. chatroomview = converse.chatboxviews.get(jid.toLowerCase());
  140. expect(chatroomview.$el.is(':visible')).toBeTruthy();
  141. chatroomview.close();
  142. });
  143. });
  144. });
  145. describe("A Chat Room", function () {
  146. beforeEach(function () {
  147. runs(function () {
  148. test_utils.closeAllChatBoxes();
  149. test_utils.clearBrowserStorage();
  150. });
  151. });
  152. it("can have spaces and special characters in its name", function () {
  153. test_utils.openChatRoom('lounge & leisure', 'localhost', 'dummy');
  154. var view = converse.chatboxviews.get(
  155. Strophe.escapeNode('lounge & leisure')+'@localhost');
  156. expect(view instanceof converse.ChatRoomView).toBe(true);
  157. });
  158. it("shows users currently present in the room", function () {
  159. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  160. var name;
  161. var view = this.chatboxviews.get('lounge@localhost'),
  162. $occupants = view.$('.occupant-list');
  163. spyOn(view, 'onChatRoomPresence').andCallThrough();
  164. var presence, role;
  165. for (var i=0; i<mock.chatroom_names.length; i++) {
  166. name = mock.chatroom_names[i];
  167. role = mock.chatroom_roles[name].role;
  168. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  169. presence = $pres({
  170. to:'dummy@localhost/pda',
  171. from:'lounge@localhost/'+name
  172. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  173. .c('item').attrs({
  174. affiliation: mock.chatroom_roles[name].affiliation,
  175. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  176. role: role
  177. }).up()
  178. .c('status').attrs({code:'110'}).nodeTree;
  179. this.connection._dataRecv(test_utils.createRequest(presence));
  180. expect(view.onChatRoomPresence).toHaveBeenCalled();
  181. expect($occupants.find('li').length).toBe(1+i);
  182. expect($($occupants.find('li')[i]).text()).toBe(mock.chatroom_names[i]);
  183. expect($($occupants.find('li')[i]).hasClass('moderator')).toBe(role === "moderator");
  184. }
  185. // Test users leaving the room
  186. // http://xmpp.org/extensions/xep-0045.html#exit
  187. for (i=mock.chatroom_names.length-1; i>-1; i--) {
  188. name = mock.chatroom_names[i];
  189. role = mock.chatroom_roles[name].role;
  190. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  191. presence = $pres({
  192. to:'dummy@localhost/pda',
  193. from:'lounge@localhost/'+name,
  194. type: 'unavailable'
  195. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  196. .c('item').attrs({
  197. affiliation: mock.chatroom_roles[name].affiliation,
  198. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  199. role: 'none'
  200. }).nodeTree;
  201. this.connection._dataRecv(test_utils.createRequest(presence));
  202. expect(view.onChatRoomPresence).toHaveBeenCalled();
  203. expect($occupants.find('li.online').length).toBe(i);
  204. }
  205. }.bind(converse));
  206. it("indicates moderators by means of a special css class and tooltip", function () {
  207. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  208. var view = this.chatboxviews.get('lounge@localhost');
  209. var presence = $pres({
  210. to:'dummy@localhost/pda',
  211. from:'lounge@localhost/moderatorman'
  212. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  213. .c('item').attrs({
  214. affiliation: 'admin',
  215. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  216. role: 'moderator',
  217. }).up()
  218. .c('status').attrs({code:'110'}).nodeTree;
  219. this.connection._dataRecv(test_utils.createRequest(presence));
  220. var occupant = view.$el.find('.occupant-list').find('li');
  221. expect(occupant.length).toBe(1);
  222. expect($(occupant).text()).toBe("moderatorman");
  223. expect($(occupant).attr('class').indexOf('moderator')).not.toBe(-1);
  224. expect($(occupant).attr('title')).toBe('This user is a moderator');
  225. }.bind(converse));
  226. it("allows the user to invite their roster contacts to enter the chat room", function () {
  227. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  228. spyOn(converse, 'emit');
  229. spyOn(window, 'prompt').andCallFake(function () {
  230. return null;
  231. });
  232. var $input;
  233. var view = this.chatboxviews.get('lounge@localhost');
  234. view.$el.find('.chat-area').remove();
  235. view.renderChatArea(); // Will init the widget
  236. test_utils.createContacts('current'); // We need roster contacts, so that we have someone to invite
  237. $input = view.$el.find('input.invited-contact.tt-input');
  238. var $hint = view.$el.find('input.invited-contact.tt-hint');
  239. runs (function () {
  240. expect($input.length).toBe(1);
  241. expect($input.attr('placeholder')).toBe('Invite');
  242. $input.val("Felix");
  243. $input.trigger('input');
  244. });
  245. waits(350); // Needed, due to debounce
  246. runs (function () {
  247. expect($input.val()).toBe('Felix');
  248. expect($hint.val()).toBe('Felix Amsel');
  249. var $sugg = view.$el.find('[data-jid="felix.amsel@localhost"]');
  250. expect($sugg.length).toBe(1);
  251. $sugg.trigger('click');
  252. expect(window.prompt).toHaveBeenCalled();
  253. });
  254. }.bind(converse));
  255. it("can be joined automatically, based upon a received invite", function () {
  256. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  257. spyOn(window, 'confirm').andCallFake(function () {
  258. return true;
  259. });
  260. test_utils.createContacts('current'); // We need roster contacts, who can invite us
  261. var view = this.chatboxviews.get('lounge@localhost');
  262. view.close();
  263. var name = mock.cur_names[0];
  264. var from_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  265. var room_jid = 'lounge@localhost';
  266. var reason = "Please join this chat room";
  267. var message = $(
  268. "<message from='"+from_jid+"' to='"+converse.bare_jid+"'>" +
  269. "<x xmlns='jabber:x:conference'" +
  270. "jid='"+room_jid+"'" +
  271. "reason='"+reason+"'/>"+
  272. "</message>"
  273. )[0];
  274. expect(converse.chatboxes.models.length).toBe(1);
  275. expect(converse.chatboxes.models[0].id).toBe("controlbox");
  276. converse.onDirectMUCInvitation(message);
  277. expect(window.confirm).toHaveBeenCalledWith(
  278. name + ' has invited you to join a chat room: '+ room_jid +
  279. ', and left the following reason: "'+reason+'"');
  280. expect(converse.chatboxes.models.length).toBe(2);
  281. expect(converse.chatboxes.models[0].id).toBe('controlbox');
  282. expect(converse.chatboxes.models[1].id).toBe(room_jid);
  283. }.bind(converse));
  284. it("shows received groupchat messages", function () {
  285. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  286. spyOn(converse, 'emit');
  287. var view = this.chatboxviews.get('lounge@localhost');
  288. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  289. var nick = mock.chatroom_names[0];
  290. var text = 'This is a received message';
  291. var message = $msg({
  292. from: 'lounge@localhost/'+nick,
  293. id: '1',
  294. to: 'dummy@localhost',
  295. type: 'groupchat'
  296. }).c('body').t(text);
  297. view.onChatRoomMessage(message.nodeTree);
  298. var $chat_content = view.$el.find('.chat-content');
  299. expect($chat_content.find('.chat-message').length).toBe(1);
  300. expect($chat_content.find('.chat-msg-content').text()).toBe(text);
  301. expect(converse.emit).toHaveBeenCalledWith('message', message.nodeTree);
  302. }.bind(converse));
  303. it("shows sent groupchat messages", function () {
  304. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  305. spyOn(converse, 'emit');
  306. var view = converse.chatboxviews.get('lounge@localhost');
  307. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  308. var text = 'This is a sent message';
  309. view.$el.find('.chat-textarea').text(text);
  310. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  311. expect(converse.emit).toHaveBeenCalledWith('messageSend', text);
  312. var message = $msg({
  313. from: 'lounge@localhost/dummy',
  314. to: 'dummy@localhost.com',
  315. type: 'groupchat',
  316. id: view.model.messages.at(0).get('msgid')
  317. }).c('body').t(text);
  318. view.onChatRoomMessage(message.nodeTree);
  319. var $chat_content = view.$el.find('.chat-content');
  320. expect($chat_content.find('.chat-message').length).toBe(1);
  321. expect($chat_content.find('.chat-msg-content').last().text()).toBe(text);
  322. // We don't emit an event if it's our own message
  323. expect(converse.emit.callCount, 1);
  324. });
  325. it("will cause the chat area to be scrolled down only if it was at the bottom already", function () {
  326. var message = 'This message is received while the chat area is scrolled up';
  327. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  328. var view = converse.chatboxviews.get('lounge@localhost');
  329. spyOn(view, 'scrollDown').andCallThrough();
  330. runs(function () {
  331. /* Create enough messages so that there's a
  332. * scrollbar.
  333. */
  334. for (var i=0; i<20; i++) {
  335. converse.chatboxes.onMessage(
  336. $msg({
  337. from: 'lounge@localhost/someone',
  338. to: 'dummy@localhost.com',
  339. type: 'groupchat',
  340. id: (new Date()).getTime(),
  341. }).c('body').t('Message: '+i).tree());
  342. }
  343. });
  344. waits(50);
  345. runs(function () {
  346. view.$content.scrollTop(0);
  347. });
  348. waits(250);
  349. runs(function () {
  350. expect(view.model.get('scrolled')).toBeTruthy();
  351. converse.chatboxes.onMessage(
  352. $msg({
  353. from: 'lounge@localhost/someone',
  354. to: 'dummy@localhost.com',
  355. type: 'groupchat',
  356. id: (new Date()).getTime(),
  357. }).c('body').t(message).tree());
  358. });
  359. waits(150);
  360. runs(function () {
  361. // Now check that the message appears inside the chatbox in the DOM
  362. var $chat_content = view.$el.find('.chat-content');
  363. var msg_txt = $chat_content.find('.chat-message:last').find('.chat-msg-content').text();
  364. expect(msg_txt).toEqual(message);
  365. expect(view.$content.scrollTop()).toBe(0);
  366. });
  367. });
  368. it("shows received chatroom subject messages", function () {
  369. var text = 'Jabber/XMPP Development | RFCs and Extensions: http://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  370. var stanza = Strophe.xmlHtmlNode(
  371. '<message xmlns="jabber:client" to="jc@opkode.com/converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">'+
  372. ' <subject>'+text+'</subject>'+
  373. ' <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>'+
  374. ' <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>'+
  375. '</message>').firstChild;
  376. test_utils.openChatRoom('jdev', 'conference.jabber.org', 'jc');
  377. converse.connection._dataRecv(test_utils.createRequest(stanza));
  378. var view = converse.chatboxviews.get('jdev@conference.jabber.org');
  379. var $chat_content = view.$el.find('.chat-content');
  380. expect($chat_content.find('.chat-info').length).toBe(1);
  381. expect($chat_content.find('.chat-info').text()).toBe('Topic set by ralphm to: '+text);
  382. });
  383. it("informs users if their nicknames has been changed.", function () {
  384. /* The service then sends two presence stanzas to the full JID
  385. * of each occupant (including the occupant who is changing his
  386. * or her room nickname), one of type "unavailable" for the old
  387. * nickname and one indicating availability for the new
  388. * nickname.
  389. *
  390. * See: http://xmpp.org/extensions/xep-0045.html#changenick
  391. *
  392. * <presence
  393. * from='coven@localhost/thirdwitch'
  394. * id='DC352437-C019-40EC-B590-AF29E879AF98'
  395. * to='hag66@shakespeare.lit/pda'
  396. * type='unavailable'>
  397. * <x xmlns='http://jabber.org/protocol/muc#user'>
  398. * <item affiliation='member'
  399. * jid='hag66@shakespeare.lit/pda'
  400. * nick='oldhag'
  401. * role='occupant'/>
  402. * <status code='303'/>
  403. * <status code='110'/>
  404. * </x>
  405. * </presence>
  406. *
  407. * <presence
  408. * from='coven@localhost/oldhag'
  409. * id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
  410. * to='hag66@shakespeare.lit/pda'>
  411. * <x xmlns='http://jabber.org/protocol/muc#user'>
  412. * <item affiliation='member'
  413. * jid='hag66@shakespeare.lit/pda'
  414. * role='occupant'/>
  415. * <status code='110'/>
  416. * </x>
  417. * </presence>
  418. */
  419. var __ = utils.__.bind(converse);
  420. test_utils.openChatRoom('lounge', 'localhost', 'oldnick');
  421. var view = this.chatboxviews.get('lounge@localhost');
  422. var $chat_content = view.$el.find('.chat-content');
  423. spyOn(view, 'onChatRoomPresence').andCallThrough();
  424. // The user has just entered the room and receives their own
  425. // presence from the server.
  426. // See example 24:
  427. // http://xmpp.org/extensions/xep-0045.html#enter-pres
  428. var presence = $pres({
  429. to:'dummy@localhost/pda',
  430. from:'lounge@localhost/oldnick',
  431. id:'DC352437-C019-40EC-B590-AF29E879AF97'
  432. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  433. .c('item').attrs({
  434. affiliation: 'member',
  435. jid: 'dummy@localhost/pda',
  436. role: 'occupant'
  437. }).up()
  438. .c('status').attrs({code:'110'}).up()
  439. .c('status').attrs({code:'210'}).nodeTree;
  440. this.connection._dataRecv(test_utils.createRequest(presence));
  441. expect(view.onChatRoomPresence).toHaveBeenCalled();
  442. var $occupants = view.$('.occupant-list');
  443. expect($occupants.children().length).toBe(1);
  444. expect($occupants.children().first(0).text()).toBe("oldnick");
  445. expect($occupants.children().first().hasClass('online')).toBe(true);
  446. expect($chat_content.find('div.chat-info').length).toBe(1);
  447. expect($chat_content.find('div.chat-info').html()).toBe(__(view.newNicknameMessages["210"], "oldnick"));
  448. presence = $pres().attrs({
  449. from:'lounge@localhost/oldnick',
  450. id:'DC352437-C019-40EC-B590-AF29E879AF98',
  451. to:'dummy@localhost/pda',
  452. type:'unavailable'
  453. })
  454. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  455. .c('item').attrs({
  456. affiliation: 'member',
  457. jid: 'dummy@localhost/pda',
  458. nick: 'newnick',
  459. role: 'occupant'
  460. }).up()
  461. .c('status').attrs({code:'303'}).up()
  462. .c('status').attrs({code:'110'}).nodeTree;
  463. this.connection._dataRecv(test_utils.createRequest(presence));
  464. expect(view.onChatRoomPresence).toHaveBeenCalled();
  465. expect($chat_content.find('div.chat-info').length).toBe(2);
  466. expect($chat_content.find('div.chat-info').last().html()).toBe(__(view.newNicknameMessages["303"], "newnick"));
  467. // The occupant is still listed (because they have affiliation
  468. // of "member"), but they don't have the "online" class
  469. // anymore.
  470. $occupants = view.$('.occupant-list');
  471. expect($occupants.children().length).toBe(1);
  472. expect($occupants.children().first().hasClass('online')).toBe(false);
  473. presence = $pres().attrs({
  474. from:'lounge@localhost/newnick',
  475. id:'5B4F27A4-25ED-43F7-A699-382C6B4AFC67',
  476. to:'dummy@localhost/pda'
  477. })
  478. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  479. .c('item').attrs({
  480. affiliation: 'member',
  481. jid: 'dummy@localhost/pda',
  482. role: 'occupant'
  483. }).up()
  484. .c('status').attrs({code:'110'}).nodeTree;
  485. this.connection._dataRecv(test_utils.createRequest(presence));
  486. expect(view.onChatRoomPresence).toHaveBeenCalled();
  487. expect($chat_content.find('div.chat-info').length).toBe(2);
  488. expect($chat_content.find('div.chat-info').last().html()).toBe(__(view.newNicknameMessages["303"], "newnick"));
  489. $occupants = view.$('.occupant-list');
  490. expect($occupants.children().length).toBe(1);
  491. expect($occupants.children().first(0).text()).toBe("newnick");
  492. }.bind(converse));
  493. it("informs users if they have been kicked out of the chat room", function () {
  494. /* <presence
  495. * from='harfleur@chat.shakespeare.lit/pistol'
  496. * to='pistol@shakespeare.lit/harfleur'
  497. * type='unavailable'>
  498. * <x xmlns='http://jabber.org/protocol/muc#user'>
  499. * <item affiliation='none' role='none'>
  500. * <actor nick='Fluellen'/>
  501. * <reason>Avaunt, you cullion!</reason>
  502. * </item>
  503. * <status code='307'/>
  504. * </x>
  505. * </presence>
  506. */
  507. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  508. var presence = $pres().attrs({
  509. from:'lounge@localhost/dummy',
  510. to:'dummy@localhost/pda',
  511. type:'unavailable'
  512. })
  513. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  514. .c('item').attrs({
  515. affiliation: 'none',
  516. jid: 'dummy@localhost/pda',
  517. role: 'none'
  518. })
  519. .c('actor').attrs({nick: 'Fluellen'}).up()
  520. .c('reason').t('Avaunt, you cullion!').up()
  521. .up()
  522. .c('status').attrs({code:'307'}).nodeTree;
  523. var view = this.chatboxviews.get('lounge@localhost');
  524. view.onChatRoomPresence(presence, {nick: 'dummy', name: 'lounge@localhost'});
  525. expect(view.$('.chat-area').is(':visible')).toBeFalsy();
  526. expect(view.$('.occupants').is(':visible')).toBeFalsy();
  527. var $chat_body = view.$('.chatroom-body');
  528. expect($chat_body.html().trim().indexOf('<p>You have been kicked from this room</p><p>The reason given is: "Avaunt, you cullion!"</p>')).not.toBe(-1);
  529. }.bind(converse));
  530. it("can be saved to, and retrieved from, browserStorage", function () {
  531. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  532. // We instantiate a new ChatBoxes collection, which by default
  533. // will be empty.
  534. test_utils.openControlBox();
  535. var newchatboxes = new this.ChatBoxes();
  536. expect(newchatboxes.length).toEqual(0);
  537. // The chatboxes will then be fetched from browserStorage inside the
  538. // onConnected method
  539. newchatboxes.onConnected();
  540. expect(newchatboxes.length).toEqual(2);
  541. // Check that the chatrooms retrieved from browserStorage
  542. // have the same attributes values as the original ones.
  543. var attrs = ['id', 'box_id', 'visible'];
  544. var new_attrs, old_attrs;
  545. for (var i=0; i<attrs.length; i++) {
  546. new_attrs = _.pluck(_.pluck(newchatboxes.models, 'attributes'), attrs[i]);
  547. old_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]);
  548. // FIXME: should have have to sort here? Order must
  549. // probably be the same...
  550. // This should be fixed once the controlbox always opens
  551. // only on the right.
  552. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  553. }
  554. this.rosterview.render();
  555. }.bind(converse));
  556. it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'", function () {
  557. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  558. var view = this.chatboxviews.get('lounge@localhost'),
  559. trimmed_chatboxes = this.minimized_chats;
  560. spyOn(view, 'minimize').andCallThrough();
  561. spyOn(view, 'maximize').andCallThrough();
  562. spyOn(converse, 'emit');
  563. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  564. runs(function () {
  565. view.$el.find('.toggle-chatbox-button').click();
  566. });
  567. waits(50);
  568. runs(function () {
  569. expect(view.minimize).toHaveBeenCalled();
  570. expect(converse.emit).toHaveBeenCalledWith('chatBoxMinimized', jasmine.any(Object));
  571. expect(converse.emit.callCount, 2);
  572. expect(view.$el.is(':visible')).toBeFalsy();
  573. expect(view.model.get('minimized')).toBeTruthy();
  574. expect(view.minimize).toHaveBeenCalled();
  575. var trimmedview = trimmed_chatboxes.get(view.model.get('id'));
  576. trimmedview.$("a.restore-chat").click();
  577. });
  578. waits(250);
  579. runs(function () {
  580. expect(view.maximize).toHaveBeenCalled();
  581. expect(converse.emit).toHaveBeenCalledWith('chatBoxMaximized', jasmine.any(Object));
  582. expect(view.$el.is(':visible')).toBeTruthy();
  583. expect(view.model.get('minimized')).toBeFalsy();
  584. expect(converse.emit.callCount, 3);
  585. });
  586. }.bind(converse));
  587. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", function () {
  588. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  589. var view = this.chatboxviews.get('lounge@localhost');
  590. spyOn(view, 'close').andCallThrough();
  591. spyOn(converse, 'emit');
  592. spyOn(view, 'leave');
  593. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  594. runs(function () {
  595. view.$el.find('.close-chatbox-button').click();
  596. });
  597. waits(50);
  598. runs(function () {
  599. expect(view.close).toHaveBeenCalled();
  600. expect(view.leave).toHaveBeenCalled();
  601. expect(this.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  602. }.bind(converse));
  603. }.bind(converse));
  604. }.bind(converse));
  605. describe("Each chat room can take special commands", function () {
  606. beforeEach(function () {
  607. runs(function () {
  608. test_utils.closeAllChatBoxes();
  609. test_utils.clearBrowserStorage();
  610. });
  611. });
  612. it("to clear messages", function () {
  613. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  614. var view = converse.chatboxviews.get('lounge@localhost');
  615. spyOn(view, 'onChatRoomMessageSubmitted').andCallThrough();
  616. spyOn(view, 'clearChatRoomMessages');
  617. view.$el.find('.chat-textarea').text('/clear');
  618. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  619. expect(view.onChatRoomMessageSubmitted).toHaveBeenCalled();
  620. expect(view.clearChatRoomMessages).toHaveBeenCalled();
  621. });
  622. it("to ban a user", function () {
  623. test_utils.openChatRoom('lounge', 'localhost', 'dummy');
  624. var view = converse.chatboxviews.get('lounge@localhost');
  625. spyOn(view, 'onChatRoomMessageSubmitted').andCallThrough();
  626. spyOn(view, 'setAffiliation').andCallThrough();
  627. spyOn(view, 'showStatusNotification').andCallThrough();
  628. spyOn(view, 'validateRoleChangeCommand').andCallThrough();
  629. view.$el.find('.chat-textarea').text('/ban');
  630. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  631. expect(view.onChatRoomMessageSubmitted).toHaveBeenCalled();
  632. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  633. expect(view.showStatusNotification).toHaveBeenCalledWith(
  634. "Error: the \"ban\" command takes two arguments, the user's nickname and optionally a reason.",
  635. true
  636. );
  637. expect(view.setAffiliation).not.toHaveBeenCalled();
  638. // Call now with the correct amount of arguments.
  639. // XXX: Calling onChatRoomMessageSubmitted directly, trying
  640. // again via triggering Event doesn't work for some weird
  641. // reason.
  642. view.onChatRoomMessageSubmitted('/ban jid This is the reason');
  643. expect(view.validateRoleChangeCommand.callCount).toBe(2);
  644. expect(view.showStatusNotification.callCount).toBe(1);
  645. expect(view.setAffiliation).toHaveBeenCalled();
  646. });
  647. }.bind(converse));
  648. describe("When attempting to enter a chatroom", function () {
  649. beforeEach(function () {
  650. var roomspanel = this.chatboxviews.get('controlbox').roomspanel;
  651. var $input = roomspanel.$el.find('input.new-chatroom-name');
  652. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  653. var $server = roomspanel.$el.find('input.new-chatroom-server');
  654. $input.val('problematic');
  655. $nick.val('dummy');
  656. $server.val('muc.localhost');
  657. roomspanel.$el.find('form').submit();
  658. }.bind(converse));
  659. afterEach(function () {
  660. var view = this.chatboxviews.get('problematic@muc.localhost');
  661. view.close();
  662. }.bind(converse));
  663. it("will show an error message if the room requires a password", function () {
  664. var presence = $pres().attrs({
  665. from:'lounge@localhost/thirdwitch',
  666. id:'n13mt3l',
  667. to:'dummy@localhost/pda',
  668. type:'error'})
  669. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  670. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  671. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  672. var view = this.chatboxviews.get('problematic@muc.localhost');
  673. spyOn(view, 'renderPasswordForm').andCallThrough();
  674. runs(function () {
  675. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  676. });
  677. waits(250);
  678. runs(function () {
  679. var $chat_body = view.$el.find('.chatroom-body');
  680. expect(view.renderPasswordForm).toHaveBeenCalled();
  681. expect($chat_body.find('form.chatroom-form').length).toBe(1);
  682. expect($chat_body.find('legend').text()).toBe('This chatroom requires a password');
  683. });
  684. }.bind(converse));
  685. it("will show an error message if the room is members-only and the user not included", function () {
  686. var presence = $pres().attrs({
  687. from:'lounge@localhost/thirdwitch',
  688. id:'n13mt3l',
  689. to:'dummy@localhost/pda',
  690. type:'error'})
  691. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  692. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  693. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  694. var view = this.chatboxviews.get('problematic@muc.localhost');
  695. spyOn(view, 'showErrorMessage').andCallThrough();
  696. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  697. expect(view.$el.find('.chatroom-body p:last').text()).toBe('You are not on the member list of this room');
  698. }.bind(converse));
  699. it("will show an error message if the user has been banned", function () {
  700. var presence = $pres().attrs({
  701. from:'lounge@localhost/thirdwitch',
  702. id:'n13mt3l',
  703. to:'dummy@localhost/pda',
  704. type:'error'})
  705. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  706. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  707. .c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  708. var view = this.chatboxviews.get('problematic@muc.localhost');
  709. spyOn(view, 'showErrorMessage').andCallThrough();
  710. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  711. expect(view.$el.find('.chatroom-body p:last').text()).toBe('You have been banned from this room');
  712. }.bind(converse));
  713. it("will show an error message if no nickname was specified for the user", function () {
  714. var presence = $pres().attrs({
  715. from:'lounge@localhost/thirdwitch',
  716. id:'n13mt3l',
  717. to:'dummy@localhost/pda',
  718. type:'error'})
  719. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  720. .c('error').attrs({by:'lounge@localhost', type:'modify'})
  721. .c('jid-malformed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  722. var view = this.chatboxviews.get('problematic@muc.localhost');
  723. spyOn(view, 'showErrorMessage').andCallThrough();
  724. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  725. expect(view.$el.find('.chatroom-body p:last').text()).toBe('No nickname was specified');
  726. }.bind(converse));
  727. it("will show an error message if the user is not allowed to have created the room", function () {
  728. var presence = $pres().attrs({
  729. from:'lounge@localhost/thirdwitch',
  730. id:'n13mt3l',
  731. to:'dummy@localhost/pda',
  732. type:'error'})
  733. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  734. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  735. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  736. var view = this.chatboxviews.get('problematic@muc.localhost');
  737. spyOn(view, 'showErrorMessage').andCallThrough();
  738. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  739. expect(view.$el.find('.chatroom-body p:last').text()).toBe('You are not allowed to create new rooms');
  740. }.bind(converse));
  741. it("will show an error message if the user's nickname doesn't conform to room policy", function () {
  742. var presence = $pres().attrs({
  743. from:'lounge@localhost/thirdwitch',
  744. id:'n13mt3l',
  745. to:'dummy@localhost/pda',
  746. type:'error'})
  747. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  748. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  749. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  750. var view = this.chatboxviews.get('problematic@muc.localhost');
  751. spyOn(view, 'showErrorMessage').andCallThrough();
  752. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  753. expect(view.$el.find('.chatroom-body p:last').text()).toBe("Your nickname doesn't conform to this room's policies");
  754. }.bind(converse));
  755. it("will show an error message if the user's nickname is already taken", function () {
  756. var presence = $pres().attrs({
  757. from:'lounge@localhost/thirdwitch',
  758. id:'n13mt3l',
  759. to:'dummy@localhost/pda',
  760. type:'error'})
  761. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  762. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  763. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  764. var view = this.chatboxviews.get('problematic@muc.localhost');
  765. spyOn(view, 'showErrorMessage').andCallThrough();
  766. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  767. expect(view.$el.find('.chatroom-body p:last').text()).toBe("Your nickname is already taken");
  768. }.bind(converse));
  769. it("will show an error message if the room doesn't yet exist", function () {
  770. var presence = $pres().attrs({
  771. from:'lounge@localhost/thirdwitch',
  772. id:'n13mt3l',
  773. to:'dummy@localhost/pda',
  774. type:'error'})
  775. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  776. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  777. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  778. var view = this.chatboxviews.get('problematic@muc.localhost');
  779. spyOn(view, 'showErrorMessage').andCallThrough();
  780. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  781. expect(view.$el.find('.chatroom-body p:last').text()).toBe("This room does not (yet) exist");
  782. }.bind(converse));
  783. it("will show an error message if the room has reached its maximum number of occupants", function () {
  784. var presence = $pres().attrs({
  785. from:'lounge@localhost/thirdwitch',
  786. id:'n13mt3l',
  787. to:'dummy@localhost/pda',
  788. type:'error'})
  789. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  790. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  791. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  792. var view = this.chatboxviews.get('problematic@muc.localhost');
  793. spyOn(view, 'showErrorMessage').andCallThrough();
  794. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  795. expect(view.$el.find('.chatroom-body p:last').text()).toBe("This room has reached its maximum number of occupants");
  796. }.bind(converse));
  797. }.bind(converse));
  798. }.bind(converse, mock, test_utils));
  799. }));