Files
danbooru/test/test_helper.rb
evazion 57bb51621e Add debugger gem.
Fix random VCR failures in Pixiv tests.

Sometimes tests randomly fail because the PHPSESSID they use in their
HTTP requests to Pixiv is different than the one that was originally
recorded by VCR. This causes VCR to complain that the requests don't
match.

This is caused by the PHPSESSID being globally cached in Memcache.
Depending on the order the tests run in (which is random), one set of
tests can use a PHPSESSID that was recorded for a /different/ set of
tests.

Improve Pixiv URL matching.

* Allow URLs that are missing the http:// part. These are sometimes seen
  in artist entries.
* Ignore URLs from random Pixiv domains such as dic.pixiv.net,
  blog.pixiv.net, etc. These are also sometimes in artist entries.

Improve normalize_for_artist_finder! URL matching.

* Normalize www.pixiv.net/stacc/username URLs.
* Correctly normalize URLs that are missing the illust ID part on the end
  (i.e. http://i2.pixiv.net/img04/img/syounen_no_uta/). These are common
  in artist entries.

Match URLs strictly when normalizing for artist entries.

Only normalize Pixiv URLs that strictly match a known format. Pass any
unrecognized URLs through without attempting to normalize them, just to
be safe.

Normalize URLs when saving artist entries.
2014-12-03 13:16:05 -08:00

116 lines
2.4 KiB
Ruby

ENV["RAILS_ENV"] = "test"
if ENV["SIMPLECOV"]
require 'simplecov'
SimpleCov.start 'rails' do
add_filter ".bundle"
add_filter "script/"
add_filter "test/"
add_filter "config/"
end
end
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
Dir[File.expand_path(File.dirname(__FILE__) + "/factories/*.rb")].each {|file| require file}
module UploadTestMethods
def upload_file(path, content_type, filename)
tempfile = Tempfile.new(filename)
FileUtils.copy_file(path, tempfile.path)
(class << tempfile; self; end).class_eval do
alias local_path path
define_method(:tempfile) {self}
define_method(:original_filename) {filename}
define_method(:content_type) {content_type}
end
tempfile
end
def upload_jpeg(path)
upload_file(path, "image/jpeg", File.basename(path))
end
def upload_zip(path)
upload_file(path, "application/zip", File.basename(path))
end
end
class ActiveSupport::TestCase
include UploadTestMethods
end
class ActionController::TestCase
include UploadTestMethods
def assert_authentication_passes(action, http_method, role, params, session)
__send__(http_method, action, params, session.merge(:user_id => @users[role].id))
assert_response :success
end
def assert_authentication_fails(action, http_method, role)
__send__(http_method, action, params, session.merge(:user_id => @users[role].id))
assert_redirected_to(new_sessions_path)
end
end
class MockMemcache
def initialize
@memory = {}
end
def flush_all
@memory = {}
end
def fetch key, expiry = 0, raw = false
if @memory.has_key?(key)
@memory[key]
else
@memory[key] = yield
end
@memory[key]
end
def incr key
@memory[key] += 1
end
def decr key
@memory[key] -= 1
end
def set key, value, expiry = 0
@memory[key] = value
end
def get key
@memory[key]
end
def delete key, delay = 0
@memory.delete key
end
def get_multi *keys
Hash[[keys.map{ |key| [key, @memory[key]] }]]
end
end
MEMCACHE = MockMemcache.new
VCR.configure do |c|
c.cassette_library_dir = "test/fixtures/vcr_cassettes"
c.hook_into :webmock
# c.allow_http_connections_when_no_cassette = true
c.default_cassette_options = {
match_requests_on: [
:method,
VCR.request_matchers.uri_without_param(:PHPSESSID)
]
}
end