From baea94297514c2691f90238fce7f8e85de42ce37 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Tue, 7 Feb 2023 22:55:39 +0100 Subject: [PATCH] Add check for pinning when the answer is already pinned --- lib/use_case/answer/pin.rb | 5 +++++ spec/lib/use_case/answer/pin_spec.rb | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/use_case/answer/pin.rb b/lib/use_case/answer/pin.rb index a14e8674..0e0d76d4 100644 --- a/lib/use_case/answer/pin.rb +++ b/lib/use_case/answer/pin.rb @@ -8,6 +8,7 @@ module UseCase def call check_ownership! + check_unpinned! answer.pinned_at = Time.now.utc answer.save! @@ -23,6 +24,10 @@ module UseCase def check_ownership! raise ::Errors::NotAuthorized unless answer.user == user end + + def check_unpinned! + raise ::Errors::BadRequest if answer.pinned_at.present? + end end end end diff --git a/spec/lib/use_case/answer/pin_spec.rb b/spec/lib/use_case/answer/pin_spec.rb index 920d8f02..26fa29d4 100644 --- a/spec/lib/use_case/answer/pin_spec.rb +++ b/spec/lib/use_case/answer/pin_spec.rb @@ -18,6 +18,17 @@ describe UseCase::Answer::Pin do expect { subject }.to change { answer.pinned_at }.from(nil).to(Time.at(1603290950).utc) end end + + context "answer is already pinned" do + before do + answer.update!(pinned_at: Time.at(1603290950).utc) + end + + it "raises an error" do + expect { subject }.to raise_error(Errors::BadRequest) + expect(answer.reload.pinned_at).to eq(Time.at(1603290950).utc) + end + end end context "as other user" do