ChatRoomSpec.js 13 KB

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