2017-11-20 20:59:36 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Remotable do
|
|
|
|
class Foo
|
|
|
|
def initialize
|
|
|
|
@attrs = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](arg)
|
|
|
|
@attrs[arg]
|
|
|
|
end
|
|
|
|
|
|
|
|
def []=(arg1, arg2)
|
|
|
|
@attrs[arg1] = arg2
|
|
|
|
end
|
|
|
|
|
|
|
|
def hoge=(arg); end
|
|
|
|
|
2019-10-08 22:10:46 -07:00
|
|
|
def hoge_file_name; end
|
|
|
|
|
2017-11-20 20:59:36 -08:00
|
|
|
def hoge_file_name=(arg); end
|
|
|
|
|
|
|
|
def has_attribute?(arg); end
|
|
|
|
|
|
|
|
def self.attachment_definitions
|
|
|
|
{ hoge: nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
before do
|
|
|
|
class Foo
|
|
|
|
include Remotable
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
remotable_attachment :hoge, 1.kilobyte
|
2017-11-20 20:59:36 -08:00
|
|
|
end
|
2020-06-29 08:59:04 -07:00
|
|
|
end
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
let(:attribute_name) { "#{hoge}_remote_url".to_sym }
|
|
|
|
let(:code) { 200 }
|
|
|
|
let(:file) { 'filename="foo.txt"' }
|
|
|
|
let(:foo) { Foo.new }
|
|
|
|
let(:headers) { { 'content-disposition' => file } }
|
|
|
|
let(:hoge) { :hoge }
|
|
|
|
let(:url) { 'https://google.com' }
|
|
|
|
|
|
|
|
it 'defines a method #hoge_remote_url=' do
|
|
|
|
expect(foo).to respond_to(:hoge_remote_url=)
|
|
|
|
end
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
it 'defines a method #reset_hoge!' do
|
|
|
|
expect(foo).to respond_to(:reset_hoge!)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defines a method #download_hoge!' do
|
|
|
|
expect(foo).to respond_to(:download_hoge!)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#hoge_remote_url=' do
|
|
|
|
before do
|
|
|
|
stub_request(:get, url).to_return(status: code, headers: headers)
|
2017-11-20 20:59:36 -08:00
|
|
|
end
|
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
it 'always returns its argument' do
|
|
|
|
[nil, '', [], {}].each do |arg|
|
|
|
|
expect(foo.hoge_remote_url = arg).to be arg
|
|
|
|
end
|
2020-06-29 04:56:55 -07:00
|
|
|
end
|
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
context 'with an invalid URL' do
|
2017-11-20 20:59:36 -08:00
|
|
|
before do
|
2020-06-29 08:59:04 -07:00
|
|
|
allow(Addressable::URI).to receive_message_chain(:parse, :normalize).with(url).with(no_args).and_raise(Addressable::URI::InvalidURIError)
|
2017-11-20 20:59:36 -08:00
|
|
|
end
|
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
it 'makes no request' do
|
|
|
|
foo.hoge_remote_url = url
|
|
|
|
expect(a_request(:get, url)).to_not have_been_made
|
2017-11-20 20:59:36 -08:00
|
|
|
end
|
2020-06-29 08:59:04 -07:00
|
|
|
end
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
context 'with scheme that is neither http nor https' do
|
|
|
|
let(:url) { 'ftp://google.com' }
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
it 'makes no request' do
|
|
|
|
foo.hoge_remote_url = url
|
|
|
|
expect(a_request(:get, url)).to_not have_been_made
|
2017-11-20 20:59:36 -08:00
|
|
|
end
|
2020-06-29 08:59:04 -07:00
|
|
|
end
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
context 'with relative URL' do
|
|
|
|
let(:url) { 'https:///path' }
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
it 'makes no request' do
|
|
|
|
foo.hoge_remote_url = url
|
|
|
|
expect(a_request(:get, url)).to_not have_been_made
|
2017-11-20 20:59:36 -08:00
|
|
|
end
|
2020-06-29 08:59:04 -07:00
|
|
|
end
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
context 'when URL has not changed' do
|
|
|
|
it 'makes no request if file is already saved' do
|
|
|
|
allow(foo).to receive(:[]).with(attribute_name).and_return(url)
|
|
|
|
allow(foo).to receive(:hoge_file_name).and_return('foo.jpg')
|
2018-08-29 12:13:49 -07:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
foo.hoge_remote_url = url
|
|
|
|
expect(a_request(:get, url)).to_not have_been_made
|
2018-08-29 12:13:49 -07:00
|
|
|
end
|
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
it 'makes request if file is not already saved' do
|
|
|
|
allow(foo).to receive(:[]).with(attribute_name).and_return(url)
|
|
|
|
allow(foo).to receive(:hoge_file_name).and_return(nil)
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
foo.hoge_remote_url = url
|
|
|
|
expect(a_request(:get, url)).to have_been_made
|
2017-11-20 20:59:36 -08:00
|
|
|
end
|
2020-06-29 08:59:04 -07:00
|
|
|
end
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
context 'when instance has no attribute for URL' do
|
|
|
|
before do
|
|
|
|
allow(foo).to receive(:has_attribute?).with(attribute_name).and_return(false)
|
|
|
|
end
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
it 'does not try to write attribute' do
|
|
|
|
expect(foo).to_not receive('[]=').with(attribute_name, url)
|
|
|
|
foo.hoge_remote_url = url
|
|
|
|
end
|
|
|
|
end
|
2019-10-08 22:10:46 -07:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
context 'when instance has an attribute for URL' do
|
|
|
|
before do
|
|
|
|
allow(foo).to receive(:has_attribute?).with(attribute_name).and_return(true)
|
|
|
|
end
|
2019-10-08 22:10:46 -07:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
it 'does not try to write attribute' do
|
|
|
|
expect(foo).to receive('[]=').with(attribute_name, url)
|
|
|
|
foo.hoge_remote_url = url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a valid URL' do
|
|
|
|
it 'makes a request' do
|
|
|
|
foo.hoge_remote_url = url
|
|
|
|
expect(a_request(:get, url)).to have_been_made
|
2017-11-20 20:59:36 -08:00
|
|
|
end
|
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
context 'when the response is not successful' do
|
|
|
|
let(:code) { 500 }
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
it 'does not assign file' do
|
2023-02-19 17:33:27 -08:00
|
|
|
expect(foo).to_not receive(:public_send).with("#{hoge}=", any_args)
|
|
|
|
expect(foo).to_not receive(:public_send).with("#{hoge}_file_name=", any_args)
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
foo.hoge_remote_url = url
|
2017-11-20 20:59:36 -08:00
|
|
|
end
|
2020-06-29 08:59:04 -07:00
|
|
|
end
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
context 'when the response is successful' do
|
|
|
|
let(:code) { 200 }
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
context 'and contains Content-Disposition header' do
|
|
|
|
let(:file) { 'filename="foo.txt"' }
|
|
|
|
let(:headers) { { 'content-disposition' => file } }
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
it 'assigns file' do
|
2020-06-30 14:58:02 -07:00
|
|
|
response_with_limit = ResponseWithLimit.new(nil, 0)
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-30 14:58:02 -07:00
|
|
|
allow(ResponseWithLimit).to receive(:new).with(anything, anything).and_return(response_with_limit)
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
expect(foo).to receive(:public_send).with("download_#{hoge}!", url)
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
foo.hoge_remote_url = url
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-30 14:58:02 -07:00
|
|
|
expect(foo).to receive(:public_send).with("#{hoge}=", response_with_limit)
|
2020-06-29 04:56:55 -07:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
foo.download_hoge!(url)
|
2017-11-20 20:59:36 -08:00
|
|
|
end
|
|
|
|
end
|
2020-06-29 08:59:04 -07:00
|
|
|
end
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
context 'when an error is raised during the request' do
|
|
|
|
before do
|
|
|
|
stub_request(:get, url).to_raise(error_class)
|
|
|
|
end
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
error_classes = [
|
|
|
|
HTTP::TimeoutError,
|
|
|
|
HTTP::ConnectionError,
|
|
|
|
OpenSSL::SSL::SSLError,
|
|
|
|
Paperclip::Errors::NotIdentifiedByImageMagickError,
|
|
|
|
Addressable::URI::InvalidURIError,
|
|
|
|
]
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
error_classes.each do |error_class|
|
|
|
|
let(:error_class) { error_class }
|
2017-11-20 20:59:36 -08:00
|
|
|
|
2020-06-29 08:59:04 -07:00
|
|
|
it 'calls Rails.logger.debug' do
|
2023-02-06 18:44:36 -08:00
|
|
|
expect(Rails.logger).to receive(:debug) do |&block|
|
|
|
|
expect(block.call).to match(/^Error fetching remote #{hoge}: /)
|
|
|
|
end
|
2020-06-29 08:59:04 -07:00
|
|
|
foo.hoge_remote_url = url
|
2017-11-20 20:59:36 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|