You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
6.5 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Remotable do
  4. class Foo
  5. def initialize
  6. @attrs = {}
  7. end
  8. def [](arg)
  9. @attrs[arg]
  10. end
  11. def []=(arg1, arg2)
  12. @attrs[arg1] = arg2
  13. end
  14. def hoge=(arg); end
  15. def hoge_file_name=(arg); end
  16. def has_attribute?(arg); end
  17. def self.attachment_definitions
  18. { hoge: nil }
  19. end
  20. end
  21. context 'Remotable module is included' do
  22. before do
  23. class Foo
  24. include Remotable
  25. remotable_attachment :hoge, 1.kilobyte
  26. end
  27. end
  28. let(:attribute_name) { "#{hoge}_remote_url".to_sym }
  29. let(:code) { 200 }
  30. let(:file) { 'filename="foo.txt"' }
  31. let(:foo) { Foo.new }
  32. let(:headers) { { 'content-disposition' => file } }
  33. let(:hoge) { :hoge }
  34. let(:url) { 'https://google.com' }
  35. let(:request) do
  36. stub_request(:get, url)
  37. .to_return(status: code, headers: headers)
  38. end
  39. it 'defines a method #hoge_remote_url=' do
  40. expect(foo).to respond_to(:hoge_remote_url=)
  41. end
  42. it 'defines a method #reset_hoge!' do
  43. expect(foo).to respond_to(:reset_hoge!)
  44. end
  45. describe '#hoge_remote_url' do
  46. before do
  47. request
  48. end
  49. it 'always returns arg' do
  50. [nil, '', [], {}].each do |arg|
  51. expect(foo.hoge_remote_url = arg).to be arg
  52. end
  53. end
  54. context 'Addressable::URI::InvalidURIError raised' do
  55. it 'makes no request' do
  56. allow(Addressable::URI).to receive_message_chain(:parse, :normalize)
  57. .with(url).with(no_args).and_raise(Addressable::URI::InvalidURIError)
  58. foo.hoge_remote_url = url
  59. expect(request).not_to have_been_requested
  60. end
  61. end
  62. context 'scheme is neither http nor https' do
  63. let(:url) { 'ftp://google.com' }
  64. it 'makes no request' do
  65. foo.hoge_remote_url = url
  66. expect(request).not_to have_been_requested
  67. end
  68. end
  69. context 'parsed_url.host is empty' do
  70. it 'makes no request' do
  71. parsed_url = double(scheme: 'https', host: double(blank?: true))
  72. allow(Addressable::URI).to receive_message_chain(:parse, :normalize)
  73. .with(url).with(no_args).and_return(parsed_url)
  74. foo.hoge_remote_url = url
  75. expect(request).not_to have_been_requested
  76. end
  77. end
  78. context 'parsed_url.host is nil' do
  79. it 'makes no request' do
  80. parsed_url = Addressable::URI.parse('https:https://example.com/path/file.png')
  81. allow(Addressable::URI).to receive_message_chain(:parse, :normalize)
  82. .with(url).with(no_args).and_return(parsed_url)
  83. foo.hoge_remote_url = url
  84. expect(request).not_to have_been_requested
  85. end
  86. end
  87. context 'foo[attribute_name] == url' do
  88. it 'makes no request' do
  89. allow(foo).to receive(:[]).with(attribute_name).and_return(url)
  90. foo.hoge_remote_url = url
  91. expect(request).not_to have_been_requested
  92. end
  93. end
  94. context "scheme is https, parsed_url.host isn't empty, and foo[attribute_name] != url" do
  95. it 'makes a request' do
  96. foo.hoge_remote_url = url
  97. expect(request).to have_been_requested
  98. end
  99. context 'response.code != 200' do
  100. let(:code) { 500 }
  101. it 'calls not send' do
  102. expect(foo).not_to receive(:send).with("#{hoge}=", any_args)
  103. expect(foo).not_to receive(:send).with("#{hoge}_file_name=", any_args)
  104. foo.hoge_remote_url = url
  105. end
  106. end
  107. context 'response.code == 200' do
  108. let(:code) { 200 }
  109. context 'response contains headers["content-disposition"]' do
  110. let(:file) { 'filename="foo.txt"' }
  111. let(:headers) { { 'content-disposition' => file } }
  112. it 'calls send' do
  113. string_io = StringIO.new('')
  114. extname = '.txt'
  115. basename = '0123456789abcdef'
  116. allow(SecureRandom).to receive(:hex).and_return(basename)
  117. allow(StringIO).to receive(:new).with(anything).and_return(string_io)
  118. expect(foo).to receive(:send).with("#{hoge}=", string_io)
  119. expect(foo).to receive(:send).with("#{hoge}_file_name=", basename + extname)
  120. foo.hoge_remote_url = url
  121. end
  122. end
  123. context 'if has_attribute?' do
  124. it 'calls foo[attribute_name] = url' do
  125. allow(foo).to receive(:has_attribute?).with(attribute_name).and_return(true)
  126. expect(foo).to receive('[]=').with(attribute_name, url)
  127. foo.hoge_remote_url = url
  128. end
  129. end
  130. context 'unless has_attribute?' do
  131. it 'calls not foo[attribute_name] = url' do
  132. allow(foo).to receive(:has_attribute?)
  133. .with(attribute_name).and_return(false)
  134. expect(foo).not_to receive('[]=').with(attribute_name, url)
  135. foo.hoge_remote_url = url
  136. end
  137. end
  138. end
  139. context 'an error raised during the request' do
  140. let(:request) { stub_request(:get, url).to_raise(error_class) }
  141. error_classes = [
  142. HTTP::TimeoutError,
  143. HTTP::ConnectionError,
  144. OpenSSL::SSL::SSLError,
  145. Paperclip::Errors::NotIdentifiedByImageMagickError,
  146. Addressable::URI::InvalidURIError,
  147. ]
  148. error_classes.each do |error_class|
  149. let(:error_class) { error_class }
  150. it 'calls Rails.logger.debug' do
  151. expect(Rails.logger).to receive(:debug).with(/^Error fetching remote #{hoge}: /)
  152. foo.hoge_remote_url = url
  153. end
  154. end
  155. end
  156. end
  157. end
  158. describe '#reset_hoge!' do
  159. context 'if url.blank?' do
  160. it 'returns nil, without clearing foo[attribute_name] and calling #hoge_remote_url=' do
  161. url = nil
  162. expect(foo).not_to receive(:send).with(:hoge_remote_url=, url)
  163. foo[attribute_name] = url
  164. expect(foo.reset_hoge!).to be_nil
  165. expect(foo[attribute_name]).to be_nil
  166. end
  167. end
  168. context 'unless url.blank?' do
  169. it 'clears foo[attribute_name] and calls #hoge_remote_url=' do
  170. foo[attribute_name] = url
  171. expect(foo).to receive(:send).with(:hoge_remote_url=, url)
  172. foo.reset_hoge!
  173. expect(foo[attribute_name]).to be ''
  174. end
  175. end
  176. end
  177. end
  178. end