ChatRoomSpec.js 14 KB

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