fix unit tests

This commit is contained in:
r888888888
2017-11-20 16:10:35 -08:00
parent f11992bd91
commit 502f1298a9
25 changed files with 101 additions and 29 deletions

View File

@@ -4,7 +4,7 @@ class BulkRevert
class ConstraintTooGeneralError < Exception ; end class ConstraintTooGeneralError < Exception ; end
def process(creator, constraints) def process(creator, constraints = {})
@constraints = constraints @constraints = constraints
ModAction.log("Processed bulk revert for #{constraints.inspect} by #{creator.name}") ModAction.log("Processed bulk revert for #{constraints.inspect} by #{creator.name}")
@@ -19,6 +19,7 @@ class BulkRevert
end end
def initialize def initialize
@constraints = {}
end end
def preview def preview

View File

@@ -12,12 +12,32 @@ class PawooApiClient
end end
def initialize(json) def initialize(json)
@json = get @json = json
end end
def profile_url def profile_url
json["url"] json["url"]
end end
def account_name
json["username"]
end
def image_url
nil
end
def image_urls
[]
end
def tags
[]
end
def commentary
nil
end
end end
class Status class Status
@@ -32,7 +52,7 @@ class PawooApiClient
@json = json @json = json
end end
def account_profile_url def profile_url
json["account"]["url"] json["account"]["url"]
end end
@@ -60,9 +80,11 @@ class PawooApiClient
end end
end end
def get_status(url) def get(url)
if id = Status.is_match?(url) if id = Status.is_match?(url)
Status.new(JSON.parse(access_token.get("/api/v1/statuses/#{id}").body)) Status.new(JSON.parse(access_token.get("/api/v1/statuses/#{id}").body))
elsif id = Account.is_match?(url)
Account.new(JSON.parse(access_token.get("/api/v1/accounts/#{id}").body))
else else
nil nil
end end

View File

@@ -16,7 +16,7 @@ module Sources::Strategies
attr_reader :image_urls attr_reader :image_urls
def self.url_match?(url) def self.url_match?(url)
PawooApiClient::Status.is_match?(url) PawooApiClient::Status.is_match?(url) || PawooApiClient::Account.is_match?(url)
end end
def referer_url def referer_url
@@ -28,13 +28,13 @@ module Sources::Strategies
end end
def api_response def api_response
@response ||= PawooApiClient.new.get_status(normalized_url) @response ||= PawooApiClient.new.get(normalized_url)
end end
def get def get
response = api_response response = api_response
@artist_name = response.account_name @artist_name = response.account_name
@profile_url = response.account_profile_url @profile_url = response.profile_url
@image_url = response.image_urls.first @image_url = response.image_urls.first
@image_urls = response.image_urls @image_urls = response.image_urls
@tags = response.tags @tags = response.tags
@@ -54,6 +54,11 @@ module Sources::Strategies
true true
end end
def normalize_for_artist_finder!
get
@profile_url
end
def dtext_artist_commentary_desc def dtext_artist_commentary_desc
DText.from_html(artist_commentary_desc) do |element| DText.from_html(artist_commentary_desc) do |element|
if element.name == "a" if element.name == "a"

View File

@@ -173,6 +173,7 @@ class FavoriteGroup < ApplicationRecord
end end
def add!(post_id) def add!(post_id)
post_id = post_id.id if post_id.is_a?(Post)
return if contains?(post_id) return if contains?(post_id)
clear_post_id_array clear_post_id_array
@@ -180,12 +181,14 @@ class FavoriteGroup < ApplicationRecord
end end
def self.purge_post(post_id) def self.purge_post(post_id)
post_id = post_id.id if post_id.is_a?(Post)
for_post(post_id).find_each do |group| for_post(post_id).find_each do |group|
group.remove!(post_id) group.remove!(post_id)
end end
end end
def remove!(post_id) def remove!(post_id)
post_id = post_id.id if post_id.is_a?(Post)
return unless contains?(post_id) return unless contains?(post_id)
clear_post_id_array clear_post_id_array

View File

@@ -13,7 +13,7 @@ class PostDisapproval < ApplicationRecord
scope :disinterest, lambda {where(:reason => ["disinterest", "legacy"])} scope :disinterest, lambda {where(:reason => ["disinterest", "legacy"])}
def self.prune! def self.prune!
PostDisapproval.where("post_id in (select _.post_id from post_disapprovals _ where _.created_at < ?)", DELETION_THRESHOLD.ago) PostDisapproval.where("post_id in (select _.post_id from post_disapprovals _ where _.created_at < ?)", DELETION_THRESHOLD.ago).delete_all
end end
def self.dmail_messages! def self.dmail_messages!

View File

@@ -4,6 +4,8 @@ class DmailFilterTest < ActiveSupport::TestCase
def setup def setup
super super
User.any_instance.stubs(:validate_sock_puppets).returns(true)
@receiver = FactoryGirl.create(:user) @receiver = FactoryGirl.create(:user)
@sender = FactoryGirl.create(:user) @sender = FactoryGirl.create(:user)
end end

View File

@@ -40,7 +40,7 @@ class ArtistUrlTest < ActiveSupport::TestCase
should "normalize nico seiga artist urls" do should "normalize nico seiga artist urls" do
url = FactoryGirl.create(:artist_url, :url => "http://seiga.nicovideo.jp/user/illust/1826959") url = FactoryGirl.create(:artist_url, :url => "http://seiga.nicovideo.jp/user/illust/1826959")
assert_equal("http://seiga.nicovideo.jp/user/illust/1826959", url.normalized_url) assert_equal("http://seiga.nicovideo.jp/user/illust/1826959/", url.normalized_url)
url = FactoryGirl.create(:artist_url, :url => "http://seiga.nicovideo.jp/seiga/im4937663") url = FactoryGirl.create(:artist_url, :url => "http://seiga.nicovideo.jp/seiga/im4937663")
assert_equal("http://seiga.nicovideo.jp/user/illust/7017777/", url.normalized_url) assert_equal("http://seiga.nicovideo.jp/user/illust/7017777/", url.normalized_url)

View File

@@ -1,6 +1,11 @@
require 'test_helper' require 'test_helper'
class BanTest < ActiveSupport::TestCase class BanTest < ActiveSupport::TestCase
def setup
super
User.any_instance.stubs(:validate_sock_puppets).returns(true)
end
context "A ban" do context "A ban" do
context "created by an admin" do context "created by an admin" do
setup do setup do

View File

@@ -3,10 +3,12 @@ require 'test_helper'
class BulkRevertTest < ActiveSupport::TestCase class BulkRevertTest < ActiveSupport::TestCase
context "#find_post_versions" do context "#find_post_versions" do
subject do subject do
BulkRevert.new(added_tags: "hoge") @user = FactoryGirl.create(:user)
BulkRevert.new
end end
setup do setup do
subject.stubs(:constraints).returns({added_tags: ["a"]})
subject.expects(:query_gbq).returns([1,2,3]) subject.expects(:query_gbq).returns([1,2,3])
end end

View File

@@ -2,6 +2,7 @@ require 'test_helper'
class CurrentUserTest < ActiveSupport::TestCase class CurrentUserTest < ActiveSupport::TestCase
setup do setup do
User.any_instance.stubs(:validate_sock_puppets).returns(true)
CurrentUser.user = nil CurrentUser.user = nil
CurrentUser.ip_addr = nil CurrentUser.ip_addr = nil
end end

View File

@@ -17,6 +17,7 @@ class DmailTest < ActiveSupport::TestCase
context "spam" do context "spam" do
setup do setup do
Dmail.any_instance.stubs(:spam?).returns(true)
@recipient = FactoryGirl.create(:user) @recipient = FactoryGirl.create(:user)
end end

View File

@@ -32,7 +32,7 @@ module Downloads
@new_medium_thumbnail = "http://i1.pixiv.net/c/600x600/img-master/img/2010/11/30/08/39/58/14901720_p0_master1200.jpg" @new_medium_thumbnail = "http://i1.pixiv.net/c/600x600/img-master/img/2010/11/30/08/39/58/14901720_p0_master1200.jpg"
@new_full_size_image = "http://i1.pixiv.net/img-original/img/2010/11/30/08/39/58/14901720_p0.png" @new_full_size_image = "http://i1.pixiv.net/img-original/img/2010/11/30/08/39/58/14901720_p0.png"
@file_size = 1_083 @file_size = 1261
end end
should "work when using new URLs" do should "work when using new URLs" do
@@ -135,7 +135,7 @@ module Downloads
context "downloading a profile image" do context "downloading a profile image" do
should "download new profile images" do should "download new profile images" do
@file_url = "http://i2.pixiv.net/img130/profile/minono_aki/8733472.jpg" @file_url = "http://i2.pixiv.net/img130/profile/minono_aki/8733472.jpg"
@file_size = 23266 @file_size = 23_444
assert_not_rewritten(@file_url) assert_not_rewritten(@file_url)
assert_downloaded(@file_size, @file_url) assert_downloaded(@file_size, @file_url)
@@ -146,7 +146,7 @@ module Downloads
context "downloading a background image" do context "downloading a background image" do
should "download the image" do should "download the image" do
@file_url = "http://i1.pixiv.net/background/img/2016/05/17/12/05/48/2074388_d4ac52034f7ca0af3e083d59fde7e97f.jpg" @file_url = "http://i1.pixiv.net/background/img/2016/05/17/12/05/48/2074388_d4ac52034f7ca0af3e083d59fde7e97f.jpg"
@file_size = 386_500 @file_size = 386_678
assert_not_rewritten(@file_url) assert_not_rewritten(@file_url)
assert_downloaded(@file_size, @file_url) assert_downloaded(@file_size, @file_url)

View File

@@ -68,6 +68,7 @@ class FavoriteTest < ActiveSupport::TestCase
end end
should "remove it from all favorite groups" do should "remove it from all favorite groups" do
assert_equal("#{@post.id}", @fav_group.post_ids)
@post.expunge! @post.expunge!
@fav_group.reload @fav_group.reload
assert_equal("", @fav_group.post_ids) assert_equal("", @fav_group.post_ids)

View File

@@ -3,6 +3,7 @@ require 'test_helper'
class JanitorTrialTest < ActiveSupport::TestCase class JanitorTrialTest < ActiveSupport::TestCase
context "A janitor trial" do context "A janitor trial" do
setup do setup do
User.any_instance.stubs(:validate_sock_puppets).returns(true)
@admin = FactoryGirl.create(:admin_user) @admin = FactoryGirl.create(:admin_user)
@user = FactoryGirl.create(:user) @user = FactoryGirl.create(:user)
CurrentUser.user = @admin CurrentUser.user = @admin

View File

@@ -1,6 +1,8 @@
require 'test_helper' require 'test_helper'
class PostArchiveTest < ActiveSupport::TestCase class PostArchiveTest < ActiveSupport::TestCase
include PoolArchiveTestHelper
context "A post" do context "A post" do
setup do setup do
Timecop.travel(1.month.ago) do Timecop.travel(1.month.ago) do
@@ -70,6 +72,7 @@ class PostArchiveTest < ActiveSupport::TestCase
context "that is tagged with a pool:<name> metatag" do context "that is tagged with a pool:<name> metatag" do
setup do setup do
mock_pool_archive_service!
@pool = FactoryGirl.create(:pool) @pool = FactoryGirl.create(:pool)
@post = FactoryGirl.create(:post, tag_string: "tagme pool:#{@pool.id}") @post = FactoryGirl.create(:post, tag_string: "tagme pool:#{@pool.id}")
end end
@@ -114,10 +117,11 @@ class PostArchiveTest < ActiveSupport::TestCase
end end
should "not create a version if updating the post fails" do should "not create a version if updating the post fails" do
@post.stubs(:apply_post_metatags).raises(NotImplementedError) @post.stubs(:set_tag_counts).raises(NotImplementedError)
assert_equal(2, @post.versions.size)
assert_raise(NotImplementedError) { @post.update(rating: "s") } assert_raise(NotImplementedError) { @post.update(rating: "s") }
assert_equal(1, @post.versions.size) assert_equal(2, @post.versions.size)
end end
should "should create a version if the rating changes" do should "should create a version if the rating changes" do

View File

@@ -1,6 +1,11 @@
require 'test_helper' require 'test_helper'
class PostDisapprovalTest < ActiveSupport::TestCase class PostDisapprovalTest < ActiveSupport::TestCase
def setup
super
User.any_instance.stubs(:validate_sock_puppets).returns(true)
end
context "In all cases" do context "In all cases" do
setup do setup do
@alice = FactoryGirl.create(:moderator_user) @alice = FactoryGirl.create(:moderator_user)

View File

@@ -1,7 +1,9 @@
require 'test_helper' require 'test_helper'
class PostPrunerTest < ActiveSupport::TestCase class PostPrunerTest < ActiveSupport::TestCase
setup do def setup
super
@user = FactoryGirl.create(:admin_user) @user = FactoryGirl.create(:admin_user)
CurrentUser.user = @user CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1" CurrentUser.ip_addr = "127.0.0.1"
@@ -21,7 +23,9 @@ class PostPrunerTest < ActiveSupport::TestCase
PostPruner.new.prune! PostPruner.new.prune!
end end
teardown do def teardown
super
CurrentUser.user = nil CurrentUser.user = nil
CurrentUser.ip_addr = nil CurrentUser.ip_addr = nil
end end

View File

@@ -14,6 +14,8 @@ class PostReplacementTest < ActiveSupport::TestCase
end end
def setup def setup
super
mock_iqdb_service! mock_iqdb_service!
Delayed::Worker.delay_jobs = true # don't delete the old images right away Delayed::Worker.delay_jobs = true # don't delete the old images right away
@@ -27,6 +29,8 @@ class PostReplacementTest < ActiveSupport::TestCase
end end
def teardown def teardown
super
CurrentUser.user = nil CurrentUser.user = nil
CurrentUser.ip_addr = nil CurrentUser.ip_addr = nil
Delayed::Worker.delay_jobs = false Delayed::Worker.delay_jobs = false

View File

@@ -1472,18 +1472,18 @@ class PostTest < ActiveSupport::TestCase
end end
context "but doesn't have a pixiv id" do context "but doesn't have a pixiv id" do
should "not save the pixiv id" do should "save the pixiv id" do
@post.pixiv_id = 1234 @post.pixiv_id = 1234
@post.update(source: "http://i1.pixiv.net/novel-cover-original/img/2016/11/03/20/10/58/7436075_f75af69f3eacd1656d3733c72aa959cf.jpg") @post.update(source: "http://i1.pixiv.net/novel-cover-original/img/2016/11/03/20/10/58/7436075_f75af69f3eacd1656d3733c72aa959cf.jpg")
assert_nil(@post.pixiv_id) assert_equal(7436075, @post.pixiv_id)
@post.pixiv_id = 1234 @post.pixiv_id = 1234
@post.update(source: "http://i2.pixiv.net/background/img/2016/10/30/12/27/30/7059005_da9946b806c10d391a81ed1117cd33d6.jpg") @post.update(source: "http://i2.pixiv.net/background/img/2016/10/30/12/27/30/7059005_da9946b806c10d391a81ed1117cd33d6.jpg")
assert_nil(@post.pixiv_id) assert_equal(7059005, @post.pixiv_id)
@post.pixiv_id = 1234 @post.pixiv_id = 1234
@post.update(source: "http://i1.pixiv.net/img15/img/omega777/novel/2612734.jpg") @post.update(source: "http://i1.pixiv.net/img15/img/omega777/novel/2612734.jpg")
assert_nil(@post.pixiv_id) assert_equal(2612734, @post.pixiv_id)
@post.pixiv_id = 1234 @post.pixiv_id = 1234
@post.update(source: "http://img08.pixiv.net/profile/nice/1408837.jpg") @post.update(source: "http://img08.pixiv.net/profile/nice/1408837.jpg")

View File

@@ -3,7 +3,8 @@ require 'test_helper'
class PostVoteTest < ActiveSupport::TestCase class PostVoteTest < ActiveSupport::TestCase
def setup def setup
super super
User.any_instance.stubs(:validate_sock_puppets).returns(true)
@supervoter = FactoryGirl.create(:user, is_super_voter: true) @supervoter = FactoryGirl.create(:user, is_super_voter: true)
@user = FactoryGirl.create(:user) @user = FactoryGirl.create(:user)
CurrentUser.user = @user CurrentUser.user = @user

View File

@@ -6,12 +6,21 @@ class SavedSearchTest < ActiveSupport::TestCase
def setup def setup
super super
User.any_instance.stubs(:validate_sock_puppets).returns(true)
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
mock_saved_search_service! mock_saved_search_service!
end end
def teardown
super
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context ".labels_for" do context ".labels_for" do
setup do setup do
@user = FactoryGirl.create(:user)
FactoryGirl.create(:saved_search, user: @user, label_string: "blah", query: "blah") FactoryGirl.create(:saved_search, user: @user, label_string: "blah", query: "blah")
FactoryGirl.create(:saved_search, user: @user, label_string: "zah", query: "blah") FactoryGirl.create(:saved_search, user: @user, label_string: "zah", query: "blah")
end end
@@ -28,7 +37,6 @@ class SavedSearchTest < ActiveSupport::TestCase
context ".queries_for" do context ".queries_for" do
setup do setup do
@user = FactoryGirl.create(:user)
FactoryGirl.create(:tag_alias, antecedent_name: "bbb", consequent_name: "ccc", creator: @user) FactoryGirl.create(:tag_alias, antecedent_name: "bbb", consequent_name: "ccc", creator: @user)
FactoryGirl.create(:saved_search, user: @user, label_string: "blah", query: "aaa") FactoryGirl.create(:saved_search, user: @user, label_string: "blah", query: "aaa")
FactoryGirl.create(:saved_search, user: @user, label_string: "zah", query: "CCC BBB AAA") FactoryGirl.create(:saved_search, user: @user, label_string: "zah", query: "CCC BBB AAA")
@@ -83,7 +91,6 @@ class SavedSearchTest < ActiveSupport::TestCase
context "Creating a saved search" do context "Creating a saved search" do
setup do setup do
@user = FactoryGirl.create(:gold_user)
FactoryGirl.create(:tag_alias, antecedent_name: "zzz", consequent_name: "yyy", creator: @user) FactoryGirl.create(:tag_alias, antecedent_name: "zzz", consequent_name: "yyy", creator: @user)
@saved_search = @user.saved_searches.create(:query => " ZZZ xxx ") @saved_search = @user.saved_searches.create(:query => " ZZZ xxx ")
end end
@@ -108,7 +115,6 @@ class SavedSearchTest < ActiveSupport::TestCase
context "Destroying a saved search" do context "Destroying a saved search" do
setup do setup do
@user = FactoryGirl.create(:gold_user)
@saved_search = @user.saved_searches.create(:tag_query => "xxx") @saved_search = @user.saved_searches.create(:tag_query => "xxx")
@saved_search.destroy @saved_search.destroy
end end
@@ -122,6 +128,7 @@ class SavedSearchTest < ActiveSupport::TestCase
context "A user with max saved searches" do context "A user with max saved searches" do
setup do setup do
@user = FactoryGirl.create(:gold_user) @user = FactoryGirl.create(:gold_user)
CurrentUser.user = @user
User.any_instance.stubs(:max_saved_searches).returns(0) User.any_instance.stubs(:max_saved_searches).returns(0)
@saved_search = @user.saved_searches.create(:query => "xxx") @saved_search = @user.saved_searches.create(:query => "xxx")
end end

View File

@@ -79,7 +79,7 @@ module Sources
end end
should "get the image url" do should "get the image url" do
assert_equal("http://orig14.deviantart.net/cb25/f/2017/160/1/9/hidden_work_by_noizave-dbc3r29.png", @site.image_url) assert_match(%r!https://orig\d+\.deviantart\.net/cb25/f/2017/160/1/9/hidden_work_by_noizave-dbc3r29\.png!, @site.image_url)
end end
end end

View File

@@ -17,7 +17,7 @@ class TagAliasCorrectionTest < ActiveSupport::TestCase
context "with a bad cache and post counts" do context "with a bad cache and post counts" do
setup do setup do
Cache.put("ta:aaa", "zzz") Cache.put("ta:#{Cache.hash('aaa')}", "zzz")
Tag.where(:name => "aaa").update_all("post_count = -3") Tag.where(:name => "aaa").update_all("post_count = -3")
@correction = TagAliasCorrection.new(@tag_alias.id) @correction = TagAliasCorrection.new(@tag_alias.id)
end end
@@ -46,7 +46,7 @@ class TagAliasCorrectionTest < ActiveSupport::TestCase
end end
should "now have the correct cache" do should "now have the correct cache" do
assert_equal("bbb", Cache.get("ta:aaa")) assert_equal("bbb", Cache.get("ta:#{Cache.hash('aaa')}"))
end end
should "now have the correct count" do should "now have the correct count" do

View File

@@ -3,6 +3,7 @@ require 'test_helper'
class UserNameChangeRequestTest < ActiveSupport::TestCase class UserNameChangeRequestTest < ActiveSupport::TestCase
context "in all cases" do context "in all cases" do
setup do setup do
User.any_instance.stubs(:validate_sock_puppets).returns(true)
@admin = FactoryGirl.create(:admin_user) @admin = FactoryGirl.create(:admin_user)
@requester = FactoryGirl.create(:user) @requester = FactoryGirl.create(:user)
CurrentUser.user = @requester CurrentUser.user = @requester

View File

@@ -3,6 +3,8 @@ require 'test_helper'
class UserRevertTest < ActiveSupport::TestCase class UserRevertTest < ActiveSupport::TestCase
context "Reverting a user's changes" do context "Reverting a user's changes" do
setup do setup do
User.any_instance.stubs(:validate_sock_puppets).returns(true)
@creator = FactoryGirl.create(:user) @creator = FactoryGirl.create(:user)
@user = FactoryGirl.create(:user) @user = FactoryGirl.create(:user)
CurrentUser.user = @user CurrentUser.user = @user