chatroom.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. (function (root, factory) {
  2. define([
  3. "mock",
  4. "test_utils"
  5. ], function (mock, test_utils) {
  6. return factory(mock, test_utils);
  7. }
  8. );
  9. } (this, function (mock, test_utils) {
  10. return describe("ChatRooms", $.proxy(function (mock, test_utils) {
  11. describe("A Chat Room", $.proxy(function () {
  12. beforeEach(function () {
  13. runs(function () {
  14. test_utils.closeAllChatBoxes();
  15. });
  16. waits(250);
  17. runs(function () {
  18. test_utils.openControlBox();
  19. });
  20. waits(250);
  21. runs(function () {
  22. test_utils.openRoomsPanel();
  23. });
  24. waits(501);
  25. runs(function () {
  26. // Open a new chatroom
  27. var roomspanel = converse.chatboxviews.get('controlbox').roomspanel;
  28. var $input = roomspanel.$el.find('input.new-chatroom-name');
  29. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  30. var $server = roomspanel.$el.find('input.new-chatroom-server');
  31. $input.val('lounge');
  32. $nick.val('dummy');
  33. $server.val('muc.localhost');
  34. roomspanel.$el.find('form').submit();
  35. });
  36. waits(250);
  37. runs(function () {
  38. test_utils.closeControlBox();
  39. });
  40. waits(250);
  41. runs(function () {});
  42. });
  43. it("shows users currently present in the room", $.proxy(function () {
  44. var chatroomview = this.chatboxviews.get('lounge@muc.localhost'),
  45. $participant_list;
  46. var roster = {}, room = {}, i;
  47. for (i=0; i<mock.chatroom_names.length-1; i++) {
  48. roster[mock.chatroom_names[i]] = {};
  49. chatroomview.onChatRoomRoster(roster, room);
  50. $participant_list = chatroomview.$el.find('.participant-list');
  51. expect($participant_list.find('li').length).toBe(1+i);
  52. expect($($participant_list.find('li')[i]).text()).toBe(mock.chatroom_names[i]);
  53. }
  54. roster[converse.bare_jid] = {};
  55. chatroomview.onChatRoomRoster(roster, room);
  56. }, converse));
  57. it("indicates moderators by means of a special css class and tooltip", $.proxy(function () {
  58. var chatroomview = this.chatboxviews.get('lounge@muc.localhost');
  59. var roster = {}, idx = mock.chatroom_names.length-1;
  60. roster[mock.chatroom_names[idx]] = {};
  61. roster[mock.chatroom_names[idx]].role = 'moderator';
  62. chatroomview.onChatRoomRoster(roster, {});
  63. var occupant = chatroomview.$el.find('.participant-list').find('li');
  64. expect(occupant.length).toBe(1);
  65. expect($(occupant).text()).toBe(mock.chatroom_names[idx]);
  66. expect($(occupant).attr('class')).toBe('moderator');
  67. expect($(occupant).attr('title')).toBe('This user is a moderator');
  68. }, converse));
  69. it("allows the user to invite their roster contacts to enter the chat room", $.proxy(function () {
  70. }, converse));
  71. it("shows received groupchat messages", $.proxy(function () {
  72. spyOn(converse, 'emit');
  73. var view = this.chatboxviews.get('lounge@muc.localhost');
  74. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  75. var nick = mock.chatroom_names[0];
  76. var text = 'This is a received message';
  77. var message = $msg({
  78. from: 'lounge@muc.localhost/'+nick,
  79. id: '1',
  80. to: 'dummy@localhost',
  81. type: 'groupchat'
  82. }).c('body').t(text);
  83. view.onChatRoomMessage(message.nodeTree);
  84. var $chat_content = view.$el.find('.chat-content');
  85. expect($chat_content.find('.chat-message').length).toBe(1);
  86. expect($chat_content.find('.chat-message-content').text()).toBe(text);
  87. expect(converse.emit).toHaveBeenCalledWith('message', message.nodeTree);
  88. }, converse));
  89. it("shows sent groupchat messages", $.proxy(function () {
  90. spyOn(converse, 'emit');
  91. var view = this.chatboxviews.get('lounge@muc.localhost');
  92. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  93. var nick = mock.chatroom_names[0];
  94. var text = 'This is a sent message';
  95. view.$el.find('.chat-textarea').text(text);
  96. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  97. expect(converse.emit).toHaveBeenCalledWith('messageSend', text);
  98. var message = $msg({
  99. from: 'lounge@muc.localhost/dummy',
  100. id: '2',
  101. to: 'dummy@localhost.com',
  102. type: 'groupchat'
  103. }).c('body').t(text);
  104. view.onChatRoomMessage(message.nodeTree);
  105. var $chat_content = view.$el.find('.chat-content');
  106. expect($chat_content.find('.chat-message').length).toBe(1);
  107. expect($chat_content.find('.chat-message-content').last().text()).toBe(text);
  108. // We don't emit an event if it's our own message
  109. expect(converse.emit.callCount, 1);
  110. }, converse));
  111. it("can be saved to, and retrieved from, browserStorage", $.proxy(function () {
  112. // We instantiate a new ChatBoxes collection, which by default
  113. // will be empty.
  114. spyOn(this.chatboxviews, 'trimChats');
  115. test_utils.openControlBox();
  116. var newchatboxes = new this.ChatBoxes();
  117. expect(newchatboxes.length).toEqual(0);
  118. // The chatboxes will then be fetched from browserStorage inside the
  119. // onConnected method
  120. newchatboxes.onConnected();
  121. expect(this.chatboxviews.trimChats).toHaveBeenCalled();
  122. expect(newchatboxes.length).toEqual(2); // XXX: Includes controlbox, is this a bug?
  123. // Check that the chatrooms retrieved from browserStorage
  124. // have the same attributes values as the original ones.
  125. attrs = ['id', 'box_id', 'visible'];
  126. for (i=0; i<attrs.length; i++) {
  127. new_attrs = _.pluck(_.pluck(newchatboxes.models, 'attributes'), attrs[i]);
  128. old_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]);
  129. // FIXME: should have have to sort here? Order must
  130. // probably be the same...
  131. // This should be fixed once the controlbox always opens
  132. // only on the right.
  133. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  134. }
  135. this.rosterview.render();
  136. }, converse));
  137. it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'", function () {
  138. var view = this.chatboxviews.get('lounge@muc.localhost'),
  139. trimmed_chatboxes = this.minimized_chats;
  140. spyOn(view, 'minimize').andCallThrough();
  141. spyOn(view, 'maximize').andCallThrough();
  142. spyOn(converse, 'emit');
  143. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  144. runs(function () {
  145. view.$el.find('.toggle-chatbox-button').click();
  146. });
  147. waits(50);
  148. runs(function () {
  149. expect(view.minimize).toHaveBeenCalled();
  150. expect(converse.emit).toHaveBeenCalledWith('chatBoxMinimized', jasmine.any(Object));
  151. expect(converse.emit.callCount, 2);
  152. expect(view.$el.is(':visible')).toBeFalsy();
  153. expect(view.model.get('minimized')).toBeTruthy();
  154. expect(view.minimize).toHaveBeenCalled();
  155. trimmedview = trimmed_chatboxes.get(view.model.get('id'));
  156. trimmedview.$("a.restore-chat").click();
  157. });
  158. waits(250);
  159. runs(function () {
  160. expect(view.maximize).toHaveBeenCalled();
  161. expect(converse.emit).toHaveBeenCalledWith('chatBoxMaximized', jasmine.any(Object));
  162. expect(view.$el.is(':visible')).toBeTruthy();
  163. expect(view.model.get('minimized')).toBeFalsy();
  164. expect(converse.emit.callCount, 3);
  165. });
  166. }.bind(converse));
  167. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
  168. var view = this.chatboxviews.get('lounge@muc.localhost'), chatroom = view.model, $el;
  169. spyOn(view, 'close').andCallThrough();
  170. spyOn(converse, 'emit');
  171. spyOn(converse.connection.muc, 'leave');
  172. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  173. runs(function () {
  174. view.$el.find('.close-chatbox-button').click();
  175. });
  176. waits(50);
  177. runs(function () {
  178. expect(view.close).toHaveBeenCalled();
  179. expect(this.connection.muc.leave).toHaveBeenCalled();
  180. expect(this.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  181. }.bind(converse));
  182. }, converse));
  183. }, converse));
  184. describe("When attempting to enter a chatroom", $.proxy(function () {
  185. beforeEach($.proxy(function () {
  186. var roomspanel = this.chatboxviews.get('controlbox').roomspanel;
  187. var $input = roomspanel.$el.find('input.new-chatroom-name');
  188. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  189. var $server = roomspanel.$el.find('input.new-chatroom-server');
  190. $input.val('problematic');
  191. $nick.val('dummy');
  192. $server.val('muc.localhost');
  193. roomspanel.$el.find('form').submit();
  194. }, converse));
  195. afterEach($.proxy(function () {
  196. var view = this.chatboxviews.get('problematic@muc.localhost');
  197. view.close();
  198. }, converse));
  199. it("will show an error message if the room requires a password", $.proxy(function () {
  200. var presence = $pres().attrs({
  201. from:'coven@chat.shakespeare.lit/thirdwitch',
  202. id:'n13mt3l',
  203. to:'hag66@shakespeare.lit/pda',
  204. type:'error'})
  205. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  206. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
  207. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  208. var view = this.chatboxviews.get('problematic@muc.localhost');
  209. spyOn(view, 'renderPasswordForm').andCallThrough();
  210. runs(function () {
  211. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  212. });
  213. waits(250);
  214. runs(function () {
  215. var $chat_body = view.$el.find('.chat-body');
  216. expect(view.renderPasswordForm).toHaveBeenCalled();
  217. expect($chat_body.find('form.chatroom-form').length).toBe(1);
  218. expect($chat_body.find('legend').text()).toBe('This chatroom requires a password');
  219. });
  220. }, converse));
  221. it("will show an error message if the room is members-only and the user not included", $.proxy(function () {
  222. var presence = $pres().attrs({
  223. from:'coven@chat.shakespeare.lit/thirdwitch',
  224. id:'n13mt3l',
  225. to:'hag66@shakespeare.lit/pda',
  226. type:'error'})
  227. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  228. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
  229. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  230. var view = this.chatboxviews.get('problematic@muc.localhost');
  231. spyOn(view, 'showErrorMessage').andCallThrough();
  232. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  233. expect(view.$el.find('.chat-body p').text()).toBe('You are not on the member list of this room');
  234. }, converse));
  235. it("will show an error message if the user has been banned", $.proxy(function () {
  236. var presence = $pres().attrs({
  237. from:'coven@chat.shakespeare.lit/thirdwitch',
  238. id:'n13mt3l',
  239. to:'hag66@shakespeare.lit/pda',
  240. type:'error'})
  241. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  242. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
  243. .c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  244. var view = this.chatboxviews.get('problematic@muc.localhost');
  245. spyOn(view, 'showErrorMessage').andCallThrough();
  246. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  247. expect(view.$el.find('.chat-body p').text()).toBe('You have been banned from this room');
  248. }, converse));
  249. it("will show an error message if no nickname was specified for the user", $.proxy(function () {
  250. var presence = $pres().attrs({
  251. from:'coven@chat.shakespeare.lit/thirdwitch',
  252. id:'n13mt3l',
  253. to:'hag66@shakespeare.lit/pda',
  254. type:'error'})
  255. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  256. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'modify'})
  257. .c('jid-malformed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  258. var view = this.chatboxviews.get('problematic@muc.localhost');
  259. spyOn(view, 'showErrorMessage').andCallThrough();
  260. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  261. expect(view.$el.find('.chat-body p').text()).toBe('No nickname was specified');
  262. }, converse));
  263. it("will show an error message if the user is not allowed to have created the room", $.proxy(function () {
  264. var presence = $pres().attrs({
  265. from:'coven@chat.shakespeare.lit/thirdwitch',
  266. id:'n13mt3l',
  267. to:'hag66@shakespeare.lit/pda',
  268. type:'error'})
  269. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  270. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  271. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  272. var view = this.chatboxviews.get('problematic@muc.localhost');
  273. spyOn(view, 'showErrorMessage').andCallThrough();
  274. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  275. expect(view.$el.find('.chat-body p').text()).toBe('You are not allowed to create new rooms');
  276. }, converse));
  277. it("will show an error message if the user's nickname doesn't conform to room policy", $.proxy(function () {
  278. var presence = $pres().attrs({
  279. from:'coven@chat.shakespeare.lit/thirdwitch',
  280. id:'n13mt3l',
  281. to:'hag66@shakespeare.lit/pda',
  282. type:'error'})
  283. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  284. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  285. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  286. var view = this.chatboxviews.get('problematic@muc.localhost');
  287. spyOn(view, 'showErrorMessage').andCallThrough();
  288. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  289. expect(view.$el.find('.chat-body p').text()).toBe("Your nickname doesn't conform to this room's policies");
  290. }, converse));
  291. it("will show an error message if the user's nickname is already taken", $.proxy(function () {
  292. var presence = $pres().attrs({
  293. from:'coven@chat.shakespeare.lit/thirdwitch',
  294. id:'n13mt3l',
  295. to:'hag66@shakespeare.lit/pda',
  296. type:'error'})
  297. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  298. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  299. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  300. var view = this.chatboxviews.get('problematic@muc.localhost');
  301. spyOn(view, 'showErrorMessage').andCallThrough();
  302. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  303. expect(view.$el.find('.chat-body p').text()).toBe("Your nickname is already taken");
  304. }, converse));
  305. it("will show an error message if the room doesn't yet exist", $.proxy(function () {
  306. var presence = $pres().attrs({
  307. from:'coven@chat.shakespeare.lit/thirdwitch',
  308. id:'n13mt3l',
  309. to:'hag66@shakespeare.lit/pda',
  310. type:'error'})
  311. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  312. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  313. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  314. var view = this.chatboxviews.get('problematic@muc.localhost');
  315. spyOn(view, 'showErrorMessage').andCallThrough();
  316. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  317. expect(view.$el.find('.chat-body p').text()).toBe("This room does not (yet) exist");
  318. }, converse));
  319. it("will show an error message if the room has reached it's maximum number of occupants", $.proxy(function () {
  320. var presence = $pres().attrs({
  321. from:'coven@chat.shakespeare.lit/thirdwitch',
  322. id:'n13mt3l',
  323. to:'hag66@shakespeare.lit/pda',
  324. type:'error'})
  325. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  326. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  327. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  328. var view = this.chatboxviews.get('problematic@muc.localhost');
  329. spyOn(view, 'showErrorMessage').andCallThrough();
  330. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  331. expect(view.$el.find('.chat-body p').text()).toBe("This room has reached it's maximum number of occupants");
  332. }, converse));
  333. }, converse));
  334. }, converse, mock, test_utils));
  335. }));