ChatRoomSpec.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. (function (root, factory) {
  2. define([
  3. "converse",
  4. "mock"
  5. ], function (converse, mock_connection) {
  6. return factory(converse, mock_connection);
  7. }
  8. );
  9. } (this, function (converse, mock_connection) {
  10. return describe("ChatRooms", $.proxy(function() {
  11. var chatroom_names = [
  12. 'Dyon van de Wege', 'Thomas Kalb', 'Dirk Theissen', 'Felix Hofmann', 'Ka Lek', 'Anne Ebersbacher'
  13. ];
  14. closeChatRoom = function (name) {
  15. converse.chatboxesview.views['lounge@muc.localhost'].closeChat();
  16. };
  17. describe("A Chat Room", $.proxy(function () {
  18. beforeEach($.proxy(function () {
  19. if (!$("div#controlbox").is(':visible')) {
  20. $('.toggle-online-users').click();
  21. }
  22. var roomspanel = this.chatboxesview.views.controlbox.roomspanel;
  23. var $input = roomspanel.$el.find('input.new-chatroom-name');
  24. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  25. var $server = roomspanel.$el.find('input.new-chatroom-server');
  26. $input.val('lounge');
  27. $nick.val('dummy');
  28. $server.val('muc.localhost');
  29. roomspanel.$el.find('form').submit();
  30. $('.toggle-online-users').click();
  31. }, converse));
  32. it("shows users currently present in the room", $.proxy(function () {
  33. var chatroomview = this.chatboxesview.views['lounge@muc.localhost'],
  34. $participant_list;
  35. var roster = {}, room = {}, i;
  36. for (i=0; i<chatroom_names.length-1; i++) {
  37. roster[chatroom_names[i]] = {};
  38. chatroomview.onChatRoomRoster(roster, room);
  39. $participant_list = chatroomview.$el.find('.participant-list');
  40. expect($participant_list.find('li').length).toBe(1+i);
  41. expect($($participant_list.find('li')[i]).text()).toBe(chatroom_names[i]);
  42. }
  43. roster[converse.bare_jid] = {};
  44. chatroomview.onChatRoomRoster(roster, room);
  45. }, converse));
  46. it("indicates moderators by means of a special css class and tooltip", $.proxy(function () {
  47. var chatroomview = this.chatboxesview.views['lounge@muc.localhost'];
  48. var roster = {}, idx = chatroom_names.length-1;
  49. roster[chatroom_names[idx]] = {};
  50. roster[chatroom_names[idx]].role = 'moderator';
  51. chatroomview.onChatRoomRoster(roster, {});
  52. var occupant = chatroomview.$el.find('.participant-list').find('li');
  53. expect(occupant.length).toBe(1);
  54. expect($(occupant).text()).toBe(chatroom_names[idx]);
  55. expect($(occupant).attr('class')).toBe('moderator');
  56. expect($(occupant).attr('title')).toBe('This user is a moderator');
  57. }, converse));
  58. it("shows received and sent groupchat messages", $.proxy(function () {
  59. var view = this.chatboxesview.views['lounge@muc.localhost'];
  60. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  61. var nick = chatroom_names[0];
  62. var text = 'This is a received message';
  63. var message = $msg({
  64. from: 'lounge@muc.localhost/'+nick,
  65. id: '1',
  66. to: 'dummy@localhost',
  67. type: 'groupchat'
  68. }).c('body').t(text);
  69. view.onChatRoomMessage(message.nodeTree);
  70. var $chat_content = view.$el.find('.chat-content');
  71. expect($chat_content.find('.chat-message').length).toBe(1);
  72. expect($chat_content.find('.chat-message-content').text()).toBe(text);
  73. }, converse));
  74. it("can be saved to, and retrieved from, localStorage", $.proxy(function () {
  75. // We instantiate a new ChatBoxes collection, which by default
  76. // will be empty.
  77. var newchatboxes = new this.ChatBoxes();
  78. expect(newchatboxes.length).toEqual(0);
  79. // The chatboxes will then be fetched from localStorage inside the
  80. // onConnected method
  81. newchatboxes.onConnected();
  82. expect(newchatboxes.length).toEqual(1);
  83. // Check that the chatrooms retrieved from localStorage
  84. // have the same attributes values as the original ones.
  85. attrs = ['id', 'box_id', 'visible'];
  86. for (i=0; i<attrs.length; i++) {
  87. new_attrs = _.pluck(_.pluck(newchatboxes.models, 'attributes'), attrs[i]);
  88. old_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]);
  89. expect(_.isEqual(new_attrs, old_attrs)).toEqual(true);
  90. }
  91. this.rosterview.render();
  92. }, converse));
  93. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
  94. var view = this.chatboxesview.views['lounge@muc.localhost'], chatroom = view.model, $el;
  95. spyOn(view, 'closeChat').andCallThrough();
  96. spyOn(converse.connection.muc, 'leave');
  97. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  98. view.$el.find('.close-chatbox-button').click();
  99. expect(view.closeChat).toHaveBeenCalled();
  100. expect(converse.connection.muc.leave).toHaveBeenCalled();
  101. }, converse));
  102. }, converse));
  103. describe("When attempting to enter a chatroom", $.proxy(function () {
  104. beforeEach($.proxy(function () {
  105. var roomspanel = this.chatboxesview.views.controlbox.roomspanel;
  106. var $input = roomspanel.$el.find('input.new-chatroom-name');
  107. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  108. var $server = roomspanel.$el.find('input.new-chatroom-server');
  109. $input.val('problematic');
  110. $nick.val('dummy');
  111. $server.val('muc.localhost');
  112. roomspanel.$el.find('form').submit();
  113. }, converse));
  114. afterEach($.proxy(function () {
  115. var view = this.chatboxesview.views['problematic@muc.localhost'];
  116. view.closeChat();
  117. }, converse));
  118. it("will show an error message if the room requires a password", $.proxy(function () {
  119. var presence = $pres().attrs({
  120.   from:'coven@chat.shakespeare.lit/thirdwitch',
  121.     id:'n13mt3l',
  122.     to:'hag66@shakespeare.lit/pda',
  123.     type:'error'})
  124. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  125. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
  126. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  127. var view = this.chatboxesview.views['problematic@muc.localhost'];
  128. spyOn(converse.connection.muc, 'removeRoom');
  129. spyOn(view, 'renderPasswordForm').andCallThrough();
  130. runs(function () {
  131. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  132. });
  133. waits(250);
  134. runs(function () {
  135. var $chat_body = view.$el.find('.chat-body');
  136. expect(converse.connection.muc.removeRoom).toHaveBeenCalled();
  137. expect(view.renderPasswordForm).toHaveBeenCalled();
  138. expect($chat_body.find('form.chatroom-form').length).toBe(1);
  139. expect($chat_body.find('legend').text()).toBe('This chat room requires a password');
  140. });
  141. }, converse));
  142. it("will show an error message if the room is members-only and the user not included", $.proxy(function () {
  143. var presence = $pres().attrs({
  144.   from:'coven@chat.shakespeare.lit/thirdwitch',
  145.     id:'n13mt3l',
  146.     to:'hag66@shakespeare.lit/pda',
  147.     type:'error'})
  148. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  149. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
  150. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  151. var view = this.chatboxesview.views['problematic@muc.localhost'];
  152. spyOn(converse.connection.muc, 'removeRoom');
  153. spyOn(view, 'renderErrorMessage').andCallThrough();
  154. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  155. expect(converse.connection.muc.removeRoom).toHaveBeenCalled();
  156. expect(view.$el.find('.chat-body p').text()).toBe('You are not on the member list of this room');
  157. }, converse));
  158. it("will show an error message if the user has been banned", $.proxy(function () {
  159. var presence = $pres().attrs({
  160.   from:'coven@chat.shakespeare.lit/thirdwitch',
  161.     id:'n13mt3l',
  162.     to:'hag66@shakespeare.lit/pda',
  163.     type:'error'})
  164. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  165. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
  166. .c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  167. var view = this.chatboxesview.views['problematic@muc.localhost'];
  168. spyOn(converse.connection.muc, 'removeRoom');
  169. spyOn(view, 'renderErrorMessage').andCallThrough();
  170. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  171. expect(converse.connection.muc.removeRoom).toHaveBeenCalled();
  172. expect(view.$el.find('.chat-body p').text()).toBe('You have been banned from this room');
  173. }, converse));
  174. it("will show an error message if no nickname was specified for the user", $.proxy(function () {
  175. var presence = $pres().attrs({
  176.   from:'coven@chat.shakespeare.lit/thirdwitch',
  177.     id:'n13mt3l',
  178.     to:'hag66@shakespeare.lit/pda',
  179.     type:'error'})
  180. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  181. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'modify'})
  182. .c('jid-malformed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  183. var view = this.chatboxesview.views['problematic@muc.localhost'];
  184. spyOn(converse.connection.muc, 'removeRoom');
  185. spyOn(view, 'renderErrorMessage').andCallThrough();
  186. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  187. expect(converse.connection.muc.removeRoom).toHaveBeenCalled();
  188. expect(view.$el.find('.chat-body p').text()).toBe('No nickname was specified');
  189. }, converse));
  190. it("will show an error message if the user is not allowed to have created the room", $.proxy(function () {
  191. var presence = $pres().attrs({
  192.   from:'coven@chat.shakespeare.lit/thirdwitch',
  193.     id:'n13mt3l',
  194.     to:'hag66@shakespeare.lit/pda',
  195.     type:'error'})
  196. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  197. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  198. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  199. var view = this.chatboxesview.views['problematic@muc.localhost'];
  200. spyOn(converse.connection.muc, 'removeRoom');
  201. spyOn(view, 'renderErrorMessage').andCallThrough();
  202. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  203. expect(converse.connection.muc.removeRoom).toHaveBeenCalled();
  204. expect(view.$el.find('.chat-body p').text()).toBe('You are not allowed to create new rooms');
  205. }, converse));
  206. it("will show an error message if the user's nickname doesn't conform to room policy", $.proxy(function () {
  207. var presence = $pres().attrs({
  208.   from:'coven@chat.shakespeare.lit/thirdwitch',
  209.     id:'n13mt3l',
  210.     to:'hag66@shakespeare.lit/pda',
  211.     type:'error'})
  212. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  213. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  214. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  215. var view = this.chatboxesview.views['problematic@muc.localhost'];
  216. spyOn(converse.connection.muc, 'removeRoom');
  217. spyOn(view, 'renderErrorMessage').andCallThrough();
  218. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  219. expect(converse.connection.muc.removeRoom).toHaveBeenCalled();
  220. expect(view.$el.find('.chat-body p').text()).toBe("Your nickname doesn't conform to this room's policies");
  221. }, converse));
  222. it("will show an error message if the user's nickname is already taken", $.proxy(function () {
  223. var presence = $pres().attrs({
  224.   from:'coven@chat.shakespeare.lit/thirdwitch',
  225.     id:'n13mt3l',
  226.     to:'hag66@shakespeare.lit/pda',
  227.     type:'error'})
  228. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  229. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  230. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  231. var view = this.chatboxesview.views['problematic@muc.localhost'];
  232. spyOn(converse.connection.muc, 'removeRoom');
  233. spyOn(view, 'renderErrorMessage').andCallThrough();
  234. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  235. expect(converse.connection.muc.removeRoom).toHaveBeenCalled();
  236. expect(view.$el.find('.chat-body p').text()).toBe("Your nickname is already taken");
  237. }, converse));
  238. it("will show an error message if the room doesn't yet exist", $.proxy(function () {
  239. var presence = $pres().attrs({
  240.   from:'coven@chat.shakespeare.lit/thirdwitch',
  241.     id:'n13mt3l',
  242.     to:'hag66@shakespeare.lit/pda',
  243.     type:'error'})
  244. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  245. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  246. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  247. var view = this.chatboxesview.views['problematic@muc.localhost'];
  248. spyOn(converse.connection.muc, 'removeRoom');
  249. spyOn(view, 'renderErrorMessage').andCallThrough();
  250. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  251. expect(converse.connection.muc.removeRoom).toHaveBeenCalled();
  252. expect(view.$el.find('.chat-body p').text()).toBe("This room does not (yet) exist");
  253. }, converse));
  254. it("will show an error message if the room has reached it's maximum number of occupants", $.proxy(function () {
  255. var presence = $pres().attrs({
  256.   from:'coven@chat.shakespeare.lit/thirdwitch',
  257.     id:'n13mt3l',
  258.     to:'hag66@shakespeare.lit/pda',
  259.     type:'error'})
  260. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  261. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  262. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  263. var view = this.chatboxesview.views['problematic@muc.localhost'];
  264. spyOn(converse.connection.muc, 'removeRoom');
  265. spyOn(view, 'renderErrorMessage').andCallThrough();
  266. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  267. expect(converse.connection.muc.removeRoom).toHaveBeenCalled();
  268. expect(view.$el.find('.chat-body p').text()).toBe("This room has reached it's maximum number of occupants");
  269. }, converse));
  270. }, converse));
  271. }, converse));
  272. }));