Respect allow long questions setting

This commit is contained in:
Karina Kwiatek 2023-01-08 14:03:54 +01:00
parent e2f6284982
commit 3a6814b908
6 changed files with 11 additions and 7 deletions

View File

@ -5,7 +5,9 @@ class Question < ApplicationRecord
has_many :answers, dependent: :destroy has_many :answers, dependent: :destroy
has_many :inboxes, dependent: :destroy has_many :inboxes, dependent: :destroy
validates :content, length: { minimum: 1, maximum: 512 } validates :content, length: { minimum: 1 }
SHORT_QUESTION_MAX_LENGTH = 512
before_destroy do before_destroy do
rep = Report.where(target_id: self.id, type: 'Reports::Question') rep = Report.where(target_id: self.id, type: 'Reports::Question')
@ -30,4 +32,6 @@ class Question < ApplicationRecord
def generated? = %w[justask retrospring_exporter].include?(author_identifier) def generated? = %w[justask retrospring_exporter].include?(author_identifier)
def anonymous? = author_is_anonymous && author_identifier.present? def anonymous? = author_is_anonymous && author_identifier.present?
def long? = content.length > SHORT_QUESTION_MAX_LENGTH
end end

View File

@ -30,6 +30,7 @@ class QuestionWorker
return true if follower.banned? return true if follower.banned?
return true if muted?(follower, question) return true if muted?(follower, question)
return true if user.muting?(question.user) return true if user.muting?(question.user)
return true if question.long? && !user.profile.allow_long_questions
false false
end end

View File

@ -28,6 +28,9 @@ module Errors
class InvalidBanDuration < BadRequest class InvalidBanDuration < BadRequest
end end
class QuestionTooLong < BadRequest
end
class Forbidden < Base class Forbidden < Base
def status def status
403 403

View File

@ -63,6 +63,7 @@ module UseCase
def check_user def check_user
raise Errors::NotAuthorized if target_user.privacy_require_user && !source_user_id raise Errors::NotAuthorized if target_user.privacy_require_user && !source_user_id
raise Errors::QuestionTooLong if content.length > ::Question::SHORT_QUESTION_MAX_LENGTH && !target_user.profile.allow_long_questions
end end
def create_question def create_question

View File

@ -61,7 +61,7 @@ describe UseCase::Question::Create do
let(:content) { "a" * 513 } let(:content) { "a" * 513 }
it "raises an error" do it "raises an error" do
expect { subject }.to raise_error(ActiveRecord::RecordInvalid) expect { subject }.to raise_error(Errors::QuestionTooLong)
end end
end end
end end

View File

@ -16,11 +16,6 @@ RSpec.describe Question, :type => :model do
expect(@question.content).to match 'Is this a question?' expect(@question.content).to match 'Is this a question?'
end end
it 'does not save questions longer than 512 characters' do
@question.content = 'X' * 513
expect{@question.save!}.to raise_error(ActiveRecord::RecordInvalid)
end
it 'has many answers' do it 'has many answers' do
5.times { |i| Answer.create(content: "This is an answer. #{i}", user: FactoryBot.create(:user), question: @question) } 5.times { |i| Answer.create(content: "This is an answer. #{i}", user: FactoryBot.create(:user), question: @question) }
expect(@question.answer_count).to match 5 expect(@question.answer_count).to match 5