forked from e621ng/e621ng
[ForumPosts] Check for existence of topic & category (#524)
* [ForumPosts] Check for existence of topic & category * [Tests] Add invalid topic category_id test * Tweak how it is getting validated If the category doesn't exist there's not point in doing anything further * Move response assert before the other one Should always be that way. If the response doesn't have the expected format the assert will for sure fail and produce a confusing error message --------- Co-authored-by: Earlopain <14981592+Earlopain@users.noreply.github.com>
This commit is contained in:
parent
0958724683
commit
10e9304f17
@ -6,10 +6,10 @@ class ForumTopic < ApplicationRecord
|
||||
has_one :original_post, -> {order("forum_posts.id asc")}, class_name: "ForumPost", foreign_key: "topic_id", inverse_of: :topic
|
||||
has_many :subscriptions, :class_name => "ForumSubscription"
|
||||
before_validation :initialize_is_hidden, :on => :create
|
||||
validate :category_valid
|
||||
validates :title, :creator_id, presence: true
|
||||
validates_associated :original_post
|
||||
validates_presence_of :original_post
|
||||
validates_associated :category
|
||||
validates :title, :length => {:maximum => 250}
|
||||
validate :category_allows_creation, on: :create
|
||||
accepts_nested_attributes_for :original_post
|
||||
@ -36,6 +36,12 @@ class ForumTopic < ApplicationRecord
|
||||
category.name
|
||||
end
|
||||
|
||||
def category_valid
|
||||
return if category
|
||||
errors.add(:category, "is invalid")
|
||||
throw :abort
|
||||
end
|
||||
|
||||
def category_allows_creation
|
||||
if category && !category.can_create_within?(creator)
|
||||
errors.add(:category, "does not allow new topics")
|
||||
|
@ -102,6 +102,13 @@ class ForumTopicsControllerTest < ActionDispatch::IntegrationTest
|
||||
forum_topic = ForumTopic.last
|
||||
assert_redirected_to(forum_topic_path(forum_topic))
|
||||
end
|
||||
|
||||
should "fail with expected error if invalid category_id is provided" do
|
||||
post_auth forum_topics_path, @user, params: { forum_topic: { title: "bababa", category_id: 0, original_post_attributes: { body: "xaxaxa" } }, format: :json }
|
||||
|
||||
assert_response :unprocessable_entity
|
||||
assert_includes(@response.parsed_body.dig("errors", "category"), "must exist")
|
||||
end
|
||||
end
|
||||
|
||||
context "destroy action" do
|
||||
|
Loading…
Reference in New Issue
Block a user