diff --git a/config/locales/errors.en.yml b/config/locales/errors.en.yml index 339bcec5..6624e7e7 100644 --- a/config/locales/errors.en.yml +++ b/config/locales/errors.en.yml @@ -34,4 +34,4 @@ en: record_not_found: "Record not found" - login_required: "You need to be logged in to perform this action" + not_authorized: "You need to be logged in to perform this action" diff --git a/lib/errors.rb b/lib/errors.rb index 1a887285..a1b1cdba 100644 --- a/lib/errors.rb +++ b/lib/errors.rb @@ -44,6 +44,12 @@ module Errors end end + class NotAuthorized < Base + def status + 401 + end + end + class UserNotFound < NotFound end @@ -51,9 +57,6 @@ module Errors class Blocked < Forbidden end - class LoginRequired < Forbidden - end - class OtherBlockedSelf < Blocked end diff --git a/lib/use_case/question/create.rb b/lib/use_case/question/create.rb index 00545a98..4cf426ee 100644 --- a/lib/use_case/question/create.rb +++ b/lib/use_case/question/create.rb @@ -61,7 +61,7 @@ module UseCase end def check_user - raise Errors::LoginRequired if target_user.privacy_require_user && !source_user_id + raise Errors::NotAuthorized if target_user.privacy_require_user && !source_user_id end def increment_asked_count diff --git a/spec/lib/use_case/question/create_spec.rb b/spec/lib/use_case/question/create_spec.rb index f4205b95..23972032 100644 --- a/spec/lib/use_case/question/create_spec.rb +++ b/spec/lib/use_case/question/create_spec.rb @@ -44,6 +44,12 @@ describe UseCase::Question::Create do end end + shared_examples "not authorized" do + it "raises an error" do + expect { subject }.to raise_error(Errors::NotAuthorized) + end + end + shared_examples "validates content" do context "content is empty" do let(:content) { "" } @@ -186,7 +192,7 @@ describe UseCase::Question::Create do target_user.update!(privacy_require_user: true) end - it_behaves_like "forbidden" + it_behaves_like "not authorized" end end end