Add logic to parse error messages when trying to enter a room

This commit is contained in:
JC Brand 2013-05-11 09:59:32 +02:00
parent c392a4e598
commit 7c1a9242d5
3 changed files with 178 additions and 7 deletions

View File

@ -1092,9 +1092,32 @@
} }
} }
} else { } else {
var error = $presence.find('error'); var $error = $presence.find('error'),
if ($(error).attr('type') == 'auth') { $chat_content = this.$el.find('.chat-content');
this.$el.find('.chat-content').append('Sorry, this chatroom is restricted'); if ($error.attr('type') == 'auth') {
if ($error.find('not-authorized').length) {
$chat_content.append('This chatroom requires a password');
} else if ($error.find('registration-required').length) {
$chat_content.append('You are not on the member list of this room');
} else if ($error.find('forbidden').length) {
$chat_content.append('You have been banned from this room');
}
} else if ($error.attr('type') == 'modify') {
if ($error.find('jid-malformed').length) {
$chat_content.append('No nickname was specified');
}
} else if ($error.attr('type') == 'cancel') {
if ($error.find('not-allowed').length) {
$chat_content.append('You are not allowed to create new rooms');
} else if ($error.find('not-acceptable').length) {
$chat_content.append("Your nickname doesn't conform to the room's policies");
} else if ($error.find('conflict').length) {
$chat_content.append("Your nickname is already taken");
} else if ($error.find('item-not-found').length) {
$chat_content.append("This room does not (yet) exist");
} else if ($error.find('service-unavailable').length) {
$chat_content.append("This room has reached it's maximum number of occupants");
}
} }
} }
return true; return true;
@ -1419,7 +1442,6 @@
this.$el.addClass('current-xmpp-contact'); this.$el.addClass('current-xmpp-contact');
this.$el.html(this.template(item.toJSON())); this.$el.html(this.template(item.toJSON()));
} }
return this; return this;
}, },
@ -1877,7 +1899,6 @@
converse.connection.send($pres().c('show').t(this.get('status')).up().c('status').t(status_message)); converse.connection.send($pres().c('show').t(this.get('status')).up().c('status').t(status_message));
this.save({'status_message': status_message}); this.save({'status_message': status_message});
} }
}); });
converse.XMPPStatusView = Backbone.View.extend({ converse.XMPPStatusView = Backbone.View.extend({
@ -1909,7 +1930,6 @@
'<a class="change-xmpp-status-message" href="#" Title="Click here to write a custom status message"></a>' + '<a class="change-xmpp-status-message" href="#" Title="Click here to write a custom status message"></a>' +
'</div>'), '</div>'),
renderStatusChangeForm: function (ev) { renderStatusChangeForm: function (ev) {
ev.preventDefault(); ev.preventDefault();
var status_message = this.model.get('status') || 'offline'; var status_message = this.model.get('status') || 'offline';

View File

@ -713,5 +713,156 @@
expect(converse.connection.muc.leave).toHaveBeenCalled(); expect(converse.connection.muc.leave).toHaveBeenCalled();
}, converse)); }, converse));
}, converse)); }, converse));
describe("When attempting to enter a chatroom", $.proxy(function () {
beforeEach($.proxy(function () {
var roomspanel = this.chatboxesview.views.controlbox.roomspanel;
var $input = roomspanel.$el.find('input.new-chatroom-name');
var $server = roomspanel.$el.find('input.new-chatroom-server');
$input.val('problematic');
$server.val('muc.localhost');
roomspanel.$el.find('form').submit();
}, converse));
afterEach($.proxy(function () {
var view = this.chatboxesview.views['problematic@muc.localhost'];
view.closeChat();
}, converse));
it("will show an error message if the room requires a password", $.proxy(function () {
var presence = $pres().attrs({
  from:'coven@chat.shakespeare.lit/thirdwitch',
    id:'n13mt3l',
    to:'hag66@shakespeare.lit/pda',
    type:'error'})
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
.c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
view.onChatRoomPresence(presence, {'nick': 'dummy'});
var $chat_content = view.$el.find('.chat-content');
expect($chat_content.text()).toBe('This chatroom requires a password');
}, converse));
it("will show an error message if the room is members-only and the user not included", $.proxy(function () {
var presence = $pres().attrs({
  from:'coven@chat.shakespeare.lit/thirdwitch',
    id:'n13mt3l',
    to:'hag66@shakespeare.lit/pda',
    type:'error'})
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
.c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
view.onChatRoomPresence(presence, {'nick': 'dummy'});
var $chat_content = view.$el.find('.chat-content');
expect($chat_content.text()).toBe('You are not on the member list of this room');
}, converse));
it("will show an error message if the user has been banned", $.proxy(function () {
var presence = $pres().attrs({
  from:'coven@chat.shakespeare.lit/thirdwitch',
    id:'n13mt3l',
    to:'hag66@shakespeare.lit/pda',
    type:'error'})
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
.c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
view.onChatRoomPresence(presence, {'nick': 'dummy'});
var $chat_content = view.$el.find('.chat-content');
expect($chat_content.text()).toBe('You have been banned from this room');
}, converse));
it("will show an error message if no nickname was specified for the user", $.proxy(function () {
var presence = $pres().attrs({
  from:'coven@chat.shakespeare.lit/thirdwitch',
    id:'n13mt3l',
    to:'hag66@shakespeare.lit/pda',
    type:'error'})
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'modify'})
.c('jid-malformed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
view.onChatRoomPresence(presence, {'nick': 'dummy'});
var $chat_content = view.$el.find('.chat-content');
expect($chat_content.text()).toBe('No nickname was specified');
}, converse));
it("will show an error message if the user is not allowed to have created the room", $.proxy(function () {
var presence = $pres().attrs({
  from:'coven@chat.shakespeare.lit/thirdwitch',
    id:'n13mt3l',
    to:'hag66@shakespeare.lit/pda',
    type:'error'})
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
.c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
view.onChatRoomPresence(presence, {'nick': 'dummy'});
var $chat_content = view.$el.find('.chat-content');
expect($chat_content.text()).toBe('You are not allowed to create new rooms');
}, converse));
it("will show an error message if the user's nickname doesn't conform to room policy", $.proxy(function () {
var presence = $pres().attrs({
  from:'coven@chat.shakespeare.lit/thirdwitch',
    id:'n13mt3l',
    to:'hag66@shakespeare.lit/pda',
    type:'error'})
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
.c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
view.onChatRoomPresence(presence, {'nick': 'dummy'});
var $chat_content = view.$el.find('.chat-content');
expect($chat_content.text()).toBe("Your nickname doesn't conform to the room's policies");
}, converse));
it("will show an error message if the user's nickname is already taken", $.proxy(function () {
var presence = $pres().attrs({
  from:'coven@chat.shakespeare.lit/thirdwitch',
    id:'n13mt3l',
    to:'hag66@shakespeare.lit/pda',
    type:'error'})
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
.c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
view.onChatRoomPresence(presence, {'nick': 'dummy'});
var $chat_content = view.$el.find('.chat-content');
expect($chat_content.text()).toBe("Your nickname is already taken");
}, converse));
it("will show an error message if the room doesn't yet exist", $.proxy(function () {
var presence = $pres().attrs({
  from:'coven@chat.shakespeare.lit/thirdwitch',
    id:'n13mt3l',
    to:'hag66@shakespeare.lit/pda',
    type:'error'})
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
.c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
view.onChatRoomPresence(presence, {'nick': 'dummy'});
var $chat_content = view.$el.find('.chat-content');
expect($chat_content.text()).toBe("This room does not (yet) exist");
}, converse));
it("will show an error message if the room has reached it's maximum number of occupants", $.proxy(function () {
var presence = $pres().attrs({
  from:'coven@chat.shakespeare.lit/thirdwitch',
    id:'n13mt3l',
    to:'hag66@shakespeare.lit/pda',
    type:'error'})
.c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
.c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
.c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
var view = this.chatboxesview.views['problematic@muc.localhost'];
view.onChatRoomPresence(presence, {'nick': 'dummy'});
var $chat_content = view.$el.find('.chat-content');
expect($chat_content.text()).toBe("This room has reached it's maximum number of occupants");
}, converse));
}, converse));
}, converse)); }, converse));
})); }));

View File

@ -2,7 +2,7 @@ require(["jquery", "spec/MainSpec"], function($) {
$(function($) { $(function($) {
var jasmineEnv = jasmine.getEnv(); var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 500; jasmineEnv.updateInterval = 250;
var htmlReporter = new jasmine.HtmlReporter(); var htmlReporter = new jasmine.HtmlReporter();