[Tests] Update tests and fix deprecations

This commit is contained in:
Kira 2021-02-13 00:59:14 -08:00
parent 9bbc5afe28
commit e685fb87c2
11 changed files with 34 additions and 156 deletions

View File

@ -34,11 +34,11 @@ module Downloads
end
def validate_url
errors[:base] << "URL must not be blank" if url.blank?
errors[:base] << "'#{url}' is not a valid url" if !url.host.present?
errors[:base] << "'#{url}' is not a valid url. Did you mean 'http://#{url}'?" if !url.scheme.in?(%w[http https])
errors.add(:base, "URL must not be blank") if url.blank?
errors.add(:base, "'#{url}' is not a valid url") if !url.host.present?
errors.add(:base, "'#{url}' is not a valid url. Did you mean 'http://#{url}'?") if !url.scheme.in?(%w[http https])
valid, reason = UploadWhitelist.is_whitelisted?(url)
errors[:base] << "'#{url}' is not whitelisted and can't be direct downloaded: #{reason}" if !valid
errors.add(:base, "'#{url}' is not whitelisted and can't be direct downloaded: #{reason}") if !valid
end
def http_get_streaming(url, file: Tempfile.new(binmode: true), headers: {}, max_size: Danbooru.config.max_file_size)

View File

@ -17,6 +17,6 @@ class EmailLinkValidator
private
def self.validator
@validator ||= ActiveSupport::MessageVerifier.new(Danbooru.config.email_key, serializer: JSON, hash: "SHA256")
@validator ||= ActiveSupport::MessageVerifier.new(Danbooru.config.email_key, serializer: JSON, digest: "SHA256")
end
end

View File

@ -19,7 +19,7 @@ class SessionCreator
user.update_column(:last_ip_addr, ip_addr)
if remember
verifier = ActiveSupport::MessageVerifier.new(Danbooru.config.remember_key, serializer: JSON, hash: "SHA256")
verifier = ActiveSupport::MessageVerifier.new(Danbooru.config.remember_key, serializer: JSON, digest: "SHA256")
cookies.encrypted[:remember] = {value: verifier.generate(user.id, purpose: "rbr", expires_in: 14.days), expires: Time.now + 14.days, httponly: true, same_site: :lax, secure: Rails.env.production?}
end
return true

View File

@ -8,7 +8,7 @@ class SessionLoader
@session = request.session
@cookies = request.cookie_jar
@params = request.parameters
@remember_validator = ActiveSupport::MessageVerifier.new(Danbooru.config.remember_key, serializer: JSON, hash: "SHA256")
@remember_validator = ActiveSupport::MessageVerifier.new(Danbooru.config.remember_key, serializer: JSON, digest: "SHA256")
end
def load

View File

@ -13,12 +13,12 @@ class UserEmailChange
end
if RateLimiter.check_limit("email:#{user.id}", 2, 24.hours)
user.errors[:base] << "Email changed too recently"
user.errors.add(:base, "Email changed too recently")
return
end
if User.authenticate(user.name, password).nil?
user.errors[:base] << "Password was incorrect"
user.errors.add(:base, "Password was incorrect")
else
user.email = new_email
user.email_verification_key = '1' if Danbooru.config.enable_email_verification?

View File

@ -2,12 +2,12 @@ class UserNameValidator < ActiveModel::EachValidator
def validate_each(rec, attr, value)
name = value
rec.errors[attr] << "already exists" if User.find_by_name(name).present?
rec.errors[attr] << "must be 2 to 20 characters long" if !name.length.between?(2, 20)
rec.errors[attr] << "must contain only alphanumeric characters, hypens, apostrophes, tildes and underscores" unless name =~ /\A[a-zA-Z0-9\-_~']+\z/
rec.errors[attr] << "must not begin with a special character" if name =~ /\A[_\-~']/
rec.errors[attr] << "must not contain consecutive special characters" if name =~ /_{2}|-{2}|~{2}|'{2}/
rec.errors[attr] << "cannot begin or end with an underscore" if name =~ /\A_|_\z/
rec.errors[attr] << "cannot be the string 'me'" if name.downcase == 'me'
rec.errors.add(attr, "already exists") if User.find_by_name(name).present?
rec.errors.add(attr, "must be 2 to 20 characters long") if !name.length.between?(2, 20)
rec.errors.add(attr, "must contain only alphanumeric characters, hypens, apostrophes, tildes and underscores") unless name =~ /\A[a-zA-Z0-9\-_~']+\z/
rec.errors.add(attr, "must not begin with a special character") if name =~ /\A[_\-~']/
rec.errors.add(attr, "must not contain consecutive special characters") if name =~ /_{2}|-{2}|~{2}|'{2}/
rec.errors.add(attr, "cannot begin or end with an underscore") if name =~ /\A_|_\z/
rec.errors.add(attr, "cannot be the string 'me'") if name.downcase == 'me'
end
end

View File

@ -3,7 +3,7 @@ FactoryBot.define do
title {FFaker::Lorem.words.join(" ")}
is_sticky { false }
is_locked { false }
category_id { 0 }
category_id { Danbooru.config.alias_implication_forum_category }
factory(:mod_up_forum_topic) do
min_level { User::Levels::MODERATOR }

View File

@ -1,106 +0,0 @@
require 'test_helper'
class ArtistCommentaryTest < ActiveSupport::TestCase
setup do
user = FactoryBot.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "A post should not have more than one commentary" do
post = FactoryBot.create(:post)
assert_raise(ActiveRecord::RecordInvalid) do
FactoryBot.create(:artist_commentary, post_id: post.id)
FactoryBot.create(:artist_commentary, post_id: post.id)
end
end
context "An artist commentary" do
context "when searched" do
setup do
@post1 = FactoryBot.create(:post, tag_string: "artcomm1")
@post2 = FactoryBot.create(:post, tag_string: "artcomm2")
@artcomm1 = FactoryBot.create(:artist_commentary, post_id: @post1.id, original_title: "foo", translated_title: "bar")
@artcomm2 = FactoryBot.create(:artist_commentary, post_id: @post2.id, original_title: "", original_description: "", translated_title: "", translated_description: "")
end
should "find the correct match" do
assert_equal([@artcomm1.id], ArtistCommentary.search(post_id: @post1.id.to_s).map(&:id))
assert_equal([@artcomm1.id], ArtistCommentary.search(text_matches: "foo").map(&:id))
assert_equal([@artcomm1.id], ArtistCommentary.search(text_matches: "f*").map(&:id))
assert_equal([@artcomm1.id], ArtistCommentary.search(post_tags_match: "artcomm1").map(&:id))
assert_equal([@artcomm1.id], ArtistCommentary.search(original_present: "yes").map(&:id))
assert_equal([@artcomm2.id], ArtistCommentary.search(original_present: "no").map(&:id))
assert_equal([@artcomm1.id], ArtistCommentary.search(translated_present: "yes").map(&:id))
assert_equal([@artcomm2.id], ArtistCommentary.search(translated_present: "no").map(&:id))
end
end
context "when created" do
should "create a new version" do
@artcomm = FactoryBot.create(:artist_commentary, original_title: "foo")
assert_equal(1, @artcomm.versions.size)
assert_equal("foo", @artcomm.versions.last.original_title)
end
end
context "when updated" do
setup do
@post = FactoryBot.create(:post)
@artcomm = FactoryBot.create(:artist_commentary, post_id: @post.id)
@artcomm.reload
end
should "add tags if requested" do
@artcomm.update(translated_title: "bar", add_commentary_tag: "1")
assert_equal(true, @post.reload.has_tag?("commentary"))
end
should "not create new version if nothing changed" do
@artcomm.save
assert_equal(1, @post.artist_commentary.versions.size)
end
should "create a new version if outside merge window" do
travel_to(2.hours.from_now) do
@artcomm.update(original_title: "bar")
assert_equal(2, @post.artist_commentary.versions.size)
assert_equal("bar", @artcomm.versions.last.original_title)
end
end
should "merge with the previous version if inside merge window" do
@artcomm.update(original_title: "bar")
@artcomm.reload
assert_equal(1, @post.artist_commentary.versions.size)
assert_equal("bar", @artcomm.versions.last.original_title)
end
should "trim whitespace from all fields" do
# \u00A0 - nonbreaking space.
@artcomm.update(
original_title: " foo\u00A0\t\n",
original_description: " foo\u00A0\t\n",
translated_title: " foo\u00A0\t\n",
translated_description: " foo\u00A0\n",
)
assert_equal("foo", @artcomm.original_title)
assert_equal("foo", @artcomm.original_description)
assert_equal("foo", @artcomm.translated_title)
assert_equal("foo", @artcomm.translated_description)
end
end
end
end

View File

@ -3,6 +3,8 @@ require 'test_helper'
class TagAliasTest < ActiveSupport::TestCase
setup do
Sidekiq::Testing::inline!
category = ForumCategory.create(id: Danbooru.config.alias_implication_forum_category, name: 'Tag Requests')
end
teardown do

View File

@ -4,6 +4,8 @@ class TagImplicationTest < ActiveSupport::TestCase
setup do
Sidekiq::Testing::inline!
category = ForumCategory.create(id: Danbooru.config.alias_implication_forum_category, name: 'Tag Requests')
end
teardown do

View File

@ -69,15 +69,15 @@ class UserTest < ActiveSupport::TestCase
should "limit comment votes" do
Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now)
Danbooru.config.stubs(:member_comment_limit).returns(10)
assert(@user.can_comment_vote?)
assert_equal(@user.can_comment_vote_with_reason?, true)
10.times do
comment = FactoryBot.create(:comment)
FactoryBot.create(:comment_vote, :comment_id => comment.id, :score => -1)
end
assert(!@user.can_comment_vote?)
assert_equal(@user.can_comment_vote_with_reason?, :throttled)
CommentVote.update_all("created_at = '1990-01-01'")
assert(@user.can_comment_vote?)
assert_equal(@user.can_comment_vote_with_reason?, true)
end
should "limit comments" do
@ -97,19 +97,15 @@ class UserTest < ActiveSupport::TestCase
should "verify" do
assert(@user.is_verified?)
@user = FactoryBot.create(:user)
@user.generate_email_verification_key
@user.save
@user.mark_unverified!
assert(!@user.is_verified?)
assert_raise(User::Error) {@user.verify!("bbb")}
assert_nothing_raised {@user.verify!(@user.email_verification_key)}
assert_nothing_raised {@user.mark_verified!}
assert(@user.is_verified?)
end
should "authenticate" do
assert(User.authenticate(@user.name, "password"), "Authentication should have succeeded")
assert(!User.authenticate(@user.name, "password2"), "Authentication should not have succeeded")
assert(User.authenticate_hash(@user.name, User.sha1("password")), "Authentication should have succeeded")
assert(!User.authenticate_hash(@user.name, User.sha1("xxx")), "Authentication should not have succeeded")
end
should "normalize its level" do
@ -142,19 +138,19 @@ class UserTest < ActiveSupport::TestCase
# U+2007: https://en.wikipedia.org/wiki/Figure_space
user = FactoryBot.build(:user, :name => "foo\u2007bar")
user.save
assert_equal(["Name cannot have whitespace or colons"], user.errors.full_messages)
assert_equal(["Name must contain only alphanumeric characters, hypens, apostrophes, tildes and underscores"], user.errors.full_messages)
end
should "not contain a colon" do
user = FactoryBot.build(:user, :name => "a:b")
user.save
assert_equal(["Name cannot have whitespace or colons"], user.errors.full_messages)
assert_equal(["Name must contain only alphanumeric characters, hypens, apostrophes, tildes and underscores"], user.errors.full_messages)
end
should "not begin with an underscore" do
user = FactoryBot.build(:user, :name => "_x")
user.save
assert_equal(["Name cannot begin or end with an underscore"], user.errors.full_messages)
assert_equal(["Name must not begin with a special character", "Name cannot begin or end with an underscore"], user.errors.full_messages)
end
should "not end with an underscore" do
@ -185,12 +181,6 @@ class UserTest < ActiveSupport::TestCase
assert(@user.to_json !~ /addr/)
end
end
context "in the xml representation" do
should "not appear" do
assert(@user.to_xml !~ /addr/)
end
end
end
context "password" do
@ -217,7 +207,7 @@ class UserTest < ActiveSupport::TestCase
@user.password = "x5"
@user.password_confirmation = "x5"
@user.save
assert_equal(["Password is too short (minimum is 5 characters)"], @user.errors.full_messages)
assert_equal(["Password is too short (minimum is 6 characters)"], @user.errors.full_messages)
end
should "should be reset" do
@ -229,25 +219,25 @@ class UserTest < ActiveSupport::TestCase
should "not change the password if the password and old password are blank" do
@user = FactoryBot.create(:user, :password => "67890")
@user.update(:password => "", :old_password => "")
assert(@user.bcrypt_password == User.sha1("67890"))
assert(@user.bcrypt_password == "67890")
end
should "not change the password if the old password is incorrect" do
@user = FactoryBot.create(:user, :password => "67890")
@user.update(:password => "12345", :old_password => "abcdefg")
assert(@user.bcrypt_password == User.sha1("67890"))
assert(@user.bcrypt_password == "67890")
end
should "not change the password if the old password is blank" do
@user = FactoryBot.create(:user, :password => "67890")
@user.update(:password => "12345", :old_password => "")
assert(@user.bcrypt_password == User.sha1("67890"))
assert(@user.bcrypt_password == "67890")
end
should "change the password if the old password is correct" do
@user = FactoryBot.create(:user, :password => "67890")
@user.update(:password => "12345", :old_password => "67890")
assert(@user.bcrypt_password == User.sha1("12345"))
assert(@user.bcrypt_password == "12345")
end
context "in the json representation" do
@ -259,16 +249,6 @@ class UserTest < ActiveSupport::TestCase
assert(@user.to_json !~ /password/)
end
end
context "in the xml representation" do
setup do
@user = FactoryBot.create(:user)
end
should "not appear" do
assert(@user.to_xml !~ /password/)
end
end
end
context "that might be a sock puppet" do