logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

remotable_spec.rb (6221B)


  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(empty?: 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 'foo[attribute_name] == url' do
  79. it 'makes no request' do
  80. allow(foo).to receive(:[]).with(attribute_name).and_return(url)
  81. foo.hoge_remote_url = url
  82. expect(request).not_to have_been_requested
  83. end
  84. end
  85. context "scheme is https, parsed_url.host isn't empty, and foo[attribute_name] != url" do
  86. it 'makes a request' do
  87. foo.hoge_remote_url = url
  88. expect(request).to have_been_requested
  89. end
  90. context 'response.code != 200' do
  91. let(:code) { 500 }
  92. it 'calls not send' do
  93. expect(foo).not_to receive(:send).with("#{hoge}=", any_args)
  94. expect(foo).not_to receive(:send).with("#{hoge}_file_name=", any_args)
  95. foo.hoge_remote_url = url
  96. end
  97. end
  98. context 'response.code == 200' do
  99. let(:code) { 200 }
  100. context 'response contains headers["content-disposition"]' do
  101. let(:file) { 'filename="foo.txt"' }
  102. let(:headers) { { 'content-disposition' => file } }
  103. it 'calls send' do
  104. string_io = StringIO.new('')
  105. extname = '.txt'
  106. basename = '0123456789abcdef'
  107. allow(SecureRandom).to receive(:hex).and_return(basename)
  108. allow(StringIO).to receive(:new).with(anything).and_return(string_io)
  109. expect(foo).to receive(:send).with("#{hoge}=", string_io)
  110. expect(foo).to receive(:send).with("#{hoge}_file_name=", basename + extname)
  111. foo.hoge_remote_url = url
  112. end
  113. end
  114. context 'if has_attribute?' do
  115. it 'calls foo[attribute_name] = url' do
  116. allow(foo).to receive(:has_attribute?).with(attribute_name).and_return(true)
  117. expect(foo).to receive('[]=').with(attribute_name, url)
  118. foo.hoge_remote_url = url
  119. end
  120. end
  121. context 'unless has_attribute?' do
  122. it 'calls not foo[attribute_name] = url' do
  123. allow(foo).to receive(:has_attribute?)
  124. .with(attribute_name).and_return(false)
  125. expect(foo).not_to receive('[]=').with(attribute_name, url)
  126. foo.hoge_remote_url = url
  127. end
  128. end
  129. end
  130. context 'an error raised during the request' do
  131. let(:request) { stub_request(:get, url).to_raise(error_class) }
  132. error_classes = [
  133. HTTP::TimeoutError,
  134. HTTP::ConnectionError,
  135. OpenSSL::SSL::SSLError,
  136. Paperclip::Errors::NotIdentifiedByImageMagickError,
  137. Addressable::URI::InvalidURIError,
  138. ]
  139. error_classes.each do |error_class|
  140. let(:error_class) { error_class }
  141. it 'calls Rails.logger.debug' do
  142. expect(Rails.logger).to receive(:debug).with(/^Error fetching remote #{hoge}: /)
  143. foo.hoge_remote_url = url
  144. end
  145. end
  146. end
  147. end
  148. end
  149. describe '#reset_hoge!' do
  150. context 'if url.blank?' do
  151. it 'returns nil, without clearing foo[attribute_name] and calling #hoge_remote_url=' do
  152. url = nil
  153. expect(foo).not_to receive(:send).with(:hoge_remote_url=, url)
  154. foo[attribute_name] = url
  155. expect(foo.reset_hoge!).to be_nil
  156. expect(foo[attribute_name]).to be_nil
  157. end
  158. end
  159. context 'unless url.blank?' do
  160. it 'clears foo[attribute_name] and calls #hoge_remote_url=' do
  161. foo[attribute_name] = url
  162. expect(foo).to receive(:send).with(:hoge_remote_url=, url)
  163. foo.reset_hoge!
  164. expect(foo[attribute_name]).to be ''
  165. end
  166. end
  167. end
  168. end
  169. end