Add check for pinning when the answer is already pinned

This commit is contained in:
Karina Kwiatek 2023-02-07 22:55:39 +01:00
parent 04303c667e
commit baea942975
2 changed files with 16 additions and 0 deletions

View File

@ -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

View File

@ -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