diff --git a/app/controllers/ajax/smile_controller.rb b/app/controllers/ajax/smile_controller.rb index 3c1721ee..3f66b871 100644 --- a/app/controllers/ajax/smile_controller.rb +++ b/app/controllers/ajax/smile_controller.rb @@ -6,6 +6,10 @@ class Ajax::SmileController < AjaxController begin current_user.smile answer + rescue Errors::Base => e + @response[:status] = e.code + @response[:message] = e.locale_tag + return rescue => e Sentry.capture_exception(e) @response[:status] = :fail @@ -44,6 +48,10 @@ class Ajax::SmileController < AjaxController begin current_user.smile_comment comment + rescue Errors::Base => e + @response[:status] = e.code + @response[:message] = e.locale_tag + return rescue => e Sentry.capture_exception(e) @response[:status] = :fail diff --git a/lib/errors.rb b/lib/errors.rb index 10cb15cd..af69f472 100644 --- a/lib/errors.rb +++ b/lib/errors.rb @@ -81,15 +81,19 @@ module Errors end class ReactingSelfBlockedOther < SelfBlockedOther + @locale_tag = "errors.self_blocked_other" end class ReactingOtherBlockedSelf < OtherBlockedSelf + @locale_tag = "errors.other_blocked_self" end class ListingSelfBlockedOther < SelfBlockedOther + @locale_tag = "errors.self_blocked_other" end class ListingOtherBlockedSelf < OtherBlockedSelf + @locale_tag = "errors.other_blocked_self" end # endregion end diff --git a/spec/controllers/ajax/smile_controller_spec.rb b/spec/controllers/ajax/smile_controller_spec.rb index 07a20de4..553b89cc 100644 --- a/spec/controllers/ajax/smile_controller_spec.rb +++ b/spec/controllers/ajax/smile_controller_spec.rb @@ -81,6 +81,54 @@ describe Ajax::SmileController, :ajax_controller, type: :controller do include_examples "returns the expected response" end + + context "when blocked by the answer's author" do + let(:other_user) { FactoryBot.create(:user) } + let(:answer) { FactoryBot.create(:answer, user: other_user) } + let(:answer_id) { answer.id } + + before do + other_user.block(user) + end + + let(:expected_response) do + { + "success" => false, + "status" => "fail", + "message" => anything + } + end + + it "does not create a smile" do + expect { subject }.not_to(change { Smile.count }) + end + + include_examples "returns the expected response" + end + + context "when blocking the answer's author" do + let(:other_user) { FactoryBot.create(:user) } + let(:answer) { FactoryBot.create(:answer, user: user) } + let(:answer_id) { answer.id } + + before do + user.block(other_user) + end + + let(:expected_response) do + { + "success" => false, + "status" => "fail", + "message" => anything + } + end + + it "does not create a smile" do + expect { subject }.not_to(change { Smile.count }) + end + + include_examples "returns the expected response" + end end describe "#destroy" do