FIX: rescue specific errors on invite failure

This commit is contained in:
Arpit Jalan 2017-05-02 15:13:33 +05:30
parent f5b11cb429
commit 77a8cae094
6 changed files with 9 additions and 7 deletions

View File

@ -74,7 +74,7 @@ class InvitesController < ApplicationController
else else
render json: failed_json, status: 422 render json: failed_json, status: 422
end end
rescue => e rescue Invite::UserExists => e
render json: {errors: [e.message]}, status: 422 render json: {errors: [e.message]}, status: 422
end end
end end

View File

@ -497,7 +497,7 @@ class TopicsController < ApplicationController
else else
render json: failed_json, status: 422 render json: failed_json, status: 422
end end
rescue => e rescue Topic::UserExists => e
render json: {errors: [e.message]}, status: 422 render json: {errors: [e.message]}, status: 422
end end
end end

View File

@ -1,6 +1,7 @@
require_dependency 'rate_limiter' require_dependency 'rate_limiter'
class Invite < ActiveRecord::Base class Invite < ActiveRecord::Base
class UserExists < StandardError; end
include RateLimiter::OnCreateRecord include RateLimiter::OnCreateRecord
include Trashable include Trashable
@ -103,7 +104,7 @@ class Invite < ActiveRecord::Base
if user if user
extend_permissions(topic, user, invited_by) if topic extend_permissions(topic, user, invited_by) if topic
raise StandardError.new I18n.t("invite.user_exists", email: lower_email, username: user.username) raise UserExists.new I18n.t("invite.user_exists", email: lower_email, username: user.username)
end end
invite = Invite.with_deleted invite = Invite.with_deleted

View File

@ -10,6 +10,7 @@ require_dependency 'discourse_tagging'
require_dependency 'search' require_dependency 'search'
class Topic < ActiveRecord::Base class Topic < ActiveRecord::Base
class UserExists < StandardError; end
include ActionView::Helpers::SanitizeHelper include ActionView::Helpers::SanitizeHelper
include RateLimiter::OnCreateRecord include RateLimiter::OnCreateRecord
include HasCustomFields include HasCustomFields
@ -722,7 +723,7 @@ SQL
if private_message? if private_message?
# If the user exists, add them to the message. # If the user exists, add them to the message.
user = User.find_by_username_or_email(username_or_email) user = User.find_by_username_or_email(username_or_email)
raise StandardError.new I18n.t("topic_invite.user_exists") if user.present? && topic_allowed_users.where(user_id: user.id).exists? raise UserExists.new I18n.t("topic_invite.user_exists") if user.present? && topic_allowed_users.where(user_id: user.id).exists?
if user && topic_allowed_users.create!(user_id: user.id) if user && topic_allowed_users.create!(user_id: user.id)
# Create a small action message # Create a small action message
@ -747,7 +748,7 @@ SQL
else else
# invite existing member to a topic # invite existing member to a topic
user = User.find_by_username(username_or_email) user = User.find_by_username(username_or_email)
raise StandardError.new I18n.t("topic_invite.user_exists") if user.present? && topic_allowed_users.where(user_id: user.id).exists? raise UserExists.new I18n.t("topic_invite.user_exists") if user.present? && topic_allowed_users.where(user_id: user.id).exists?
if user && topic_allowed_users.create!(user_id: user.id) if user && topic_allowed_users.create!(user_id: user.id)
# rate limit topic invite # rate limit topic invite

View File

@ -162,7 +162,7 @@ describe Invite do
it "works" do it "works" do
# doesn't create an invite # doesn't create an invite
expect { topic.invite_by_email(topic.user, coding_horror.email) }.to raise_error(StandardError) expect { topic.invite_by_email(topic.user, coding_horror.email) }.to raise_error(Invite::UserExists)
# gives the user permission to access the topic # gives the user permission to access the topic
expect(topic.allowed_users.include?(coding_horror)).to eq(true) expect(topic.allowed_users.include?(coding_horror)).to eq(true)

View File

@ -1678,7 +1678,7 @@ describe Topic do
it "should add user to the group" do it "should add user to the group" do
expect(Guardian.new(walter).can_see?(group_private_topic)).to be_falsey expect(Guardian.new(walter).can_see?(group_private_topic)).to be_falsey
expect { group_private_topic.invite(group_manager, walter.email) }.to raise_error(StandardError) expect { group_private_topic.invite(group_manager, walter.email) }.to raise_error(Invite::UserExists)
expect(walter.groups).to include(group) expect(walter.groups).to include(group)
expect(Guardian.new(walter).can_see?(group_private_topic)).to be_truthy expect(Guardian.new(walter).can_see?(group_private_topic)).to be_truthy
end end