Raise error on unpermitted params.
Fail loudly if we forget to whitelist a param instead of silently ignoring it. misc models: convert to strong params. artist commentaries: convert to strong params. * Disallow changing or setting post_id to a nonexistent post. artists: convert to strong params. * Disallow setting `is_banned` in create/update actions. Changing it this way instead of with the ban/unban actions would leave the artist in a partially banned state. bans: convert to strong params. * Disallow changing the user_id after the ban has been created. comments: convert to strong params. favorite groups: convert to strong params. news updates: convert to strong params. post appeals: convert to strong params. post flags: convert to strong params. * Disallow users from setting the `is_deleted` / `is_resolved` flags. ip bans: convert to strong params. user feedbacks: convert to strong params. * Disallow users from setting `disable_dmail_notification` when creating feedbacks. * Disallow changing the user_id after the feedback has been created. notes: convert to strong params. wiki pages: convert to strong params. * Also fix non-Builders being able to delete wiki pages. saved searches: convert to strong params. pools: convert to strong params. * Disallow setting `post_count` or `is_deleted` in create/update actions. janitor trials: convert to strong params. post disapprovals: convert to strong params. * Factor out quick-mod bar to shared partial. * Fix quick-mod bar to use `Post#is_approvable?` to determine visibility of Approve button. dmail filters: convert to strong params. password resets: convert to strong params. user name change requests: convert to strong params. posts: convert to strong params. users: convert to strong params. * Disallow setting password_hash, last_logged_in_at, last_forum_read_at, has_mail, and dmail_filter_attributes[user_id]. * Remove initialize_default_image_size (dead code). uploads: convert to strong params. * Remove `initialize_status` because status already defaults to pending in the database. tag aliases/implications: convert to strong params. tags: convert to strong params. forum posts: convert to strong params. * Disallow changing the topic_id after creating the post. * Disallow setting is_deleted (destroy/undelete actions should be used instead). * Remove is_sticky / is_locked (nonexistent attributes). forum topics: convert to strong params. * merges https://github.com/evazion/danbooru/tree/wip-rails-5.1 * lock pg gem to 0.21 (1.0.0 is incompatible with rails 5.1.4) * switch to factorybot and change all references Co-authored-by: r888888888 <r888888888@gmail.com> Co-authored-by: evazion <noizave@gmail.com> add diffs
This commit is contained in:
@@ -3,7 +3,7 @@ require 'test_helper'
|
||||
class CommentTest < ActiveSupport::TestCase
|
||||
context "A comment" do
|
||||
setup do
|
||||
user = FactoryGirl.create(:user)
|
||||
user = FactoryBot.create(:user)
|
||||
CurrentUser.user = user
|
||||
CurrentUser.ip_addr = "127.0.0.1"
|
||||
end
|
||||
@@ -15,16 +15,16 @@ class CommentTest < ActiveSupport::TestCase
|
||||
|
||||
context "that mentions a user" do
|
||||
setup do
|
||||
@post = FactoryGirl.create(:post)
|
||||
@post = FactoryBot.create(:post)
|
||||
Danbooru.config.stubs(:member_comment_limit).returns(100)
|
||||
Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now)
|
||||
end
|
||||
|
||||
context "added in an edit" do
|
||||
should "dmail the added user" do
|
||||
@user1 = FactoryGirl.create(:user)
|
||||
@user2 = FactoryGirl.create(:user)
|
||||
@comment = FactoryGirl.create(:comment, :post_id => @post.id, :body => "@#{@user1.name}")
|
||||
@user1 = FactoryBot.create(:user)
|
||||
@user2 = FactoryBot.create(:user)
|
||||
@comment = FactoryBot.create(:comment, :post_id => @post.id, :body => "@#{@user1.name}")
|
||||
|
||||
assert_no_difference("@user1.dmails.count") do
|
||||
assert_difference("@user2.dmails.count") do
|
||||
@@ -37,32 +37,32 @@ class CommentTest < ActiveSupport::TestCase
|
||||
|
||||
context "in a quote block" do
|
||||
setup do
|
||||
@user2 = FactoryGirl.create(:user, :created_at => 2.weeks.ago)
|
||||
@user2 = FactoryBot.create(:user, :created_at => 2.weeks.ago)
|
||||
end
|
||||
|
||||
should "not create a dmail" do
|
||||
assert_difference("Dmail.count", 0) do
|
||||
FactoryGirl.create(:comment, :post_id => @post.id, :body => "[quote]@#{@user2.name}[/quote]")
|
||||
FactoryBot.create(:comment, :post_id => @post.id, :body => "[quote]@#{@user2.name}[/quote]")
|
||||
end
|
||||
|
||||
assert_difference("Dmail.count", 0) do
|
||||
FactoryGirl.create(:comment, :post_id => @post.id, :body => "[quote]@#{@user2.name}[/quote] blah [quote]@#{@user2.name}[/quote]")
|
||||
FactoryBot.create(:comment, :post_id => @post.id, :body => "[quote]@#{@user2.name}[/quote] blah [quote]@#{@user2.name}[/quote]")
|
||||
end
|
||||
|
||||
assert_difference("Dmail.count", 0) do
|
||||
FactoryGirl.create(:comment, :post_id => @post.id, :body => "[quote][quote]@#{@user2.name}[/quote][/quote]")
|
||||
FactoryBot.create(:comment, :post_id => @post.id, :body => "[quote][quote]@#{@user2.name}[/quote][/quote]")
|
||||
end
|
||||
|
||||
assert_difference("Dmail.count", 1) do
|
||||
FactoryGirl.create(:comment, :post_id => @post.id, :body => "[quote]@#{@user2.name}[/quote] @#{@user2.name}")
|
||||
FactoryBot.create(:comment, :post_id => @post.id, :body => "[quote]@#{@user2.name}[/quote] @#{@user2.name}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "outside a quote block" do
|
||||
setup do
|
||||
@user2 = FactoryGirl.create(:user)
|
||||
@comment = FactoryGirl.build(:comment, :post_id => @post.id, :body => "Hey @#{@user2.name} check this out!")
|
||||
@user2 = FactoryBot.create(:user)
|
||||
@comment = FactoryBot.build(:comment, :post_id => @post.id, :body => "Hey @#{@user2.name} check this out!")
|
||||
end
|
||||
|
||||
should "create a dmail" do
|
||||
@@ -89,7 +89,7 @@ class CommentTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "fail creation" do
|
||||
comment = FactoryGirl.build(:comment)
|
||||
comment = FactoryBot.build(:comment)
|
||||
comment.save
|
||||
assert_equal(["You can not post comments within 1 week of sign up"], comment.errors.full_messages)
|
||||
end
|
||||
@@ -103,9 +103,9 @@ class CommentTest < ActiveSupport::TestCase
|
||||
|
||||
context "that is then deleted" do
|
||||
setup do
|
||||
@post = FactoryGirl.create(:post)
|
||||
@comment = FactoryGirl.create(:comment, :post_id => @post.id)
|
||||
@comment.update({is_deleted: true}, as: :member)
|
||||
@post = FactoryBot.create(:post)
|
||||
@comment = FactoryBot.create(:comment, :post_id => @post.id)
|
||||
@comment.update(is_deleted: true)
|
||||
@post.reload
|
||||
end
|
||||
|
||||
@@ -115,59 +115,59 @@ class CommentTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "be created" do
|
||||
comment = FactoryGirl.build(:comment)
|
||||
comment = FactoryBot.build(:comment)
|
||||
comment.save
|
||||
assert(comment.errors.empty?, comment.errors.full_messages.join(", "))
|
||||
end
|
||||
|
||||
should "not validate if the post does not exist" do
|
||||
comment = FactoryGirl.build(:comment, :post_id => -1)
|
||||
comment = FactoryBot.build(:comment, :post_id => -1)
|
||||
|
||||
assert_not(comment.valid?)
|
||||
assert_equal(["must exist"], comment.errors[:post])
|
||||
end
|
||||
|
||||
should "not bump the parent post" do
|
||||
post = FactoryGirl.create(:post)
|
||||
comment = FactoryGirl.create(:comment, :do_not_bump_post => true, :post => post)
|
||||
post = FactoryBot.create(:post)
|
||||
comment = FactoryBot.create(:comment, :do_not_bump_post => true, :post => post)
|
||||
post.reload
|
||||
assert_nil(post.last_comment_bumped_at)
|
||||
|
||||
comment = FactoryGirl.create(:comment, :post => post)
|
||||
comment = FactoryBot.create(:comment, :post => post)
|
||||
post.reload
|
||||
assert_not_nil(post.last_comment_bumped_at)
|
||||
end
|
||||
|
||||
should "not bump the post after exceeding the threshold" do
|
||||
Danbooru.config.stubs(:comment_threshold).returns(1)
|
||||
p = FactoryGirl.create(:post)
|
||||
c1 = FactoryGirl.create(:comment, :post => p)
|
||||
p = FactoryBot.create(:post)
|
||||
c1 = FactoryBot.create(:comment, :post => p)
|
||||
Timecop.travel(2.seconds.from_now) do
|
||||
c2 = FactoryGirl.create(:comment, :post => p)
|
||||
c2 = FactoryBot.create(:comment, :post => p)
|
||||
end
|
||||
p.reload
|
||||
assert_equal(c1.created_at.to_s, p.last_comment_bumped_at.to_s)
|
||||
end
|
||||
|
||||
should "always record the last_commented_at properly" do
|
||||
post = FactoryGirl.create(:post)
|
||||
post = FactoryBot.create(:post)
|
||||
Danbooru.config.stubs(:comment_threshold).returns(1)
|
||||
|
||||
c1 = FactoryGirl.create(:comment, :do_not_bump_post => true, :post => post)
|
||||
c1 = FactoryBot.create(:comment, :do_not_bump_post => true, :post => post)
|
||||
post.reload
|
||||
assert_equal(c1.created_at.to_s, post.last_commented_at.to_s)
|
||||
|
||||
Timecop.travel(2.seconds.from_now) do
|
||||
c2 = FactoryGirl.create(:comment, :post => post)
|
||||
c2 = FactoryBot.create(:comment, :post => post)
|
||||
post.reload
|
||||
assert_equal(c2.created_at.to_s, post.last_commented_at.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
should "not record the user id of the voter" do
|
||||
user = FactoryGirl.create(:user)
|
||||
post = FactoryGirl.create(:post)
|
||||
c1 = FactoryGirl.create(:comment, :post => post)
|
||||
user = FactoryBot.create(:user)
|
||||
post = FactoryBot.create(:post)
|
||||
c1 = FactoryBot.create(:comment, :post => post)
|
||||
CurrentUser.scoped(user, "127.0.0.1") do
|
||||
c1.vote!("up")
|
||||
c1.reload
|
||||
@@ -176,9 +176,9 @@ class CommentTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "not allow duplicate votes" do
|
||||
user = FactoryGirl.create(:user)
|
||||
post = FactoryGirl.create(:post)
|
||||
c1 = FactoryGirl.create(:comment, :post => post)
|
||||
user = FactoryBot.create(:user)
|
||||
post = FactoryBot.create(:post)
|
||||
c1 = FactoryBot.create(:comment, :post => post)
|
||||
|
||||
assert_nothing_raised { c1.vote!("down") }
|
||||
exception = assert_raises(ActiveRecord::RecordInvalid) { c1.vote!("down") }
|
||||
@@ -186,24 +186,24 @@ class CommentTest < ActiveSupport::TestCase
|
||||
assert_equal(1, CommentVote.count)
|
||||
assert_equal(-1, CommentVote.last.score)
|
||||
|
||||
c2 = FactoryGirl.create(:comment, :post => post)
|
||||
c2 = FactoryBot.create(:comment, :post => post)
|
||||
assert_nothing_raised { c2.vote!("down") }
|
||||
assert_equal(2, CommentVote.count)
|
||||
end
|
||||
|
||||
should "not allow upvotes by the creator" do
|
||||
user = FactoryGirl.create(:user)
|
||||
post = FactoryGirl.create(:post)
|
||||
c1 = FactoryGirl.create(:comment, :post => post)
|
||||
user = FactoryBot.create(:user)
|
||||
post = FactoryBot.create(:post)
|
||||
c1 = FactoryBot.create(:comment, :post => post)
|
||||
|
||||
exception = assert_raises(ActiveRecord::RecordInvalid) { c1.vote!("up") }
|
||||
assert_equal("Validation failed: You cannot upvote your own comments", exception.message)
|
||||
end
|
||||
|
||||
should "allow undoing of votes" do
|
||||
user = FactoryGirl.create(:user)
|
||||
post = FactoryGirl.create(:post)
|
||||
comment = FactoryGirl.create(:comment, :post => post)
|
||||
user = FactoryBot.create(:user)
|
||||
post = FactoryBot.create(:post)
|
||||
comment = FactoryBot.create(:comment, :post => post)
|
||||
CurrentUser.scoped(user, "127.0.0.1") do
|
||||
comment.vote!("up")
|
||||
comment.unvote!
|
||||
@@ -214,9 +214,9 @@ class CommentTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "be searchable" do
|
||||
c1 = FactoryGirl.create(:comment, :body => "aaa bbb ccc")
|
||||
c2 = FactoryGirl.create(:comment, :body => "aaa ddd")
|
||||
c3 = FactoryGirl.create(:comment, :body => "eee")
|
||||
c1 = FactoryBot.create(:comment, :body => "aaa bbb ccc")
|
||||
c2 = FactoryBot.create(:comment, :body => "aaa ddd")
|
||||
c3 = FactoryBot.create(:comment, :body => "eee")
|
||||
|
||||
matches = Comment.body_matches("aaa")
|
||||
assert_equal(2, matches.count)
|
||||
@@ -225,7 +225,7 @@ class CommentTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "default to id_desc order when searched with no options specified" do
|
||||
comms = FactoryGirl.create_list(:comment, 3)
|
||||
comms = FactoryBot.create_list(:comment, 3)
|
||||
matches = Comment.search({})
|
||||
|
||||
assert_equal([comms[2].id, comms[1].id, comms[0].id], matches.map(&:id))
|
||||
@@ -233,38 +233,38 @@ class CommentTest < ActiveSupport::TestCase
|
||||
|
||||
context "that is edited by a moderator" do
|
||||
setup do
|
||||
@post = FactoryGirl.create(:post)
|
||||
@comment = FactoryGirl.create(:comment, :post_id => @post.id)
|
||||
@mod = FactoryGirl.create(:moderator_user)
|
||||
@post = FactoryBot.create(:post)
|
||||
@comment = FactoryBot.create(:comment, :post_id => @post.id)
|
||||
@mod = FactoryBot.create(:moderator_user)
|
||||
CurrentUser.user = @mod
|
||||
end
|
||||
|
||||
should "create a mod action" do
|
||||
assert_difference("ModAction.count") do
|
||||
@comment.update_attributes({:body => "nope"}, :as => :moderator)
|
||||
@comment.update_attributes(:body => "nope")
|
||||
end
|
||||
end
|
||||
|
||||
should "credit the moderator as the updater" do
|
||||
@comment.update({ body: "test" }, as: :moderator)
|
||||
@comment.update(body: "test")
|
||||
assert_equal(@mod.id, @comment.updater_id)
|
||||
end
|
||||
end
|
||||
|
||||
context "that is below the score threshold" do
|
||||
should "be hidden if not stickied" do
|
||||
user = FactoryGirl.create(:user, :comment_threshold => 0)
|
||||
post = FactoryGirl.create(:post)
|
||||
comment = FactoryGirl.create(:comment, :post => post, :score => -5)
|
||||
user = FactoryBot.create(:user, :comment_threshold => 0)
|
||||
post = FactoryBot.create(:post)
|
||||
comment = FactoryBot.create(:comment, :post => post, :score => -5)
|
||||
|
||||
assert_equal([comment], post.comments.hidden(user))
|
||||
assert_equal([], post.comments.visible(user))
|
||||
end
|
||||
|
||||
should "be visible if stickied" do
|
||||
user = FactoryGirl.create(:user, :comment_threshold => 0)
|
||||
post = FactoryGirl.create(:post)
|
||||
comment = FactoryGirl.create(:comment, :post => post, :score => -5, :is_sticky => true)
|
||||
user = FactoryBot.create(:user, :comment_threshold => 0)
|
||||
post = FactoryBot.create(:post)
|
||||
comment = FactoryBot.create(:comment, :post => post, :score => -5, :is_sticky => true)
|
||||
|
||||
assert_equal([], post.comments.hidden(user))
|
||||
assert_equal([comment], post.comments.visible(user))
|
||||
@@ -273,7 +273,7 @@ class CommentTest < ActiveSupport::TestCase
|
||||
|
||||
context "that is quoted" do
|
||||
should "strip [quote] tags correctly" do
|
||||
comment = FactoryGirl.create(:comment, body: <<-EOS.strip_heredoc)
|
||||
comment = FactoryBot.create(:comment, body: <<-EOS.strip_heredoc)
|
||||
paragraph one
|
||||
|
||||
[quote]
|
||||
|
||||
Reference in New Issue
Block a user