A platform for community discussion. Free, open, simple.
FIX: Tests could get stucked in infinite loop if it fails to resolve IP of a hostname.
Guo Xiang Tanee69d58a59c8
diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rbindex 7de892a84e0..0bbef3a4706 100644--- a/app/jobs/regular/pull_hotlinked_images.rb+++ b/app/jobs/regular/pull_hotlinked_images.rb@@ -62,7 +62,9 @@ module Jobsif is_valid_image_url(src)begin# have we already downloaded that file?- unless downloaded_images.include?(src) || large_images.include?(src) || broken_images.include?(src)+ schemeless_src = remove_scheme(original_src)++ unless downloaded_images.include?(schemeless_src) || large_images.include?(schemeless_src) || broken_images.include?(schemeless_src)if hotlinked = download(src)if File.size(hotlinked.path) <= @max_sizefilename = File.basename(URI.parse(src).path)@@ -70,17 +72,17 @@ module Jobsupload = UploadCreator.new(hotlinked, filename, origin: src).create_for(post.user_id)if upload.persisted?downloaded_urls[src] = upload.url- downloaded_images[src.sub(/^https?:/i, "")] = upload.id+ downloaded_images[remove_scheme(src)] = upload.idhas_downloaded_image = trueelselog(:info, "Failed to pull hotlinked image for post: #{post_id}: #{src} - #{upload.errors.full_messages.join("\n")}")endelse- large_images << original_src.sub(/^https?:/i, "")+ large_images << remove_scheme(src)has_new_large_image = trueendelse- broken_images << original_src.sub(/^https?:/i, "")+ broken_images << remove_scheme(src)has_new_broken_image = trueendend@@ -170,6 +172,11 @@ module Jobs)end+ private++ def remove_scheme(src)+ src.sub(/^https?:/i, "")+ endendenddiff --git a/lib/final_destination.rb b/lib/final_destination.rbindex e85aff8c01e..3b91788a525 100644--- a/lib/final_destination.rb+++ b/lib/final_destination.rb@@ -312,11 +312,11 @@ class FinalDestinationenddef self.lookup_ip(host)- # TODO clean this up in the test suite, cause it is a mess- # if Rails.env == "test"- # STDERR.puts "WARNING FinalDestination.lookup_ip was called with host: #{host}, this is network call that should be mocked"- # end- IPSocket::getaddress(host)+ if Rails.env.test?+ "0.0.0.0"+ else+ IPSocket::getaddress(host)+ endrescue SocketErrornilenddiff --git a/spec/models/user_avatar_spec.rb b/spec/models/user_avatar_spec.rbindex eb7f723064e..75f31ac8db8 100644--- a/spec/models/user_avatar_spec.rb+++ b/spec/models/user_avatar_spec.rb@@ -46,6 +46,9 @@ describe UserAvatar dodescribe 'when avatar url returns an invalid status code' doit 'should not do anything' do+ stub_request(:get, "http://thisfakesomething.something.com/")+ .to_return(status: 500, body: "", headers: {})+url = "http://thisfakesomething.something.com/"UserAvatar.import_url_for_user(url, user)