5

In development and production, we can access images in the browser. This does not work for the test environment whilst tests are running.

The URL we are using for images is formatted like this:

http://localhost:3000/images/ABC # for development. Port for tests is 3030

: and it redirects to an ActiveStorage attachment service URL - which looks like this:

http://localhost:3000/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaEpJaDFTVEd0WlpUbHFlVGh0YW1od1JucEdkMUJuYWxaelNuSUdPZ1pGVkE9PSIsImV4cCI6IjIwMTgtMDYtMDhUMDc6MDM6MDUuNDI2WiIsInB1ciI6ImJsb2Jfa2V5In19--abef581f4df7a19ae1a35a2be03edd028659441f/454.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%22454.jpg%22%3B+filename%2A%3DUTF-8%27%27454.jpg

Using byebug (binding.pry like thing) whilst the test is running, we can ascertain things are setup correctly (or are they?):

(byebug) @request.host
"localhost"
(byebug) @request.port
3030
(byebug) File.open("blah.html", 'w'){|file| file.write(response.body)}
1854
(byebug) get(image_source_path(id: 'ABC'))
302
(byebug) response.redirect_url
"http://localhost:3030/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaEpJaDEwUWpkVmRFeHJUalV5YUdsYU5XOUdPR0Y2UVZCVFpsb0dPZ1pGVkE9PSIsImV4cCI6IjIwMTgtMDYtMDhUMDc6MjU6NDEuNzkzWiIsInB1ciI6ImJsb2Jfa2V5In19--42f8cd5ca06b07704a6550a19aaa143a6eeb0c6e/pig_admirer.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%22pig_admirer.jpg%22%3B+filename%2A%3DUTF-8%27%27pig_admirer.jpg"
(byebug) get(response.redirect_url)
200
(byebug) response.content_length
3618075

: it is indeed returning a ~3.5MiB image. However, when saving a page as HTML ('blah.html' in that sequence), when opening that HTML whilst byebug is running, the images do not load - the calls to ...images/ABC are rejected.

This is a problem because we are using wicked_pdf to generate a PDF, and in the test environment when the PDF is generated it cannot access those images to put into the PDF.

Perhaps the test server can be made to serve responses outside of rspec? Otherwise, there will be no images in the PDF.


Additional notes - config of environment

In the test we added:

before do
  host! 'localhost:3030'
end

: though apparently host! is deprecated. And when we tried relocating it to /rails_helper.rb it broke a whole bunch of other tests.

In /config/environments/test.rb we have, amongst other things:

Rails.application.configure do
  config.active_storage.service = :test
  config.consider_all_requests_local = true
  config.action_controller.perform_caching = false
  config.host = 'localhost:3030'
  config.action_controller.default_url_options = { host: 'localhost:3030' }
  config.assets.compile = true
  config.allow_concurrency = true
end

Active storage is configured correctly in /config/storage.yml:

test:
  service: Disk
  root: <%= Rails.root.join("tmp/test_storage") %>
Ali
  • 1,326
  • 1
  • 17
  • 38
xxjjnn
  • 14,591
  • 19
  • 61
  • 94
  • did you ever solve this issue? I'm having a similar problem with ActiveStorage attachments in a Prawn PDF generator. – Andy Harvey Aug 29 '18 at 16:40
  • aha! Yes sorry, coworker said that tests are single-threaded so there is no solution. We used a stubbing approach. Its not really a solution though... is your problem with prawn outside of tests? If so please ask a separate question and put a link to it in a comment – xxjjnn Aug 30 '18 at 11:16
  • Thanks @xxjjnn, while I encountered this in Prawn, it seems to be a more general issue related to accessing the file in a test env. More details here: https://stackoverflow.com/questions/52071890/how-can-i-access-an-activestorage-object-via-url-in-a-test-environment – Andy Harvey Aug 30 '18 at 11:21

0 Answers0