From 0eb3f53b45cf2ab101a1eb1aa36208518efa7189 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 12 Jun 2022 21:19:37 +0200 Subject: [PATCH] Add tests for asking questions while blocked --- app/controllers/ajax/question_controller.rb | 10 ++++-- .../ajax/question_controller_spec.rb | 36 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/app/controllers/ajax/question_controller.rb b/app/controllers/ajax/question_controller.rb index e35a9e7a..0df36d3f 100644 --- a/app/controllers/ajax/question_controller.rb +++ b/app/controllers/ajax/question_controller.rb @@ -66,8 +66,14 @@ class Ajax::QuestionController < AjaxController return end - raise Errors::AskingOtherBlockedSelf if target_user.blocking?(current_user) - raise Errors::AskingSelfBlockedOther if current_user&.blocking?(target_user) + if target_user.blocking?(current_user) + question.delete + raise Errors::AskingOtherBlockedSelf + end + if current_user&.blocking?(target_user) + question.delete + raise Errors::AskingSelfBlockedOther + end if !target_user.privacy_allow_anonymous_questions && question.author_is_anonymous question.delete diff --git a/spec/controllers/ajax/question_controller_spec.rb b/spec/controllers/ajax/question_controller_spec.rb index 6055b886..9d0224c3 100644 --- a/spec/controllers/ajax/question_controller_spec.rb +++ b/spec/controllers/ajax/question_controller_spec.rb @@ -127,6 +127,42 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do include_examples "creates the question" end end + + context "when the sender is blocked by the user" do + before(:each) do + target_user.block(user) + end + + let(:anonymous_question) { "false" } + let(:user_allows_anonymous_questions) { true } + let(:expected_response) do + { + "message" => I18n.t("errors.asking_other_blocked_self"), + "status" => "asking_other_blocked_self", + "success" => false + } + end + + include_examples "does not create the question", check_for_inbox: false + end + + context "when the sender is blocking the user" do + before(:each) do + user.block(target_user) + end + + let(:anonymous_question) { "false" } + let(:user_allows_anonymous_questions) { true } + let(:expected_response) do + { + "message" => I18n.t("errors.asking_self_blocked_other"), + "status" => "asking_self_blocked_other", + "success" => false + } + end + + include_examples "does not create the question", check_for_inbox: false + end end context "when rcpt is followers" do