Add use case for pinning answers

This commit is contained in:
Karina Kwiatek 2023-01-29 21:00:47 +01:00
parent c2baa86c09
commit ed4ec98455
2 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,28 @@
# frozen_string_literal: true
module UseCase
module Answer
class Pin < UseCase::Base
option :user, type: Types.Instance(::User)
option :answer, type: Types.Instance(::Answer)
def call
check_ownership!
answer.pinned_at = Time.now.utc
answer.save!
{
status: 200,
resource: answer
}
end
private
def check_ownership!
raise ::Errors::NotAuthorized unless answer.user == user
end
end
end
end

View File

@ -0,0 +1,32 @@
# frozen_string_literal: true
require "rails_helper"
describe UseCase::Answer::Pin do
include ActiveSupport::Testing::TimeHelpers
subject { UseCase::Answer::Pin.call(user:, answer:) }
context "answer exists" do
let(:answer) { FactoryBot.create(:answer, user: FactoryBot.create(:user)) }
context "as answer owner" do
let(:user) { answer.user }
it "pins the answer" do
travel_to(Time.at(1603290950).utc) do
expect { subject }.to change { answer.pinned_at }.from(nil).to(Time.at(1603290950).utc)
end
end
end
context "as other user" do
let(:user) { FactoryBot.create(:user) }
it "does not pin the answer" do
expect { subject }.to raise_error(Errors::NotAuthorized)
expect(answer.reload.pinned_at).to eq(nil)
end
end
end
end